Skip to content

Commit

Permalink
Added ability to ignore Kustomize component directory if it does not
Browse files Browse the repository at this point in the history
exist

Signed-off-by: Brad Wadsworth <[email protected]>
  • Loading branch information
bradkwadsworth committed Jan 26, 2025
1 parent 544aea1 commit aaf19de
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions pkg/apis/application/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion util/kustomize/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"sort"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion util/kustomize/kustomize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit aaf19de

Please sign in to comment.