@@ -30,6 +30,8 @@ import (
30
30
"k8s.io/client-go/rest"
31
31
"k8s.io/client-go/util/flowcontrol"
32
32
cliflag "k8s.io/component-base/cli/flag"
33
+ "k8s.io/component-base/logs"
34
+ logsv1 "k8s.io/component-base/logs/api/v1"
33
35
"k8s.io/component-base/term"
34
36
"k8s.io/klog/v2"
35
37
controllerruntime "sigs.k8s.io/controller-runtime"
@@ -72,13 +74,32 @@ import (
72
74
73
75
// NewAgentCommand creates a *cobra.Command object with default parameters
74
76
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 )
75
88
opts := options .NewOptions ()
89
+ opts .AddFlags (genericFlagSet , controllers .ControllerNames ())
76
90
77
91
cmd := & cobra.Command {
78
92
Use : names .KarmadaAgentComponentName ,
79
93
Long : `The karmada-agent is the agent of member clusters. It can register a specific cluster to the Karmada control
80
94
plane and sync manifests from the Karmada control plane to the member cluster. In addition, it also syncs the status of member
81
95
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
+ },
82
103
RunE : func (_ * cobra.Command , _ []string ) error {
83
104
// validate options
84
105
if errs := opts .Validate (); len (errs ) != 0 {
@@ -99,16 +120,6 @@ cluster and manifests to the Karmada control plane.`,
99
120
},
100
121
}
101
122
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
-
112
123
cmd .AddCommand (sharedcommand .NewCmdVersion (names .KarmadaAgentComponentName ))
113
124
cmd .Flags ().AddFlagSet (genericFlagSet )
114
125
cmd .Flags ().AddFlagSet (logsFlagSet )
0 commit comments