|
| 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 | + "strings" |
| 23 | + "time" |
| 24 | +) |
| 25 | + |
| 26 | +type SidecarConfiguration struct { |
| 27 | + ShowVersion bool |
| 28 | + |
| 29 | + KubeConfig string |
| 30 | + CSIAddress string |
| 31 | + |
| 32 | + LeaderElection bool |
| 33 | + LeaderElectionNamespace string |
| 34 | + LeaderElectionLeaseDuration time.Duration |
| 35 | + LeaderElectionRenewDeadline time.Duration |
| 36 | + LeaderElectionRetryPeriod time.Duration |
| 37 | + LeaderElectionLabels stringMap |
| 38 | + |
| 39 | + KubeAPIQPS float64 |
| 40 | + KubeAPIBurst int |
| 41 | + |
| 42 | + HttpEndpoint string |
| 43 | + MetricsAddress string |
| 44 | + MetricsPath string |
| 45 | +} |
| 46 | + |
| 47 | +var Configuration = SidecarConfiguration{} |
| 48 | + |
| 49 | +func RegisterCommonFlags(flags *flag.FlagSet) { |
| 50 | + flags.BoolVar(&Configuration.ShowVersion, "version", false, "Show version.") |
| 51 | + flags.StringVar(&Configuration.KubeConfig, "kubeconfig", "", "Absolute path to the kubeconfig file. Required only when running out of cluster.") |
| 52 | + flags.StringVar(&Configuration.CSIAddress, "csi-address", "/run/csi/socket", "The gRPC endpoint for Target CSI Volume.") |
| 53 | + flags.BoolVar(&Configuration.LeaderElection, "leader-election", false, "Enable leader election.") |
| 54 | + flags.StringVar(&Configuration.LeaderElectionNamespace, "leader-election-namespace", "", "Namespace where the leader election resource lives. Defaults to the pod namespace if not set.") |
| 55 | + 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.") |
| 56 | + 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.") |
| 57 | + 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.") |
| 58 | + 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'") |
| 59 | + flags.Float64Var(&Configuration.KubeAPIQPS, "kube-api-qps", 5, "QPS to use while communicating with the kubernetes apiserver. Defaults to 5.0.") |
| 60 | + flags.IntVar(&Configuration.KubeAPIBurst, "kube-api-burst", 10, "Burst to use while communicating with the kubernetes apiserver. Defaults to 10.") |
| 61 | + flags.StringVar(&Configuration.HttpEndpoint, "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.") |
| 62 | + flags.StringVar(&Configuration.MetricsAddress, "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.") |
| 63 | + flag.StringVar(&Configuration.MetricsPath, "metrics-path", "/metrics", "The HTTP path where prometheus metrics will be exposed. Default is `/metrics`.") |
| 64 | +} |
| 65 | + |
| 66 | +type stringMap map[string]string |
| 67 | + |
| 68 | +func (sm *stringMap) String() string { |
| 69 | + return fmt.Sprintf("%s", *sm) |
| 70 | +} |
| 71 | + |
| 72 | +func (sm *stringMap) Set(value string) error { |
| 73 | + outMap := *sm |
| 74 | + items := strings.Split(value, ",") |
| 75 | + for _, i := range items { |
| 76 | + label := strings.Split(i, ":") |
| 77 | + if len(label) != 2 { |
| 78 | + return fmt.Errorf("malformed item in list of labels: %s", i) |
| 79 | + } |
| 80 | + outMap[label[0]] = label[1] |
| 81 | + } |
| 82 | + return nil |
| 83 | +} |
0 commit comments