Skip to content

Commit 4814a37

Browse files
committed
refactor: Move leader election logic into csi-lib-utils and genericize
1 parent e24562b commit 4814a37

File tree

713 files changed

+71791
-27833
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

713 files changed

+71791
-27833
lines changed

config/config.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package config
2+
3+
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"
10+
"github.com/kubernetes-csi/csi-lib-utils/standardflags"
11+
utilfeature "k8s.io/apiserver/pkg/util/feature"
12+
"k8s.io/client-go/kubernetes"
13+
"k8s.io/client-go/rest"
14+
"k8s.io/client-go/tools/clientcmd"
15+
"k8s.io/klog/v2"
16+
)
17+
18+
func BuildConfig(kubeconfig string, opts standardflags.SidecarConfiguration) (*rest.Config, error) {
19+
config, err := buildConfig(kubeconfig)
20+
if err != nil {
21+
return config, err
22+
}
23+
config.QPS = float32(opts.KubeAPIQPS)
24+
config.Burst = opts.KubeAPIBurst
25+
return config, nil
26+
}
27+
28+
29+
func buildConfig(kubeconfig string) (*rest.Config, error) {
30+
if kubeconfig != "" {
31+
return clientcmd.BuildConfigFromFlags("", kubeconfig)
32+
}
33+
return rest.InClusterConfig()
34+
}
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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 features
18+
19+
import (
20+
"k8s.io/apiserver/pkg/util/feature"
21+
"k8s.io/component-base/featuregate"
22+
)
23+
24+
const (
25+
// owner: @torredil @gnufied @msau42
26+
// kep: https://kep.k8s.io/4876
27+
// alpha: v1.33
28+
// beta: v1.34
29+
//
30+
// Makes CSINode.Spec.Drivers[*].Allocatable.Count mutable, allowing CSI drivers to
31+
// update the number of volumes that can be allocated on a node. Additionally, enables
32+
// setting ErrorCode field in VolumeAttachment status.
33+
MutableCSINodeAllocatableCount featuregate.Feature = "MutableCSINodeAllocatableCount"
34+
35+
// owner: @rhrmo
36+
// alpha: v1.34
37+
//
38+
// Releases leader election lease on sigterm / sigint.
39+
ReleaseLeaderElectionOnExit featuregate.Feature = "ReleaseLeaderElectionOnExit"
40+
)
41+
42+
func init() {
43+
feature.DefaultMutableFeatureGate.Add(defaultKubernetesFeatureGates)
44+
}
45+
46+
// defaultKubernetesFeatureGates consists of all known feature keys specific to external-attacher.
47+
// To add a new feature, define a key for it above and add it here.
48+
var defaultKubernetesFeatureGates = map[featuregate.Feature]featuregate.FeatureSpec{
49+
ReleaseLeaderElectionOnExit: {Default: false, PreRelease: featuregate.Alpha},
50+
MutableCSINodeAllocatableCount: {Default: false, PreRelease: featuregate.Beta},
51+
}
52+

go.mod

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
module github.com/kubernetes-csi/csi-lib-utils
22

3-
go 1.24.0
3+
go 1.24.6
44

55
require (
66
github.com/container-storage-interface/spec v1.11.0
77
github.com/prometheus/client_golang v1.22.0
88
github.com/stretchr/testify v1.10.0
9-
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.58.0
10-
go.opentelemetry.io/otel/trace v1.33.0
9+
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.60.0
10+
go.opentelemetry.io/otel/trace v1.35.0
1111
go.uber.org/automaxprocs v1.6.0
12-
google.golang.org/grpc v1.69.0
12+
google.golang.org/grpc v1.72.1
1313
google.golang.org/protobuf v1.36.5
14-
k8s.io/api v0.33.1
15-
k8s.io/client-go v0.33.1
16-
k8s.io/component-base v0.33.1
14+
k8s.io/api v0.34.1
15+
k8s.io/client-go v0.34.1
16+
k8s.io/component-base v0.34.1
1717
k8s.io/klog/v2 v2.130.1
1818
)
1919

