From 12fd87f40b33b687109a211bcf6ab60b151090e5 Mon Sep 17 00:00:00 2001 From: Rakshith R Date: Mon, 23 Jan 2023 17:30:55 +0530 Subject: [PATCH] csi: add default version to images if it is missing Rook will now append internal default versions to custom csi image variables passed down if version is missing in them. This will enable users using their private registries to just pass different image names while rook takes care of adding default versions to them. Resolves: #10529 Signed-off-by: Rakshith R --- .../Ceph-CSI/custom-images.md | 7 +++ pkg/operator/ceph/csi/csi.go | 15 +++--- pkg/operator/ceph/csi/util.go | 13 +++++ pkg/operator/ceph/csi/util_test.go | 51 +++++++++++++++++++ 4 files changed, 78 insertions(+), 8 deletions(-) diff --git a/Documentation/Storage-Configuration/Ceph-CSI/custom-images.md b/Documentation/Storage-Configuration/Ceph-CSI/custom-images.md index 1f70dbd24..03abecd81 100644 --- a/Documentation/Storage-Configuration/Ceph-CSI/custom-images.md +++ b/Documentation/Storage-Configuration/Ceph-CSI/custom-images.md @@ -27,6 +27,13 @@ ROOK_CSI_SNAPSHOTTER_IMAGE: "registry.k8s.io/sig-storage/csi-snapshotter:v6.2.1" ROOK_CSIADDONS_IMAGE: "quay.io/csiaddons/k8s-sidecar:v0.5.0" ``` +### **Use private repository** + +If image version is not passed along with the image name in any of the variables above, +Rook will add the corresponding default version to that image. +Example: if `ROOK_CSI_CEPH_IMAGE: "quay.io/private-repo/cephcsi"` is passed, +Rook will add internal default version and consume it as `"quay.io/private-repo/cephcsi:v3.7.2"`. + ### **Use default images** If you would like Rook to use the default upstream images, then you may simply remove all diff --git a/pkg/operator/ceph/csi/csi.go b/pkg/operator/ceph/csi/csi.go index 92c96fea7..bea1dcd52 100644 --- a/pkg/operator/ceph/csi/csi.go +++ b/pkg/operator/ceph/csi/csi.go @@ -217,8 +217,6 @@ func (r *ReconcileCSI) setParams(ver *version.Info) error { logger.Infof("Kubernetes version is %s.%s", ver.Major, ver.Minor) - CSIParam.ResizerImage = k8sutil.GetValue(r.opConfig.Parameters, "ROOK_CSI_RESIZER_IMAGE", DefaultResizerImage) - logLevel := k8sutil.GetValue(r.opConfig.Parameters, "CSI_LOG_LEVEL", "") CSIParam.LogLevel = defaultLogLevel if logLevel != "" { @@ -259,13 +257,14 @@ func (r *ReconcileCSI) setParams(ver *version.Info) error { logger.Errorf("failed to get nodes. Defaulting the number of replicas of provisioner pods to %d. %v", CSIParam.ProvisionerReplicas, err) } - CSIParam.CSIPluginImage = k8sutil.GetValue(r.opConfig.Parameters, "ROOK_CSI_CEPH_IMAGE", DefaultCSIPluginImage) - CSIParam.RegistrarImage = k8sutil.GetValue(r.opConfig.Parameters, "ROOK_CSI_REGISTRAR_IMAGE", DefaultRegistrarImage) - CSIParam.ProvisionerImage = k8sutil.GetValue(r.opConfig.Parameters, "ROOK_CSI_PROVISIONER_IMAGE", DefaultProvisionerImage) - CSIParam.AttacherImage = k8sutil.GetValue(r.opConfig.Parameters, "ROOK_CSI_ATTACHER_IMAGE", DefaultAttacherImage) - CSIParam.SnapshotterImage = k8sutil.GetValue(r.opConfig.Parameters, "ROOK_CSI_SNAPSHOTTER_IMAGE", DefaultSnapshotterImage) + CSIParam.CSIPluginImage = getImage(r.opConfig.Parameters, "ROOK_CSI_CEPH_IMAGE", DefaultCSIPluginImage) + CSIParam.RegistrarImage = getImage(r.opConfig.Parameters, "ROOK_CSI_REGISTRAR_IMAGE", DefaultRegistrarImage) + CSIParam.ProvisionerImage = getImage(r.opConfig.Parameters, "ROOK_CSI_PROVISIONER_IMAGE", DefaultProvisionerImage) + CSIParam.AttacherImage = getImage(r.opConfig.Parameters, "ROOK_CSI_ATTACHER_IMAGE", DefaultAttacherImage) + CSIParam.SnapshotterImage = getImage(r.opConfig.Parameters, "ROOK_CSI_SNAPSHOTTER_IMAGE", DefaultSnapshotterImage) + CSIParam.ResizerImage = getImage(r.opConfig.Parameters, "ROOK_CSI_RESIZER_IMAGE", DefaultResizerImage) CSIParam.KubeletDirPath = k8sutil.GetValue(r.opConfig.Parameters, "ROOK_CSI_KUBELET_DIR_PATH", DefaultKubeletDirPath) - CSIParam.CSIAddonsImage = k8sutil.GetValue(r.opConfig.Parameters, "ROOK_CSIADDONS_IMAGE", DefaultCSIAddonsImage) + CSIParam.CSIAddonsImage = getImage(r.opConfig.Parameters, "ROOK_CSIADDONS_IMAGE", DefaultCSIAddonsImage) CSIParam.CSIDomainLabels = k8sutil.GetValue(r.opConfig.Parameters, "CSI_TOPOLOGY_DOMAIN_LABELS", "") csiCephFSPodLabels := k8sutil.GetValue(r.opConfig.Parameters, "ROOK_CSI_CEPHFS_POD_LABELS", "") CSIParam.CSICephFSPodLabels = k8sutil.ParseStringToLabels(csiCephFSPodLabels) diff --git a/pkg/operator/ceph/csi/util.go b/pkg/operator/ceph/csi/util.go index 0403946da..5d0cc2ab8 100644 --- a/pkg/operator/ceph/csi/util.go +++ b/pkg/operator/ceph/csi/util.go @@ -18,6 +18,7 @@ package csi import ( "bytes" + "fmt" "strconv" "strings" "text/template" @@ -252,3 +253,15 @@ func applyVolumeMountToContainer(opConfig map[string]string, configName, contain } } } + +// getImage returns the image for the given setting name. If the image does not contain version, +// the default version is appended from the default image. +func getImage(data map[string]string, settingName, defaultImage string) string { + image := k8sutil.GetValue(data, settingName, defaultImage) + if !strings.Contains(image, ":") { + version := strings.SplitN(defaultImage, ":", 2)[1] + image = fmt.Sprintf("%s:%s", image, version) + } + + return image +} diff --git a/pkg/operator/ceph/csi/util_test.go b/pkg/operator/ceph/csi/util_test.go index 241e0fdfe..9d4e2a838 100644 --- a/pkg/operator/ceph/csi/util_test.go +++ b/pkg/operator/ceph/csi/util_test.go @@ -251,3 +251,54 @@ func Test_applyVolumeMountToContainer(t *testing.T) { assert.Len(t, ds.Spec.Template.Spec.Containers[1].VolumeMounts, defaultVolumes+1) } + +func Test_getImage(t *testing.T) { + type args struct { + data map[string]string + settingName string + defaultImage string + } + tests := []struct { + name string + args args + want string + }{ + { + name: "test with default image", + args: args{ + data: map[string]string{}, + settingName: "ROOK_CSI_CEPH_IMAGE", + defaultImage: "quay.io/cephcsi/cephcsi:v3.7.2", + }, + want: DefaultCSIPluginImage, + }, + { + name: "test with user image", + args: args{ + data: map[string]string{ + "ROOK_CSI_CEPH_IMAGE": "registry.io/private/cephcsi:v8", + }, + settingName: "ROOK_CSI_CEPH_IMAGE", + defaultImage: "quay.io/cephcsi/cephcsi:v3.7.2", + }, + want: "registry.io/private/cephcsi:v8", + }, + { + name: "test with user image without version", + args: args{ + data: map[string]string{ + "ROOK_CSI_CEPH_IMAGE": "registry.io/private/cephcsi", + }, + settingName: "ROOK_CSI_CEPH_IMAGE", + defaultImage: "quay.io/cephcsi/cephcsi:v3.7.2", + }, + want: "registry.io/private/cephcsi:v3.7.2", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := getImage(tt.args.data, tt.args.settingName, tt.args.defaultImage) + assert.Equal(t, tt.want, got) + }) + } +}