Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
105 changes: 105 additions & 0 deletions config/config.go
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) {

logger := klog.Background()

// Prepare http endpoint for metrics + leader election healthz
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm honestly not sure if we want to move this into this function as well, I did it because we need access to mux for le.PrepareHealthCheck(mux, leaderelection.DefaultHealthCheckTimeout) and as far as I can tell nothing else in the sidecar's main.go files reference the mux.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it looks odd here. Get the mux from the callers, as an argument.

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)
}
}
}
52 changes: 52 additions & 0 deletions features/features.go
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

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},
}

43 changes: 23 additions & 20 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
module github.com/kubernetes-csi/csi-lib-utils

go 1.24.0
go 1.24.6

require (
github.com/container-storage-interface/spec v1.11.0
github.com/prometheus/client_golang v1.22.0
github.com/stretchr/testify v1.10.0
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.58.0
go.opentelemetry.io/otel/trace v1.33.0
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.60.0
go.opentelemetry.io/otel/trace v1.35.0
go.uber.org/automaxprocs v1.6.0
google.golang.org/grpc v1.69.0
google.golang.org/grpc v1.72.1
google.golang.org/protobuf v1.36.5
k8s.io/api v0.33.1
k8s.io/client-go v0.33.1
k8s.io/component-base v0.33.1
k8s.io/api v0.34.1
k8s.io/client-go v0.34.1
k8s.io/component-base v0.34.1
k8s.io/klog/v2 v2.130.1
)

Expand All @@ -22,49 +22,52 @@ require (
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/emicklei/go-restful/v3 v3.12.2 // indirect
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/gnostic-models v0.6.9 // indirect
github.com/google/gnostic-models v0.7.0 // indirect
github.com/google/go-cmp v0.7.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mailru/easyjson v0.9.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.62.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/x448/float16 v0.8.4 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/otel v1.33.0 // indirect
go.opentelemetry.io/otel/metric v1.33.0 // indirect
go.opentelemetry.io/otel v1.35.0 // indirect
go.opentelemetry.io/otel/metric v1.35.0 // indirect
go.yaml.in/yaml/v2 v2.4.2 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/net v0.38.0 // indirect
golang.org/x/oauth2 v0.27.0 // indirect
golang.org/x/sys v0.31.0 // indirect
golang.org/x/term v0.30.0 // indirect
golang.org/x/text v0.23.0 // indirect
golang.org/x/time v0.9.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241216192217-9240e9c98484 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250303144028-a0af3efb3deb // indirect
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apimachinery v0.33.1 // indirect
k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff // indirect
k8s.io/utils v0.0.0-20241210054802-24370beab758 // indirect
k8s.io/apimachinery v0.34.1 // indirect
k8s.io/apiserver v0.34.1
k8s.io/kube-openapi v0.0.0-20250710124328-f3f2b991d03b // indirect
k8s.io/utils v0.0.0-20250604170112-4c0f3b243397 // indirect
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 // indirect
sigs.k8s.io/randfill v1.0.0 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.6.0 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
sigs.k8s.io/structured-merge-diff/v6 v6.3.0 // indirect
sigs.k8s.io/yaml v1.6.0 // indirect
)
Loading