Skip to content

Commit 33d46e5

Browse files
authored
added json logging support in karmada-agent (#6442)
* added json logging support in karmada-agent Signed-off-by: Yifan <[email protected]> * flush logs at the end of the main function of karmada-agent Signed-off-by: Yifan <[email protected]> --------- Signed-off-by: Yifan <[email protected]>
1 parent 312fbd0 commit 33d46e5

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

artifacts/agent/karmada-agent.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ spec:
3939
- --metrics-bind-address=$(POD_IP):8080
4040
- --health-probe-bind-address=$(POD_IP):10357
4141
- --feature-gates=CustomizedClusterResourceModeling=true,MultiClusterService=true
42+
- --logging-format=json
4243
- --v=4
4344
livenessProbe:
4445
httpGet:

cmd/agent/app/agent.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import (
3030
"k8s.io/client-go/rest"
3131
"k8s.io/client-go/util/flowcontrol"
3232
cliflag "k8s.io/component-base/cli/flag"
33+
"k8s.io/component-base/logs"
34+
logsv1 "k8s.io/component-base/logs/api/v1"
3335
"k8s.io/component-base/term"
3436
"k8s.io/klog/v2"
3537
controllerruntime "sigs.k8s.io/controller-runtime"
@@ -72,13 +74,32 @@ import (
7274

7375
// NewAgentCommand creates a *cobra.Command object with default parameters
7476
func NewAgentCommand(ctx context.Context) *cobra.Command {
77+
logConfig := logsv1.NewLoggingConfiguration()
78+
fss := cliflag.NamedFlagSets{}
79+
80+
// Set klog flags
81+
logsFlagSet := fss.FlagSet("logs")
82+
logs.AddFlags(logsFlagSet, logs.SkipLoggingConfigurationFlags())
83+
logsv1.AddFlags(logConfig, logsFlagSet)
84+
klogflag.Add(logsFlagSet)
85+
86+
genericFlagSet := fss.FlagSet("generic")
87+
genericFlagSet.AddGoFlagSet(flag.CommandLine)
7588
opts := options.NewOptions()
89+
opts.AddFlags(genericFlagSet, controllers.ControllerNames())
7690

7791
cmd := &cobra.Command{
7892
Use: names.KarmadaAgentComponentName,
7993
Long: `The karmada-agent is the agent of member clusters. It can register a specific cluster to the Karmada control
8094
plane and sync manifests from the Karmada control plane to the member cluster. In addition, it also syncs the status of member
8195
cluster and manifests to the Karmada control plane.`,
96+
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
97+
if err := logsv1.ValidateAndApply(logConfig, features.FeatureGate); err != nil {
98+
return err
99+
}
100+
logs.InitLogs()
101+
return nil
102+
},
82103
RunE: func(_ *cobra.Command, _ []string) error {
83104
// validate options
84105
if errs := opts.Validate(); len(errs) != 0 {
@@ -99,16 +120,6 @@ cluster and manifests to the Karmada control plane.`,
99120
},
100121
}
101122

102-
fss := cliflag.NamedFlagSets{}
103-
104-
genericFlagSet := fss.FlagSet("generic")
105-
genericFlagSet.AddGoFlagSet(flag.CommandLine)
106-
opts.AddFlags(genericFlagSet, controllers.ControllerNames())
107-
108-
// Set klog flags
109-
logsFlagSet := fss.FlagSet("logs")
110-
klogflag.Add(logsFlagSet)
111-
112123
cmd.AddCommand(sharedcommand.NewCmdVersion(names.KarmadaAgentComponentName))
113124
cmd.Flags().AddFlagSet(genericFlagSet)
114125
cmd.Flags().AddFlagSet(logsFlagSet)

cmd/agent/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"os"
2121

2222
"k8s.io/component-base/cli"
23+
"k8s.io/component-base/logs"
2324
_ "k8s.io/component-base/logs/json/register" // for JSON log format registration
2425
"k8s.io/klog/v2"
2526
controllerruntime "sigs.k8s.io/controller-runtime"
@@ -37,5 +38,7 @@ func main() {
3738
controllerruntime.SetLogger(klog.Background())
3839
cmd := app.NewAgentCommand(ctx)
3940
code := cli.Run(cmd)
41+
// Ensure any buffered log entries are flushed
42+
logs.FlushLogs()
4043
os.Exit(code)
4144
}

0 commit comments

Comments
 (0)