Skip to content

Commit 642fbfb

Browse files
authored
Merge pull request #528 from DerekFrank/use-central-flags
refactor: use common sidecar flags functionality
2 parents aa89854 + f47e523 commit 642fbfb

5 files changed

Lines changed: 28 additions & 66 deletions

File tree

cmd/csi-resizer/main.go

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

6464
var (
6565
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.")
66-
kubeConfig = flag.String("kubeconfig", "", "Absolute path to the kubeconfig")
6766
resyncPeriod = flag.Duration("resync-period", time.Minute*10, "Resync period for cache")
6867
workers = flag.Int("workers", 10, "Concurrency to process multiple resize requests")
6968

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

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

7773
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.")
7874
retryIntervalMax = flag.Duration("retry-interval-max", 5*time.Minute, "Maximum retry interval of failed volume resize.")
7975

80-
enableLeaderElection = flag.Bool("leader-election", false, "Enable leader election.")
81-
leaderElectionNamespace = flag.String("leader-election-namespace", "", "Namespace where the leader election resource lives. Defaults to the pod namespace if not set.")
82-
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.")
83-
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.")
84-
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.")
85-
86-
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.")
87-
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.")
88-
metricsPath = flag.String("metrics-path", "/metrics", "The HTTP path where prometheus metrics will be exposed. Default is `/metrics`.")
89-
90-
kubeAPIQPS = flag.Float64("kube-api-qps", 5, "QPS to use while communicating with the kubernetes apiserver. Defaults to 5.0.")
91-
kubeAPIBurst = flag.Int("kube-api-burst", 10, "Burst to use while communicating with the kubernetes apiserver. Defaults to 10.")
92-
9376
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.")
9477

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

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

120-
if *metricsAddress != "" && *httpEndpoint != "" {
104+
if standardflags.Configuration.MetricsAddress != "" && standardflags.Configuration.HttpEndpoint != "" {
121105
klog.ErrorS(nil, "Only one of `--metrics-address` and `--http-endpoint` can be set.")
122106
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
123107
}
124-
addr := *metricsAddress
108+
addr := standardflags.Configuration.MetricsAddress
125109
if addr == "" {
126-
addr = *httpEndpoint
110+
addr = standardflags.Configuration.HttpEndpoint
127111
}
128112
if err := utilfeature.DefaultMutableFeatureGate.SetFromMap(featureGates); err != nil {
129113
klog.ErrorS(err, "Failed to set feature gates")
@@ -132,8 +116,8 @@ func main() {
132116

133117
var config *rest.Config
134118
var err error
135-
if *master != "" || *kubeConfig != "" {
136-
config, err = clientcmd.BuildConfigFromFlags(*master, *kubeConfig)
119+
if *master != "" || standardflags.Configuration.KubeConfig != "" {
120+
config, err = clientcmd.BuildConfigFromFlags(*master, standardflags.Configuration.KubeConfig)
137121
} else {
138122
config, err = rest.InClusterConfig()
139123
}
@@ -142,8 +126,8 @@ func main() {
142126
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
143127
}
144128

145-
config.QPS = float32(*kubeAPIQPS)
146-
config.Burst = *kubeAPIBurst
129+
config.QPS = float32(standardflags.Configuration.KubeAPIQPS)
130+
config.Burst = standardflags.Configuration.KubeAPIBurst
147131
config.ContentType = runtime.ContentTypeProtobuf
148132

149133
kubeClient, err := kubernetes.NewForConfig(config)
@@ -174,7 +158,7 @@ func main() {
174158
metricsManager := metrics.NewCSIMetricsManager("" /* driverName */)
175159

176160
ctx := context.Background()
177-
csiClient, err := csi.New(ctx, *csiAddress, *timeout, metricsManager)
161+
csiClient, err := csi.New(ctx, standardflags.Configuration.CSIAddress, *timeout, metricsManager)
178162
if err != nil {
179163
klog.ErrorS(err, "Failed to create CSI client")
180164
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
@@ -190,7 +174,7 @@ func main() {
190174
translator := csitrans.New()
191175
if translator.IsMigratedCSIDriverByName(driverName) {
192176
metricsManager = metrics.NewCSIMetricsManagerWithOptions(driverName, metrics.WithMigration())
193-
migratedCsiClient, err := csi.New(ctx, *csiAddress, *timeout, metricsManager)
177+
migratedCsiClient, err := csi.New(ctx, standardflags.Configuration.CSIAddress, *timeout, metricsManager)
194178
if err != nil {
195179
klog.ErrorS(err, "Failed to create MigratedCSI client")
196180
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
@@ -235,13 +219,13 @@ func main() {
235219

236220
// Start HTTP server for metrics + leader election healthz
237221
if addr != "" {
238-
metricsManager.RegisterToServer(mux, *metricsPath)
222+
metricsManager.RegisterToServer(mux, standardflags.Configuration.MetricsPath)
239223
metricsManager.SetDriverName(driverName)
240224
go func() {
241225
klog.InfoS("ServeMux listening", "address", addr)
242226
err := http.ListenAndServe(addr, mux)
243227
if err != nil {
244-
klog.ErrorS(err, "Failed to start HTTP server", "address", addr, "metricsPath", *metricsPath)
228+
klog.ErrorS(err, "Failed to start HTTP server", "address", addr, "metricsPath", standardflags.Configuration.MetricsPath)
245229
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
246230
}
247231
}()
@@ -320,37 +304,15 @@ func main() {
320304
}
321305
}
322306

323-
if !*enableLeaderElection {
324-
run(ctx)
325-
} else {
326-
lockName := "external-resizer-" + util.SanitizeName(leaseHolder)
327-
leKubeClient, err := kubernetes.NewForConfig(config)
328-
if err != nil {
329-
klog.ErrorS(err, "Failed to create leKubeClient")
330-
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
331-
}
332-
le := leaderelection.NewLeaderElection(leKubeClient, lockName, run)
333-
if *httpEndpoint != "" {
334-
le.PrepareHealthCheck(mux, leaderelection.DefaultHealthCheckTimeout)
335-
}
336-
337-
if *leaderElectionNamespace != "" {
338-
le.WithNamespace(*leaderElectionNamespace)
339-
}
340-
341-
le.WithLeaseDuration(*leaderElectionLeaseDuration)
342-
le.WithRenewDeadline(*leaderElectionRenewDeadline)
343-
le.WithRetryPeriod(*leaderElectionRetryPeriod)
344-
if utilfeature.DefaultFeatureGate.Enabled(features.ReleaseLeaderElectionOnExit) {
345-
le.WithReleaseOnCancel(true)
346-
le.WithContext(ctx)
347-
}
348-
349-
if err := le.Run(); err != nil {
350-
klog.ErrorS(err, "Error initializing leader election")
351-
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
352-
}
353-
}
307+
leaderelection.RunWithLeaderElection(
308+
ctx,
309+
config,
310+
standardflags.Configuration,
311+
run,
312+
"external-resizer-"+util.SanitizeName(leaseHolder),
313+
mux,
314+
utilfeature.DefaultFeatureGate.Enabled(features.ReleaseLeaderElectionOnExit),
315+
)
354316
}
355317

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

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.25.5
55
require (
66
github.com/container-storage-interface/spec v1.12.0
77
github.com/google/go-cmp v0.7.0
8-
github.com/kubernetes-csi/csi-lib-utils v0.23.1
8+
github.com/kubernetes-csi/csi-lib-utils v0.23.2
99
google.golang.org/grpc v1.78.0
1010
k8s.io/api v0.35.0
1111
k8s.io/apimachinery v0.35.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
121121
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
122122
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
123123
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
124-
github.com/kubernetes-csi/csi-lib-utils v0.23.1 h1:G/vjykE0AJgxAOkoSGGnk2lyo9il9LqB4h7xjPWLQ/4=
125-
github.com/kubernetes-csi/csi-lib-utils v0.23.1/go.mod h1:aIcqnC6EyesZpe7kX5PxHUZePw1tKrYFKwg7RaqlPh8=
124+
github.com/kubernetes-csi/csi-lib-utils v0.23.2 h1:+x9W4RRyuRnJcTiSHKEFpl0cYUeLUqshM5ioPeYPRXw=
125+
github.com/kubernetes-csi/csi-lib-utils v0.23.2/go.mod h1:aIcqnC6EyesZpe7kX5PxHUZePw1tKrYFKwg7RaqlPh8=
126126
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
127127
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
128128
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=

vendor/github.com/kubernetes-csi/csi-lib-utils/standardflags/flags.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ github.com/inconshreveable/mousetrap
168168
# github.com/json-iterator/go v1.1.12
169169
## explicit; go 1.12
170170
github.com/json-iterator/go
171-
# github.com/kubernetes-csi/csi-lib-utils v0.23.1
171+
# github.com/kubernetes-csi/csi-lib-utils v0.23.2
172172
## explicit; go 1.24.6
173173
github.com/kubernetes-csi/csi-lib-utils/accessmodes
174174
github.com/kubernetes-csi/csi-lib-utils/connection

0 commit comments

Comments
 (0)