diff --git a/go.mod b/go.mod index 424f0341..678b4d7f 100644 --- a/go.mod +++ b/go.mod @@ -24,7 +24,7 @@ require ( github.com/fluxcd/pkg/chartutil v1.26.0 github.com/fluxcd/pkg/envsubst v1.7.0 github.com/fluxcd/pkg/git v0.50.0 - github.com/fluxcd/pkg/kustomize v1.32.0 + github.com/fluxcd/pkg/kustomize v1.35.0 github.com/fluxcd/pkg/oci v0.66.0 github.com/fluxcd/pkg/runtime v0.108.0 github.com/fluxcd/pkg/sourceignore v0.18.0 diff --git a/go.sum b/go.sum index 03dd511d..408c07cc 100644 --- a/go.sum +++ b/go.sum @@ -206,8 +206,8 @@ github.com/fluxcd/pkg/git v0.50.0 h1:gbGmSTjQ6CxqFmT9ZkLfYh8kG7CHqc7gHoPqcygixK8 github.com/fluxcd/pkg/git v0.50.0/go.mod h1:OgaHoS0iR0GuLl+f778X7NrGy1pDH7xcpF/nsCRgJ9g= github.com/fluxcd/pkg/gittestserver v0.29.0 h1:2j03zKVL6iVn6oiUuecG/O/3Q1pULWM9JrF/HSjkpnc= github.com/fluxcd/pkg/gittestserver v0.29.0/go.mod h1:O8151jV0ppBZTb9IUXMjxh6hZpkiuLq8JQHDBPOkZFw= -github.com/fluxcd/pkg/kustomize v1.32.0 h1:5lLT2dgR+JrcoJHB7/K50o0AcJikKvXcRd3r7jIYZC8= -github.com/fluxcd/pkg/kustomize v1.32.0/go.mod h1:Xz1QIUIKexXuSolRQY63843wSycPVuIsVhE9C+aJWl8= +github.com/fluxcd/pkg/kustomize v1.35.0 h1:Hl3Y6WPPo1btDyUTrT+CS3b4GbAqCuE+bZebpIG2cxg= +github.com/fluxcd/pkg/kustomize v1.35.0/go.mod h1:M1jIz03OQ86QmcsO+2W3y0+X4B0dbh2QLUF8h3gzyJE= github.com/fluxcd/pkg/oci v0.66.0 h1:nlBA3/MvJShjA+VFPKDRoXLUpH+e8THVvCMST+qUTlI= github.com/fluxcd/pkg/oci v0.66.0/go.mod h1:9Z4Juu/BhLLKHKOkOalfwIkZYsjTViD0YDHzW9w5R9c= github.com/fluxcd/pkg/runtime v0.108.0 h1:sSXz6FWcRT+tkfddiCmehrYaEKqkVFkcBWDhGMNJtH4= diff --git a/internal/build/build.go b/internal/build/build.go index 9a59c4bb..f94f54b4 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -620,6 +620,7 @@ func (b *Builder) do(ctx context.Context, kustomization kustomizev1.Kustomizatio res, kustomize.SubstituteWithDryRun(b.dryRun), kustomize.SubstituteWithStrict(b.strictSubst), + kustomize.SubstituteWithAlways(kustomization.GetSubstituteStrategy() == kustomizev1.SubstituteStrategyAlways), ) if err != nil { return nil, fmt.Errorf("var substitution failed for '%s': %w", res.GetName(), err) diff --git a/internal/build/build_test.go b/internal/build/build_test.go index 800bb1f4..ed3ff5da 100644 --- a/internal/build/build_test.go +++ b/internal/build/build_test.go @@ -875,3 +875,58 @@ func Test_Build_preserveAllLabels(t *testing.T) { t.Errorf("expected owner namespace label \"flux-system\", got %v", got) } } + +func Test_Build_substituteStrategy(t *testing.T) { + tests := []struct { + name string + ksFile string + wantColor string + wantShape string + }{ + { + // The default WithVariables strategy skips substitution when no + // variables are defined, leaving the defaulted expressions untouched. + name: "WithVariables skips substitution without variables", + ksFile: "testdata/substitute-strategy/with-variables.yaml", + wantColor: "${color:=blue}", + wantShape: "${shape:=square}", + }, + { + // The Always strategy performs substitution even without variables, + // resolving the expressions to their defaults. + name: "Always substitutes defaults without variables", + ksFile: "testdata/substitute-strategy/always.yaml", + wantColor: "blue", + wantShape: "square", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b, err := NewBuilder("test-ks", "testdata/substitute-strategy/resources", + WithDryRun(true), + WithNamespace("flux-system"), + WithKustomizationFile(tt.ksFile), + ) + if err != nil { + t.Fatalf("unable to create builder: %v", err) + } + + objects, err := b.Build() + if err != nil { + t.Fatalf("build failed: %v", err) + } + if len(objects) == 0 { + t.Fatal("expected at least one object, got none") + } + + data, _, _ := unstructured.NestedStringMap(objects[0].Object, "data") + if got := data["color"]; got != tt.wantColor { + t.Errorf("expected color=%q, got %q", tt.wantColor, got) + } + if got := data["shape"]; got != tt.wantShape { + t.Errorf("expected shape=%q, got %q", tt.wantShape, got) + } + }) + } +} diff --git a/internal/build/testdata/substitute-strategy/always.yaml b/internal/build/testdata/substitute-strategy/always.yaml new file mode 100644 index 00000000..c7fce66e --- /dev/null +++ b/internal/build/testdata/substitute-strategy/always.yaml @@ -0,0 +1,9 @@ +apiVersion: kustomize.toolkit.fluxcd.io/v1 +kind: Kustomization +metadata: + name: test-ks + namespace: flux-system +spec: + path: "./resources" + postBuild: + substituteStrategy: Always diff --git a/internal/build/testdata/substitute-strategy/resources/configmap.yaml b/internal/build/testdata/substitute-strategy/resources/configmap.yaml new file mode 100644 index 00000000..bcca1157 --- /dev/null +++ b/internal/build/testdata/substitute-strategy/resources/configmap.yaml @@ -0,0 +1,7 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: test-config +data: + color: "${color:=blue}" + shape: "${shape:=square}" diff --git a/internal/build/testdata/substitute-strategy/resources/kustomization.yaml b/internal/build/testdata/substitute-strategy/resources/kustomization.yaml new file mode 100644 index 00000000..9fb65fa6 --- /dev/null +++ b/internal/build/testdata/substitute-strategy/resources/kustomization.yaml @@ -0,0 +1,4 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +resources: + - configmap.yaml diff --git a/internal/build/testdata/substitute-strategy/with-variables.yaml b/internal/build/testdata/substitute-strategy/with-variables.yaml new file mode 100644 index 00000000..ea9dd735 --- /dev/null +++ b/internal/build/testdata/substitute-strategy/with-variables.yaml @@ -0,0 +1,8 @@ +apiVersion: kustomize.toolkit.fluxcd.io/v1 +kind: Kustomization +metadata: + name: test-ks + namespace: flux-system +spec: + path: "./resources" + postBuild: {}