Skip to content

Commit 4eb6a5e

Browse files
committed
refactor: use common sidecar flags functionality
1 parent 099f112 commit 4eb6a5e

File tree

1 file changed

+23
-61
lines changed

1 file changed

+23
-61
lines changed

cmd/csi-resizer/main.go

Lines changed: 23 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -62,33 +62,16 @@ import (
6262

6363
var (
6464
master = flag.String("master", "", "Master URL to build a client config from. Either this or kubeconfig needs to be set if the provisioner is being run out of cluster.")
65-
kubeConfig = flag.String("kubeconfig", "", "Absolute path to the kubeconfig")
6665
resyncPeriod = flag.Duration("resync-period", time.Minute*10, "Resync period for cache")
6766
workers = flag.Int("workers", 10, "Concurrency to process multiple resize requests")
6867

6968
extraModifyMetadata = flag.Bool("extra-modify-metadata", false, "If set, add pv/pvc metadata to plugin modify requests as parameters.")
7069

71-
csiAddress = flag.String("csi-address", "/run/csi/socket", "Address of the CSI driver socket.")
72-
timeout = flag.Duration("timeout", 10*time.Second, "Timeout for waiting for CSI driver socket.")
73-
74-
showVersion = flag.Bool("version", false, "Show version")
70+
timeout = flag.Duration("timeout", 10*time.Second, "Timeout for waiting for CSI driver socket.")
7571

7672
retryIntervalStart = flag.Duration("retry-interval-start", time.Second, "Initial retry interval of failed volume resize. It exponentially increases with each failure, up to retry-interval-max.")
7773
retryIntervalMax = flag.Duration("retry-interval-max", 5*time.Minute, "Maximum retry interval of failed volume resize.")
7874

79-
enableLeaderElection = flag.Bool("leader-election", false, "Enable leader election.")
80-
leaderElectionNamespace = flag.String("leader-election-namespace", "", "Namespace where the leader election resource lives. Defaults to the pod namespace if not set.")
81-
leaderElectionLeaseDuration = flag.Duration("leader-election-lease-duration", 15*time.Second, "Duration, in seconds, that non-leader candidates will wait to force acquire leadership. Defaults to 15 seconds.")
82-
leaderElectionRenewDeadline = flag.Duration("leader-election-renew-deadline", 10*time.Second, "Duration, in seconds, that the acting leader will retry refreshing leadership before giving up. Defaults to 10 seconds.")
83-
leaderElectionRetryPeriod = flag.Duration("leader-election-retry-period", 5*time.Second, "Duration, in seconds, the LeaderElector clients should wait between tries of actions. Defaults to 5 seconds.")
84-
85-
metricsAddress = flag.String("metrics-address", "", "(deprecated) The TCP network address where the prometheus metrics endpoint will listen (example: `:8080`). The default is empty string, which means metrics endpoint is disabled. Only one of `--metrics-address` and `--http-endpoint` can be set.")
86-
httpEndpoint = flag.String("http-endpoint", "", "The TCP network address where the HTTP server for diagnostics, including metrics and leader election health check, will listen (example: `:8080`). The default is empty string, which means the server is disabled. Only one of `--metrics-address` and `--http-endpoint` can be set.")
87-
metricsPath = flag.String("metrics-path", "/metrics", "The HTTP path where prometheus metrics will be exposed. Default is `/metrics`.")
88-
89-
kubeAPIQPS = flag.Float64("kube-api-qps", 5, "QPS to use while communicating with the kubernetes apiserver. Defaults to 5.0.")
90-
kubeAPIBurst = flag.Int("kube-api-burst", 10, "Burst to use while communicating with the kubernetes apiserver. Defaults to 10.")
91-
9275
handleVolumeInUseError = flag.Bool("handle-volume-inuse-error", true, "Flag to turn on/off capability to handle volume in use error in resizer controller. Defaults to true if not set.")
9376

9477
featureGates map[string]bool
@@ -103,26 +86,27 @@ func main() {
10386
c := logsapi.NewLoggingConfiguration()
10487
logsapi.AddGoFlags(c, flag.CommandLine)
10588
logs.InitLogs()
89+
standardflags.RegisterCommonFlags(flag.CommandLine)
10690
standardflags.AddAutomaxprocs(klog.Infof)
10791
flag.Parse()
10892
if err := logsapi.ValidateAndApply(c, fg); err != nil {
10993
klog.ErrorS(err, "LoggingConfiguration is invalid")
11094
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
11195
}
11296

113-
if *showVersion {
97+
if standardflags.Configuration.ShowVersion {
11498
fmt.Println(os.Args[0], version)
11599
os.Exit(0)
116100
}
117101
klog.InfoS("Version", "version", version)
118102

119-
if *metricsAddress != "" && *httpEndpoint != "" {
103+
if standardflags.Configuration.MetricsAddress != "" && standardflags.Configuration.HttpEndpoint != "" {
120104
klog.ErrorS(nil, "Only one of `--metrics-address` and `--http-endpoint` can be set.")
121105
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
122106
}
123-
addr := *metricsAddress
107+
addr := standardflags.Configuration.MetricsAddress
124108
if addr == "" {
125-
addr = *httpEndpoint
109+
addr = standardflags.Configuration.HttpEndpoint
126110
}
127111
if err := utilfeature.DefaultMutableFeatureGate.SetFromMap(featureGates); err != nil {
128112
klog.ErrorS(err, "Failed to set feature gates")
@@ -131,8 +115,8 @@ func main() {
131115

132116
var config *rest.Config
133117
var err error
134-
if *master != "" || *kubeConfig != "" {
135-
config, err = clientcmd.BuildConfigFromFlags(*master, *kubeConfig)
118+
if *master != "" || standardflags.Configuration.KubeConfig != "" {
119+
config, err = clientcmd.BuildConfigFromFlags(*master, standardflags.Configuration.KubeConfig)
136120
} else {
137121
config, err = rest.InClusterConfig()
138122
}
@@ -141,8 +125,8 @@ func main() {
141125
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
142126
}
143127

144-
config.QPS = float32(*kubeAPIQPS)
145-
config.Burst = *kubeAPIBurst
128+
config.QPS = float32(standardflags.Configuration.KubeAPIQPS)
129+
config.Burst = standardflags.Configuration.KubeAPIBurst
146130
config.ContentType = runtime.ContentTypeProtobuf
147131

148132
kubeClient, err := kubernetes.NewForConfig(config)
@@ -158,7 +142,7 @@ func main() {
158142
metricsManager := metrics.NewCSIMetricsManager("" /* driverName */)
159143

160144
ctx := context.Background()
161-
csiClient, err := csi.New(ctx, *csiAddress, *timeout, metricsManager)
145+
csiClient, err := csi.New(ctx, standardflags.Configuration.CSIAddress, *timeout, metricsManager)
162146
if err != nil {
163147
klog.ErrorS(err, "Failed to create CSI client")
164148
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
@@ -174,7 +158,7 @@ func main() {
174158
translator := csitrans.New()
175159
if translator.IsMigratedCSIDriverByName(driverName) {
176160
metricsManager = metrics.NewCSIMetricsManagerWithOptions(driverName, metrics.WithMigration())
177-
migratedCsiClient, err := csi.New(ctx, *csiAddress, *timeout, metricsManager)
161+
migratedCsiClient, err := csi.New(ctx, standardflags.Configuration.CSIAddress, *timeout, metricsManager)
178162
if err != nil {
179163
klog.ErrorS(err, "Failed to create MigratedCSI client")
180164
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
@@ -211,13 +195,13 @@ func main() {
211195

212196
// Start HTTP server for metrics + leader election healthz
213197
if addr != "" {
214-
metricsManager.RegisterToServer(mux, *metricsPath)
198+
metricsManager.RegisterToServer(mux, standardflags.Configuration.MetricsPath)
215199
metricsManager.SetDriverName(driverName)
216200
go func() {
217201
klog.InfoS("ServeMux listening", "address", addr)
218202
err := http.ListenAndServe(addr, mux)
219203
if err != nil {
220-
klog.ErrorS(err, "Failed to start HTTP server", "address", addr, "metricsPath", *metricsPath)
204+
klog.ErrorS(err, "Failed to start HTTP server", "address", addr, "metricsPath", standardflags.Configuration.MetricsPath)
221205
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
222206
}
223207
}()
@@ -279,37 +263,15 @@ func main() {
279263
}
280264
}
281265

282-
if !*enableLeaderElection {
283-
run(ctx)
284-
} else {
285-
lockName := "external-resizer-" + util.SanitizeName(resizerName)
286-
leKubeClient, err := kubernetes.NewForConfig(config)
287-
if err != nil {
288-
klog.ErrorS(err, "Failed to create leKubeClient")
289-
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
290-
}
291-
le := leaderelection.NewLeaderElection(leKubeClient, lockName, run)
292-
if *httpEndpoint != "" {
293-
le.PrepareHealthCheck(mux, leaderelection.DefaultHealthCheckTimeout)
294-
}
295-
296-
if *leaderElectionNamespace != "" {
297-
le.WithNamespace(*leaderElectionNamespace)
298-
}
299-
300-
le.WithLeaseDuration(*leaderElectionLeaseDuration)
301-
le.WithRenewDeadline(*leaderElectionRenewDeadline)
302-
le.WithRetryPeriod(*leaderElectionRetryPeriod)
303-
if utilfeature.DefaultFeatureGate.Enabled(features.ReleaseLeaderElectionOnExit) {
304-
le.WithReleaseOnCancel(true)
305-
le.WithContext(ctx)
306-
}
307-
308-
if err := le.Run(); err != nil {
309-
klog.ErrorS(err, "Error initializing leader election")
310-
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
311-
}
312-
}
266+
leaderelection.RunWithLeaderElection(
267+
ctx,
268+
config,
269+
standardflags.Configuration,
270+
run,
271+
"external-resizer-"+util.SanitizeName(resizerName),
272+
mux,
273+
utilfeature.DefaultFeatureGate.Enabled(features.ReleaseLeaderElectionOnExit),
274+
)
313275
}
314276

315277
func getDriverName(client csi.Client, timeout time.Duration) (string, error) {

0 commit comments

Comments
 (0)