From aaf19de118bb9af6b726f90b4a86e1bf244541fd Mon Sep 17 00:00:00 2001 From: Brad Wadsworth Date: Sun, 26 Jan 2025 15:38:24 -0600 Subject: [PATCH] Added ability to ignore Kustomize component directory if it does not exist Signed-off-by: Brad Wadsworth --- pkg/apis/application/v1alpha1/types.go | 2 ++ util/kustomize/kustomize.go | 16 +++++++++++++++- util/kustomize/kustomize_test.go | 3 ++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/pkg/apis/application/v1alpha1/types.go b/pkg/apis/application/v1alpha1/types.go index a53500172d94d..37ffeb8fdb423 100644 --- a/pkg/apis/application/v1alpha1/types.go +++ b/pkg/apis/application/v1alpha1/types.go @@ -653,6 +653,8 @@ type ApplicationSourceKustomize struct { Patches KustomizePatches `json:"patches,omitempty" protobuf:"bytes,12,opt,name=patches"` // Components specifies a list of kustomize components to add to the kustomization before building Components []string `json:"components,omitempty" protobuf:"bytes,13,rep,name=components"` + // IgnoreMissingComponents prevents kustomize from failing when components do not exist locally by not appending them to kustomization file + IgnoreMissingComponents bool `json:"ignoreMissingComponents,omitempty" protobuf:"bytes,8,opt,name=ignoreMissingComponents"` // LabelWithoutSelector specifies whether to apply common labels to resource selectors or not LabelWithoutSelector bool `json:"labelWithoutSelector,omitempty" protobuf:"bytes,14,opt,name=labelWithoutSelector"` // KubeVersion specifies the Kubernetes API version to pass to Helm when templating manifests. By default, Argo CD diff --git a/util/kustomize/kustomize.go b/util/kustomize/kustomize.go index ed6417b106d2a..fe1a25edb0358 100644 --- a/util/kustomize/kustomize.go +++ b/util/kustomize/kustomize.go @@ -6,6 +6,7 @@ import ( "net/url" "os" "os/exec" + "path" "path/filepath" "regexp" "sort" @@ -310,8 +311,21 @@ func (k *kustomize) Build(opts *v1alpha1.ApplicationSourceKustomize, kustomizeOp } // add components + foundComponents := opts.Components + if opts.IgnoreMissingComponents { + foundComponents = make([]string, 0) + for _, c := range opts.Components { + resolvedPath := path.Join(k.path, c) + _, err := os.Stat(resolvedPath) + if err != nil { + log.Debugf("%s component directory does not exist", resolvedPath) + continue + } + foundComponents = append(foundComponents, c) + } + } args := []string{"edit", "add", "component"} - args = append(args, opts.Components...) + args = append(args, foundComponents...) cmd := exec.Command(k.getBinaryPath(), args...) cmd.Dir = k.path cmd.Env = env diff --git a/util/kustomize/kustomize_test.go b/util/kustomize/kustomize_test.go index bf8d122bd379b..55de018374501 100644 --- a/util/kustomize/kustomize_test.go +++ b/util/kustomize/kustomize_test.go @@ -445,7 +445,8 @@ func TestKustomizeBuildComponents(t *testing.T) { kustomize := NewKustomizeApp(appPath, appPath, git.NopCreds{}, "", "", "", "") kustomizeSource := v1alpha1.ApplicationSourceKustomize{ - Components: []string{"./components"}, + Components: []string{"./components", "./missing-components"}, + IgnoreMissingComponents: true, } objs, _, _, err := kustomize.Build(&kustomizeSource, nil, nil, nil) require.NoError(t, err)