Skip to content

Commit e24562b

Browse files
committed
Adding common flags to the standard flags package
1 parent 00c4441 commit e24562b

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

standardflags/flags.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package standardflags
18+
19+
import (
20+
"flag"
21+
"fmt"
22+
"time"
23+
"strings"
24+
)
25+
26+
27+
type SidecarConfiguration struct {
28+
ShowVersion bool
29+
30+
Master string
31+
KubeConfig string
32+
CSIAddress string
33+
Resync time.Duration
34+
35+
RetryIntervalStart time.Duration
36+
RetryIntervalMax time.Duration
37+
38+
LeaderElection bool
39+
LeaderElectionNamespace string
40+
LeaderElectionLeaseDuration time.Duration
41+
LeaderElectionRenewDeadline time.Duration
42+
LeaderElectionRetryPeriod time.Duration
43+
LeaderElectionLabels stringMap
44+
45+
KubeAPIQPS float64
46+
KubeAPIBurst int
47+
}
48+
49+
var Configuration = SidecarConfiguration{}
50+
51+
func RegisterCommonFlags(flags *flag.FlagSet) {
52+
flags.BoolVar(&Configuration.ShowVersion, "version", false, "Show version.")
53+
flags.StringVar(&Configuration.Master, "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.")
54+
flags.StringVar(&Configuration.KubeConfig, "kubeconfig", "", "Absolute path to the kubeconfig file. Required only when running out of cluster.")
55+
flags.StringVar(&Configuration.CSIAddress, "csi-address", "/run/csi/socket", "The gRPC endpoint for Target CSI Volume.")
56+
flags.DurationVar(&Configuration.RetryIntervalStart, "retry-interval-start", time.Second, "Initial retry interval of failed create volume or deletion. It doubles with each failure, up to retry-interval-max.")
57+
flags.DurationVar(&Configuration.RetryIntervalMax, "retry-interval-max", 5*time.Minute, "Maximum retry interval of failed create volume or deletion.")
58+
flags.BoolVar(&Configuration.LeaderElection, "leader-election", false, "Enable leader election.")
59+
flags.StringVar(&Configuration.LeaderElectionNamespace, "leader-election-namespace", "", "Namespace where the leader election resource lives. Defaults to the pod namespace if not set.")
60+
flags.DurationVar(&Configuration.LeaderElectionLeaseDuration, "leader-election-lease-duration", 15*time.Second, "Duration, in seconds, that non-leader candidates will wait to force acquire leadership. Defaults to 15 seconds.")
61+
flags.DurationVar(&Configuration.LeaderElectionRenewDeadline, "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.")
62+
flags.DurationVar(&Configuration.LeaderElectionRetryPeriod, "leader-election-retry-period", 5*time.Second, "Duration, in seconds, the LeaderElector clients should wait between tries of actions. Defaults to 5 seconds.")
63+
flags.Var(&Configuration.LeaderElectionLabels, "leader-election-labels", "List of labels to add to lease when given replica becomes leader. Formatted as a comma seperated list of key:value labels. Example: 'my-label:my-value,my-second-label:my-second-value'")
64+
flags.Float64Var(&Configuration.KubeAPIQPS, "kube-api-qps", 5, "QPS to use while communicating with the kubernetes apiserver. Defaults to 5.0.")
65+
flags.IntVar(&Configuration.KubeAPIBurst, "kube-api-burst", 10, "Burst to use while communicating with the kubernetes apiserver. Defaults to 10.")
66+
}
67+
68+
69+
type stringMap map[string]string
70+
71+
func (sm *stringMap) String() string {
72+
return fmt.Sprintf("%s", *sm)
73+
}
74+
75+
func (sm *stringMap) Set(value string) error {
76+
outMap := *sm
77+
items := strings.Split(value, ",")
78+
for _, i := range items {
79+
label := strings.Split(i, ":")
80+
if len(label) != 2 {
81+
return fmt.Errorf("malformed item in list of labels", "arg", i)
82+
}
83+
outMap[label[0]] = label[1]
84+
}
85+
return nil
86+
}
87+

0 commit comments

Comments
 (0)