@@ -31,6 +31,8 @@ import (
31
31
"k8s.io/client-go/rest"
32
32
"k8s.io/client-go/util/flowcontrol"
33
33
cliflag "k8s.io/component-base/cli/flag"
34
+ "k8s.io/component-base/logs"
35
+ logsv1 "k8s.io/component-base/logs/api/v1"
34
36
"k8s.io/component-base/term"
35
37
"k8s.io/klog/v2"
36
38
resourceclient "k8s.io/metrics/pkg/client/clientset/versioned/typed/metrics/v1beta1"
@@ -97,44 +99,52 @@ import (
97
99
98
100
// NewControllerManagerCommand creates a *cobra.Command object with default parameters
99
101
func NewControllerManagerCommand (ctx context.Context ) * cobra.Command {
102
+ logConfig := logsv1 .NewLoggingConfiguration ()
103
+ fss := cliflag.NamedFlagSets {}
104
+
105
+ logsFlagSet := fss .FlagSet ("logs" )
106
+ logs .AddFlags (logsFlagSet , logs .SkipLoggingConfigurationFlags ())
107
+ logsv1 .AddFlags (logConfig , logsFlagSet )
108
+ klogflag .Add (logsFlagSet )
109
+
110
+ genericFlagSet := fss .FlagSet ("generic" )
111
+ // Add the flag(--kubeconfig) that is added by controller-runtime.
112
+ // (https://github.com/kubernetes-sigs/controller-runtime/blob/v0.11.1/pkg/client/config/config.go#L39),
113
+ // and update the flag usage.
114
+ genericFlagSet .AddGoFlagSet (flag .CommandLine )
115
+ genericFlagSet .Lookup ("kubeconfig" ).Usage = "Path to karmada control plane kubeconfig file."
100
116
opts := options .NewOptions ()
117
+ opts .AddFlags (genericFlagSet , controllers .ControllerNames (), sets .List (controllersDisabledByDefault ))
101
118
102
119
cmd := & cobra.Command {
103
120
Use : names .KarmadaControllerManagerComponentName ,
104
121
Long : `The karmada-controller-manager runs various controllers.
105
- The controllers watch Karmada objects and then talk to the underlying clusters' API servers
106
- to create regular Kubernetes resources.` ,
122
+ The controllers watch Karmada objects and then talk to the underlying
123
+ clusters' API servers to create regular Kubernetes resources.` ,
124
+
125
+ PersistentPreRunE : func (_ * cobra.Command , _ []string ) error {
126
+ if err := logsv1 .ValidateAndApply (logConfig , features .FeatureGate ); err != nil {
127
+ return err
128
+ }
129
+ logs .InitLogs ()
130
+ return nil
131
+ },
132
+
107
133
RunE : func (_ * cobra.Command , _ []string ) error {
108
- // validate options
109
134
if errs := opts .Validate (); len (errs ) != 0 {
110
135
return errs .ToAggregate ()
111
136
}
112
-
113
137
return Run (ctx , opts )
114
138
},
115
139
}
116
140
117
- fss := cliflag.NamedFlagSets {}
118
-
119
- // Set generic flags
120
- genericFlagSet := fss .FlagSet ("generic" )
121
- // Add the flag(--kubeconfig) that is added by controller-runtime
122
- // (https://github.com/kubernetes-sigs/controller-runtime/blob/v0.11.1/pkg/client/config/config.go#L39),
123
- // and update the flag usage.
124
- genericFlagSet .AddGoFlagSet (flag .CommandLine )
125
- genericFlagSet .Lookup ("kubeconfig" ).Usage = "Path to karmada control plane kubeconfig file."
126
- opts .AddFlags (genericFlagSet , controllers .ControllerNames (), sets .List (controllersDisabledByDefault ))
127
-
128
- // Set klog flags
129
- logsFlagSet := fss .FlagSet ("logs" )
130
- klogflag .Add (logsFlagSet )
131
-
132
141
cmd .AddCommand (sharedcommand .NewCmdVersion (names .KarmadaControllerManagerComponentName ))
133
142
cmd .Flags ().AddFlagSet (genericFlagSet )
134
143
cmd .Flags ().AddFlagSet (logsFlagSet )
135
144
136
145
cols , _ , _ := term .TerminalSize (cmd .OutOrStdout ())
137
146
sharedcli .SetUsageAndHelpFunc (cmd , fss , cols )
147
+
138
148
return cmd
139
149
}
140
150
0 commit comments