From 0e89480ef5714e719af61d2f30844b1197b290a2 Mon Sep 17 00:00:00 2001 From: Manabu McCloskey Date: Tue, 26 Dec 2023 22:31:44 +0000 Subject: [PATCH 1/2] ensure multi source spec is handled Signed-off-by: Manabu McCloskey --- pkg/controllers/custompackage/controller.go | 2 +- .../custompackage/controller_test.go | 32 +++++++++++++++++++ .../customPackages/testDir/app2.yaml | 19 +++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 pkg/controllers/custompackage/test/resources/customPackages/testDir/app2.yaml diff --git a/pkg/controllers/custompackage/controller.go b/pkg/controllers/custompackage/controller.go index c0af3e75..3c623d33 100644 --- a/pkg/controllers/custompackage/controller.go +++ b/pkg/controllers/custompackage/controller.go @@ -86,7 +86,7 @@ func (r *Reconciler) reconcileCustomPackage(ctx context.Context, resource *v1alp synced := true if app.Spec.HasMultipleSources() { for j := range app.Spec.Sources { - s := app.Spec.Sources[j] + s := &app.Spec.Sources[j] res, repo, sErr := r.reconcileArgocdSource(ctx, resource, appName, resource.Spec.ArgoCD.ApplicationFile, s.RepoURL) if sErr != nil { return res, sErr diff --git a/pkg/controllers/custompackage/controller_test.go b/pkg/controllers/custompackage/controller_test.go index 58158201..389d0341 100644 --- a/pkg/controllers/custompackage/controller_test.go +++ b/pkg/controllers/custompackage/controller_test.go @@ -111,6 +111,23 @@ func TestReconcileCustomPkg(t *testing.T) { }, }, }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "test3", + Namespace: "test", + UID: "abc", + }, + Spec: v1alpha1.CustomPackageSpec{ + Replicate: true, + GitServerURL: "https://cnoe.io", + InternalGitServeURL: "http://internal.cnoe.io", + ArgoCD: v1alpha1.ArgoCDPackageSpec{ + ApplicationFile: filepath.Join(cwd, "test/resources/customPackages/testDir/app2.yaml"), + Name: "my-app2", + Namespace: "argocd", + }, + }, + }, } for _, n := range []string{"argocd", "test"} { @@ -182,4 +199,19 @@ func TestReconcileCustomPkg(t *testing.T) { t.Fatalf("expected %s arogapp : %v", n, err) } } + + localApp2 := argov1alpha1.Application{ + ObjectMeta: metav1.ObjectMeta{ + Name: "my-app2", + Namespace: "argocd", + }, + } + err = c.Get(context.Background(), client.ObjectKeyFromObject(&localApp2), &localApp2) + if err != nil { + t.Fatalf("failed getting my-app2 %v", err) + } + + if strings.HasPrefix(localApp2.Spec.Sources[0].RepoURL, "cnoe://") { + t.Fatalf("cnoe:// prefix should be removed") + } } diff --git a/pkg/controllers/custompackage/test/resources/customPackages/testDir/app2.yaml b/pkg/controllers/custompackage/test/resources/customPackages/testDir/app2.yaml new file mode 100644 index 00000000..c4d4083a --- /dev/null +++ b/pkg/controllers/custompackage/test/resources/customPackages/testDir/app2.yaml @@ -0,0 +1,19 @@ +apiVersion: argoproj.io/v1alpha1 +kind: Application +metadata: + name: my-app2 + namespace: argocd +spec: + destination: + namespace: my-app2 + server: "https://kubernetes.default.svc" + sources: + - repoURL: cnoe://busybox + targetRevision: HEAD + path: "." + project: default + syncPolicy: + automated: + selfHeal: true + syncOptions: + - CreateNamespace=true From 29d1e3f6cf3e2c73b59622f614e0362ec1a8744b Mon Sep 17 00:00:00 2001 From: Manabu Mccloskey Date: Tue, 2 Jan 2024 11:14:41 -0800 Subject: [PATCH 2/2] handle a case where the same directory is specified multiple times Signed-off-by: Manabu Mccloskey --- pkg/controllers/custompackage/controller.go | 7 ++++++- .../resources/customPackages/testDir/app2.yaml | 7 +++++-- .../testDir/app2/one/busybox.yaml | 17 +++++++++++++++++ .../testDir/app2/two/busybox.yaml | 17 +++++++++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 pkg/controllers/custompackage/test/resources/customPackages/testDir/app2/one/busybox.yaml create mode 100644 pkg/controllers/custompackage/test/resources/customPackages/testDir/app2/two/busybox.yaml diff --git a/pkg/controllers/custompackage/controller.go b/pkg/controllers/custompackage/controller.go index 3c623d33..adb24375 100644 --- a/pkg/controllers/custompackage/controller.go +++ b/pkg/controllers/custompackage/controller.go @@ -190,8 +190,13 @@ func (r *Reconciler) reconcileGitRepo(ctx context.Context, resource *v1alpha1.Cu } return nil }) + // it's possible for an application to specify the same directory multiple times in the spec. + // if there is a repository already created for this package, no further action is necessary. + if !errors.IsAlreadyExists(err) { + return repo, err + } - return repo, err + return repo, nil } func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { diff --git a/pkg/controllers/custompackage/test/resources/customPackages/testDir/app2.yaml b/pkg/controllers/custompackage/test/resources/customPackages/testDir/app2.yaml index c4d4083a..14299115 100644 --- a/pkg/controllers/custompackage/test/resources/customPackages/testDir/app2.yaml +++ b/pkg/controllers/custompackage/test/resources/customPackages/testDir/app2.yaml @@ -8,9 +8,12 @@ spec: namespace: my-app2 server: "https://kubernetes.default.svc" sources: - - repoURL: cnoe://busybox + - repoURL: cnoe://app2 targetRevision: HEAD - path: "." + path: "one" + - repoURL: cnoe://app2 + targetRevision: HEAD + path: "two" project: default syncPolicy: automated: diff --git a/pkg/controllers/custompackage/test/resources/customPackages/testDir/app2/one/busybox.yaml b/pkg/controllers/custompackage/test/resources/customPackages/testDir/app2/one/busybox.yaml new file mode 100644 index 00000000..47a63156 --- /dev/null +++ b/pkg/controllers/custompackage/test/resources/customPackages/testDir/app2/one/busybox.yaml @@ -0,0 +1,17 @@ +apiVersion: v1 +kind: Pod +metadata: + name: busybox + namespace: argocd + labels: + abc: ded + notused: remove-me +spec: + containers: + - image: alpine:3.18 + command: + - sleep + - "3600" + imagePullPolicy: IfNotPresent + name: busybox + restartPolicy: Always diff --git a/pkg/controllers/custompackage/test/resources/customPackages/testDir/app2/two/busybox.yaml b/pkg/controllers/custompackage/test/resources/customPackages/testDir/app2/two/busybox.yaml new file mode 100644 index 00000000..47a63156 --- /dev/null +++ b/pkg/controllers/custompackage/test/resources/customPackages/testDir/app2/two/busybox.yaml @@ -0,0 +1,17 @@ +apiVersion: v1 +kind: Pod +metadata: + name: busybox + namespace: argocd + labels: + abc: ded + notused: remove-me +spec: + containers: + - image: alpine:3.18 + command: + - sleep + - "3600" + imagePullPolicy: IfNotPresent + name: busybox + restartPolicy: Always