From 8099edd1486376b2244a2a9d8af2e25e7381c45a Mon Sep 17 00:00:00 2001 From: Manabu McCloskey Date: Wed, 10 Jul 2024 16:27:49 -0700 Subject: [PATCH] support matrix git generator (#338) Signed-off-by: Manabu McCloskey --- pkg/controllers/custompackage/controller.go | 17 +++++ .../custompackage/controller_test.go | 71 +++++++++++++++++++ .../applicationSet/generator-matrix.yaml | 34 +++++++++ 3 files changed, 122 insertions(+) create mode 100644 pkg/controllers/custompackage/test/resources/customPackages/applicationSet/generator-matrix.yaml diff --git a/pkg/controllers/custompackage/controller.go b/pkg/controllers/custompackage/controller.go index 9fb878ef..12480bef 100644 --- a/pkg/controllers/custompackage/controller.go +++ b/pkg/controllers/custompackage/controller.go @@ -219,6 +219,23 @@ func (r *Reconciler) reconcileArgoCDAppSet(ctx context.Context, resource *v1alph } } } + if g.Matrix != nil { + for j := range g.Matrix.Generators { + nestedGenerator := g.Matrix.Generators[j] + if nestedGenerator.Git != nil { + res, repo, gErr := r.reconcileArgoCDSource(ctx, resource, nestedGenerator.Git.RepoURL, appSet.GetName()) + if gErr != nil { + return res, fmt.Errorf("reconciling git generator URL %s, %s: %w", nestedGenerator.Git.RepoURL, resource.Spec.ArgoCD.ApplicationFile, gErr) + } + if repo != nil { + nestedGenerator.Git.RepoURL = repo.Status.InternalGitRepositoryUrl + if repo.Status.InternalGitRepositoryUrl == "" { + notSyncedRepos += 1 + } + } + } + } + } } gitGeneratorsSynced := notSyncedRepos == 0 diff --git a/pkg/controllers/custompackage/controller_test.go b/pkg/controllers/custompackage/controller_test.go index d69085bd..bf6d1248 100644 --- a/pkg/controllers/custompackage/controller_test.go +++ b/pkg/controllers/custompackage/controller_test.go @@ -465,6 +465,70 @@ func TestReconcileCustomPkgAppSet(t *testing.T) { }, }, }, + { + input: v1alpha1.CustomPackage{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test4", + Namespace: "test", + UID: "test4", + }, + Spec: v1alpha1.CustomPackageSpec{ + Replicate: true, + GitServerURL: "https://cnoe.io", + InternalGitServeURL: "http://internal.cnoe.io", + ArgoCD: v1alpha1.ArgoCDPackageSpec{ + ApplicationFile: filepath.Join(cwd, "test/resources/customPackages/applicationSet/generator-matrix.yaml"), + Type: "ApplicationSet", + }, + }, + }, + expectedGitRepo: v1alpha1.GitRepository{ + ObjectMeta: metav1.ObjectMeta{ + Name: localRepoName("generator-matrix", "test/resources/customPackages/applicationSet/test1"), + Namespace: "test", + }, + Spec: v1alpha1.GitRepositorySpec{ + Source: v1alpha1.GitRepositorySource{ + Type: "local", + Path: filepath.Join(cwd, "test/resources/customPackages/applicationSet/test1"), + }, + Provider: v1alpha1.Provider{ + Name: v1alpha1.GitProviderGitea, + GitURL: "https://cnoe.io", + InternalGitURL: "http://internal.cnoe.io", + OrganizationName: v1alpha1.GiteaAdminUserName, + }, + }, + }, + expectedApplicationSet: argov1alpha1.ApplicationSet{ + ObjectMeta: metav1.ObjectMeta{ + Name: "generator-matrix", + Namespace: "argocd", + }, + Spec: argov1alpha1.ApplicationSetSpec{ + Generators: []argov1alpha1.ApplicationSetGenerator{ + { + Matrix: &argov1alpha1.MatrixGenerator{ + Generators: []argov1alpha1.ApplicationSetNestedGenerator{ + { + Git: &argov1alpha1.GitGenerator{ + RepoURL: "", + }, + }, + }, + }, + }, + }, + Template: argov1alpha1.ApplicationSetTemplate{ + Spec: argov1alpha1.ApplicationSpec{ + Source: &argov1alpha1.ApplicationSource{ + RepoURL: "", + }, + }, + }, + }, + }, + }, } for i := range cases { @@ -502,6 +566,13 @@ func TestReconcileCustomPkgAppSet(t *testing.T) { if exg.Git != nil { assert.Equal(t, exg.Git.RepoURL, appset.Spec.Generators[j].Git.RepoURL) } + if exg.Matrix != nil { + for k := range exg.Matrix.Generators { + if exg.Matrix.Generators[k].Git != nil { + assert.Equal(t, exg.Matrix.Generators[k].Git.RepoURL, appset.Spec.Generators[j].Matrix.Generators[k].Git.RepoURL) + } + } + } } } } diff --git a/pkg/controllers/custompackage/test/resources/customPackages/applicationSet/generator-matrix.yaml b/pkg/controllers/custompackage/test/resources/customPackages/applicationSet/generator-matrix.yaml new file mode 100644 index 00000000..6cb5acef --- /dev/null +++ b/pkg/controllers/custompackage/test/resources/customPackages/applicationSet/generator-matrix.yaml @@ -0,0 +1,34 @@ +apiVersion: argoproj.io/v1alpha1 +kind: ApplicationSet +metadata: + name: generator-matrix + namespace: argocd +spec: + goTemplate: true + goTemplateOptions: + - missingkey=error + generators: + - matrix: + generators: + - git: + repoURL: "cnoe://test1" + revision: HEAD + files: + - path: "**/config.yaml" + template: + metadata: + name: "{{ .name }}" + labels: + environment: "{{ .environment }}" + spec: + project: default + source: + repoURL: "cnoe://test1" + targetRevision: HEAD + path: "{{ .manifestPath }}/manifests" + destination: + server: https://kubernetes.default.svc + namespace: "{{ .namespace }}" + syncPolicy: + syncOptions: + - CreateNamespace=true