@@ -30,7 +30,7 @@ type KustomizationSpec struct {
3030 // value to retry failures.
3131 // +optional
3232 RetryInterval *metav1.Duration ` json:"retryInterval,omitempty"`
33-
33+
3434 // The KubeConfig for reconciling the Kustomization on a remote cluster.
3535 // When specified, KubeConfig takes precedence over ServiceAccountName.
3636 // +optional
@@ -42,6 +42,11 @@ type KustomizationSpec struct {
4242 // +optional
4343 Path string ` json:"path,omitempty"`
4444
45+ // PostBuild describes which actions to perform on the YAML manifest
46+ // generated by building the kustomize overlay.
47+ // +optional
48+ PostBuild *PostBuild ` json:"postBuild,omitempty"`
49+
4550 // Enables garbage collection.
4651 // +required
4752 Prune bool ` json:"prune"`
@@ -147,6 +152,21 @@ type Image struct {
147152}
148153```
149154
155+ The post-build section defines which actions to perform on the YAML manifest after kustomize build:
156+
157+ ``` go
158+ type PostBuild struct {
159+ // Substitute holds a map of key/value pairs.
160+ // The variables defined in your YAML manifests
161+ // that match any of the keys defined in the map
162+ // will be substituted with the set value.
163+ // Includes support for bash string replacement functions
164+ // e.g. ${var:=default}, ${var:position} and ${var/substring/replacement}.
165+ // +optional
166+ Substitute map [string ]string ` json:"substitute,omitempty"`
167+ }
168+ ```
169+
150170The status sub-resource records the result of the last reconciliation:
151171
152172``` go
@@ -663,6 +683,70 @@ spec:
663683 digest: sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3
664684` ` `
665685
686+ # # Variable substitution
687+
688+ With `spec.postBuild.substitute` you can provide a map of key/value pairs holding the
689+ variables to be substituted in the final YAML manifest, after kustomize build.
690+
691+ This offers basic templating for your manifests including support
692+ for [bash string replacement functions](https://github.com/drone/envsubst) e.g. :
693+
694+ - ` ${var:=default}`
695+ - ` ${var:position}`
696+ - ` ${var:position:length}`
697+ - ` ${var/substring/replacement}`
698+
699+ Assuming you have manifests with the following variables :
700+
701+ ` ` ` yaml
702+ apiVersion: v1
703+ kind: Namespace
704+ metadata:
705+ name: apps
706+ labels:
707+ environment: ${cluster_env:=dev}
708+ region: "${cluster_region}"
709+ ` ` `
710+
711+ You can specify the variables and their values in the Kustomization definition under
712+ the `substitute` post build section :
713+
714+ ` ` ` ` yaml
715+ apiVersion : kustomize.toolkit.fluxcd.io/v1beta1
716+ kind : Kustomization
717+ metadata :
718+ name : apps
719+ spec :
720+ interval : 5m
721+ path : " ./apps/"
722+ postBuild :
723+ substitute :
724+ cluster_env : " prod"
725+ cluster_region : " eu-central-1"
726+ ` ` ` `
727+
728+ Note that you should prefix the variables that get replaced by kustomize-controller
729+ to avoid conflicts with any existing scripts embedded in ConfigMaps or container commands.
730+
731+ You can replicate the controller post-build substitutions locally using
732+ [kustomize](https://github.com/kubernetes-sigs/kustomize)
733+ and Drone's [envsubst](https://github.com/drone/envsubst) :
734+
735+ ` ` ` console
736+ $ go install github.com/drone/envsubst/cmd/envsubst
737+
738+ $ export cluster_region=eu-central-1
739+ $ kustomize build ./apps/ | $GOPATH/bin/envsubst
740+ ---
741+ apiVersion: v1
742+ kind: Namespace
743+ metadata:
744+ name: apps
745+ labels:
746+ environment: dev
747+ region: eu-central-1
748+ ` ` `
749+
666750# # Remote Clusters / Cluster-API
667751
668752If the `kubeConfig` field is set, objects will be applied, health-checked, pruned, and deleted for the default
0 commit comments