Skip to content

Commit

Permalink
Add Server Ingress checks to apply the changes in case it exists (#1553)
Browse files Browse the repository at this point in the history
Signed-off-by: nmirasch <[email protected]>
  • Loading branch information
nmirasch authored Oct 3, 2024
1 parent 2ef2bc4 commit 6e304b2
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 14 deletions.
43 changes: 30 additions & 13 deletions controllers/argocd/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package argocd
import (
"context"
"fmt"
"reflect"

networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -92,21 +93,14 @@ func (r *ReconcileArgoCD) reconcileIngresses(cr *argoproj.ArgoCD) error {
// reconcileArgoServerIngress will ensure that the ArgoCD Server Ingress is present.
func (r *ReconcileArgoCD) reconcileArgoServerIngress(cr *argoproj.ArgoCD) error {
ingress := newIngressWithSuffix("server", cr)
if argoutil.IsObjectFound(r.Client, cr.Namespace, ingress.Name, ingress) {
if !cr.Spec.Server.Ingress.Enabled {
existingIngress := newIngressWithSuffix("server", cr)
objectFound := argoutil.IsObjectFound(r.Client, cr.Namespace, ingress.Name, existingIngress)

if !cr.Spec.Server.Ingress.Enabled {
if objectFound {
// 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
}

if !cr.Spec.Server.Ingress.Enabled {
return nil // Ingress not enabled, move along...
}

Expand Down Expand Up @@ -164,7 +158,30 @@ func (r *ReconcileArgoCD) reconcileArgoServerIngress(cr *argoproj.ArgoCD) error
if len(cr.Spec.Server.Ingress.TLS) > 0 {
ingress.Spec.TLS = cr.Spec.Server.Ingress.TLS
}

if objectFound {
changed := false
// If Ingress found and enabled, make sure the ingressClassName is up-to-date
if existingIngress.Spec.IngressClassName != cr.Spec.Server.Ingress.IngressClassName {
changed = true
existingIngress.Spec.IngressClassName = cr.Spec.Server.Ingress.IngressClassName
}
if !reflect.DeepEqual(cr.Spec.Server.Ingress.Annotations, existingIngress.ObjectMeta.Annotations) {
changed = true
existingIngress.ObjectMeta.Annotations = cr.Spec.Server.Ingress.Annotations
}
if !reflect.DeepEqual(ingress.Spec.Rules, existingIngress.Spec.Rules) {
changed = true
existingIngress.Spec.Rules = ingress.Spec.Rules
}
if !reflect.DeepEqual(ingress.Spec.TLS, existingIngress.Spec.TLS) {
changed = true
existingIngress.Spec.TLS = ingress.Spec.TLS
}
if changed {
return r.Client.Update(context.TODO(), existingIngress)
}
return nil // Ingress with no changes to apply, do nothing
}
if err := controllerutil.SetControllerReference(cr, ingress, r.Scheme); err != nil {
return err
}
Expand Down
62 changes: 61 additions & 1 deletion controllers/argocd/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,67 @@ func TestReconcileArgoCD_reconcile_ServerIngress_ingressClassName(t *testing.T)
})
}
}
func TestReconcileArgoCD_reconcile_ServerIngress_serverHost(t *testing.T) {
logf.SetLogger(ZapLogger(true))

nginx := "nginx"

tests := []struct {
name string
ingressClassName *string
host string
}{
{
name: "New Server host specified",
ingressClassName: &nginx,
host: "foo.bar",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {

a := makeTestArgoCD(func(a *argoproj.ArgoCD) {
a.Spec.Server.Ingress.Enabled = true
a.Spec.Server.Ingress.IngressClassName = test.ingressClassName
})

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)
err := r.reconcileArgoServerIngress(a)
assert.NoError(t, err)

ingress := &networkingv1.Ingress{}
err = r.Client.Get(context.TODO(), types.NamespacedName{
Name: "argocd-server",
Namespace: testNamespace,
}, ingress)
assert.NoError(t, err)
assert.Equal(t, test.ingressClassName, ingress.Spec.IngressClassName)
assert.Equal(t, "argocd", ingress.Spec.TLS[0].Hosts[0])
a = makeTestArgoCD(func(a *argoproj.ArgoCD) {
a.Spec.Server.Ingress.Enabled = true
a.Spec.Server.Ingress.IngressClassName = test.ingressClassName
a.Spec.Server.Host = test.host
})

err = r.reconcileArgoServerIngress(a)
assert.NoError(t, err)
err = r.Client.Get(context.TODO(), types.NamespacedName{
Name: "argocd-server",
Namespace: testNamespace,
}, ingress)
assert.NoError(t, err)
assert.Equal(t, test.host, ingress.Spec.TLS[0].Hosts[0])
assert.Equal(t, test.host, ingress.Spec.Rules[0].Host)
assert.Equal(t, test.ingressClassName, ingress.Spec.IngressClassName)
})
}
}
func TestReconcileArgoCD_reconcile_ServerIngress_ingressClassName_update(t *testing.T) {
logf.SetLogger(ZapLogger(true))

Expand Down Expand Up @@ -102,7 +162,7 @@ func TestReconcileArgoCD_reconcile_ServerIngress_ingressClassName_update(t *test
Namespace: testNamespace,
}, updatedIngress)
assert.NoError(t, err)
assert.Equal(t, a.Spec.Server.Ingress.IngressClassName, updatedIngress.Spec.IngressClassName)
assert.Equal(t, *a.Spec.Server.Ingress.IngressClassName, *updatedIngress.Spec.IngressClassName)

}

Expand Down

0 comments on commit 6e304b2

Please sign in to comment.