Skip to content

Commit 888ae82

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

File tree

14 files changed

+80
-1266
lines changed

14 files changed

+80
-1266
lines changed

config/config.go

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
package config
22

33
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"
104
"github.com/kubernetes-csi/csi-lib-utils/standardflags"
11-
utilfeature "k8s.io/apiserver/pkg/util/feature"
12-
"k8s.io/client-go/kubernetes"
135
"k8s.io/client-go/rest"
146
"k8s.io/client-go/tools/clientcmd"
15-
"k8s.io/klog/v2"
167
)
178

189
func BuildConfig(kubeconfig string, opts standardflags.SidecarConfiguration) (*rest.Config, error) {
@@ -33,73 +24,3 @@ func buildConfig(kubeconfig string) (*rest.Config, error) {
3324
return rest.InClusterConfig()
3425
}
3526

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: 53 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,57 @@ func (l *leaderElection) Run() error {
215217
return nil // should never reach here
216218
}
217219

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

standardflags/flags.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ package standardflags
1919
import (
2020
"flag"
2121
"fmt"
22-
"time"
2322
"strings"
23+
"time"
2424
)
2525

2626

@@ -40,25 +40,22 @@ type SidecarConfiguration struct {
4040
LeaderElectionLeaseDuration time.Duration
4141
LeaderElectionRenewDeadline time.Duration
4242
LeaderElectionRetryPeriod time.Duration
43-
LeaderElectionLabels stringMap
43+
LeaderElectionLabels stringMap
4444

4545
KubeAPIQPS float64
4646
KubeAPIBurst int
4747

48-
HttpEndpoint string
48+
HttpEndpoint string
4949
MetricsAddress string
50-
MetricsPath string
50+
MetricsPath string
5151
}
5252

5353
var Configuration = SidecarConfiguration{}
5454

5555
func RegisterCommonFlags(flags *flag.FlagSet) {
5656
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.")
5857
flags.StringVar(&Configuration.KubeConfig, "kubeconfig", "", "Absolute path to the kubeconfig file. Required only when running out of cluster.")
5958
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.")
6259
flags.BoolVar(&Configuration.LeaderElection, "leader-election", false, "Enable leader election.")
6360
flags.StringVar(&Configuration.LeaderElectionNamespace, "leader-election-namespace", "", "Namespace where the leader election resource lives. Defaults to the pod namespace if not set.")
6461
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 +69,6 @@ func RegisterCommonFlags(flags *flag.FlagSet) {
7269
flag.StringVar(&Configuration.MetricsPath, "metrics-path", "/metrics", "The HTTP path where prometheus metrics will be exposed. Default is `/metrics`.")
7370
}
7471

75-
7672
type stringMap map[string]string
7773

7874
func (sm *stringMap) String() string {
@@ -91,4 +87,3 @@ func (sm *stringMap) Set(value string) error {
9187
}
9288
return nil
9389
}
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.

vendor/go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc/version.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.

0 commit comments

Comments
 (0)