Skip to content

Commit

Permalink
feat: Kustomize ignore missing components (#18634) (#21674)
Browse files Browse the repository at this point in the history
Signed-off-by: Brad Wadsworth <[email protected]>
  • Loading branch information
bradkwadsworth authored Feb 7, 2025
1 parent 922dd77 commit 0973409
Show file tree
Hide file tree
Showing 25 changed files with 1,984 additions and 766 deletions.
4 changes: 4 additions & 0 deletions assets/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions cmd/argocd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,7 @@ type unsetOpts struct {
kustomizeNamespace bool
kustomizeImages []string
kustomizeReplicas []string
ignoreMissingComponents bool
parameters []string
valuesFiles []string
valuesLiteral bool
Expand All @@ -903,6 +904,7 @@ func (o *unsetOpts) KustomizeIsZero() bool {
!o.nameSuffix &&
!o.kustomizeVersion &&
!o.kustomizeNamespace &&
!o.ignoreMissingComponents &&
len(o.kustomizeImages) == 0 &&
len(o.kustomizeReplicas) == 0
}
Expand Down Expand Up @@ -1008,6 +1010,7 @@ func NewApplicationUnsetCommand(clientOpts *argocdclient.ClientOptions) *cobra.C
command.Flags().BoolVar(&opts.kustomizeNamespace, "kustomize-namespace", false, "Kustomize namespace")
command.Flags().StringArrayVar(&opts.kustomizeImages, "kustomize-image", []string{}, "Kustomize images name (e.g. --kustomize-image node --kustomize-image mysql)")
command.Flags().StringArrayVar(&opts.kustomizeReplicas, "kustomize-replica", []string{}, "Kustomize replicas name (e.g. --kustomize-replica my-deployment --kustomize-replica my-statefulset)")
command.Flags().BoolVar(&opts.ignoreMissingComponents, "ignore-missing-components", false, "Unset the kustomize ignore-missing-components option (revert to false)")
command.Flags().StringArrayVar(&opts.pluginEnvs, "plugin-env", []string{}, "Unset plugin env variables (e.g --plugin-env name)")
command.Flags().BoolVar(&opts.passCredentials, "pass-credentials", false, "Unset passCredentials")
command.Flags().BoolVar(&opts.ref, "ref", false, "Unset ref on the source")
Expand Down Expand Up @@ -1048,6 +1051,11 @@ func unset(source *argoappv1.ApplicationSource, opts unsetOpts) (updated bool, n
source.Kustomize.Namespace = ""
}

if opts.ignoreMissingComponents && source.Kustomize.IgnoreMissingComponents {
source.Kustomize.IgnoreMissingComponents = false
updated = true
}

for _, kustomizeImage := range opts.kustomizeImages {
for i, item := range source.Kustomize.Images {
if argoappv1.KustomizeImage(kustomizeImage).Match(item) {
Expand Down
16 changes: 13 additions & 3 deletions cmd/argocd/commands/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1053,9 +1053,10 @@ func TestPrintApplicationNames(t *testing.T) {
func Test_unset(t *testing.T) {
kustomizeSource := &v1alpha1.ApplicationSource{
Kustomize: &v1alpha1.ApplicationSourceKustomize{
NamePrefix: "some-prefix",
NameSuffix: "some-suffix",
Version: "123",
IgnoreMissingComponents: true,
NamePrefix: "some-prefix",
NameSuffix: "some-suffix",
Version: "123",
Images: v1alpha1.KustomizeImages{
"old1=new:tag",
"old2=new:tag",
Expand Down Expand Up @@ -1155,6 +1156,15 @@ func Test_unset(t *testing.T) {
assert.False(t, updated)
assert.False(t, nothingToUnset)

assert.True(t, kustomizeSource.Kustomize.IgnoreMissingComponents)
updated, nothingToUnset = unset(kustomizeSource, unsetOpts{ignoreMissingComponents: true})
assert.False(t, kustomizeSource.Kustomize.IgnoreMissingComponents)
assert.True(t, updated)
assert.False(t, nothingToUnset)
updated, nothingToUnset = unset(kustomizeSource, unsetOpts{ignoreMissingComponents: true})
assert.False(t, updated)
assert.False(t, nothingToUnset)

assert.Len(t, helmSource.Helm.Parameters, 2)
updated, nothingToUnset = unset(helmSource, unsetOpts{parameters: []string{"name-1"}})
assert.Len(t, helmSource.Helm.Parameters, 1)
Expand Down
34 changes: 21 additions & 13 deletions cmd/util/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type AppOptions struct {
kustomizeNamespace string
kustomizeKubeVersion string
kustomizeApiVersions []string
ignoreMissingComponents bool
pluginEnvs []string
Validate bool
directoryExclude string
Expand Down Expand Up @@ -163,6 +164,7 @@ func AddAppFlags(command *cobra.Command, opts *AppOptions) {
command.Flags().StringArrayVar(&opts.jsonnetLibs, "jsonnet-libs", []string{}, "Additional jsonnet libs (prefixed by repoRoot)")
command.Flags().StringArrayVar(&opts.kustomizeImages, "kustomize-image", []string{}, "Kustomize images (e.g. --kustomize-image node:8.15.0 --kustomize-image mysql=mariadb,alpine@sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d)")
command.Flags().StringArrayVar(&opts.kustomizeReplicas, "kustomize-replica", []string{}, "Kustomize replicas (e.g. --kustomize-replica my-development=2 --kustomize-replica my-statefulset=4)")
command.Flags().BoolVar(&opts.ignoreMissingComponents, "ignore-missing-components", false, "Ignore locally missing component directories when setting Kustomize components")
command.Flags().StringArrayVar(&opts.pluginEnvs, "plugin-env", []string{}, "Additional plugin envs")
command.Flags().BoolVar(&opts.Validate, "validate", true, "Validation of repo and cluster")
command.Flags().StringArrayVar(&opts.kustomizeCommonLabels, "kustomize-common-label", []string{}, "Set common labels in Kustomize")
Expand Down Expand Up @@ -307,19 +309,20 @@ func SetAppSpecOptions(flags *pflag.FlagSet, spec *argoappv1.ApplicationSpec, ap
}

type kustomizeOpts struct {
namePrefix string
nameSuffix string
images []string
replicas []string
version string
commonLabels map[string]string
commonAnnotations map[string]string
labelWithoutSelector bool
forceCommonLabels bool
forceCommonAnnotations bool
namespace string
kubeVersion string
apiVersions []string
namePrefix string
nameSuffix string
images []string
replicas []string
version string
commonLabels map[string]string
commonAnnotations map[string]string
labelWithoutSelector bool
forceCommonLabels bool
forceCommonAnnotations bool
namespace string
kubeVersion string
apiVersions []string
ignoreMissingComponents bool
}

func setKustomizeOpt(src *argoappv1.ApplicationSource, opts kustomizeOpts) {
Expand Down Expand Up @@ -359,6 +362,9 @@ func setKustomizeOpt(src *argoappv1.ApplicationSource, opts kustomizeOpts) {
if opts.forceCommonAnnotations {
src.Kustomize.ForceCommonAnnotations = opts.forceCommonAnnotations
}
if opts.ignoreMissingComponents {
src.Kustomize.IgnoreMissingComponents = opts.ignoreMissingComponents
}
for _, image := range opts.images {
src.Kustomize.MergeImage(argoappv1.KustomizeImage(image))
}
Expand Down Expand Up @@ -766,6 +772,8 @@ func ConstructSource(source *argoappv1.ApplicationSource, appOpts AppOptions, fl
setKustomizeOpt(source, kustomizeOpts{forceCommonLabels: appOpts.kustomizeForceCommonLabels})
case "kustomize-force-common-annotation":
setKustomizeOpt(source, kustomizeOpts{forceCommonAnnotations: appOpts.kustomizeForceCommonAnnotations})
case "ignore-missing-components":
setKustomizeOpt(source, kustomizeOpts{ignoreMissingComponents: appOpts.ignoreMissingComponents})
case "jsonnet-tla-str":
setJsonnetOpt(source, appOpts.jsonnetTlaStr, false)
case "jsonnet-tla-code":
Expand Down
6 changes: 6 additions & 0 deletions cmd/util/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ func Test_setKustomizeOpt(t *testing.T) {
setKustomizeOpt(&src, kustomizeOpts{commonLabels: map[string]string{"foo1": "bar1", "foo2": "bar2"}, labelWithoutSelector: true})
assert.Equal(t, &v1alpha1.ApplicationSourceKustomize{CommonLabels: map[string]string{"foo1": "bar1", "foo2": "bar2"}, LabelWithoutSelector: true}, src.Kustomize)
})
t.Run("IgnoreMissingComponents", func(t *testing.T) {
src := v1alpha1.ApplicationSource{}
setKustomizeOpt(&src, kustomizeOpts{ignoreMissingComponents: true})
t.Logf("HERE IS THE SOURCE\n %+v\n", src)
assert.True(t, src.Kustomize.IgnoreMissingComponents)
})
}

func Test_setJsonnetOpt(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions docs/operator-manual/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ spec:
count: 4
components:
- ../component # relative to the kustomization.yaml (`source.path`).
# Ignore locally missing component directories when using Kustomize Components. Defaults to false
ignoreMissingComponents: true
patches:
- target:
kind: Deployment
Expand Down
1 change: 1 addition & 0 deletions docs/user-guide/commands/argocd_admin_app_generate-spec.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/user-guide/commands/argocd_app_add-source.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/user-guide/commands/argocd_app_create.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/user-guide/commands/argocd_app_set.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/user-guide/commands/argocd_app_unset.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion docs/user-guide/kustomize.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ The following configuration options are available for Kustomize:
* `commonAnnotationsEnvsubst` is a boolean value which enables env variables substition in annotation values
* `patches` is a list of Kustomize patches that supports inline updates
* `components` is a list of Kustomize components
* `ignoreMissingComponents` prevents kustomize from failing when components do not exist locally by not appending them to kustomization file

To use Kustomize with an overlay, point your path to the overlay.

Expand Down Expand Up @@ -130,7 +131,8 @@ 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.
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].

Outside of Argo CD, to utilize components, you must add the following to the `kustomization.yaml` that the Application references. For example:
```yaml
Expand Down Expand Up @@ -158,6 +160,7 @@ spec:
kustomize:
components:
- ../component # relative to the kustomization.yaml (`source.path`).
ignoreMissingComponents: true
```
## Private Remote Bases
Expand Down
Loading

0 comments on commit 0973409

Please sign in to comment.