From 108f04493daa2eb648139909aab1e6e6aa805cb7 Mon Sep 17 00:00:00 2001 From: Mangaal Date: Thu, 30 Jan 2025 11:28:28 +0530 Subject: [PATCH 1/6] add change check for redis ha proxy deployment Signed-off-by: Mangaal --- controllers/argocd/deployment.go | 47 ++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/controllers/argocd/deployment.go b/controllers/argocd/deployment.go index 71f031f3f..fc1c80d91 100644 --- a/controllers/argocd/deployment.go +++ b/controllers/argocd/deployment.go @@ -834,7 +834,48 @@ func (r *ReconcileArgoCD) reconcileRedisHAProxyDeployment(cr *argoproj.ArgoCD) e changed = true } updateNodePlacement(existing, deploy, &changed, &explanation) - + if !reflect.DeepEqual(deploy.Spec.Template.Spec.Volumes, existing.Spec.Template.Spec.Volumes) { + existing.Spec.Template.Spec.Volumes = deploy.Spec.Template.Spec.Volumes + if changed { + explanation += ", " + } + explanation += "volumes" + changed = true + } + if !reflect.DeepEqual(deploy.Spec.Template.Spec.Containers[0].VolumeMounts, + existing.Spec.Template.Spec.Containers[0].VolumeMounts) { + existing.Spec.Template.Spec.Containers[0].VolumeMounts = deploy.Spec.Template.Spec.Containers[0].VolumeMounts + if changed { + explanation += ", " + } + explanation += "container volume mounts" + changed = true + } + if !reflect.DeepEqual(deploy.Spec.Template.Spec.InitContainers, existing.Spec.Template.Spec.InitContainers) { + existing.Spec.Template.Spec.InitContainers = deploy.Spec.Template.Spec.InitContainers + if changed { + explanation += ", " + } + explanation += "init containers" + changed = true + } + if !reflect.DeepEqual(deploy.Spec.Template.Spec.Containers[0].Env, + existing.Spec.Template.Spec.Containers[0].Env) { + existing.Spec.Template.Spec.Containers[0].Env = deploy.Spec.Template.Spec.Containers[0].Env + if changed { + explanation += ", " + } + explanation += "container env" + changed = true + } + if !reflect.DeepEqual(deploy.Spec.Template.Spec.Containers[0].Command, existing.Spec.Template.Spec.Containers[0].Command) { + existing.Spec.Template.Spec.Containers[0].Command = deploy.Spec.Template.Spec.Containers[0].Command + if changed { + explanation += ", " + } + explanation += "container command" + changed = true + } if !reflect.DeepEqual(deploy.Spec.Template.Spec.Containers[0].Resources, existing.Spec.Template.Spec.Containers[0].Resources) { existing.Spec.Template.Spec.Containers[0].Resources = deploy.Spec.Template.Spec.Containers[0].Resources if changed { @@ -843,7 +884,6 @@ func (r *ReconcileArgoCD) reconcileRedisHAProxyDeployment(cr *argoproj.ArgoCD) e explanation += "container resources" changed = true } - if !reflect.DeepEqual(deploy.Spec.Template.Spec.Containers[0].SecurityContext, existing.Spec.Template.Spec.Containers[0].SecurityContext) { existing.Spec.Template.Spec.Containers[0].SecurityContext = deploy.Spec.Template.Spec.Containers[0].SecurityContext if changed { @@ -852,7 +892,6 @@ func (r *ReconcileArgoCD) reconcileRedisHAProxyDeployment(cr *argoproj.ArgoCD) e explanation += "container security context" changed = true } - if !reflect.DeepEqual(deploy.Spec.Template.Spec.InitContainers[0].Resources, existing.Spec.Template.Spec.InitContainers[0].Resources) { existing.Spec.Template.Spec.InitContainers[0].Resources = deploy.Spec.Template.Spec.InitContainers[0].Resources if changed { @@ -861,7 +900,6 @@ func (r *ReconcileArgoCD) reconcileRedisHAProxyDeployment(cr *argoproj.ArgoCD) e explanation += "init container resources" changed = true } - if !reflect.DeepEqual(deploy.Spec.Template.Spec.InitContainers[0].SecurityContext, existing.Spec.Template.Spec.InitContainers[0].SecurityContext) { existing.Spec.Template.Spec.InitContainers[0].SecurityContext = deploy.Spec.Template.Spec.InitContainers[0].SecurityContext if changed { @@ -870,7 +908,6 @@ func (r *ReconcileArgoCD) reconcileRedisHAProxyDeployment(cr *argoproj.ArgoCD) e explanation += "init container security context" changed = true } - if changed { argoutil.LogResourceUpdate(log, existing, "updating", explanation) return r.Client.Update(context.TODO(), existing) From cefa27a02765134548f762d9ec6cdc10e73e1425 Mon Sep 17 00:00:00 2001 From: Mangaal Date: Thu, 30 Jan 2025 12:06:47 +0530 Subject: [PATCH 2/6] add change check for redis ha server deployment Signed-off-by: Mangaal --- controllers/argocd/statefulset.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/controllers/argocd/statefulset.go b/controllers/argocd/statefulset.go index 205b57ba1..90a794477 100644 --- a/controllers/argocd/statefulset.go +++ b/controllers/argocd/statefulset.go @@ -452,6 +452,14 @@ func (r *ReconcileArgoCD) reconcileRedisStatefulSet(cr *argoproj.ArgoCD) error { explanation += fmt.Sprintf("container '%s' image", container.Name) changed = true } + if !reflect.DeepEqual(ss.Spec.Template.Spec.Containers[i].VolumeMounts, existing.Spec.Template.Spec.Containers[i].VolumeMounts) { + existing.Spec.Template.Spec.Containers[i].VolumeMounts = ss.Spec.Template.Spec.Containers[i].VolumeMounts + if changed { + explanation += ", " + } + explanation += fmt.Sprintf("container '%s' VolumeMounts", container.Name) + changed = true + } if !reflect.DeepEqual(ss.Spec.Template.Spec.Containers[i].Resources, existing.Spec.Template.Spec.Containers[i].Resources) { existing.Spec.Template.Spec.Containers[i].Resources = ss.Spec.Template.Spec.Containers[i].Resources @@ -473,10 +481,21 @@ func (r *ReconcileArgoCD) reconcileRedisStatefulSet(cr *argoproj.ArgoCD) error { if !reflect.DeepEqual(ss.Spec.Template.Spec.Containers[i].Env, existing.Spec.Template.Spec.Containers[i].Env) { existing.Spec.Template.Spec.Containers[i].Env = ss.Spec.Template.Spec.Containers[i].Env + if changed { + explanation += ", " + } + explanation += fmt.Sprintf("container '%s' env", container.Name) changed = true } } - + if !reflect.DeepEqual(ss.Spec.Template.Spec.Volumes, existing.Spec.Template.Spec.Volumes) { + existing.Spec.Template.Spec.Volumes = ss.Spec.Template.Spec.Volumes + if changed { + explanation += ", " + } + explanation += "volumes" + changed = true + } if !reflect.DeepEqual(ss.Spec.Template.Spec.InitContainers[0].Resources, existing.Spec.Template.Spec.InitContainers[0].Resources) { existing.Spec.Template.Spec.InitContainers[0].Resources = ss.Spec.Template.Spec.InitContainers[0].Resources if changed { @@ -497,6 +516,10 @@ func (r *ReconcileArgoCD) reconcileRedisStatefulSet(cr *argoproj.ArgoCD) error { if !reflect.DeepEqual(ss.Spec.Template.Spec.InitContainers[0].Env, existing.Spec.Template.Spec.InitContainers[0].Env) { existing.Spec.Template.Spec.InitContainers[0].Env = ss.Spec.Template.Spec.InitContainers[0].Env + if changed { + explanation += ", " + } + explanation += fmt.Sprintf("init container '%s' env", existing.Spec.Template.Spec.InitContainers[0].Name) changed = true } From 46d846e5c9e7972588b9206d3b1ebe8af913faf5 Mon Sep 17 00:00:00 2001 From: Mangaal Date: Mon, 3 Feb 2025 15:55:18 +0530 Subject: [PATCH 3/6] add unit test Signed-off-by: Mangaal --- controllers/argocd/deployment.go | 6 +- controllers/argocd/deployment_test.go | 150 +++++++++++++++++++++++++ controllers/argocd/statefulset_test.go | 103 +++++++++++++++++ 3 files changed, 256 insertions(+), 3 deletions(-) diff --git a/controllers/argocd/deployment.go b/controllers/argocd/deployment.go index fc1c80d91..5a8debbf5 100644 --- a/controllers/argocd/deployment.go +++ b/controllers/argocd/deployment.go @@ -851,12 +851,12 @@ func (r *ReconcileArgoCD) reconcileRedisHAProxyDeployment(cr *argoproj.ArgoCD) e explanation += "container volume mounts" changed = true } - if !reflect.DeepEqual(deploy.Spec.Template.Spec.InitContainers, existing.Spec.Template.Spec.InitContainers) { - existing.Spec.Template.Spec.InitContainers = deploy.Spec.Template.Spec.InitContainers + if !reflect.DeepEqual(deploy.Spec.Template.Spec.InitContainers[0].Env, existing.Spec.Template.Spec.InitContainers[0].Env) { + existing.Spec.Template.Spec.InitContainers[0].Env = deploy.Spec.Template.Spec.InitContainers[0].Env if changed { explanation += ", " } - explanation += "init containers" + explanation += "init containers env" changed = true } if !reflect.DeepEqual(deploy.Spec.Template.Spec.Containers[0].Env, diff --git a/controllers/argocd/deployment_test.go b/controllers/argocd/deployment_test.go index aa117cd99..49de9f080 100644 --- a/controllers/argocd/deployment_test.go +++ b/controllers/argocd/deployment_test.go @@ -887,6 +887,156 @@ func TestReconcileArgoCD_reconcileDeployments_HA_proxy_with_resources(t *testing assert.Equal(t, deployment.Spec.Template.Spec.Containers[0].Resources, newResources) assert.Equal(t, deployment.Spec.Template.Spec.InitContainers[0].Resources, newResources) } +func TestReconcileArgoCD_reconcileRedisHAProxyDeployment_ModifyContainerSpec(t *testing.T) { + logf.SetLogger(ZapLogger(true)) + + a := makeTestArgoCD(func(a *argoproj.ArgoCD) { + a.Spec.HA.Enabled = true + }) + + resObjs := []client.Object{a} + subresObjs := []client.Object{a} + runtimeObjs := []runtime.Object{} + sch := makeTestReconcilerScheme(argoproj.AddToScheme) + cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs) + r := makeTestReconciler(cl, sch) + + assert.NoError(t, r.reconcileRedisHAProxyDeployment(a)) + + deployment := &appsv1.Deployment{} + assert.NoError(t, r.Client.Get( + context.TODO(), + types.NamespacedName{ + Name: "argocd-redis-ha-haproxy", + Namespace: a.Namespace, + }, + deployment)) + + // Modify the deployment container environment variables + deployment.Spec.Template.Spec.Containers[0].Env = append(deployment.Spec.Template.Spec.Containers[0].Env, corev1.EnvVar{ + Name: "TEST_ENV", + Value: "test", + }) + + assert.NoError(t, r.Client.Update(context.TODO(), deployment)) + + // Reconcile again + assert.NoError(t, r.reconcileRedisHAProxyDeployment(a)) + + // Check if the environment variable changes were reverted + assert.NoError(t, r.Client.Get( + context.TODO(), + types.NamespacedName{ + Name: "argocd-redis-ha-haproxy", + Namespace: a.Namespace, + }, + deployment)) + + assert.NotContains(t, deployment.Spec.Template.Spec.Containers[0].Env, corev1.EnvVar{ + Name: "TEST_ENV", + Value: "test", + }) + + // Modify the deployment container command + deployment.Spec.Template.Spec.Containers[0].Command = append(deployment.Spec.Template.Spec.Containers[0].Command, "new-command") + + assert.NoError(t, r.Client.Update(context.TODO(), deployment)) + + // Reconcile again + assert.NoError(t, r.reconcileRedisHAProxyDeployment(a)) + + // Check if the environment variable changes were reverted + assert.NoError(t, r.Client.Get( + context.TODO(), + types.NamespacedName{ + Name: "argocd-redis-ha-haproxy", + Namespace: a.Namespace, + }, + deployment)) + + assert.NotContains(t, deployment.Spec.Template.Spec.Containers[0].Command, "new-command") + + // Modify the deployment initcontainer environment variables + deployment.Spec.Template.Spec.InitContainers[0].Env = append(deployment.Spec.Template.Spec.InitContainers[0].Env, corev1.EnvVar{ + Name: "TEST_ENV", + Value: "test", + }) + + assert.NoError(t, r.Client.Update(context.TODO(), deployment)) + + // Reconcile again + assert.NoError(t, r.reconcileRedisHAProxyDeployment(a)) + + // Check if the environment variable changes were reverted + assert.NoError(t, r.Client.Get( + context.TODO(), + types.NamespacedName{ + Name: "argocd-redis-ha-haproxy", + Namespace: a.Namespace, + }, + deployment)) + + assert.NotContains(t, deployment.Spec.Template.Spec.InitContainers[0].Env, corev1.EnvVar{ + Name: "TEST_ENV", + Value: "test", + }) + + // Modify the deployment volumes + deployment.Spec.Template.Spec.Volumes = append(deployment.Spec.Template.Spec.Volumes, corev1.Volume{ + Name: "test-volume", + VolumeSource: corev1.VolumeSource{ + EmptyDir: &corev1.EmptyDirVolumeSource{}, + }, + }) + deployment.Spec.Template.Spec.Containers[0].VolumeMounts = append(deployment.Spec.Template.Spec.Containers[0].VolumeMounts, corev1.VolumeMount{ + Name: "test-volume", + MountPath: "/test", + }) + + assert.NoError(t, r.Client.Update(context.TODO(), deployment)) + + // Reconcile again + assert.NoError(t, r.reconcileRedisHAProxyDeployment(a)) + + // Check if the volume changes were reverted + assert.NoError(t, r.Client.Get( + context.TODO(), + types.NamespacedName{ + Name: "argocd-redis-ha-haproxy", + Namespace: a.Namespace, + }, + deployment)) + + assert.NotContains(t, deployment.Spec.Template.Spec.Volumes, corev1.Volume{ + Name: "test-volume", + VolumeSource: corev1.VolumeSource{ + EmptyDir: &corev1.EmptyDirVolumeSource{}, + }, + }) + assert.NotContains(t, deployment.Spec.Template.Spec.Containers[0].VolumeMounts, corev1.VolumeMount{ + Name: "test-volume", + MountPath: "/test", + }) + + // Modify the deployment command + deployment.Spec.Template.Spec.Containers[0].Command = append(deployment.Spec.Template.Spec.Containers[0].Command, "test-command") + + assert.NoError(t, r.Client.Update(context.TODO(), deployment)) + + // Reconcile again + assert.NoError(t, r.reconcileRedisHAProxyDeployment(a)) + + // Check if the command changes were reverted + assert.NoError(t, r.Client.Get( + context.TODO(), + types.NamespacedName{ + Name: "argocd-redis-ha-haproxy", + Namespace: a.Namespace, + }, + deployment)) + + assert.NotContains(t, deployment.Spec.Template.Spec.Containers[0].Command, "test-command") +} func TestReconcileArgoCD_reconcileRepoDeployment_updatesVolumeMounts(t *testing.T) { logf.SetLogger(ZapLogger(true)) diff --git a/controllers/argocd/statefulset_test.go b/controllers/argocd/statefulset_test.go index 58b2b4426..593dcf66a 100644 --- a/controllers/argocd/statefulset_test.go +++ b/controllers/argocd/statefulset_test.go @@ -836,3 +836,106 @@ func TestReconcileArgoCD_sidecarcontainer(t *testing.T) { assert.Equal(t, 1, len(ss.Spec.Template.Spec.Containers)) } +func TestReconcileArgoCD_reconcileRedisStatefulSet_ModifyContainerSpec(t *testing.T) { + logf.SetLogger(ZapLogger(true)) + + a := makeTestArgoCD() + a.Spec.HA.Enabled = true + + resObjs := []client.Object{a} + subresObjs := []client.Object{a} + runtimeObjs := []runtime.Object{} + sch := makeTestReconcilerScheme(argoproj.AddToScheme) + cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs) + r := makeTestReconciler(cl, sch) + + // Initial reconciliation to create the StatefulSet + assert.NoError(t, r.reconcileRedisStatefulSet(a)) + + s := newStatefulSetWithSuffix("redis-ha-server", "redis", a) + assert.NoError(t, r.Client.Get(context.TODO(), types.NamespacedName{Name: s.Name, Namespace: a.Namespace}, s)) + + // Modify the container environment variable + s.Spec.Template.Spec.Containers[0].Env = append(s.Spec.Template.Spec.Containers[0].Env, corev1.EnvVar{ + Name: "NEW_ENV_VAR", + Value: "new-value", + }) + assert.NoError(t, r.Client.Update(context.TODO(), s)) + + // Reconcile again and check if the environment variable is reverted + assert.NoError(t, r.reconcileRedisStatefulSet(a)) + assert.NoError(t, r.Client.Get(context.TODO(), types.NamespacedName{Name: s.Name, Namespace: a.Namespace}, s)) + + envVarFound := false + for _, env := range s.Spec.Template.Spec.Containers[0].Env { + if env.Name == "NEW_ENV_VAR" { + envVarFound = true + break + } + } + assert.False(t, envVarFound, "NEW_ENV_VAR should not be present") + + // Modify the container environment variable + s.Spec.Template.Spec.Containers[0].Command = append(s.Spec.Template.Spec.Containers[0].Command, "new-command") + assert.NoError(t, r.Client.Update(context.TODO(), s)) + + // Reconcile again and check if the environment variable is reverted + assert.NoError(t, r.reconcileRedisStatefulSet(a)) + assert.NoError(t, r.Client.Get(context.TODO(), types.NamespacedName{Name: s.Name, Namespace: a.Namespace}, s)) + + assert.NotContains(t, s.Spec.Template.Spec.Containers[0].Command, "new-command") + + // Modify the initcontainer environment variable + s.Spec.Template.Spec.Containers[0].Env = append(s.Spec.Template.Spec.InitContainers[0].Env, corev1.EnvVar{ + Name: "NEW_ENV_VAR", + Value: "new-value", + }) + assert.NoError(t, r.Client.Update(context.TODO(), s)) + + // Reconcile again and check if the environment variable is reverted + assert.NoError(t, r.reconcileRedisStatefulSet(a)) + assert.NoError(t, r.Client.Get(context.TODO(), types.NamespacedName{Name: s.Name, Namespace: a.Namespace}, s)) + + envVarFound = false + for _, env := range s.Spec.Template.Spec.InitContainers[0].Env { + if env.Name == "NEW_ENV_VAR" { + envVarFound = true + break + } + } + assert.False(t, envVarFound, "NEW_ENV_VAR should not be present") + + // Modify the container volume and volume mount + s.Spec.Template.Spec.Containers[0].VolumeMounts = append(s.Spec.Template.Spec.Containers[0].VolumeMounts, corev1.VolumeMount{ + Name: "new-volume", + MountPath: "/new/path", + }) + s.Spec.Template.Spec.Volumes = append(s.Spec.Template.Spec.Volumes, corev1.Volume{ + Name: "new-volume", + VolumeSource: corev1.VolumeSource{ + EmptyDir: &corev1.EmptyDirVolumeSource{}, + }, + }) + assert.NoError(t, r.Client.Update(context.TODO(), s)) + // Reconcile again and check if the volume mount is reverted + assert.NoError(t, r.reconcileRedisStatefulSet(a)) + assert.NoError(t, r.Client.Get(context.TODO(), types.NamespacedName{Name: s.Name, Namespace: a.Namespace}, s)) + + volumeMountFound := false + for _, vm := range s.Spec.Template.Spec.Containers[0].VolumeMounts { + if vm.Name == "new-volume" { + volumeMountFound = true + break + } + } + assert.False(t, volumeMountFound, "new-volume should not be present in volume mounts") + + volumeFound := false + for _, v := range s.Spec.Template.Spec.Volumes { + if v.Name == "new-volume" { + volumeFound = true + break + } + } + assert.False(t, volumeFound, "new-volume should not be present in volumes") +} From b89d82d2f984b385348172daad71cd728f30a31e Mon Sep 17 00:00:00 2001 From: Mangaal Date: Mon, 3 Feb 2025 16:08:29 +0530 Subject: [PATCH 4/6] Remove unnecessary check Signed-off-by: Mangaal --- controllers/argocd/deployment.go | 8 -------- controllers/argocd/statefulset_test.go | 10 ---------- 2 files changed, 18 deletions(-) diff --git a/controllers/argocd/deployment.go b/controllers/argocd/deployment.go index 5a8debbf5..e2a9e261d 100644 --- a/controllers/argocd/deployment.go +++ b/controllers/argocd/deployment.go @@ -868,14 +868,6 @@ func (r *ReconcileArgoCD) reconcileRedisHAProxyDeployment(cr *argoproj.ArgoCD) e explanation += "container env" changed = true } - if !reflect.DeepEqual(deploy.Spec.Template.Spec.Containers[0].Command, existing.Spec.Template.Spec.Containers[0].Command) { - existing.Spec.Template.Spec.Containers[0].Command = deploy.Spec.Template.Spec.Containers[0].Command - if changed { - explanation += ", " - } - explanation += "container command" - changed = true - } if !reflect.DeepEqual(deploy.Spec.Template.Spec.Containers[0].Resources, existing.Spec.Template.Spec.Containers[0].Resources) { existing.Spec.Template.Spec.Containers[0].Resources = deploy.Spec.Template.Spec.Containers[0].Resources if changed { diff --git a/controllers/argocd/statefulset_test.go b/controllers/argocd/statefulset_test.go index 593dcf66a..e80502f07 100644 --- a/controllers/argocd/statefulset_test.go +++ b/controllers/argocd/statefulset_test.go @@ -875,16 +875,6 @@ func TestReconcileArgoCD_reconcileRedisStatefulSet_ModifyContainerSpec(t *testin } assert.False(t, envVarFound, "NEW_ENV_VAR should not be present") - // Modify the container environment variable - s.Spec.Template.Spec.Containers[0].Command = append(s.Spec.Template.Spec.Containers[0].Command, "new-command") - assert.NoError(t, r.Client.Update(context.TODO(), s)) - - // Reconcile again and check if the environment variable is reverted - assert.NoError(t, r.reconcileRedisStatefulSet(a)) - assert.NoError(t, r.Client.Get(context.TODO(), types.NamespacedName{Name: s.Name, Namespace: a.Namespace}, s)) - - assert.NotContains(t, s.Spec.Template.Spec.Containers[0].Command, "new-command") - // Modify the initcontainer environment variable s.Spec.Template.Spec.Containers[0].Env = append(s.Spec.Template.Spec.InitContainers[0].Env, corev1.EnvVar{ Name: "NEW_ENV_VAR", From 0da29240248ff72e3a5fd1b2ddcbc8cd321424f3 Mon Sep 17 00:00:00 2001 From: Mangaal Date: Mon, 3 Feb 2025 16:17:41 +0530 Subject: [PATCH 5/6] Remove unnecessary check in unit test Signed-off-by: Mangaal --- controllers/argocd/deployment_test.go | 38 --------------------------- 1 file changed, 38 deletions(-) diff --git a/controllers/argocd/deployment_test.go b/controllers/argocd/deployment_test.go index 49de9f080..f2a83a3e5 100644 --- a/controllers/argocd/deployment_test.go +++ b/controllers/argocd/deployment_test.go @@ -937,25 +937,6 @@ func TestReconcileArgoCD_reconcileRedisHAProxyDeployment_ModifyContainerSpec(t * Value: "test", }) - // Modify the deployment container command - deployment.Spec.Template.Spec.Containers[0].Command = append(deployment.Spec.Template.Spec.Containers[0].Command, "new-command") - - assert.NoError(t, r.Client.Update(context.TODO(), deployment)) - - // Reconcile again - assert.NoError(t, r.reconcileRedisHAProxyDeployment(a)) - - // Check if the environment variable changes were reverted - assert.NoError(t, r.Client.Get( - context.TODO(), - types.NamespacedName{ - Name: "argocd-redis-ha-haproxy", - Namespace: a.Namespace, - }, - deployment)) - - assert.NotContains(t, deployment.Spec.Template.Spec.Containers[0].Command, "new-command") - // Modify the deployment initcontainer environment variables deployment.Spec.Template.Spec.InitContainers[0].Env = append(deployment.Spec.Template.Spec.InitContainers[0].Env, corev1.EnvVar{ Name: "TEST_ENV", @@ -1017,25 +998,6 @@ func TestReconcileArgoCD_reconcileRedisHAProxyDeployment_ModifyContainerSpec(t * Name: "test-volume", MountPath: "/test", }) - - // Modify the deployment command - deployment.Spec.Template.Spec.Containers[0].Command = append(deployment.Spec.Template.Spec.Containers[0].Command, "test-command") - - assert.NoError(t, r.Client.Update(context.TODO(), deployment)) - - // Reconcile again - assert.NoError(t, r.reconcileRedisHAProxyDeployment(a)) - - // Check if the command changes were reverted - assert.NoError(t, r.Client.Get( - context.TODO(), - types.NamespacedName{ - Name: "argocd-redis-ha-haproxy", - Namespace: a.Namespace, - }, - deployment)) - - assert.NotContains(t, deployment.Spec.Template.Spec.Containers[0].Command, "test-command") } func TestReconcileArgoCD_reconcileRepoDeployment_updatesVolumeMounts(t *testing.T) { From 298dd08ced25eb4a62c7663f5a285ecaa88ba27f Mon Sep 17 00:00:00 2001 From: Mangaal Date: Mon, 3 Feb 2025 17:23:44 +0530 Subject: [PATCH 6/6] add a single test for initcontainer Signed-off-by: Mangaal --- controllers/argocd/deployment.go | 22 +++------------------- controllers/argocd/statefulset.go | 25 +++---------------------- 2 files changed, 6 insertions(+), 41 deletions(-) diff --git a/controllers/argocd/deployment.go b/controllers/argocd/deployment.go index e2a9e261d..52ba1f800 100644 --- a/controllers/argocd/deployment.go +++ b/controllers/argocd/deployment.go @@ -851,12 +851,12 @@ func (r *ReconcileArgoCD) reconcileRedisHAProxyDeployment(cr *argoproj.ArgoCD) e explanation += "container volume mounts" changed = true } - if !reflect.DeepEqual(deploy.Spec.Template.Spec.InitContainers[0].Env, existing.Spec.Template.Spec.InitContainers[0].Env) { - existing.Spec.Template.Spec.InitContainers[0].Env = deploy.Spec.Template.Spec.InitContainers[0].Env + if !reflect.DeepEqual(deploy.Spec.Template.Spec.InitContainers, existing.Spec.Template.Spec.InitContainers) { + existing.Spec.Template.Spec.InitContainers = deploy.Spec.Template.Spec.InitContainers if changed { explanation += ", " } - explanation += "init containers env" + explanation += "init containers" changed = true } if !reflect.DeepEqual(deploy.Spec.Template.Spec.Containers[0].Env, @@ -884,22 +884,6 @@ func (r *ReconcileArgoCD) reconcileRedisHAProxyDeployment(cr *argoproj.ArgoCD) e explanation += "container security context" changed = true } - if !reflect.DeepEqual(deploy.Spec.Template.Spec.InitContainers[0].Resources, existing.Spec.Template.Spec.InitContainers[0].Resources) { - existing.Spec.Template.Spec.InitContainers[0].Resources = deploy.Spec.Template.Spec.InitContainers[0].Resources - if changed { - explanation += ", " - } - explanation += "init container resources" - changed = true - } - if !reflect.DeepEqual(deploy.Spec.Template.Spec.InitContainers[0].SecurityContext, existing.Spec.Template.Spec.InitContainers[0].SecurityContext) { - existing.Spec.Template.Spec.InitContainers[0].SecurityContext = deploy.Spec.Template.Spec.InitContainers[0].SecurityContext - if changed { - explanation += ", " - } - explanation += "init container security context" - changed = true - } if changed { argoutil.LogResourceUpdate(log, existing, "updating", explanation) return r.Client.Update(context.TODO(), existing) diff --git a/controllers/argocd/statefulset.go b/controllers/argocd/statefulset.go index 90a794477..b70218c53 100644 --- a/controllers/argocd/statefulset.go +++ b/controllers/argocd/statefulset.go @@ -496,33 +496,14 @@ func (r *ReconcileArgoCD) reconcileRedisStatefulSet(cr *argoproj.ArgoCD) error { explanation += "volumes" changed = true } - if !reflect.DeepEqual(ss.Spec.Template.Spec.InitContainers[0].Resources, existing.Spec.Template.Spec.InitContainers[0].Resources) { - existing.Spec.Template.Spec.InitContainers[0].Resources = ss.Spec.Template.Spec.InitContainers[0].Resources - if changed { - explanation += ", " - } - explanation += fmt.Sprintf("init container '%s' resources", existing.Spec.Template.Spec.InitContainers[0].Name) - changed = true - } - - if !reflect.DeepEqual(ss.Spec.Template.Spec.InitContainers[0].SecurityContext, existing.Spec.Template.Spec.InitContainers[0].SecurityContext) { - existing.Spec.Template.Spec.InitContainers[0].SecurityContext = ss.Spec.Template.Spec.InitContainers[0].SecurityContext - if changed { - explanation += ", " - } - explanation += fmt.Sprintf("init container '%s' security context", existing.Spec.Template.Spec.InitContainers[0].Name) - changed = true - } - - if !reflect.DeepEqual(ss.Spec.Template.Spec.InitContainers[0].Env, existing.Spec.Template.Spec.InitContainers[0].Env) { - existing.Spec.Template.Spec.InitContainers[0].Env = ss.Spec.Template.Spec.InitContainers[0].Env + if !reflect.DeepEqual(ss.Spec.Template.Spec.InitContainers, existing.Spec.Template.Spec.InitContainers) { + existing.Spec.Template.Spec.InitContainers = ss.Spec.Template.Spec.InitContainers if changed { explanation += ", " } - explanation += fmt.Sprintf("init container '%s' env", existing.Spec.Template.Spec.InitContainers[0].Name) + explanation += "init containers" changed = true } - if changed { argoutil.LogResourceUpdate(log, existing, "updating", explanation) return r.Client.Update(context.TODO(), existing)