diff --git a/controllers/argocd/route.go b/controllers/argocd/route.go index d4c75da1d..e7cc53ce9 100644 --- a/controllers/argocd/route.go +++ b/controllers/argocd/route.go @@ -384,7 +384,7 @@ func (r *ReconcileArgoCD) reconcileApplicationSetControllerWebhookRoute(cr *argo func shortenHostname(hostname string) (string, error) { if hostname == "" { - return "", fmt.Errorf("hostname is empty") + return "", nil } // Split the hostname into labels diff --git a/controllers/argocd/route_test.go b/controllers/argocd/route_test.go index 4e365beb0..cd3b1fedd 100644 --- a/controllers/argocd/route_test.go +++ b/controllers/argocd/route_test.go @@ -2,6 +2,7 @@ package argocd import ( "context" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -29,7 +30,6 @@ func TestReconcileRouteSetLabels(t *testing.T) { logf.SetLogger(ZapLogger(true)) argoCD := makeArgoCD(func(a *argoproj.ArgoCD) { a.Spec.Server.Route.Enabled = true - a.Spec.Server.Host = "test.hostname.com" labels := make(map[string]string) labels["my-key"] = "my-value" a.Spec.Server.Route.Labels = labels @@ -69,7 +69,6 @@ func TestReconcileRouteSetsInsecure(t *testing.T) { logf.SetLogger(ZapLogger(true)) argoCD := makeArgoCD(func(a *argoproj.ArgoCD) { a.Spec.Server.Route.Enabled = true - a.Spec.Server.Host = "test.hostname.com" }) resObjs := []client.Object{argoCD} @@ -146,7 +145,6 @@ func TestReconcileRouteUnsetsInsecure(t *testing.T) { argoCD := makeArgoCD(func(a *argoproj.ArgoCD) { a.Spec.Server.Route.Enabled = true a.Spec.Server.Insecure = true - a.Spec.Server.Host = "test.hostname.com" }) resObjs := []client.Object{argoCD} @@ -220,63 +218,84 @@ func TestReconcileRouteForShorteningHostname(t *testing.T) { routeAPIFound = true ctx := context.Background() logf.SetLogger(ZapLogger(true)) - hostname := "myhostnameaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.redhat.com" - argoCD := makeArgoCD(func(a *argoproj.ArgoCD) { - a.Spec.Server.Route.Enabled = true - a.Spec.ApplicationSet = &argoproj.ArgoCDApplicationSet{ - WebhookServer: argoproj.WebhookServerSpec{ - Route: argoproj.ArgoCDRouteSpec{ - Enabled: true, - }, - Host: hostname, - }, - } - }) - - resObjs := []client.Object{argoCD} - subresObjs := []client.Object{argoCD} - runtimeObjs := []runtime.Object{} - sch := makeTestReconcilerScheme(argoproj.AddToScheme, configv1.Install, routev1.Install) - cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs) - r := makeTestReconciler(cl, sch) - - assert.NoError(t, createNamespace(r, argoCD.Namespace, "")) - - req := reconcile.Request{ - NamespacedName: types.NamespacedName{ - Name: testArgoCDName, - Namespace: testNamespace, + tests := []struct { + testName string + expected string + hostname string + }{ + { + testName: "longHostname", + hostname: "myhostnameaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.redhat.com", + expected: "myhostnameaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.redhat.com", + }, + { + testName: "twentySixLetterHostname", + hostname: "myhostnametwentysixletteraaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.redhat.com", + expected: "myhostnametwentysixletteraaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.redhat.com", }, } - // Check if it returns error when hostname is empty - _, err := r.Reconcile(context.TODO(), req) - assert.EqualError(t, err, "hostname is empty") + for _, v := range tests { + t.Run(v.testName, func(t *testing.T) { + + argoCD := makeArgoCD(func(a *argoproj.ArgoCD) { + a.Spec.Server.Route.Enabled = true + a.Spec.ApplicationSet = &argoproj.ArgoCDApplicationSet{ + WebhookServer: argoproj.WebhookServerSpec{ + Route: argoproj.ArgoCDRouteSpec{ + Enabled: true, + }, + Host: v.hostname, + }, + } + }) + + resObjs := []client.Object{argoCD} + subresObjs := []client.Object{argoCD} + runtimeObjs := []runtime.Object{} + sch := makeTestReconcilerScheme(argoproj.AddToScheme, configv1.Install, routev1.Install) + cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs) + r := makeTestReconciler(cl, sch) + + assert.NoError(t, createNamespace(r, argoCD.Namespace, "")) + + req := reconcile.Request{ + NamespacedName: types.NamespacedName{ + Name: testArgoCDName, + Namespace: testNamespace, + }, + } - // second reconciliation after changing the hostname. - err = r.Client.Get(ctx, req.NamespacedName, argoCD) - fatalIfError(t, err, "failed to load ArgoCD %q: %s", testArgoCDName+"-server", err) + // Check if it returns nil when hostname is empty + _, err := r.Reconcile(context.TODO(), req) + assert.NoError(t, err) - argoCD.Spec.Server.Host = hostname - err = r.Client.Update(ctx, argoCD) - fatalIfError(t, err, "failed to update the ArgoCD: %s", err) + // second reconciliation after changing the hostname. + err = r.Client.Get(ctx, req.NamespacedName, argoCD) + fatalIfError(t, err, "failed to load ArgoCD %q: %s", testArgoCDName+"-server", err) - _, err = r.Reconcile(context.TODO(), req) - assert.NoError(t, err) + argoCD.Spec.Server.Host = v.hostname + err = r.Client.Update(ctx, argoCD) + fatalIfError(t, err, "failed to update the ArgoCD: %s", err) - loaded := &routev1.Route{} - err = r.Client.Get(ctx, types.NamespacedName{Name: testArgoCDName + "-server", Namespace: testNamespace}, loaded) - fatalIfError(t, err, "failed to load route %q: %s", testArgoCDName+"-server", err) + _, err = r.Reconcile(context.TODO(), req) + assert.NoError(t, err) - wantRoute := &routev1.RouteSpec{ - Host: "myhostnameaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.redhat.com", - } + loaded := &routev1.Route{} + err = r.Client.Get(ctx, types.NamespacedName{Name: testArgoCDName + "-server", Namespace: testNamespace}, loaded) + fatalIfError(t, err, "failed to load route %q: %s", testArgoCDName+"-server", err) - if diff := cmp.Diff(wantRoute.Host, loaded.Spec.Host); diff != "" { - t.Fatalf("failed to reconcile route:\n%s", diff) - } + if diff := cmp.Diff(v.expected, loaded.Spec.Host); diff != "" { + t.Fatalf("failed to reconcile route:\n%s", diff) + } + // Check if first label is greater than 20 + labels := strings.Split(loaded.Spec.Host, ".") + assert.True(t, len(labels[0]) > 20) + + }) + } } func makeReconciler(t *testing.T, acd *argoproj.ArgoCD, objs ...runtime.Object) *ReconcileArgoCD {