Skip to content

Commit 5c04e92

Browse files
committed
add daemonset hook for per node volume limit
Since NodeChecker now checks max attachment limit value it is now safe to add a hook for reflecting maxAllowedBlockVolumesPerNode field of clusterCSIDriver into daemonset as env variable.
1 parent 3566264 commit 5c04e92

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

pkg/operator/vspherecontroller/driver_starter.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import (
44
"context"
55
"crypto/sha256"
66
"fmt"
7+
"github.com/openshift/api/features"
78
"os"
9+
"strconv"
810

911
"github.com/openshift/library-go/pkg/operator/resource/resourcehash"
1012
"github.com/openshift/vmware-vsphere-csi-driver-operator/pkg/operator/utils"
1113

1214
operatorapi "github.com/openshift/api/operator/v1"
1315
"github.com/openshift/library-go/pkg/controller/factory"
16+
"github.com/openshift/library-go/pkg/operator/configobserver/featuregates"
1417
"github.com/openshift/library-go/pkg/operator/csi/csicontrollerset"
1518
"github.com/openshift/library-go/pkg/operator/csi/csidrivercontrollerservicecontroller"
1619
"github.com/openshift/library-go/pkg/operator/csi/csidrivernodeservicecontroller"
@@ -23,6 +26,8 @@ import (
2326
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2427
corev1informers "k8s.io/client-go/informers/core/v1"
2528
"k8s.io/klog/v2"
29+
30+
operatorlistersv1 "github.com/openshift/client-go/operator/listers/operator/v1"
2631
)
2732

2833
func (c *VSphereController) createCSIDriver() {
@@ -150,6 +155,7 @@ func (c *VSphereController) createCSIDriver() {
150155
c.apiClients.ConfigMapInformer,
151156
),
152157
WithSecretDaemonSetAnnotationHook(driverConfigSecretName, defaultNamespace, c.apiClients.SecretInformer),
158+
WithMaxVolumesPerNodeDaemonSetHook(c.apiClients.ClusterCSIDriverInformer.Lister(), c.featureGates),
153159
).WithServiceMonitorController(
154160
"VMWareVSphereDriverServiceMonitorController",
155161
c.apiClients.DynamicClient,
@@ -324,3 +330,46 @@ func getOperatorSyncState(operatorClient v1helpers.OperatorClientWithFinalizers)
324330
}
325331
return opSpec.ManagementState
326332
}
333+
334+
// WithMaxVolumesPerNodeDaemonSetHook sets the MAX_VOLUMES_PER_NODE environment variable in the DaemonSet container specifications.
335+
func WithMaxVolumesPerNodeDaemonSetHook(clusterCSIDriverLister operatorlistersv1.ClusterCSIDriverLister, featureGate featuregates.FeatureGate) csidrivernodeservicecontroller.DaemonSetHookFunc {
336+
return func(opSpec *operatorapi.OperatorSpec, ds *appsv1.DaemonSet) error {
337+
if !featureGate.Enabled(features.FeatureGateVSphereConfigurableMaxAllowedBlockVolumesPerNode) {
338+
return nil
339+
}
340+
341+
// Get ClusterCSIDriver object
342+
clusterCSIDriver, err := clusterCSIDriverLister.Get(utils.VSphereDriverName)
343+
if err != nil {
344+
return fmt.Errorf("failed to get ClusterCSIDriver: %v", err)
345+
}
346+
347+
// Get the maximum volume limit
348+
maxVolumesPerNode := utils.GetMaxVolumesPerNode(clusterCSIDriver)
349+
350+
// Set the environment variable in all containers
351+
containers := ds.Spec.Template.Spec.Containers
352+
for i := range containers {
353+
container := &containers[i]
354+
355+
// Find and update existing MAX_VOLUMES_PER_NODE env var if it exists
356+
envVarFound := false
357+
for j := range container.Env {
358+
if container.Env[j].Name == "MAX_VOLUMES_PER_NODE" {
359+
container.Env[j].Value = strconv.FormatInt(int64(maxVolumesPerNode), 10)
360+
envVarFound = true
361+
break
362+
}
363+
}
364+
365+
// If not found, append it
366+
if !envVarFound {
367+
container.Env = append(container.Env, v1.EnvVar{
368+
Name: "MAX_VOLUMES_PER_NODE",
369+
Value: strconv.FormatInt(int64(maxVolumesPerNode), 10),
370+
})
371+
}
372+
}
373+
return nil
374+
}
375+
}

0 commit comments

Comments
 (0)