@@ -22,49 +22,52 @@ require (
2222
github.com/blang/semver/v4 v4.0.0 // indirect
2323
github.com/cespare/xxhash/v2 v2.3.0 // indirect
2424
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
25-
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
26-
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
25+
github.com/emicklei/go-restful/v3 v3.12.2 // indirect
26+
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
2727
github.com/go-logr/logr v1.4.2 // indirect
2828
github.com/go-logr/stdr v1.2.2 // indirect
2929
github.com/go-openapi/jsonpointer v0.21.0 // indirect
3030
github.com/go-openapi/jsonreference v0.21.0 // indirect
3131
github.com/go-openapi/swag v0.23.0 // indirect
3232
github.com/gogo/protobuf v1.3.2 // indirect
33-
github.com/google/gnostic-models v0.6.9 // indirect
33+
github.com/google/gnostic-models v0.7.0 // indirect
3434
github.com/google/go-cmp v0.7.0 // indirect
3535
github.com/google/uuid v1.6.0 // indirect
3636
github.com/josharian/intern v1.0.0 // indirect
3737
github.com/json-iterator/go v1.1.12 // indirect
3838
github.com/kylelemons/godebug v1.1.0 // indirect
3939
github.com/mailru/easyjson v0.9.0 // indirect
4040
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
41-
github.com/modern-go/reflect2 v1.0.2 // indirect
41+
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
4242
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
4343
github.com/pkg/errors v0.9.1 // indirect
4444
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
4545
github.com/prometheus/client_model v0.6.1 // indirect
4646
github.com/prometheus/common v0.62.0 // indirect
4747
github.com/prometheus/procfs v0.15.1 // indirect
48-
github.com/spf13/pflag v1.0.5 // indirect
48+
github.com/spf13/pflag v1.0.6 // indirect
4949
github.com/x448/float16 v0.8.4 // indirect
5050
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
51-
go.opentelemetry.io/otel v1.33.0 // indirect
52-
go.opentelemetry.io/otel/metric v1.33.0 // indirect
51+
go.opentelemetry.io/otel v1.35.0 // indirect
52+
go.opentelemetry.io/otel/metric v1.35.0 // indirect
53+
go.yaml.in/yaml/v2 v2.4.2 // indirect
54+
go.yaml.in/yaml/v3 v3.0.4 // indirect
5355
golang.org/x/net v0.38.0 // indirect
5456
golang.org/x/oauth2 v0.27.0 // indirect
5557
golang.org/x/sys v0.31.0 // indirect
5658
golang.org/x/term v0.30.0 // indirect
5759
golang.org/x/text v0.23.0 // indirect
5860
golang.org/x/time v0.9.0 // indirect
59-
google.golang.org/genproto/googleapis/rpc v0.0.0-20241216192217-9240e9c98484 // indirect
61+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250303144028-a0af3efb3deb // indirect
6062
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
6163
gopkg.in/inf.v0 v0.9.1 // indirect
6264
gopkg.in/yaml.v3 v3.0.1 // indirect
63-
k8s.io/apimachinery v0.33.1 // indirect
64-
k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff // indirect
65-
k8s.io/utils v0.0.0-20241210054802-24370beab758 // indirect
65+
k8s.io/apimachinery v0.34.1 // indirect
66+
k8s.io/apiserver v0.34.1
67+
k8s.io/kube-openapi v0.0.0-20250710124328-f3f2b991d03b // indirect
68+
k8s.io/utils v0.0.0-20250604170112-4c0f3b243397 // indirect
6669
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 // indirect
6770
sigs.k8s.io/randfill v1.0.0 // indirect
68-
sigs.k8s.io/structured-merge-diff/v4 v4.6.0 // indirect
69-
sigs.k8s.io/yaml v1.4.0 // indirect
71+
sigs.k8s.io/structured-merge-diff/v6 v6.3.0 // indirect
72+
sigs.k8s.io/yaml v1.6.0 // indirect
7073
)

0 commit comments

Comments
 (0)