Skip to content

Commit

Permalink
Driver Controller: Align with standard library utilities
Browse files Browse the repository at this point in the history
Signed-off-by: nb-ohad <[email protected]>
  • Loading branch information
nb-ohad committed Jul 17, 2024
1 parent ce75aa1 commit e7c12f8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 44 deletions.
23 changes: 12 additions & 11 deletions internal/controller/driver_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package controller

import (
"cmp"
"context"
"encoding/json"
"errors"
Expand Down Expand Up @@ -329,12 +330,12 @@ func (r *driverReconcile) reconcileK8sCsiDriver() error {
// unless we gureente that we started from the same point.
existingCsiDriver.Spec.DeepCopyInto(&desiredCsiDriver.Spec)
desiredCsiDriver.Spec.PodInfoOnMount = ptr.To(false)
desiredCsiDriver.Spec.AttachRequired = utils.FirstNonNil(
desiredCsiDriver.Spec.AttachRequired = cmp.Or(
r.driver.Spec.AttachRequired,
ptr.To(true),
)
desiredCsiDriver.Spec.FSGroupPolicy = ptr.To(
utils.FirstNonEmpty(
cmp.Or(
r.driver.Spec.FsGroupPolicy,
r.driver.Spec.FsGroupPolicy,
storagev1.FileFSGroupPolicy,
Expand Down Expand Up @@ -399,14 +400,14 @@ func (r *driverReconcile) reconcileControllerPluginDeployment() error {
}

appName := deploy.Name
leaderElectionSpec := utils.FirstNonNil(r.driver.Spec.LeaderElection, &defaultLeaderElection)
pluginSpec := utils.FirstNonNil(r.driver.Spec.ControllerPlugin, &csiv1a1.ControllerPluginSpec{})
serviceAccountName := utils.FirstNonEmpty(
leaderElectionSpec := cmp.Or(r.driver.Spec.LeaderElection, &defaultLeaderElection)
pluginSpec := cmp.Or(r.driver.Spec.ControllerPlugin, &csiv1a1.ControllerPluginSpec{})
serviceAccountName := cmp.Or(
ptr.Deref(pluginSpec.ServiceAccountName, ""),
fmt.Sprintf("csi-%s-ctrlplugin-sa", r.driverType),
)
imagePullPolicy := utils.FirstNonEmpty(pluginSpec.ImagePullPolicy, corev1.PullIfNotPresent)
grpcTimeout := utils.FirstNonZero(r.driver.Spec.GRpcTimeout, defaultGRrpcTimeout)
imagePullPolicy := cmp.Or(pluginSpec.ImagePullPolicy, corev1.PullIfNotPresent)
grpcTimeout := cmp.Or(r.driver.Spec.GRpcTimeout, defaultGRrpcTimeout)
logLevel := ptr.Deref(r.driver.Spec.Log, csiv1a1.LogSpec{}).LogLevel
forceKernelClient := r.isCephFsDriver() && r.driver.Spec.CephFsClientType == csiv1a1.KernelCephFsClient

Expand Down Expand Up @@ -733,14 +734,14 @@ func (r *driverReconcile) reconcileNodePluginDeamonSet() error {
}

appName := daemonSet.Name
pluginSpec := utils.FirstNonNil(r.driver.Spec.NodePlugin, &csiv1a1.NodePluginSpec{})
serviceAccountName := utils.FirstNonEmpty(
pluginSpec := cmp.Or(r.driver.Spec.NodePlugin, &csiv1a1.NodePluginSpec{})
serviceAccountName := cmp.Or(
ptr.Deref(pluginSpec.ServiceAccountName, ""),
fmt.Sprintf("csi-%s-nodeplugin-sa", r.driverType),
)
imagePullPolicy := utils.FirstNonEmpty(pluginSpec.ImagePullPolicy, corev1.PullIfNotPresent)
imagePullPolicy := cmp.Or(pluginSpec.ImagePullPolicy, corev1.PullIfNotPresent)
logLevel := ptr.Deref(r.driver.Spec.Log, csiv1a1.LogSpec{}).LogLevel
kubeletDirPath := utils.FirstNonEmpty(pluginSpec.KubeletDirPath, defaultKubeletDirPath)
kubeletDirPath := cmp.Or(pluginSpec.KubeletDirPath, defaultKubeletDirPath)
forceKernelClient := r.isCephFsDriver() && r.driver.Spec.CephFsClientType == csiv1a1.KernelCephFsClient

daemonSet.Spec = appsv1.DaemonSetSpec{
Expand Down
35 changes: 2 additions & 33 deletions internal/utils/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ limitations under the License.
package utils

import (
"cmp"
"strings"
"sync"

"golang.org/x/exp/constraints"
)

// RunConcurrently runs all the of the given functions concurrently returning a channel with
Expand Down Expand Up @@ -67,38 +66,8 @@ func If[T any](cond bool, trueVal, falseVal T) T {
}
}

// FirstNonNil returns the first non nil argument or nil if all arguments are nil
func FirstNonNil[T any](ptrs ...*T) *T {
for _, ptr := range ptrs {
if ptr != nil {
return ptr
}
}
return nil
}

// FirstNonEmpty returns the first non empty string or an empty string if all
// arguments are empty strings
func FirstNonEmpty[T ~string](strings ...T) T {
for _, str := range strings {
if str != "" {
return str
}
}
return ""
}

func FirstNonZero[T constraints.Integer](numbers ...T) T {
for _, num := range numbers {
if num != 0 {
return num
}
}
return 0
}

// Clamp a number between min and max
func Clamp[T constraints.Ordered](val, low, high T) T {
func Clamp[T cmp.Ordered](val, low, high T) T {
if val < low {
return low
} else if val > high {
Expand Down

0 comments on commit e7c12f8

Please sign in to comment.