From e1bcc6559dcf7dfadfa6810ce0f413410661091f Mon Sep 17 00:00:00 2001 From: Rizwana777 Date: Mon, 3 Jun 2024 16:16:30 +0530 Subject: [PATCH] Update ingressClassName of ingress based on ArgoCD ingressClassName Signed-off-by: Rizwana777 --- controllers/argocd/ingress.go | 6 +++++ controllers/argocd/ingress_test.go | 43 ++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/controllers/argocd/ingress.go b/controllers/argocd/ingress.go index 0a1943052..824b5a0f6 100644 --- a/controllers/argocd/ingress.go +++ b/controllers/argocd/ingress.go @@ -97,6 +97,12 @@ func (r *ReconcileArgoCD) reconcileArgoServerIngress(cr *argoproj.ArgoCD) error // Ingress exists but enabled flag has been set to false, delete the Ingress return r.Client.Delete(context.TODO(), ingress) } + + // If Ingress found and enabled, make sure the ingressClassName is up-to-date + if ingress.Spec.IngressClassName != cr.Spec.Server.Ingress.IngressClassName { + ingress.Spec.IngressClassName = cr.Spec.Server.Ingress.IngressClassName + return r.Client.Update(context.TODO(), ingress) + } return nil // Ingress found and enabled, do nothing } diff --git a/controllers/argocd/ingress_test.go b/controllers/argocd/ingress_test.go index 6eedf0dc3..66ca9b54f 100644 --- a/controllers/argocd/ingress_test.go +++ b/controllers/argocd/ingress_test.go @@ -6,6 +6,7 @@ import ( "github.com/stretchr/testify/assert" networkingv1 "k8s.io/api/networking/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" @@ -63,6 +64,48 @@ func TestReconcileArgoCD_reconcile_ServerIngress_ingressClassName(t *testing.T) } } +func TestReconcileArgoCD_reconcile_ServerIngress_ingressClassName_update(t *testing.T) { + logf.SetLogger(ZapLogger(true)) + + nginx := "nginx" + existingIngressClassName := "test-name" + + a := makeTestArgoCD(func(a *argoproj.ArgoCD) { + a.Spec.Server.Ingress.Enabled = true + a.Spec.Server.Ingress.IngressClassName = &nginx + }) + + // Existing ingress with different ingressClassName + ingress := &networkingv1.Ingress{ + ObjectMeta: metav1.ObjectMeta{ + Name: "argocd-server", + Namespace: a.Namespace, + }, + Spec: networkingv1.IngressSpec{ + IngressClassName: &existingIngressClassName, + }, + } + + resObjs := []client.Object{a, ingress} + subresObjs := []client.Object{a} + runtimeObjs := []runtime.Object{} + sch := makeTestReconcilerScheme(argoproj.AddToScheme) + cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs) + r := makeTestReconciler(cl, sch) + + err := r.reconcileArgoServerIngress(a) + assert.NoError(t, err) + + updatedIngress := &networkingv1.Ingress{} + err = r.Client.Get(context.TODO(), types.NamespacedName{ + Name: "argocd-server", + Namespace: testNamespace, + }, updatedIngress) + assert.NoError(t, err) + assert.Equal(t, a.Spec.Server.Ingress.IngressClassName, updatedIngress.Spec.IngressClassName) + +} + func TestReconcileArgoCD_reconcile_ServerGRPCIngress_ingressClassName(t *testing.T) { logf.SetLogger(ZapLogger(true))