diff --git a/controllers/argocd/statefulset.go b/controllers/argocd/statefulset.go index fb36f62c1..37e3b208d 100644 --- a/controllers/argocd/statefulset.go +++ b/controllers/argocd/statefulset.go @@ -469,7 +469,7 @@ func (r *ReconcileArgoCD) reconcileRedisStatefulSet(cr *argoproj.ArgoCD) error { return r.Client.Create(context.TODO(), ss) } -func getArgoControllerContainerEnv(cr *argoproj.ArgoCD) []corev1.EnvVar { +func getArgoControllerContainerEnv(cr *argoproj.ArgoCD, replicas int32) []corev1.EnvVar { env := make([]corev1.EnvVar, 0) env = append(env, corev1.EnvVar{ @@ -489,10 +489,10 @@ func getArgoControllerContainerEnv(cr *argoproj.ArgoCD) []corev1.EnvVar { }, }) - if cr.Spec.Controller.Sharding.Enabled { + if cr.Spec.Controller.Sharding.Enabled || (cr.Spec.Controller.Sharding.DynamicScalingEnabled != nil && *cr.Spec.Controller.Sharding.DynamicScalingEnabled) { env = append(env, corev1.EnvVar{ Name: "ARGOCD_CONTROLLER_REPLICAS", - Value: fmt.Sprint(cr.Spec.Controller.Sharding.Replicas), + Value: fmt.Sprint(replicas), }) } @@ -564,7 +564,7 @@ func (r *ReconcileArgoCD) reconcileApplicationControllerStatefulSet(cr *argoproj ss.Spec.Replicas = &replicas controllerEnv := cr.Spec.Controller.Env // Sharding setting explicitly overrides a value set in the env - controllerEnv = argoutil.EnvMerge(controllerEnv, getArgoControllerContainerEnv(cr), true) + controllerEnv = argoutil.EnvMerge(controllerEnv, getArgoControllerContainerEnv(cr, replicas), true) // Let user specify their own environment first controllerEnv = argoutil.EnvMerge(controllerEnv, proxyEnvVars(), false) diff --git a/controllers/argocd/statefulset_test.go b/controllers/argocd/statefulset_test.go index d68155c9f..89195d93a 100644 --- a/controllers/argocd/statefulset_test.go +++ b/controllers/argocd/statefulset_test.go @@ -411,6 +411,29 @@ func TestReconcileArgoCD_reconcileApplicationController_withSharding(t *testing. }}, }, }, + { + sharding: argoproj.ArgoCDApplicationControllerShardSpec{ + DynamicScalingEnabled: boolPtr(true), + MinShards: 2, + MaxShards: 4, + ClustersPerShard: 1, + }, + replicas: 2, + vars: []corev1.EnvVar{ + {Name: "ARGOCD_CONTROLLER_REPLICAS", Value: "2"}, + {Name: "HOME", Value: "/home/argocd"}, + {Name: "REDIS_PASSWORD", Value: "", + ValueFrom: &corev1.EnvVarSource{ + SecretKeyRef: &corev1.SecretKeySelector{ + LocalObjectReference: corev1.LocalObjectReference{ + Name: fmt.Sprintf("argocd-redis-initial-password"), + }, + Key: "admin.password", + }, + }, + }, + }, + }, } for _, st := range tests { diff --git a/controllers/argocd/util_test.go b/controllers/argocd/util_test.go index 020526183..68bcba92b 100644 --- a/controllers/argocd/util_test.go +++ b/controllers/argocd/util_test.go @@ -630,7 +630,7 @@ func TestGetArgoApplicationContainerEnv(t *testing.T) { for _, tt := range cmdTests { cr := makeTestArgoCD(tt.opts...) - env := getArgoControllerContainerEnv(cr) + env := getArgoControllerContainerEnv(cr, 1) if !reflect.DeepEqual(env, tt.want) { t.Fatalf("got %#v, want %#v", env, tt.want) diff --git a/tests/k8s/1-032_validate_dynamic_scaling/01-assert.yaml b/tests/k8s/1-032_validate_dynamic_scaling/01-assert.yaml index dc419f168..3af7e5e74 100644 --- a/tests/k8s/1-032_validate_dynamic_scaling/01-assert.yaml +++ b/tests/k8s/1-032_validate_dynamic_scaling/01-assert.yaml @@ -3,4 +3,24 @@ kind: ArgoCD metadata: name: argocd status: - phase: Available \ No newline at end of file + phase: Available +--- +apiVersion: kuttl.dev/v1beta1 +kind: TestAssert +timeout: 720 +commands: + - script: | + stsReplicas=$(kubectl get sts argocd-application-controller -n $NAMESPACE -o jsonpath='{.status.replicas}') + echo "$stsReplicas" + if test "$stsReplicas" != "1"; then + echo "FAILED! Number of replicas not equal to desired replicas" + exit 1 + fi + + replicaEnvVariable=$(kubectl exec -it -n $NAMESPACE argocd-application-controller-0 -- printenv | grep -i ARGOCD_CONTROLLER_REPLICAS) + echo "$replicaEnvVariable" + if test "$replicaEnvVariable" != "ARGOCD_CONTROLLER_REPLICAS=1"; then + echo "FAILED! ARGOCD_CONTROLLER_REPLICAS not equal to desired replicas" + exit 1 + fi + exit 0 diff --git a/tests/k8s/1-032_validate_dynamic_scaling/01-install.yaml b/tests/k8s/1-032_validate_dynamic_scaling/01-install.yaml index b70c30661..d505156fc 100644 --- a/tests/k8s/1-032_validate_dynamic_scaling/01-install.yaml +++ b/tests/k8s/1-032_validate_dynamic_scaling/01-install.yaml @@ -13,4 +13,4 @@ spec: apiVersion: kuttl.dev/v1beta1 kind: TestStep commands: - - command: sleep 30s \ No newline at end of file + - command: sleep 60s diff --git a/tests/k8s/1-032_validate_dynamic_scaling/02-add-cluster-secrets.yaml b/tests/k8s/1-032_validate_dynamic_scaling/02-add-cluster-secrets.yaml index 92e4d7e56..d05f310c1 100644 --- a/tests/k8s/1-032_validate_dynamic_scaling/02-add-cluster-secrets.yaml +++ b/tests/k8s/1-032_validate_dynamic_scaling/02-add-cluster-secrets.yaml @@ -23,4 +23,4 @@ stringData: apiVersion: kuttl.dev/v1beta1 kind: TestStep commands: - - command: sleep 30s + - command: sleep 60s diff --git a/tests/k8s/1-032_validate_dynamic_scaling/02-assert.yaml b/tests/k8s/1-032_validate_dynamic_scaling/02-assert.yaml index 2a7afd31e..38c4b5ce0 100644 --- a/tests/k8s/1-032_validate_dynamic_scaling/02-assert.yaml +++ b/tests/k8s/1-032_validate_dynamic_scaling/02-assert.yaml @@ -1,6 +1,3 @@ -apiVersion: kuttl.dev/v1beta1 -kind: TestAssert -timeout: 720 --- apiVersion: argoproj.io/v1alpha1 kind: ArgoCD @@ -15,3 +12,23 @@ metadata: name: argocd-application-controller status: replicas: 3 +--- +apiVersion: kuttl.dev/v1beta1 +kind: TestAssert +timeout: 720 +commands: + - script: | + stsReplicas=$(kubectl get sts argocd-application-controller -n $NAMESPACE -o jsonpath='{.status.replicas}') + echo "$stsReplicas" + if test "$stsReplicas" != "3"; then + echo "FAILED! Number of replicas not equal to desired replicas" + exit 1 + fi + + replicaEnvVariable=$(kubectl exec -it -n $NAMESPACE argocd-application-controller-0 -- printenv | grep -i ARGOCD_CONTROLLER_REPLICAS) + echo "$replicaEnvVariable" + if test "$replicaEnvVariable" != "ARGOCD_CONTROLLER_REPLICAS=3"; then + echo "FAILED! ARGOCD_CONTROLLER_REPLICAS not equal to desired replicas" + exit 1 + fi + exit 0 diff --git a/tests/k8s/1-032_validate_dynamic_scaling/03-assert.yaml b/tests/k8s/1-032_validate_dynamic_scaling/03-assert.yaml index cd383a7ac..ab154c719 100644 --- a/tests/k8s/1-032_validate_dynamic_scaling/03-assert.yaml +++ b/tests/k8s/1-032_validate_dynamic_scaling/03-assert.yaml @@ -17,11 +17,18 @@ apiVersion: kuttl.dev/v1beta1 kind: TestAssert timeout: 720 commands: -- script: | - stsReplicas=$(kubectl get sts argocd-application-controller -n $NAMESPACE -o jsonpath='{.status.replicas}') - echo "$stsReplicas" - if test "$stsReplicas" != "4"; then - echo "FAILED! Number of replicas not equal to maxShards" - exit 1 - fi - exit 0 + - script: | + stsReplicas=$(kubectl get sts argocd-application-controller -n $NAMESPACE -o jsonpath='{.status.replicas}') + echo "$stsReplicas" + if test "$stsReplicas" != "4"; then + echo "FAILED! Number of replicas not equal to maxShards" + exit 1 + fi + + replicaEnvVariable=$(kubectl exec -it -n $NAMESPACE argocd-application-controller-0 -- printenv | grep -i ARGOCD_CONTROLLER_REPLICAS) + echo "$replicaEnvVariable" + if test "$replicaEnvVariable" != "ARGOCD_CONTROLLER_REPLICAS=4"; then + echo "FAILED! ARGOCD_CONTROLLER_REPLICAS not equal to maxShards" + exit 1 + fi + exit 0