Skip to content

Commit f9b84fa

Browse files
committed
Review feedback and using the new functionality in the sidecars
1 parent 00e101c commit f9b84fa

File tree

14 files changed

+97
-1275
lines changed

14 files changed

+97
-1275
lines changed

config/config.go

Lines changed: 15 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
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+
*/
116
package config
217

318
import (
4-
"context"
5-
"net/http"
6-
7-
"github.com/kubernetes-csi/csi-lib-utils/features"
8-
"github.com/kubernetes-csi/csi-lib-utils/leaderelection"
9-
"github.com/kubernetes-csi/csi-lib-utils/metrics"
1019
"github.com/kubernetes-csi/csi-lib-utils/standardflags"
11-
utilfeature "k8s.io/apiserver/pkg/util/feature"
12-
"k8s.io/client-go/kubernetes"
1320
"k8s.io/client-go/rest"
1421
"k8s.io/client-go/tools/clientcmd"
15-
"k8s.io/klog/v2"
1622
)
1723

1824
func BuildConfig(kubeconfig string, opts standardflags.SidecarConfiguration) (*rest.Config, error) {
@@ -25,81 +31,9 @@ func BuildConfig(kubeconfig string, opts standardflags.SidecarConfiguration) (*r
2531
return config, nil
2632
}
2733

28-
2934
func buildConfig(kubeconfig string) (*rest.Config, error) {
3035
if kubeconfig != "" {
3136
return clientcmd.BuildConfigFromFlags("", kubeconfig)
3237
}
3338
return rest.InClusterConfig()
3439
}
35-
36-
func RunWithLeaderElection(ctx context.Context,
37-
config *rest.Config,
38-
opts standardflags.SidecarConfiguration,
39-
run func(context.Context),
40-
driverName string,
41-
metricsManager metrics.CSIMetricsManager) {
42-
43-
logger := klog.Background()
44-
45-
// Prepare http endpoint for metrics + leader election healthz
46-
mux := http.NewServeMux()
47-
addr := opts.MetricsAddress
48-
if addr == "" {
49-
addr = opts.HttpEndpoint
50-
}
51-
52-
if addr != "" {
53-
metricsManager.RegisterToServer(mux, opts.MetricsPath)
54-
metricsManager.SetDriverName(driverName)
55-
go func() {
56-
logger.Info("ServeMux listening", "address", addr, "metricsPath", opts.MetricsPath)
57-
err := http.ListenAndServe(addr, mux)
58-
if err != nil {
59-
logger.Error(err, "Failed to start HTTP server at specified address and metrics path", "address", addr, "metricsPath", opts.MetricsPath)
60-
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
61-
}
62-
}()
63-
}
64-
65-
if !opts.LeaderElection {
66-
run(klog.NewContext(context.Background(), logger))
67-
} else {
68-
// Create a new clientset for leader election. When the attacher
69-
// gets busy and its client gets throttled, the leader election
70-
// can proceed without issues.
71-
leClientset, err := kubernetes.NewForConfig(config)
72-
if err != nil {
73-
logger.Error(err, "Failed to create leaderelection client")
74-
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
75-
}
76-
77-
// Name of config map with leader election lock
78-
le := leaderelection.NewLeaderElection(leClientset, driverName, run)
79-
if opts.HttpEndpoint != "" {
80-
le.PrepareHealthCheck(mux, leaderelection.DefaultHealthCheckTimeout)
81-
}
82-
83-
if opts.LeaderElectionNamespace != "" {
84-
le.WithNamespace(opts.LeaderElectionNamespace)
85-
}
86-
87-
// TODO: uncomment once https://github.com/kubernetes-csi/csi-lib-utils/pull/200 is merged
88-
//if opts.LeaderElectionLabels != nil {
89-
// le.WithLabels(opts.LeaderElectionLabels)
90-
//}
91-
92-
le.WithLeaseDuration(opts.LeaderElectionLeaseDuration)
93-
le.WithRenewDeadline(opts.LeaderElectionRenewDeadline)
94-
le.WithRetryPeriod(opts.LeaderElectionRetryPeriod)
95-
if utilfeature.DefaultFeatureGate.Enabled(features.ReleaseLeaderElectionOnExit) {
96-
le.WithReleaseOnCancel(true)
97-
le.WithContext(ctx)
98-
}
99-
100-
if err := le.Run(); err != nil {
101-
logger.Error(err, "Failed to initialize leader election")
102-
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
103-
}
104-
}
105-
}

features/features.go

Lines changed: 0 additions & 52 deletions
This file was deleted.

go.mod

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ require (
6363
gopkg.in/inf.v0 v0.9.1 // indirect
6464
gopkg.in/yaml.v3 v3.0.1 // indirect
6565
k8s.io/apimachinery v0.34.1 // indirect
66-
<<<<<<< HEAD
67-
k8s.io/apiserver v0.34.1
68-
=======
69-
>>>>>>> master
7066
k8s.io/kube-openapi v0.0.0-20250710124328-f3f2b991d03b // indirect
7167
k8s.io/utils v0.0.0-20250604170112-4c0f3b243397 // indirect
7268
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 // indirect

leaderelection/leader_election.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ import (
2525
"strings"
2626
"time"
2727

28+
"github.com/kubernetes-csi/csi-lib-utils/standardflags"
2829
v1 "k8s.io/api/core/v1"
2930
"k8s.io/client-go/kubernetes"
3031
"k8s.io/client-go/kubernetes/scheme"
3132
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
33+
"k8s.io/client-go/rest"
3234
"k8s.io/client-go/tools/leaderelection"
3335
"k8s.io/client-go/tools/leaderelection/resourcelock"
3436
"k8s.io/client-go/tools/record"
@@ -215,6 +217,58 @@ func (l *leaderElection) Run() error {
215217
return nil // should never reach here
216218
}
217219

220+
func RunWithLeaderElection(
221+
ctx context.Context,
222+
config *rest.Config,
223+
opts standardflags.SidecarConfiguration,
224+
run func(context.Context),
225+
driverName string,
226+
mux *http.ServeMux,
227+
releaseOnExit bool) {
228+
229+
logger := klog.FromContext(ctx)
230+
231+
if !opts.LeaderElection {
232+
run(klog.NewContext(context.Background(), logger))
233+
} else {
234+
// Create a new clientset for leader election. When the attacher
235+
// gets busy and its client gets throttled, the leader election
236+
// can proceed without issues.
237+
leClientset, err := kubernetes.NewForConfig(config)
238+
if err != nil {
239+
logger.Error(err, "Failed to create leaderelection client")
240+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
241+
}
242+
243+
// Name of config map with leader election lock
244+
le := NewLeaderElection(leClientset, driverName, run)
245+
if opts.HttpEndpoint != "" {
246+
le.PrepareHealthCheck(mux, DefaultHealthCheckTimeout)
247+
}
248+
249+
if opts.LeaderElectionNamespace != "" {
250+
le.WithNamespace(opts.LeaderElectionNamespace)
251+
}
252+
253+
if opts.LeaderElectionLabels != nil {
254+
le.WithLabels(opts.LeaderElectionLabels)
255+
}
256+
257+
le.WithLeaseDuration(opts.LeaderElectionLeaseDuration)
258+
le.WithRenewDeadline(opts.LeaderElectionRenewDeadline)
259+
le.WithRetryPeriod(opts.LeaderElectionRetryPeriod)
260+
if releaseOnExit {
261+
le.WithReleaseOnCancel(true)
262+
le.WithContext(ctx)
263+
}
264+
265+
if err := le.Run(); err != nil {
266+
logger.Error(err, "Failed to initialize leader election")
267+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
268+
}
269+
}
270+
}
271+
218272
func defaultLeaderElectionIdentity() (string, error) {
219273
return os.Hostname()
220274
}

standardflags/flags.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
66
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
http://www.apache.org/licenses/LICENSE-2.0
99
1010
Unless required by applicable law or agreed to in writing, software
1111
distributed under the License is distributed on an "AS IS" BASIS,
@@ -19,46 +19,37 @@ package standardflags
1919
import (
2020
"flag"
2121
"fmt"
22-
"time"
2322
"strings"
23+
"time"
2424
)
2525

26-
2726
type SidecarConfiguration struct {
2827
ShowVersion bool
2928

30-
Master string
3129
KubeConfig string
3230
CSIAddress string
33-
Resync time.Duration
34-
35-
RetryIntervalStart time.Duration
36-
RetryIntervalMax time.Duration
3731

3832
LeaderElection bool
3933
LeaderElectionNamespace string
4034
LeaderElectionLeaseDuration time.Duration
4135
LeaderElectionRenewDeadline time.Duration
4236
LeaderElectionRetryPeriod time.Duration
43-
LeaderElectionLabels stringMap
37+
LeaderElectionLabels stringMap
4438

4539
KubeAPIQPS float64
4640
KubeAPIBurst int
4741

48-
HttpEndpoint string
42+
HttpEndpoint string
4943
MetricsAddress string
50-
MetricsPath string
44+
MetricsPath string
5145
}
5246

5347
var Configuration = SidecarConfiguration{}
5448

5549
func RegisterCommonFlags(flags *flag.FlagSet) {
5650
flags.BoolVar(&Configuration.ShowVersion, "version", false, "Show version.")
57-
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.")
5851
flags.StringVar(&Configuration.KubeConfig, "kubeconfig", "", "Absolute path to the kubeconfig file. Required only when running out of cluster.")
5952
flags.StringVar(&Configuration.CSIAddress, "csi-address", "/run/csi/socket", "The gRPC endpoint for Target CSI Volume.")
60-
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.")
61-
flags.DurationVar(&Configuration.RetryIntervalMax, "retry-interval-max", 5*time.Minute, "Maximum retry interval of failed create volume or deletion.")
6253
flags.BoolVar(&Configuration.LeaderElection, "leader-election", false, "Enable leader election.")
6354
flags.StringVar(&Configuration.LeaderElectionNamespace, "leader-election-namespace", "", "Namespace where the leader election resource lives. Defaults to the pod namespace if not set.")
6455
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.")
@@ -72,7 +63,6 @@ func RegisterCommonFlags(flags *flag.FlagSet) {
7263
flag.StringVar(&Configuration.MetricsPath, "metrics-path", "/metrics", "The HTTP path where prometheus metrics will be exposed. Default is `/metrics`.")
7364
}
7465

75-
7666
type stringMap map[string]string
7767

7868
func (sm *stringMap) String() string {
@@ -91,4 +81,3 @@ func (sm *stringMap) Set(value string) error {
9181
}
9282
return nil
9383
}
94-

vendor/go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc/stats_handler.go

Lines changed: 20 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)