From e32ec615b5a6eb7948b167552f458a06f639d131 Mon Sep 17 00:00:00 2001
From: Anand Kumar Singh <anandrkskd@gmail.com>
Date: Tue, 27 Aug 2024 12:31:50 +0530
Subject: [PATCH 1/2] fix: allow argocd spec.image to override default image
 for appset controller

Signed-off-by: Anand Kumar Singh <anandrkskd@gmail.com>
---
 controllers/argocd/applicationset.go      |  5 ++++-
 controllers/argocd/applicationset_test.go | 27 +++++++++++++++++++++++
 2 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/controllers/argocd/applicationset.go b/controllers/argocd/applicationset.go
index 7580ff46c..1e0b69bda 100644
--- a/controllers/argocd/applicationset.go
+++ b/controllers/argocd/applicationset.go
@@ -764,9 +764,12 @@ func getApplicationSetContainerImage(cr *argoproj.ArgoCD) string {
 	tag := ""
 
 	// First pull from spec, if it exists
-	if cr.Spec.ApplicationSet != nil {
+	if cr.Spec.ApplicationSet.Image != "" && cr.Spec.ApplicationSet.Version != "" {
 		img = cr.Spec.ApplicationSet.Image
 		tag = cr.Spec.ApplicationSet.Version
+	} else if cr.Spec.Image != "" && cr.Spec.Version != "" && cr.Spec.ApplicationSet.Image == "" && cr.Spec.ApplicationSet.Version == "" {
+		img = cr.Spec.Image
+		tag = cr.Spec.Version
 	}
 
 	// If spec is empty, use the defaults
diff --git a/controllers/argocd/applicationset_test.go b/controllers/argocd/applicationset_test.go
index dee07044e..e2ae546d5 100644
--- a/controllers/argocd/applicationset_test.go
+++ b/controllers/argocd/applicationset_test.go
@@ -344,9 +344,31 @@ func TestReconcileApplicationSet_Deployments_SpecOverride(t *testing.T) {
 	tests := []struct {
 		name                   string
 		appSetField            *argoproj.ArgoCDApplicationSet
+		argocdField            argoproj.ArgoCDSpec
 		envVars                map[string]string
 		expectedContainerImage string
 	}{
+		{
+			name:        "fields are set in argocd spec and not on appsetspec",
+			appSetField: &argoproj.ArgoCDApplicationSet{},
+			argocdField: argoproj.ArgoCDSpec{
+				Image:   "test",
+				Version: "sha256:b835999eb5cf75d01a2678cd971095926d9c2566c9ffe746d04b83a6a0a2849f",
+			},
+			expectedContainerImage: "test@sha256:b835999eb5cf75d01a2678cd971095926d9c2566c9ffe746d04b83a6a0a2849f",
+		},
+		{
+			name: "fields are set in both argocdSpec and on appsetSpec",
+			appSetField: &argoproj.ArgoCDApplicationSet{
+				Image:   "custom-image",
+				Version: "sha256:b835999eb5cf75d01a2678cd971095926d9c2566c9ffe746d04b83a6a0a2849f",
+			},
+			argocdField: argoproj.ArgoCDSpec{
+				Image:   "test",
+				Version: "sha256:b835999eb5cf75d01a2678cd9710952566c9ffe746d04b83a6a0a2849f926d9c",
+			},
+			expectedContainerImage: "custom-image@sha256:b835999eb5cf75d01a2678cd971095926d9c2566c9ffe746d04b83a6a0a2849f",
+		},
 		{
 			name:                   "unspecified fields should use default",
 			appSetField:            &argoproj.ArgoCDApplicationSet{},
@@ -411,6 +433,11 @@ func TestReconcileApplicationSet_Deployments_SpecOverride(t *testing.T) {
 			cm := newConfigMapWithName(getCAConfigMapName(a), a)
 			r.Client.Create(context.Background(), cm, &client.CreateOptions{})
 
+			if test.argocdField.Image != "" {
+				a.Spec.Image = test.argocdField.Image
+				a.Spec.Version = test.argocdField.Version
+			}
+
 			a.Spec.ApplicationSet = test.appSetField
 
 			sa := corev1.ServiceAccount{}

From ec69f19b9fe3047c5a8109e30988051cb5a9dcf0 Mon Sep 17 00:00:00 2001
From: Anand Kumar Singh <anandrkskd@gmail.com>
Date: Thu, 29 Aug 2024 13:03:11 +0530
Subject: [PATCH 2/2] fix logic

Signed-off-by: Anand Kumar Singh <anandrkskd@gmail.com>
---
 controllers/argocd/applicationset.go | 30 ++++++++++++----------------
 1 file changed, 13 insertions(+), 17 deletions(-)

diff --git a/controllers/argocd/applicationset.go b/controllers/argocd/applicationset.go
index 1e0b69bda..fdc14e5b5 100644
--- a/controllers/argocd/applicationset.go
+++ b/controllers/argocd/applicationset.go
@@ -758,28 +758,24 @@ func (r *ReconcileArgoCD) reconcileApplicationSetRoleBinding(cr *argoproj.ArgoCD
 }
 
 func getApplicationSetContainerImage(cr *argoproj.ArgoCD) string {
-	defaultImg, defaultTag := false, false
-
-	img := ""
-	tag := ""
 
-	// First pull from spec, if it exists
-	if cr.Spec.ApplicationSet.Image != "" && cr.Spec.ApplicationSet.Version != "" {
-		img = cr.Spec.ApplicationSet.Image
-		tag = cr.Spec.ApplicationSet.Version
-	} else if cr.Spec.Image != "" && cr.Spec.Version != "" && cr.Spec.ApplicationSet.Image == "" && cr.Spec.ApplicationSet.Version == "" {
+	defaultImg, defaultTag := false, false
+	img := cr.Spec.ApplicationSet.Image
+	if img == "" {
 		img = cr.Spec.Image
-		tag = cr.Spec.Version
+		if img == "" {
+			img = common.ArgoCDDefaultArgoImage
+			defaultImg = true
+		}
 	}
 
-	// If spec is empty, use the defaults
-	if img == "" {
-		img = common.ArgoCDDefaultArgoImage
-		defaultImg = true
-	}
+	tag := cr.Spec.ApplicationSet.Version
 	if tag == "" {
-		tag = common.ArgoCDDefaultArgoVersion
-		defaultTag = true
+		tag = cr.Spec.Version
+		if tag == "" {
+			tag = common.ArgoCDDefaultArgoVersion
+			defaultTag = true
+		}
 	}
 
 	// If an env var is specified then use that, but don't override the spec values (if they are present)