-
Notifications
You must be signed in to change notification settings - Fork 61
Adding a package to contain common sidecar flags #202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
e24562b
4814a37
00e101c
f9b84fa
ff335e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
|
|
||
| "github.com/kubernetes-csi/csi-lib-utils/features" | ||
| "github.com/kubernetes-csi/csi-lib-utils/leaderelection" | ||
| "github.com/kubernetes-csi/csi-lib-utils/metrics" | ||
| "github.com/kubernetes-csi/csi-lib-utils/standardflags" | ||
| utilfeature "k8s.io/apiserver/pkg/util/feature" | ||
| "k8s.io/client-go/kubernetes" | ||
| "k8s.io/client-go/rest" | ||
| "k8s.io/client-go/tools/clientcmd" | ||
| "k8s.io/klog/v2" | ||
| ) | ||
|
|
||
| func BuildConfig(kubeconfig string, opts standardflags.SidecarConfiguration) (*rest.Config, error) { | ||
| config, err := buildConfig(kubeconfig) | ||
| if err != nil { | ||
| return config, err | ||
| } | ||
| config.QPS = float32(opts.KubeAPIQPS) | ||
| config.Burst = opts.KubeAPIBurst | ||
| return config, nil | ||
| } | ||
|
|
||
|
|
||
| func buildConfig(kubeconfig string) (*rest.Config, error) { | ||
| if kubeconfig != "" { | ||
| return clientcmd.BuildConfigFromFlags("", kubeconfig) | ||
| } | ||
| return rest.InClusterConfig() | ||
| } | ||
|
|
||
| func RunWithLeaderElection(ctx context.Context, | ||
| config *rest.Config, | ||
| opts standardflags.SidecarConfiguration, | ||
| run func(context.Context), | ||
| driverName string, | ||
| metricsManager metrics.CSIMetricsManager) { | ||
DerekFrank marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| logger := klog.Background() | ||
DerekFrank marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Prepare http endpoint for metrics + leader election healthz | ||
|
||
| mux := http.NewServeMux() | ||
| addr := opts.MetricsAddress | ||
| if addr == "" { | ||
| addr = opts.HttpEndpoint | ||
| } | ||
|
|
||
| if addr != "" { | ||
| metricsManager.RegisterToServer(mux, opts.MetricsPath) | ||
| metricsManager.SetDriverName(driverName) | ||
| go func() { | ||
| logger.Info("ServeMux listening", "address", addr, "metricsPath", opts.MetricsPath) | ||
| err := http.ListenAndServe(addr, mux) | ||
| if err != nil { | ||
| logger.Error(err, "Failed to start HTTP server at specified address and metrics path", "address", addr, "metricsPath", opts.MetricsPath) | ||
| klog.FlushAndExit(klog.ExitFlushTimeout, 1) | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| if !opts.LeaderElection { | ||
| run(klog.NewContext(context.Background(), logger)) | ||
| } else { | ||
| // Create a new clientset for leader election. When the attacher | ||
| // gets busy and its client gets throttled, the leader election | ||
| // can proceed without issues. | ||
| leClientset, err := kubernetes.NewForConfig(config) | ||
| if err != nil { | ||
| logger.Error(err, "Failed to create leaderelection client") | ||
| klog.FlushAndExit(klog.ExitFlushTimeout, 1) | ||
| } | ||
|
|
||
| // Name of config map with leader election lock | ||
| le := leaderelection.NewLeaderElection(leClientset, driverName, run) | ||
| if opts.HttpEndpoint != "" { | ||
| le.PrepareHealthCheck(mux, leaderelection.DefaultHealthCheckTimeout) | ||
| } | ||
|
|
||
| if opts.LeaderElectionNamespace != "" { | ||
| le.WithNamespace(opts.LeaderElectionNamespace) | ||
| } | ||
|
|
||
| // TODO: uncomment once https://github.com/kubernetes-csi/csi-lib-utils/pull/200 is merged | ||
| //if opts.LeaderElectionLabels != nil { | ||
| // le.WithLabels(opts.LeaderElectionLabels) | ||
| //} | ||
|
|
||
| le.WithLeaseDuration(opts.LeaderElectionLeaseDuration) | ||
| le.WithRenewDeadline(opts.LeaderElectionRenewDeadline) | ||
| le.WithRetryPeriod(opts.LeaderElectionRetryPeriod) | ||
| if utilfeature.DefaultFeatureGate.Enabled(features.ReleaseLeaderElectionOnExit) { | ||
| le.WithReleaseOnCancel(true) | ||
| le.WithContext(ctx) | ||
| } | ||
|
|
||
| if err := le.Run(); err != nil { | ||
| logger.Error(err, "Failed to initialize leader election") | ||
| klog.FlushAndExit(klog.ExitFlushTimeout, 1) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| Copyright 2025 The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package features | ||
DerekFrank marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| import ( | ||
| "k8s.io/apiserver/pkg/util/feature" | ||
| "k8s.io/component-base/featuregate" | ||
| ) | ||
|
|
||
| const ( | ||
| // owner: @torredil @gnufied @msau42 | ||
| // kep: https://kep.k8s.io/4876 | ||
| // alpha: v1.33 | ||
| // beta: v1.34 | ||
| // | ||
| // Makes CSINode.Spec.Drivers[*].Allocatable.Count mutable, allowing CSI drivers to | ||
| // update the number of volumes that can be allocated on a node. Additionally, enables | ||
| // setting ErrorCode field in VolumeAttachment status. | ||
| MutableCSINodeAllocatableCount featuregate.Feature = "MutableCSINodeAllocatableCount" | ||
|
|
||
| // owner: @rhrmo | ||
| // alpha: v1.34 | ||
| // | ||
| // Releases leader election lease on sigterm / sigint. | ||
| ReleaseLeaderElectionOnExit featuregate.Feature = "ReleaseLeaderElectionOnExit" | ||
| ) | ||
|
|
||
| func init() { | ||
| feature.DefaultMutableFeatureGate.Add(defaultKubernetesFeatureGates) | ||
| } | ||
|
|
||
| // defaultKubernetesFeatureGates consists of all known feature keys specific to external-attacher. | ||
| // To add a new feature, define a key for it above and add it here. | ||
| var defaultKubernetesFeatureGates = map[featuregate.Feature]featuregate.FeatureSpec{ | ||
| ReleaseLeaderElectionOnExit: {Default: false, PreRelease: featuregate.Alpha}, | ||
| MutableCSINodeAllocatableCount: {Default: false, PreRelease: featuregate.Beta}, | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.