diff --git a/docs/user-guide/kustomize.md b/docs/user-guide/kustomize.md index f524e0a4e9..a22984a2cb 100644 --- a/docs/user-guide/kustomize.md +++ b/docs/user-guide/kustomize.md @@ -133,7 +133,7 @@ spec: ## Components Kustomize [components](https://github.com/kubernetes-sigs/kustomize/blob/master/examples/components.md) encapsulate both resources and patches together. They provide a powerful way to modularize and reuse configuration in Kubernetes applications. -If Kustomize is passed a non-existing component directory, it will error out. Missing component directories can be ignored (meaning, not passed to Kustomize) using `ignoreMissingComponents`. This can be particularly helpful to implement a [default/override pattern]. +If Kustomize is passed a non-existing component directory, it will error out. Missing or invalid (missing a Kustomization file) component directories can be ignored (meaning, not passed to Kustomize) using `ignoreMissingComponents`. This can be particularly helpful to implement a [default/override pattern]. Outside of Argo CD, to utilize components, you must add the following to the `kustomization.yaml` that the Application references. For example: ```yaml diff --git a/util/kustomize/kustomize.go b/util/kustomize/kustomize.go index 922c3d145a..159c3af55c 100644 --- a/util/kustomize/kustomize.go +++ b/util/kustomize/kustomize.go @@ -351,17 +351,24 @@ func (k *kustomize) Build(opts *v1alpha1.ApplicationSourceKustomize, kustomizeOp return nil, nil, nil, fmt.Errorf("failed to open the repo folder: %w", err) } + kustomizationFileValidNames := []string{"kustomization.yaml", "kustomization.yml", "Kustomization"} + component: for _, c := range opts.Components { - resolvedPath, err := filepath.Rel(k.repoRoot, filepath.Join(k.path, c)) + componentDir, err := filepath.Rel(k.repoRoot, filepath.Join(k.path, c)) if err != nil { return nil, nil, nil, fmt.Errorf("kustomize components path failed: %w", err) } - _, err = root.Stat(resolvedPath) - if err != nil { - log.Debugf("%s component directory does not exist", resolvedPath) - continue + for _, kustomizationFile := range kustomizationFileValidNames { + kustomization, err := root.Stat(filepath.Join(componentDir, kustomizationFile)) + if err == nil && kustomization.Mode().IsRegular() { + foundComponents = append(foundComponents, c) + log.Infof("Adding component '%s' to kustomization.yaml", c) + break component + } } - foundComponents = append(foundComponents, c) + log.Infof("Ignoring component '%s': directory does not exist or unable to find one of %s", + componentDir, + strings.Join(kustomizationFileValidNames, ", ")) } } diff --git a/util/kustomize/kustomize_test.go b/util/kustomize/kustomize_test.go index 7e517c8541..ff804b76cb 100644 --- a/util/kustomize/kustomize_test.go +++ b/util/kustomize/kustomize_test.go @@ -495,13 +495,29 @@ func TestKustomizeBuildComponents(t *testing.T) { } _, _, _, err = kustomize.Build(&kustomizeSource, nil, nil, nil) require.Error(t, err) + kustomizeSource = v1alpha1.ApplicationSourceKustomize{ + Components: []string{"./components", "./components_no_kustomization"}, + IgnoreMissingComponents: false, + } + _, _, _, err = kustomize.Build(&kustomizeSource, nil, nil, nil) + require.Error(t, err) + + // `kustomize edit add component` works if the directory exists even if there is no kustomization.yaml inside, + // but then `kustomize build` fails + // Reset the kustomize app so we don't let components_no_kustomization in the kustomization.yaml + appPath, err = testDataDir(t, kustomization6) + require.NoError(t, err) + kustomize = NewKustomizeApp(appPath, appPath, git.NopCreds{}, "", "", "", "") kustomizeSource = v1alpha1.ApplicationSourceKustomize{ - Components: []string{"./components", "./missing-components"}, + Components: []string{"./components", "./missing-components", "./components_no_kustomization"}, IgnoreMissingComponents: true, } objs, _, _, err := kustomize.Build(&kustomizeSource, nil, nil, nil) require.NoError(t, err) + for _, o := range objs { + assert.NotEqual(t, "notproduced", o.GetName()) + } obj := objs[0] assert.Equal(t, "nginx-deployment", obj.GetName()) assert.Equal(t, map[string]string{ diff --git a/util/kustomize/testdata/kustomization_yaml_components/components_no_kustomization/configmap.yaml b/util/kustomize/testdata/kustomization_yaml_components/components_no_kustomization/configmap.yaml new file mode 100644 index 0000000000..e2f54a8349 --- /dev/null +++ b/util/kustomize/testdata/kustomization_yaml_components/components_no_kustomization/configmap.yaml @@ -0,0 +1,6 @@ +kind: ConfigMap +apiVersion: v1 +metadata: + name: notproduced +data: + somekey: somevalue