From fd8866cee05a1c395e13ea16d679051b773118e6 Mon Sep 17 00:00:00 2001 From: Tanmay Jain Date: Tue, 6 Aug 2024 11:28:29 +0530 Subject: [PATCH 1/7] Making the aerospike-init image namespace configurable --- Jenkinsfile | 3 +- api/v1/aerospikecluster_types.go | 2 + api/v1/utils.go | 51 ++++++++++++---- api/v1/zz_generated.deepcopy.go | 5 ++ .../asdb.aerospike.com_aerospikeclusters.yaml | 8 +++ config/manager/manager.yaml | 3 + controllers/rack.go | 9 --- ..._aerospikeclusters.asdb.aerospike.com.yaml | 8 +++ ...perator-controller-manager-deployment.yaml | 2 + .../aerospike-kubernetes-operator/values.yaml | 3 + test/podspec_test.go | 60 +++++++++++++++---- test/test.sh | 5 +- 12 files changed, 126 insertions(+), 33 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index a34b36e10..8736668c5 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -25,6 +25,7 @@ pipeline { BUNDLE_IMG="${OPERATOR_BUNDLE_IMAGE_CANDIDATE_NAME}" AEROSPIKE_CUSTOM_INIT_REGISTRY="568976754000.dkr.ecr.ap-south-1.amazonaws.com" + AEROSPIKE_CUSTOM_INIT_REGISTRY_NAMESPACE="aerospike" } stages { @@ -92,7 +93,7 @@ pipeline { dir("${env.GO_REPO}") { sh "rsync -aK ${env.WORKSPACE}/../../aerospike-kubernetes-operator-resources/secrets/ config/samples/secrets" sh "set +x; docker login --username AWS 568976754000.dkr.ecr.ap-south-1.amazonaws.com -p \$(aws ecr get-login-password --region ap-south-1); set -x" - sh "./test/test.sh -b ${OPERATOR_BUNDLE_IMAGE_CANDIDATE_NAME} -c ${OPERATOR_CATALOG_IMAGE_CANDIDATE_NAME} -r ${AEROSPIKE_CUSTOM_INIT_REGISTRY}" + sh "./test/test.sh -b ${OPERATOR_BUNDLE_IMAGE_CANDIDATE_NAME} -c ${OPERATOR_CATALOG_IMAGE_CANDIDATE_NAME} -r ${AEROSPIKE_CUSTOM_INIT_REGISTRY} -n ${AEROSPIKE_CUSTOM_INIT_REGISTRY_NAMESPACE}" } } diff --git a/api/v1/aerospikecluster_types.go b/api/v1/aerospikecluster_types.go index 18d235822..9f6e21c9c 100644 --- a/api/v1/aerospikecluster_types.go +++ b/api/v1/aerospikecluster_types.go @@ -318,6 +318,8 @@ type AerospikeInitContainerSpec struct { //nolint:govet // for readability // ImageRegistry is the name of image registry for aerospike-init container image // ImageRegistry, e.g. docker.io, redhat.access.com ImageRegistry string `json:"imageRegistry,omitempty"` + // ImageRegistryNamespace is the name of namespace in registry for aerospike-init container image + ImageRegistryNamespace *string `json:"imageRegistryNamespace,omitempty"` // SecurityContext that will be added to aerospike-init container created by operator. SecurityContext *corev1.SecurityContext `json:"securityContext,omitempty"` // Define resources requests and limits for Aerospike init Container. diff --git a/api/v1/utils.go b/api/v1/utils.go index ce14db874..025aa09d6 100644 --- a/api/v1/utils.go +++ b/api/v1/utils.go @@ -71,6 +71,7 @@ const ( AerospikeServerContainerName = "aerospike-server" AerospikeInitContainerName = "aerospike-init" AerospikeInitContainerRegistryEnvVar = "AEROSPIKE_KUBERNETES_INIT_REGISTRY" + AerospikeInitContainerRegistryNamespaceEnvVar = "AEROSPIKE_KUBERNETES_INIT_REGISTRY_NAMESPACE" AerospikeInitContainerDefaultRegistry = "docker.io" AerospikeInitContainerDefaultRegistryNamespace = "aerospike" AerospikeInitContainerDefaultRepoAndTag = "aerospike-kubernetes-init:2.2.1" @@ -121,33 +122,63 @@ func GetWorkDirectory(aerospikeConfigSpec AerospikeConfigSpec) string { return DefaultWorkDirectory } -func getInitContainerImage(registry string) string { +func getInitContainerImage(registry, namespace string) string { return fmt.Sprintf( "%s/%s/%s", strings.TrimSuffix(registry, "/"), - strings.TrimSuffix(AerospikeInitContainerDefaultRegistryNamespace, "/"), + strings.TrimSuffix(namespace, "/"), AerospikeInitContainerDefaultRepoAndTag, ) } func GetAerospikeInitContainerImage(aeroCluster *AerospikeCluster) string { + registry := getInitContainerImageRegistry(aeroCluster) + namespace := getInitContainerImageRegistryNamespace(aeroCluster) + + return getInitContainerImage(registry, namespace) +} + +func getInitContainerImageRegistryNamespace(aeroCluster *AerospikeCluster) string { + // Given in CR + var namespace *string + if aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec != nil { + namespace = aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistryNamespace + } + + if namespace == nil { + // Given in EnvVar + envRegistryNamespace, found := os.LookupEnv(AerospikeInitContainerRegistryNamespaceEnvVar) + if found { + namespace = &envRegistryNamespace + } + } + + if namespace == nil { + return AerospikeInitContainerDefaultRegistryNamespace + } + + return *namespace +} + +func getInitContainerImageRegistry(aeroCluster *AerospikeCluster) string { // Given in CR registry := "" if aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec != nil { registry = aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistry } - if registry != "" { - return getInitContainerImage(registry) + if registry == "" { + // Given in EnvVar + envRegistry, found := os.LookupEnv(AerospikeInitContainerRegistryEnvVar) + if found { + registry = envRegistry + } } - // Given in EnvVar - registry, found := os.LookupEnv(AerospikeInitContainerRegistryEnvVar) - if found { - return getInitContainerImage(registry) + if registry == "" { + return AerospikeInitContainerDefaultRegistry } - // Use default - return getInitContainerImage(AerospikeInitContainerDefaultRegistry) + return registry } func ClusterNamespacedName(aeroCluster *AerospikeCluster) string { diff --git a/api/v1/zz_generated.deepcopy.go b/api/v1/zz_generated.deepcopy.go index 03adc6db2..e10ba8842 100644 --- a/api/v1/zz_generated.deepcopy.go +++ b/api/v1/zz_generated.deepcopy.go @@ -360,6 +360,11 @@ func (in *AerospikeContainerSpec) DeepCopy() *AerospikeContainerSpec { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *AerospikeInitContainerSpec) DeepCopyInto(out *AerospikeInitContainerSpec) { *out = *in + if in.ImageRegistryNamespace != nil { + in, out := &in.ImageRegistryNamespace, &out.ImageRegistryNamespace + *out = new(string) + **out = **in + } if in.SecurityContext != nil { in, out := &in.SecurityContext, &out.SecurityContext *out = new(corev1.SecurityContext) diff --git a/config/crd/bases/asdb.aerospike.com_aerospikeclusters.yaml b/config/crd/bases/asdb.aerospike.com_aerospikeclusters.yaml index 7b5ad432e..7cc835cd4 100644 --- a/config/crd/bases/asdb.aerospike.com_aerospikeclusters.yaml +++ b/config/crd/bases/asdb.aerospike.com_aerospikeclusters.yaml @@ -623,6 +623,10 @@ spec: aerospike-init container image ImageRegistry, e.g. docker.io, redhat.access.com type: string + imageRegistryNamespace: + description: ImageRegistryNamespace is the name of namespace + in registry for aerospike-init container image + type: string resources: description: Define resources requests and limits for Aerospike init Container. Only Memory and Cpu resources can be given @@ -9996,6 +10000,10 @@ spec: aerospike-init container image ImageRegistry, e.g. docker.io, redhat.access.com type: string + imageRegistryNamespace: + description: ImageRegistryNamespace is the name of namespace + in registry for aerospike-init container image + type: string resources: description: Define resources requests and limits for Aerospike init Container. Only Memory and Cpu resources can be given diff --git a/config/manager/manager.yaml b/config/manager/manager.yaml index 9577e5084..2ac29cfa4 100644 --- a/config/manager/manager.yaml +++ b/config/manager/manager.yaml @@ -61,6 +61,9 @@ spec: - name: AEROSPIKE_KUBERNETES_INIT_REGISTRY # this is the registry used to pull aerospike-init image value: docker.io + - name: AEROSPIKE_KUBERNETES_INIT_REGISTRY_NAMESPACE + # this is the namespace in registry used to pull aerospike-init image + value: aerospike serviceAccountName: controller-manager terminationGracePeriodSeconds: 10 diff --git a/controllers/rack.go b/controllers/rack.go index 061e25d5e..48e6678e8 100644 --- a/controllers/rack.go +++ b/controllers/rack.go @@ -1081,15 +1081,6 @@ func (r *SingleClusterReconciler) rollingRestartRack(found *appsv1.StatefulSet, } } - if len(failedPods) != 0 && r.isAnyPodInImageFailedState(podList, ignorablePodNames) { - return found, reconcileError( - fmt.Errorf( - "cannot Rolling restart AerospikeCluster. " + - "A pod is already in failed state due to image related issues", - ), - ) - } - err = r.updateSTS(found, rackState) if err != nil { return found, reconcileError( diff --git a/helm-charts/aerospike-kubernetes-operator/crds/customresourcedefinition_aerospikeclusters.asdb.aerospike.com.yaml b/helm-charts/aerospike-kubernetes-operator/crds/customresourcedefinition_aerospikeclusters.asdb.aerospike.com.yaml index 7b5ad432e..7cc835cd4 100644 --- a/helm-charts/aerospike-kubernetes-operator/crds/customresourcedefinition_aerospikeclusters.asdb.aerospike.com.yaml +++ b/helm-charts/aerospike-kubernetes-operator/crds/customresourcedefinition_aerospikeclusters.asdb.aerospike.com.yaml @@ -623,6 +623,10 @@ spec: aerospike-init container image ImageRegistry, e.g. docker.io, redhat.access.com type: string + imageRegistryNamespace: + description: ImageRegistryNamespace is the name of namespace + in registry for aerospike-init container image + type: string resources: description: Define resources requests and limits for Aerospike init Container. Only Memory and Cpu resources can be given @@ -9996,6 +10000,10 @@ spec: aerospike-init container image ImageRegistry, e.g. docker.io, redhat.access.com type: string + imageRegistryNamespace: + description: ImageRegistryNamespace is the name of namespace + in registry for aerospike-init container image + type: string resources: description: Define resources requests and limits for Aerospike init Container. Only Memory and Cpu resources can be given diff --git a/helm-charts/aerospike-kubernetes-operator/templates/aerospike-operator-controller-manager-deployment.yaml b/helm-charts/aerospike-kubernetes-operator/templates/aerospike-operator-controller-manager-deployment.yaml index 7a5741d66..162112b62 100644 --- a/helm-charts/aerospike-kubernetes-operator/templates/aerospike-operator-controller-manager-deployment.yaml +++ b/helm-charts/aerospike-kubernetes-operator/templates/aerospike-operator-controller-manager-deployment.yaml @@ -57,6 +57,8 @@ spec: value: {{ .Values.watchNamespaces | quote }} - name: AEROSPIKE_KUBERNETES_INIT_REGISTRY value: {{ .Values.aerospikeKubernetesInitRegistry }} + - name: AEROSPIKE_KUBERNETES_INIT_REGISTRY_NAMESPACE + value: {{ .Values.aerospikeKubernetesInitRegistryNamespace }} {{- if .Values.extraEnv }} {{- range $key, $value := .Values.extraEnv }} - name: "{{ $key }}" diff --git a/helm-charts/aerospike-kubernetes-operator/values.yaml b/helm-charts/aerospike-kubernetes-operator/values.yaml index 3befb4221..c29a2a95b 100644 --- a/helm-charts/aerospike-kubernetes-operator/values.yaml +++ b/helm-charts/aerospike-kubernetes-operator/values.yaml @@ -33,6 +33,9 @@ watchNamespaces: "default" # Registry used to pull aerospike-init image aerospikeKubernetesInitRegistry: "docker.io" +# Namespace in registry used to pull aerospike-init image +aerospikeKubernetesInitRegistryNamespace: "aerospike" + ## Resources - limits / requests resources: limits: diff --git a/test/podspec_test.go b/test/podspec_test.go index 90a019e28..0444660d6 100644 --- a/test/podspec_test.go +++ b/test/podspec_test.go @@ -6,6 +6,7 @@ import ( "os" "reflect" "strings" + "time" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -16,8 +17,9 @@ import ( ) var ( - customInitRegistryEnvVar = "CUSTOM_INIT_REGISTRY" - imagePullSecretNameEnvVar = "IMAGE_PULL_SECRET_NAME" //nolint:gosec // for testing + customInitRegistryEnvVar = "CUSTOM_INIT_REGISTRY" + customInitRegistryNamespaceEnvVar = "CUSTOM_INIT_REGISTRY_NAMESPACE" + imagePullSecretNameEnvVar = "IMAGE_PULL_SECRET_NAME" //nolint:gosec // for testing ) var _ = Describe( @@ -427,9 +429,11 @@ var _ = Describe( } }) - It("Should be able to set/update aerospike-init custom registry", func() { + It("Should be able to set/update aerospike-init custom registry and namespace", func() { operatorEnvVarRegistry := "docker.io" + operatorEnvVarRegistryNamespace := "aerospike" customRegistry := getEnvVar(customInitRegistryEnvVar) + customRegistryNamespace := getEnvVar(customInitRegistryNamespaceEnvVar) imagePullSecret := getEnvVar(imagePullSecretNameEnvVar) By("Updating imagePullSecret") @@ -450,21 +454,52 @@ var _ = Describe( Expect(err).ToNot(HaveOccurred()) aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistry = customRegistry + aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistryNamespace = &customRegistryNamespace err = updateCluster(k8sClient, ctx, aeroCluster) Expect(err).ToNot(HaveOccurred()) - validateImageRegistry(k8sClient, ctx, aeroCluster, customRegistry) + validateImageRegistryNamespace(k8sClient, ctx, aeroCluster, customRegistry, customRegistryNamespace) - By("Using envVar registry") + By("Using envVar registry and namespace") aeroCluster, err = getCluster(k8sClient, ctx, clusterNamespacedName) Expect(err).ToNot(HaveOccurred()) // Empty imageRegistry, should use operator envVar docker.io aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistry = "" + aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistryNamespace = nil err = updateCluster(k8sClient, ctx, aeroCluster) Expect(err).ToNot(HaveOccurred()) - validateImageRegistry(k8sClient, ctx, aeroCluster, operatorEnvVarRegistry) + validateImageRegistryNamespace(k8sClient, ctx, aeroCluster, operatorEnvVarRegistry, + operatorEnvVarRegistryNamespace) + }) + + It("Should be able to recover cluster after setting correct aerospike-init custom registry", func() { + operatorEnvVarRegistry := "docker.io" + operatorEnvVarRegistryNamespace := "aerospike" + incorrectCustomRegistry := "incorrect.registry" + + By("Using incorrect registry in CR") + aeroCluster, err := getCluster(k8sClient, ctx, clusterNamespacedName) + Expect(err).ToNot(HaveOccurred()) + + aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistry = incorrectCustomRegistry + aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistryNamespace = &operatorEnvVarRegistryNamespace + err = updateClusterWithTO(k8sClient, ctx, aeroCluster, time.Minute*1) + Expect(err).Should(HaveOccurred()) + + By("Using correct registry name") + aeroCluster, err = getCluster(k8sClient, ctx, clusterNamespacedName) + Expect(err).ToNot(HaveOccurred()) + + // Empty imageRegistry, should use operator envVar docker.io + aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistry = operatorEnvVarRegistry + aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistryNamespace = &operatorEnvVarRegistryNamespace + err = updateCluster(k8sClient, ctx, aeroCluster) + Expect(err).ToNot(HaveOccurred()) + + validateImageRegistryNamespace(k8sClient, ctx, aeroCluster, operatorEnvVarRegistry, + operatorEnvVarRegistryNamespace) }) }) Context( @@ -521,7 +556,6 @@ var _ = Describe( ) }, ) - }, ) @@ -532,15 +566,17 @@ func getEnvVar(envVar string) string { return envVarVal } -func validateImageRegistry( - k8sClient client.Client, _ goctx.Context, aeroCluster *asdbv1.AerospikeCluster, registry string, -) { +func validateImageRegistryNamespace(k8sClient client.Client, _ goctx.Context, aeroCluster *asdbv1.AerospikeCluster, + registry, namespace string) { stsList, err := getSTSList(aeroCluster, k8sClient) Expect(err).ToNot(HaveOccurred()) + expectedImagePrefix := fmt.Sprintf("%s/%s", registry, namespace) + for stsIndex := range stsList.Items { image := stsList.Items[stsIndex].Spec.Template.Spec.InitContainers[0].Image - hasPrefix := strings.HasPrefix(image, registry) - Expect(hasPrefix).To(BeTrue(), fmt.Sprintf("expected registry %s, found image %s", registry, image)) + hasPrefix := strings.HasPrefix(image, expectedImagePrefix) + Expect(hasPrefix).To(BeTrue(), fmt.Sprintf("expected registry/namespace %s, found image %s", + expectedImagePrefix, image)) } } diff --git a/test/test.sh b/test/test.sh index 8b93e24f2..8b1b83d54 100755 --- a/test/test.sh +++ b/test/test.sh @@ -11,7 +11,7 @@ set -e # test.sh -c aerospike/aerospike-kubernetes-operator-bundle:1.1.0 -f ".*RackManagement.*" -a "--connect-through-network-type=hostInternal" # test.sh -c -f "" -a "" -while getopts "b:c:f:a:r:p:" opt +while getopts "b:c:f:a:r:p:n:" opt do case "$opt" in b ) BUNDLE="$OPTARG" ;; @@ -20,6 +20,7 @@ do a ) ARGS="$OPTARG" ;; r ) REGISTRY="$OPTARG" ;; p ) CRED_PATH="$OPTARG" ;; + n ) NAMESPACE="$OPTARG" ;; esac done @@ -27,6 +28,7 @@ done # Defaults CRED_PATH=${CRED_PATH:-$HOME/.docker/config.json} REGISTRY=${REGISTRY:-568976754000.dkr.ecr.ap-south-1.amazonaws.com} +NAMESPACE=${NAMESPACE:-aerospike} DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" @@ -60,6 +62,7 @@ echo "| Starting tests.... |" echo "---------------------" export CUSTOM_INIT_REGISTRY="$REGISTRY" +export CUSTOM_INIT_REGISTRY_NAMESPACE="$NAMESPACE" export IMAGE_PULL_SECRET_NAME="$IMAGE_PULL_SECRET" make test FOCUS="$FOCUS" ARGS="$ARGS" From ce580d71ea65154314c87b1021354f5f4f8aeef7 Mon Sep 17 00:00:00 2001 From: Tanmay Jain Date: Wed, 14 Aug 2024 12:28:06 +0530 Subject: [PATCH 2/7] addressing comments --- test/podspec_test.go | 17 +++++++++-------- test/test.sh | 6 +++--- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/test/podspec_test.go b/test/podspec_test.go index 0444660d6..8907c0ee2 100644 --- a/test/podspec_test.go +++ b/test/podspec_test.go @@ -449,7 +449,7 @@ var _ = Describe( err = updateCluster(k8sClient, ctx, aeroCluster) Expect(err).ToNot(HaveOccurred()) - By("Using registry in CR") + By("Using registry and namespace in CR") aeroCluster, err = getCluster(k8sClient, ctx, clusterNamespacedName) Expect(err).ToNot(HaveOccurred()) @@ -474,27 +474,28 @@ var _ = Describe( operatorEnvVarRegistryNamespace) }) - It("Should be able to recover cluster after setting correct aerospike-init custom registry", func() { + It("Should be able to recover cluster after setting correct aerospike-init custom registry/namespace", func() { operatorEnvVarRegistry := "docker.io" operatorEnvVarRegistryNamespace := "aerospike" - incorrectCustomRegistry := "incorrect.registry" + incorrectCustomRegistryNamespace := "incorrectnamespace" By("Using incorrect registry in CR") aeroCluster, err := getCluster(k8sClient, ctx, clusterNamespacedName) Expect(err).ToNot(HaveOccurred()) - aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistry = incorrectCustomRegistry - aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistryNamespace = &operatorEnvVarRegistryNamespace + aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistryNamespace = &incorrectCustomRegistryNamespace err = updateClusterWithTO(k8sClient, ctx, aeroCluster, time.Minute*1) Expect(err).Should(HaveOccurred()) + validateImageRegistryNamespace(k8sClient, ctx, aeroCluster, operatorEnvVarRegistry, + incorrectCustomRegistryNamespace) + By("Using correct registry name") aeroCluster, err = getCluster(k8sClient, ctx, clusterNamespacedName) Expect(err).ToNot(HaveOccurred()) - // Empty imageRegistry, should use operator envVar docker.io - aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistry = operatorEnvVarRegistry - aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistryNamespace = &operatorEnvVarRegistryNamespace + // Nil ImageRegistryNamespace, should use operator envVar aerospike + aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistryNamespace = nil err = updateCluster(k8sClient, ctx, aeroCluster) Expect(err).ToNot(HaveOccurred()) diff --git a/test/test.sh b/test/test.sh index 8b1b83d54..a84d8754e 100755 --- a/test/test.sh +++ b/test/test.sh @@ -20,7 +20,7 @@ do a ) ARGS="$OPTARG" ;; r ) REGISTRY="$OPTARG" ;; p ) CRED_PATH="$OPTARG" ;; - n ) NAMESPACE="$OPTARG" ;; + n ) REGISTRY_NAMESPACE="$OPTARG" ;; esac done @@ -28,7 +28,7 @@ done # Defaults CRED_PATH=${CRED_PATH:-$HOME/.docker/config.json} REGISTRY=${REGISTRY:-568976754000.dkr.ecr.ap-south-1.amazonaws.com} -NAMESPACE=${NAMESPACE:-aerospike} +REGISTRY_NAMESPACE=${REGISTRY_NAMESPACE:-aerospike} DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" @@ -62,7 +62,7 @@ echo "| Starting tests.... |" echo "---------------------" export CUSTOM_INIT_REGISTRY="$REGISTRY" -export CUSTOM_INIT_REGISTRY_NAMESPACE="$NAMESPACE" +export CUSTOM_INIT_REGISTRY_NAMESPACE="$REGISTRY_NAMESPACE" export IMAGE_PULL_SECRET_NAME="$IMAGE_PULL_SECRET" make test FOCUS="$FOCUS" ARGS="$ARGS" From 35cf80df0ba2f09d0f504216e2f71c3b72ca4e70 Mon Sep 17 00:00:00 2001 From: Tanmay Jain Date: Mon, 9 Sep 2024 17:09:47 +0530 Subject: [PATCH 3/7] making init image repo and tag also configurable. --- Jenkinsfile | 3 +- api/v1/aerospikecluster_types.go | 2 + api/v1/utils.go | 57 ++++++++++++++----- .../asdb.aerospike.com_aerospikeclusters.yaml | 8 +++ config/manager/manager.yaml | 3 + ..._aerospikeclusters.asdb.aerospike.com.yaml | 8 +++ ...perator-controller-manager-deployment.yaml | 2 + .../aerospike-kubernetes-operator/values.yaml | 3 + test/podspec_test.go | 31 +++++----- test/test.sh | 5 +- 10 files changed, 92 insertions(+), 30 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 8736668c5..15f667984 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -26,6 +26,7 @@ pipeline { AEROSPIKE_CUSTOM_INIT_REGISTRY="568976754000.dkr.ecr.ap-south-1.amazonaws.com" AEROSPIKE_CUSTOM_INIT_REGISTRY_NAMESPACE="aerospike" + AEROSPIKE_CUSTOM_INIT_NAMETAG="aerospike" } stages { @@ -93,7 +94,7 @@ pipeline { dir("${env.GO_REPO}") { sh "rsync -aK ${env.WORKSPACE}/../../aerospike-kubernetes-operator-resources/secrets/ config/samples/secrets" sh "set +x; docker login --username AWS 568976754000.dkr.ecr.ap-south-1.amazonaws.com -p \$(aws ecr get-login-password --region ap-south-1); set -x" - sh "./test/test.sh -b ${OPERATOR_BUNDLE_IMAGE_CANDIDATE_NAME} -c ${OPERATOR_CATALOG_IMAGE_CANDIDATE_NAME} -r ${AEROSPIKE_CUSTOM_INIT_REGISTRY} -n ${AEROSPIKE_CUSTOM_INIT_REGISTRY_NAMESPACE}" + sh "./test/test.sh -b ${OPERATOR_BUNDLE_IMAGE_CANDIDATE_NAME} -c ${OPERATOR_CATALOG_IMAGE_CANDIDATE_NAME} -r ${AEROSPIKE_CUSTOM_INIT_REGISTRY} -n ${AEROSPIKE_CUSTOM_INIT_REGISTRY_NAMESPACE} -t ${AEROSPIKE_CUSTOM_INIT_NAMETAG}" } } diff --git a/api/v1/aerospikecluster_types.go b/api/v1/aerospikecluster_types.go index 9f6e21c9c..dbe9cc432 100644 --- a/api/v1/aerospikecluster_types.go +++ b/api/v1/aerospikecluster_types.go @@ -320,6 +320,8 @@ type AerospikeInitContainerSpec struct { //nolint:govet // for readability ImageRegistry string `json:"imageRegistry,omitempty"` // ImageRegistryNamespace is the name of namespace in registry for aerospike-init container image ImageRegistryNamespace *string `json:"imageRegistryNamespace,omitempty"` + // ImageNameAndTag is the name:tag of aerospike-init container image + ImageNameAndTag string `json:"imageNameAndTag,omitempty"` // SecurityContext that will be added to aerospike-init container created by operator. SecurityContext *corev1.SecurityContext `json:"securityContext,omitempty"` // Define resources requests and limits for Aerospike init Container. diff --git a/api/v1/utils.go b/api/v1/utils.go index 025aa09d6..a5a35cc84 100644 --- a/api/v1/utils.go +++ b/api/v1/utils.go @@ -70,8 +70,11 @@ const ( const ( AerospikeServerContainerName = "aerospike-server" AerospikeInitContainerName = "aerospike-init" + AerospikeInitContainerNameAndTag = "nameAndTag" + AerospikeInitContainerRegistry = "registry" AerospikeInitContainerRegistryEnvVar = "AEROSPIKE_KUBERNETES_INIT_REGISTRY" AerospikeInitContainerRegistryNamespaceEnvVar = "AEROSPIKE_KUBERNETES_INIT_REGISTRY_NAMESPACE" + AerospikeInitContainerNameTagEnvVar = "AEROSPIKE_KUBERNETES_INIT_NAMETAG" AerospikeInitContainerDefaultRegistry = "docker.io" AerospikeInitContainerDefaultRegistryNamespace = "aerospike" AerospikeInitContainerDefaultRepoAndTag = "aerospike-kubernetes-init:2.2.1" @@ -122,19 +125,20 @@ func GetWorkDirectory(aerospikeConfigSpec AerospikeConfigSpec) string { return DefaultWorkDirectory } -func getInitContainerImage(registry, namespace string) string { +func getInitContainerImage(registry, namespace, repoAndTag string) string { return fmt.Sprintf( "%s/%s/%s", strings.TrimSuffix(registry, "/"), strings.TrimSuffix(namespace, "/"), - AerospikeInitContainerDefaultRepoAndTag, + repoAndTag, ) } func GetAerospikeInitContainerImage(aeroCluster *AerospikeCluster) string { - registry := getInitContainerImageRegistry(aeroCluster) + registry := getInitContainerImageValue(aeroCluster, AerospikeInitContainerRegistry) namespace := getInitContainerImageRegistryNamespace(aeroCluster) + repoAndTag := getInitContainerImageValue(aeroCluster, AerospikeInitContainerNameAndTag) - return getInitContainerImage(registry, namespace) + return getInitContainerImage(registry, namespace, repoAndTag) } func getInitContainerImageRegistryNamespace(aeroCluster *AerospikeCluster) string { @@ -159,26 +163,49 @@ func getInitContainerImageRegistryNamespace(aeroCluster *AerospikeCluster) strin return *namespace } -func getInitContainerImageRegistry(aeroCluster *AerospikeCluster) string { - // Given in CR - registry := "" +func getInitContainerImageValue(aeroCluster *AerospikeCluster, valueType string) string { + var value string + + // Check in CR based on the valueType if aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec != nil { - registry = aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistry + switch valueType { + case AerospikeInitContainerRegistry: + value = aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistry + case AerospikeInitContainerNameAndTag: + value = aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageNameAndTag + } } - if registry == "" { - // Given in EnvVar - envRegistry, found := os.LookupEnv(AerospikeInitContainerRegistryEnvVar) + // Check in EnvVar if not found in CR + if value == "" { + var ( + envVar string + found bool + ) + + switch valueType { + case AerospikeInitContainerRegistry: + envVar, found = os.LookupEnv(AerospikeInitContainerRegistryEnvVar) + case AerospikeInitContainerNameAndTag: + envVar, found = os.LookupEnv(AerospikeInitContainerNameTagEnvVar) + } + if found { - registry = envRegistry + value = envVar } } - if registry == "" { - return AerospikeInitContainerDefaultRegistry + // Return default values if still not found + if value == "" { + switch valueType { + case AerospikeInitContainerRegistry: + return AerospikeInitContainerDefaultRegistry + case AerospikeInitContainerNameAndTag: + return AerospikeInitContainerDefaultRepoAndTag + } } - return registry + return value } func ClusterNamespacedName(aeroCluster *AerospikeCluster) string { diff --git a/config/crd/bases/asdb.aerospike.com_aerospikeclusters.yaml b/config/crd/bases/asdb.aerospike.com_aerospikeclusters.yaml index 7cc835cd4..5fa8b1a0e 100644 --- a/config/crd/bases/asdb.aerospike.com_aerospikeclusters.yaml +++ b/config/crd/bases/asdb.aerospike.com_aerospikeclusters.yaml @@ -618,6 +618,10 @@ spec: description: AerospikeInitContainerSpec configures the aerospike-init container created by the operator. properties: + imageNameAndTag: + description: ImageNameAndTag is the name:tag of aerospike-init + container image + type: string imageRegistry: description: ImageRegistry is the name of image registry for aerospike-init container image ImageRegistry, e.g. docker.io, @@ -9995,6 +9999,10 @@ spec: description: AerospikeInitContainerSpec configures the aerospike-init container created by the operator. properties: + imageNameAndTag: + description: ImageNameAndTag is the name:tag of aerospike-init + container image + type: string imageRegistry: description: ImageRegistry is the name of image registry for aerospike-init container image ImageRegistry, e.g. docker.io, diff --git a/config/manager/manager.yaml b/config/manager/manager.yaml index 2ac29cfa4..a8dcebc72 100644 --- a/config/manager/manager.yaml +++ b/config/manager/manager.yaml @@ -64,6 +64,9 @@ spec: - name: AEROSPIKE_KUBERNETES_INIT_REGISTRY_NAMESPACE # this is the namespace in registry used to pull aerospike-init image value: aerospike + - name: AEROSPIKE_KUBERNETES_INIT_NAMETAG + # this is the name and tag of aerospike-init image + value: aerospike-kubernetes-init:2.2.1 serviceAccountName: controller-manager terminationGracePeriodSeconds: 10 diff --git a/helm-charts/aerospike-kubernetes-operator/crds/customresourcedefinition_aerospikeclusters.asdb.aerospike.com.yaml b/helm-charts/aerospike-kubernetes-operator/crds/customresourcedefinition_aerospikeclusters.asdb.aerospike.com.yaml index 7cc835cd4..5fa8b1a0e 100644 --- a/helm-charts/aerospike-kubernetes-operator/crds/customresourcedefinition_aerospikeclusters.asdb.aerospike.com.yaml +++ b/helm-charts/aerospike-kubernetes-operator/crds/customresourcedefinition_aerospikeclusters.asdb.aerospike.com.yaml @@ -618,6 +618,10 @@ spec: description: AerospikeInitContainerSpec configures the aerospike-init container created by the operator. properties: + imageNameAndTag: + description: ImageNameAndTag is the name:tag of aerospike-init + container image + type: string imageRegistry: description: ImageRegistry is the name of image registry for aerospike-init container image ImageRegistry, e.g. docker.io, @@ -9995,6 +9999,10 @@ spec: description: AerospikeInitContainerSpec configures the aerospike-init container created by the operator. properties: + imageNameAndTag: + description: ImageNameAndTag is the name:tag of aerospike-init + container image + type: string imageRegistry: description: ImageRegistry is the name of image registry for aerospike-init container image ImageRegistry, e.g. docker.io, diff --git a/helm-charts/aerospike-kubernetes-operator/templates/aerospike-operator-controller-manager-deployment.yaml b/helm-charts/aerospike-kubernetes-operator/templates/aerospike-operator-controller-manager-deployment.yaml index 162112b62..9cbed370d 100644 --- a/helm-charts/aerospike-kubernetes-operator/templates/aerospike-operator-controller-manager-deployment.yaml +++ b/helm-charts/aerospike-kubernetes-operator/templates/aerospike-operator-controller-manager-deployment.yaml @@ -59,6 +59,8 @@ spec: value: {{ .Values.aerospikeKubernetesInitRegistry }} - name: AEROSPIKE_KUBERNETES_INIT_REGISTRY_NAMESPACE value: {{ .Values.aerospikeKubernetesInitRegistryNamespace }} + - name: AEROSPIKE_KUBERNETES_INIT_NAMETAG + value: {{ .Values.aerospikeKubernetesInitNameTag }} {{- if .Values.extraEnv }} {{- range $key, $value := .Values.extraEnv }} - name: "{{ $key }}" diff --git a/helm-charts/aerospike-kubernetes-operator/values.yaml b/helm-charts/aerospike-kubernetes-operator/values.yaml index 1fa9210c2..ecc34149f 100644 --- a/helm-charts/aerospike-kubernetes-operator/values.yaml +++ b/helm-charts/aerospike-kubernetes-operator/values.yaml @@ -36,6 +36,9 @@ aerospikeKubernetesInitRegistry: "docker.io" # Namespace in registry used to pull aerospike-init image aerospikeKubernetesInitRegistryNamespace: "aerospike" +# Name and tag of aerospike-init image +aerospikeKubernetesInitNameTag: "aerospike-kubernetes-init:2.2.1" + ## Resources - limits / requests resources: limits: diff --git a/test/podspec_test.go b/test/podspec_test.go index 8907c0ee2..52f760867 100644 --- a/test/podspec_test.go +++ b/test/podspec_test.go @@ -5,7 +5,6 @@ import ( "fmt" "os" "reflect" - "strings" "time" . "github.com/onsi/ginkgo/v2" @@ -19,6 +18,7 @@ import ( var ( customInitRegistryEnvVar = "CUSTOM_INIT_REGISTRY" customInitRegistryNamespaceEnvVar = "CUSTOM_INIT_REGISTRY_NAMESPACE" + customInitNameAndTagEnvVar = "CUSTOM_INIT_NAMETAG" imagePullSecretNameEnvVar = "IMAGE_PULL_SECRET_NAME" //nolint:gosec // for testing ) @@ -432,8 +432,10 @@ var _ = Describe( It("Should be able to set/update aerospike-init custom registry and namespace", func() { operatorEnvVarRegistry := "docker.io" operatorEnvVarRegistryNamespace := "aerospike" + operatorEnvVarNameAndTag := "aerospike-kubernetes-init:2.2.1" customRegistry := getEnvVar(customInitRegistryEnvVar) customRegistryNamespace := getEnvVar(customInitRegistryNamespaceEnvVar) + customInitNameAndTag := getEnvVar(customInitNameAndTagEnvVar) imagePullSecret := getEnvVar(imagePullSecretNameEnvVar) By("Updating imagePullSecret") @@ -449,16 +451,18 @@ var _ = Describe( err = updateCluster(k8sClient, ctx, aeroCluster) Expect(err).ToNot(HaveOccurred()) - By("Using registry and namespace in CR") + By("Using registry, namespace and name in CR") aeroCluster, err = getCluster(k8sClient, ctx, clusterNamespacedName) Expect(err).ToNot(HaveOccurred()) aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistry = customRegistry aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistryNamespace = &customRegistryNamespace + aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageNameAndTag = customInitNameAndTag err = updateCluster(k8sClient, ctx, aeroCluster) Expect(err).ToNot(HaveOccurred()) - validateImageRegistryNamespace(k8sClient, ctx, aeroCluster, customRegistry, customRegistryNamespace) + validateImageRegistryNamespace(k8sClient, ctx, aeroCluster, customRegistry, + customRegistryNamespace, customInitNameAndTag) By("Using envVar registry and namespace") aeroCluster, err = getCluster(k8sClient, ctx, clusterNamespacedName) @@ -467,19 +471,21 @@ var _ = Describe( // Empty imageRegistry, should use operator envVar docker.io aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistry = "" aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistryNamespace = nil + aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageNameAndTag = "" err = updateCluster(k8sClient, ctx, aeroCluster) Expect(err).ToNot(HaveOccurred()) validateImageRegistryNamespace(k8sClient, ctx, aeroCluster, operatorEnvVarRegistry, - operatorEnvVarRegistryNamespace) + operatorEnvVarRegistryNamespace, operatorEnvVarNameAndTag) }) It("Should be able to recover cluster after setting correct aerospike-init custom registry/namespace", func() { operatorEnvVarRegistry := "docker.io" operatorEnvVarRegistryNamespace := "aerospike" + operatorEnvVarNameAndTag := "aerospike-kubernetes-init:2.2.1" incorrectCustomRegistryNamespace := "incorrectnamespace" - By("Using incorrect registry in CR") + By("Using incorrect registry namespace in CR") aeroCluster, err := getCluster(k8sClient, ctx, clusterNamespacedName) Expect(err).ToNot(HaveOccurred()) @@ -488,9 +494,9 @@ var _ = Describe( Expect(err).Should(HaveOccurred()) validateImageRegistryNamespace(k8sClient, ctx, aeroCluster, operatorEnvVarRegistry, - incorrectCustomRegistryNamespace) + incorrectCustomRegistryNamespace, operatorEnvVarNameAndTag) - By("Using correct registry name") + By("Using correct registry namespace in CR") aeroCluster, err = getCluster(k8sClient, ctx, clusterNamespacedName) Expect(err).ToNot(HaveOccurred()) @@ -500,7 +506,7 @@ var _ = Describe( Expect(err).ToNot(HaveOccurred()) validateImageRegistryNamespace(k8sClient, ctx, aeroCluster, operatorEnvVarRegistry, - operatorEnvVarRegistryNamespace) + operatorEnvVarRegistryNamespace, operatorEnvVarNameAndTag) }) }) Context( @@ -568,16 +574,15 @@ func getEnvVar(envVar string) string { } func validateImageRegistryNamespace(k8sClient client.Client, _ goctx.Context, aeroCluster *asdbv1.AerospikeCluster, - registry, namespace string) { + registry, namespace, nameAndTag string) { stsList, err := getSTSList(aeroCluster, k8sClient) Expect(err).ToNot(HaveOccurred()) - expectedImagePrefix := fmt.Sprintf("%s/%s", registry, namespace) + expectedImage := fmt.Sprintf("%s/%s/%s", registry, namespace, nameAndTag) for stsIndex := range stsList.Items { image := stsList.Items[stsIndex].Spec.Template.Spec.InitContainers[0].Image - hasPrefix := strings.HasPrefix(image, expectedImagePrefix) - Expect(hasPrefix).To(BeTrue(), fmt.Sprintf("expected registry/namespace %s, found image %s", - expectedImagePrefix, image)) + Expect(image == expectedImage).To(BeTrue(), fmt.Sprintf("expected init image %s, found image %s", + expectedImage, image)) } } diff --git a/test/test.sh b/test/test.sh index a84d8754e..db6d06b8e 100755 --- a/test/test.sh +++ b/test/test.sh @@ -11,7 +11,7 @@ set -e # test.sh -c aerospike/aerospike-kubernetes-operator-bundle:1.1.0 -f ".*RackManagement.*" -a "--connect-through-network-type=hostInternal" # test.sh -c -f "" -a "" -while getopts "b:c:f:a:r:p:n:" opt +while getopts "b:c:f:a:r:p:n:t:" opt do case "$opt" in b ) BUNDLE="$OPTARG" ;; @@ -21,6 +21,7 @@ do r ) REGISTRY="$OPTARG" ;; p ) CRED_PATH="$OPTARG" ;; n ) REGISTRY_NAMESPACE="$OPTARG" ;; + t ) INIT_IMAGE_NAME_AND_TAG="$OPTARG" ;; esac done @@ -29,6 +30,7 @@ done CRED_PATH=${CRED_PATH:-$HOME/.docker/config.json} REGISTRY=${REGISTRY:-568976754000.dkr.ecr.ap-south-1.amazonaws.com} REGISTRY_NAMESPACE=${REGISTRY_NAMESPACE:-aerospike} +INIT_IMAGE_NAME_AND_TAG=${INIT_IMAGE_NAME_AND_TAG:-aerospike-kubernetes-init:2.2.1} DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" @@ -63,6 +65,7 @@ echo "---------------------" export CUSTOM_INIT_REGISTRY="$REGISTRY" export CUSTOM_INIT_REGISTRY_NAMESPACE="$REGISTRY_NAMESPACE" +export CUSTOM_INIT_NAMETAG="$INIT_IMAGE_NAME_AND_TAG" export IMAGE_PULL_SECRET_NAME="$IMAGE_PULL_SECRET" make test FOCUS="$FOCUS" ARGS="$ARGS" From 1458518f2a01c6fe7775f529ad43d379ba05501d Mon Sep 17 00:00:00 2001 From: Tanmay Jain Date: Tue, 10 Sep 2024 18:50:54 +0530 Subject: [PATCH 4/7] Addressing comments --- Jenkinsfile | 4 +- api/v1/utils.go | 40 ++++++------------- config/manager/manager.yaml | 2 +- ...perator-controller-manager-deployment.yaml | 2 +- test/cluster/cluster_test.go | 2 +- test/cluster/podspec_test.go | 16 ++++---- test/test.sh | 6 +-- 7 files changed, 28 insertions(+), 44 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 12e9c2ebe..db2d6d55e 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -24,7 +24,7 @@ pipeline { AEROSPIKE_CUSTOM_INIT_REGISTRY="568976754000.dkr.ecr.ap-south-1.amazonaws.com" AEROSPIKE_CUSTOM_INIT_REGISTRY_NAMESPACE="aerospike" - AEROSPIKE_CUSTOM_INIT_NAMETAG="aerospike" + AEROSPIKE_CUSTOM_INIT_NAME_TAG="aerospike-kubernetes-init:2.2.1" } stages { @@ -92,7 +92,7 @@ pipeline { dir("${env.GO_REPO}") { sh "rsync -aK ${env.WORKSPACE}/../../aerospike-kubernetes-operator-resources/secrets/ config/samples/secrets" sh "set +x; docker login --username AWS 568976754000.dkr.ecr.ap-south-1.amazonaws.com -p \$(aws ecr get-login-password --region ap-south-1); set -x" - sh "./test/test.sh -b ${OPERATOR_BUNDLE_IMAGE_CANDIDATE_NAME} -c ${OPERATOR_CATALOG_IMAGE_CANDIDATE_NAME} -r ${AEROSPIKE_CUSTOM_INIT_REGISTRY} -n ${AEROSPIKE_CUSTOM_INIT_REGISTRY_NAMESPACE} -t ${AEROSPIKE_CUSTOM_INIT_NAMETAG}" + sh "./test/test.sh -b ${OPERATOR_BUNDLE_IMAGE_CANDIDATE_NAME} -c ${OPERATOR_CATALOG_IMAGE_CANDIDATE_NAME} -r ${AEROSPIKE_CUSTOM_INIT_REGISTRY} -n ${AEROSPIKE_CUSTOM_INIT_REGISTRY_NAMESPACE} -t ${AEROSPIKE_CUSTOM_INIT_NAME_TAG}" } } diff --git a/api/v1/utils.go b/api/v1/utils.go index a5a35cc84..e07febf11 100644 --- a/api/v1/utils.go +++ b/api/v1/utils.go @@ -70,11 +70,9 @@ const ( const ( AerospikeServerContainerName = "aerospike-server" AerospikeInitContainerName = "aerospike-init" - AerospikeInitContainerNameAndTag = "nameAndTag" - AerospikeInitContainerRegistry = "registry" AerospikeInitContainerRegistryEnvVar = "AEROSPIKE_KUBERNETES_INIT_REGISTRY" AerospikeInitContainerRegistryNamespaceEnvVar = "AEROSPIKE_KUBERNETES_INIT_REGISTRY_NAMESPACE" - AerospikeInitContainerNameTagEnvVar = "AEROSPIKE_KUBERNETES_INIT_NAMETAG" + AerospikeInitContainerNameTagEnvVar = "AEROSPIKE_KUBERNETES_INIT_NAME_TAG" AerospikeInitContainerDefaultRegistry = "docker.io" AerospikeInitContainerDefaultRegistryNamespace = "aerospike" AerospikeInitContainerDefaultRepoAndTag = "aerospike-kubernetes-init:2.2.1" @@ -134,9 +132,11 @@ func getInitContainerImage(registry, namespace, repoAndTag string) string { } func GetAerospikeInitContainerImage(aeroCluster *AerospikeCluster) string { - registry := getInitContainerImageValue(aeroCluster, AerospikeInitContainerRegistry) + registry := getInitContainerImageValue(aeroCluster, AerospikeInitContainerRegistryEnvVar, + AerospikeInitContainerDefaultRegistry) namespace := getInitContainerImageRegistryNamespace(aeroCluster) - repoAndTag := getInitContainerImageValue(aeroCluster, AerospikeInitContainerNameAndTag) + repoAndTag := getInitContainerImageValue(aeroCluster, AerospikeInitContainerNameTagEnvVar, + AerospikeInitContainerDefaultRepoAndTag) return getInitContainerImage(registry, namespace, repoAndTag) } @@ -163,46 +163,30 @@ func getInitContainerImageRegistryNamespace(aeroCluster *AerospikeCluster) strin return *namespace } -func getInitContainerImageValue(aeroCluster *AerospikeCluster, valueType string) string { +func getInitContainerImageValue(aeroCluster *AerospikeCluster, envVar, defaultValue string) string { var value string // Check in CR based on the valueType if aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec != nil { - switch valueType { - case AerospikeInitContainerRegistry: + switch envVar { + case AerospikeInitContainerRegistryEnvVar: value = aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistry - case AerospikeInitContainerNameAndTag: + case AerospikeInitContainerNameTagEnvVar: value = aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageNameAndTag } } // Check in EnvVar if not found in CR if value == "" { - var ( - envVar string - found bool - ) - - switch valueType { - case AerospikeInitContainerRegistry: - envVar, found = os.LookupEnv(AerospikeInitContainerRegistryEnvVar) - case AerospikeInitContainerNameAndTag: - envVar, found = os.LookupEnv(AerospikeInitContainerNameTagEnvVar) - } - + envVal, found := os.LookupEnv(envVar) if found { - value = envVar + value = envVal } } // Return default values if still not found if value == "" { - switch valueType { - case AerospikeInitContainerRegistry: - return AerospikeInitContainerDefaultRegistry - case AerospikeInitContainerNameAndTag: - return AerospikeInitContainerDefaultRepoAndTag - } + return defaultValue } return value diff --git a/config/manager/manager.yaml b/config/manager/manager.yaml index a8dcebc72..d5dc95a08 100644 --- a/config/manager/manager.yaml +++ b/config/manager/manager.yaml @@ -64,7 +64,7 @@ spec: - name: AEROSPIKE_KUBERNETES_INIT_REGISTRY_NAMESPACE # this is the namespace in registry used to pull aerospike-init image value: aerospike - - name: AEROSPIKE_KUBERNETES_INIT_NAMETAG + - name: AEROSPIKE_KUBERNETES_INIT_NAME_TAG # this is the name and tag of aerospike-init image value: aerospike-kubernetes-init:2.2.1 serviceAccountName: controller-manager diff --git a/helm-charts/aerospike-kubernetes-operator/templates/aerospike-operator-controller-manager-deployment.yaml b/helm-charts/aerospike-kubernetes-operator/templates/aerospike-operator-controller-manager-deployment.yaml index 9cbed370d..a28a2e440 100644 --- a/helm-charts/aerospike-kubernetes-operator/templates/aerospike-operator-controller-manager-deployment.yaml +++ b/helm-charts/aerospike-kubernetes-operator/templates/aerospike-operator-controller-manager-deployment.yaml @@ -59,7 +59,7 @@ spec: value: {{ .Values.aerospikeKubernetesInitRegistry }} - name: AEROSPIKE_KUBERNETES_INIT_REGISTRY_NAMESPACE value: {{ .Values.aerospikeKubernetesInitRegistryNamespace }} - - name: AEROSPIKE_KUBERNETES_INIT_NAMETAG + - name: AEROSPIKE_KUBERNETES_INIT_NAME_TAG value: {{ .Values.aerospikeKubernetesInitNameTag }} {{- if .Values.extraEnv }} {{- range $key, $value := .Values.extraEnv }} diff --git a/test/cluster/cluster_test.go b/test/cluster/cluster_test.go index c660a241b..c1f726b59 100644 --- a/test/cluster/cluster_test.go +++ b/test/cluster/cluster_test.go @@ -658,7 +658,7 @@ func DeployClusterForDiffStorageTest( } repFact := nHosts - //nolint:wsl //Comments are for test-case description + Context( "Positive", func() { // Cluster with n nodes, enterprise can be more than 8 diff --git a/test/cluster/podspec_test.go b/test/cluster/podspec_test.go index d3120fb21..2a5226540 100644 --- a/test/cluster/podspec_test.go +++ b/test/cluster/podspec_test.go @@ -18,7 +18,7 @@ import ( var ( customInitRegistryEnvVar = "CUSTOM_INIT_REGISTRY" customInitRegistryNamespaceEnvVar = "CUSTOM_INIT_REGISTRY_NAMESPACE" - customInitNameAndTagEnvVar = "CUSTOM_INIT_NAMETAG" + customInitNameAndTagEnvVar = "CUSTOM_INIT_NAME_TAG" imagePullSecretNameEnvVar = "IMAGE_PULL_SECRET_NAME" //nolint:gosec // for testing ) @@ -429,7 +429,7 @@ var _ = Describe( } }) - It("Should be able to set/update aerospike-init custom registry and namespace", func() { + It("Should be able to set/update aerospike-init custom registry, namespace and name", func() { operatorEnvVarRegistry := "docker.io" operatorEnvVarRegistryNamespace := "aerospike" operatorEnvVarNameAndTag := "aerospike-kubernetes-init:2.2.1" @@ -461,10 +461,10 @@ var _ = Describe( err = updateCluster(k8sClient, ctx, aeroCluster) Expect(err).ToNot(HaveOccurred()) - validateImageRegistryNamespace(k8sClient, ctx, aeroCluster, customRegistry, + validateInitImage(k8sClient, aeroCluster, customRegistry, customRegistryNamespace, customInitNameAndTag) - By("Using envVar registry and namespace") + By("Using envVar registry, namespace and name") aeroCluster, err = getCluster(k8sClient, ctx, clusterNamespacedName) Expect(err).ToNot(HaveOccurred()) @@ -475,7 +475,7 @@ var _ = Describe( err = updateCluster(k8sClient, ctx, aeroCluster) Expect(err).ToNot(HaveOccurred()) - validateImageRegistryNamespace(k8sClient, ctx, aeroCluster, operatorEnvVarRegistry, + validateInitImage(k8sClient, aeroCluster, operatorEnvVarRegistry, operatorEnvVarRegistryNamespace, operatorEnvVarNameAndTag) }) @@ -493,7 +493,7 @@ var _ = Describe( err = updateClusterWithTO(k8sClient, ctx, aeroCluster, time.Minute*1) Expect(err).Should(HaveOccurred()) - validateImageRegistryNamespace(k8sClient, ctx, aeroCluster, operatorEnvVarRegistry, + validateInitImage(k8sClient, aeroCluster, operatorEnvVarRegistry, incorrectCustomRegistryNamespace, operatorEnvVarNameAndTag) By("Using correct registry namespace in CR") @@ -505,7 +505,7 @@ var _ = Describe( err = updateCluster(k8sClient, ctx, aeroCluster) Expect(err).ToNot(HaveOccurred()) - validateImageRegistryNamespace(k8sClient, ctx, aeroCluster, operatorEnvVarRegistry, + validateInitImage(k8sClient, aeroCluster, operatorEnvVarRegistry, operatorEnvVarRegistryNamespace, operatorEnvVarNameAndTag) }) }) @@ -573,7 +573,7 @@ func getEnvVar(envVar string) string { return envVarVal } -func validateImageRegistryNamespace(k8sClient client.Client, _ goctx.Context, aeroCluster *asdbv1.AerospikeCluster, +func validateInitImage(k8sClient client.Client, aeroCluster *asdbv1.AerospikeCluster, registry, namespace, nameAndTag string) { stsList, err := getSTSList(aeroCluster, k8sClient) Expect(err).ToNot(HaveOccurred()) diff --git a/test/test.sh b/test/test.sh index a8adbc4e7..0fcfe528f 100755 --- a/test/test.sh +++ b/test/test.sh @@ -21,7 +21,7 @@ do r ) REGISTRY="$OPTARG" ;; p ) CRED_PATH="$OPTARG" ;; n ) REGISTRY_NAMESPACE="$OPTARG" ;; - t ) INIT_IMAGE_NAME_AND_TAG="$OPTARG" ;; + t ) INIT_IMAGE_NAME_TAG="$OPTARG" ;; esac done @@ -30,7 +30,7 @@ done CRED_PATH=${CRED_PATH:-$HOME/.docker/config.json} REGISTRY=${REGISTRY:-568976754000.dkr.ecr.ap-south-1.amazonaws.com} REGISTRY_NAMESPACE=${REGISTRY_NAMESPACE:-aerospike} -INIT_IMAGE_NAME_AND_TAG=${INIT_IMAGE_NAME_AND_TAG:-aerospike-kubernetes-init:2.2.1} +INIT_IMAGE_NAME_TAG=${INIT_IMAGE_NAME_TAG:-aerospike-kubernetes-init:2.2.1} DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" @@ -65,7 +65,7 @@ echo "---------------------" export CUSTOM_INIT_REGISTRY="$REGISTRY" export CUSTOM_INIT_REGISTRY_NAMESPACE="$REGISTRY_NAMESPACE" -export CUSTOM_INIT_NAMETAG="$INIT_IMAGE_NAME_AND_TAG" +export CUSTOM_INIT_NAME_TAG="$INIT_IMAGE_NAME_TAG" export IMAGE_PULL_SECRET_NAME="$IMAGE_PULL_SECRET" make all-test FOCUS="$FOCUS" ARGS="$ARGS" From 2450ec6598ab544ba892fb2e122a70d60677e89e Mon Sep 17 00:00:00 2001 From: Tanmay Jain Date: Tue, 10 Sep 2024 23:14:08 +0530 Subject: [PATCH 5/7] nit --- test/cluster/cluster_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cluster/cluster_test.go b/test/cluster/cluster_test.go index c1f726b59..c660a241b 100644 --- a/test/cluster/cluster_test.go +++ b/test/cluster/cluster_test.go @@ -658,7 +658,7 @@ func DeployClusterForDiffStorageTest( } repFact := nHosts - + //nolint:wsl //Comments are for test-case description Context( "Positive", func() { // Cluster with n nodes, enterprise can be more than 8 From 7895a1c31048c18928fdeb336b0b1a18eb8f6673 Mon Sep 17 00:00:00 2001 From: Tanmay Jain Date: Thu, 12 Sep 2024 12:31:57 +0530 Subject: [PATCH 6/7] changing log prefix --- test/backup/test_utils.go | 2 +- test/backup_service/test_utils.go | 2 +- test/cluster/batch_restart_pods_test.go | 14 +++++++------- test/cluster/cluster_helper.go | 2 +- test/restore/test_utils.go | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/test/backup/test_utils.go b/test/backup/test_utils.go index 307780fb1..6efd74d65 100644 --- a/test/backup/test_utils.go +++ b/test/backup/test_utils.go @@ -33,7 +33,7 @@ var testCtx = context.TODO() var backupServiceName, backupServiceNamespace string -var pkgLog = ctrl.Log.WithName("backup") +var pkgLog = ctrl.Log.WithName("aerospikebackup") var aerospikeNsNm = types.NamespacedName{ Name: "aerocluster", diff --git a/test/backup_service/test_utils.go b/test/backup_service/test_utils.go index 1b27d563a..72142a4a3 100644 --- a/test/backup_service/test_utils.go +++ b/test/backup_service/test_utils.go @@ -34,7 +34,7 @@ const ( var testCtx = context.TODO() -var pkgLog = ctrl.Log.WithName("backupservice") +var pkgLog = ctrl.Log.WithName("aerospikebackupservice") func NewBackupService() (*asdbv1beta1.AerospikeBackupService, error) { configBytes, err := getBackupServiceConfBytes() diff --git a/test/cluster/batch_restart_pods_test.go b/test/cluster/batch_restart_pods_test.go index 27803a1fb..aa708b340 100644 --- a/test/cluster/batch_restart_pods_test.go +++ b/test/cluster/batch_restart_pods_test.go @@ -250,7 +250,7 @@ func BatchRollingRestart(ctx goctx.Context, clusterNamespacedName types.Namespac aeroCluster, err := getCluster(k8sClient, ctx, clusterNamespacedName) Expect(err).ToNot(HaveOccurred()) - aeroCluster.Spec.PodSpec.AerospikeContainerSpec.Resources = schedulableResource("1Gi") + aeroCluster.Spec.PodSpec.AerospikeContainerSpec.Resources = schedulableResource("200m") err = updateCluster(k8sClient, ctx, aeroCluster) Expect(err).ToNot(HaveOccurred()) @@ -279,7 +279,7 @@ func BatchRollingRestart(ctx goctx.Context, clusterNamespacedName types.Namespac Expect(err).ToNot(HaveOccurred()) // schedule batch of pods - err = rollingRestartTest(k8sClient, ctx, clusterNamespacedName, percent("100%"), "1Gi") + err = rollingRestartTest(k8sClient, ctx, clusterNamespacedName, percent("100%"), "200m") Expect(err).ToNot(HaveOccurred()) By("Using RollingUpdateBatchSize Count greater than pods in rack") @@ -288,7 +288,7 @@ func BatchRollingRestart(ctx goctx.Context, clusterNamespacedName types.Namespac Expect(err).ToNot(HaveOccurred()) // Schedule batch of pods - err = rollingRestartTest(k8sClient, ctx, clusterNamespacedName, count(10), "2Gi") + err = rollingRestartTest(k8sClient, ctx, clusterNamespacedName, count(10), "300m") Expect(err).ToNot(HaveOccurred()) }) @@ -299,7 +299,7 @@ func BatchRollingRestart(ctx goctx.Context, clusterNamespacedName types.Namespac err := batchRollingRestartTest(k8sClient, ctx, clusterNamespacedName, percent("90%")) Expect(err).ToNot(HaveOccurred()) - err = rollingRestartTest(k8sClient, ctx, clusterNamespacedName, percent("90%"), "1Gi") + err = rollingRestartTest(k8sClient, ctx, clusterNamespacedName, percent("90%"), "200m") Expect(err).ToNot(HaveOccurred()) By("Update RollingUpdateBatchSize Count") @@ -307,7 +307,7 @@ func BatchRollingRestart(ctx goctx.Context, clusterNamespacedName types.Namespac err = batchRollingRestartTest(k8sClient, ctx, clusterNamespacedName, count(3)) Expect(err).ToNot(HaveOccurred()) - err = rollingRestartTest(k8sClient, ctx, clusterNamespacedName, count(3), "2Gi") + err = rollingRestartTest(k8sClient, ctx, clusterNamespacedName, count(3), "300m") Expect(err).ToNot(HaveOccurred()) }) @@ -319,7 +319,7 @@ func BatchRollingRestart(ctx goctx.Context, clusterNamespacedName types.Namespac Expect(err).ToNot(HaveOccurred()) aeroCluster.Spec.RackConfig.RollingUpdateBatchSize = count(3) - aeroCluster.Spec.PodSpec.AerospikeContainerSpec.Resources = schedulableResource("1Gi") + aeroCluster.Spec.PodSpec.AerospikeContainerSpec.Resources = schedulableResource("200m") err = k8sClient.Update(ctx, aeroCluster) Expect(err).ToNot(HaveOccurred()) @@ -339,7 +339,7 @@ func BatchRollingRestart(ctx goctx.Context, clusterNamespacedName types.Namespac By("Again Update RollingUpdateBatchSize Count") - err = rollingRestartTest(k8sClient, ctx, clusterNamespacedName, count(3), "1Gi") + err = rollingRestartTest(k8sClient, ctx, clusterNamespacedName, count(3), "200m") Expect(err).ToNot(HaveOccurred()) }) } diff --git a/test/cluster/cluster_helper.go b/test/cluster/cluster_helper.go index 3eaa477bd..b15e86834 100644 --- a/test/cluster/cluster_helper.go +++ b/test/cluster/cluster_helper.go @@ -47,7 +47,7 @@ const ( var ( storageClass = "ssd" namespace = "test" - pkgLog = ctrl.Log.WithName("cluster") + pkgLog = ctrl.Log.WithName("aerospikecluster") ) const aerospikeConfigSecret string = "aerospike-config-secret" //nolint:gosec // for testing diff --git a/test/restore/test_utils.go b/test/restore/test_utils.go index 04fdbaf81..1dc5ca04d 100644 --- a/test/restore/test_utils.go +++ b/test/restore/test_utils.go @@ -30,7 +30,7 @@ var backupServiceName, backupServiceNamespace string var backupDataPath string -var pkgLog = ctrl.Log.WithName("restore") +var pkgLog = ctrl.Log.WithName("aerospikerestore") var backupNsNm = types.NamespacedName{ Name: "sample-backup", From 088a58de118729ad7067a83e5e4b172252699fd9 Mon Sep 17 00:00:00 2001 From: Tanmay Jain Date: Thu, 12 Sep 2024 12:44:50 +0530 Subject: [PATCH 7/7] addressing comment --- api/v1/utils.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/v1/utils.go b/api/v1/utils.go index e07febf11..f69f92a04 100644 --- a/api/v1/utils.go +++ b/api/v1/utils.go @@ -75,7 +75,7 @@ const ( AerospikeInitContainerNameTagEnvVar = "AEROSPIKE_KUBERNETES_INIT_NAME_TAG" AerospikeInitContainerDefaultRegistry = "docker.io" AerospikeInitContainerDefaultRegistryNamespace = "aerospike" - AerospikeInitContainerDefaultRepoAndTag = "aerospike-kubernetes-init:2.2.1" + AerospikeInitContainerDefaultNameAndTag = "aerospike-kubernetes-init:2.2.1" AerospikeAppLabel = "app" AerospikeAppLabelValue = "aerospike-cluster" AerospikeCustomResourceLabel = "aerospike.com/cr" @@ -136,7 +136,7 @@ func GetAerospikeInitContainerImage(aeroCluster *AerospikeCluster) string { AerospikeInitContainerDefaultRegistry) namespace := getInitContainerImageRegistryNamespace(aeroCluster) repoAndTag := getInitContainerImageValue(aeroCluster, AerospikeInitContainerNameTagEnvVar, - AerospikeInitContainerDefaultRepoAndTag) + AerospikeInitContainerDefaultNameAndTag) return getInitContainerImage(registry, namespace, repoAndTag) }