Merge pull request #5945 from fluxcd/substitute-always
Honor `ks.spec.postBuild.substituteStrategy`
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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=
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
@@ -0,0 +1,7 @@
|
||||
apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: test-config
|
||||
data:
|
||||
color: "${color:=blue}"
|
||||
shape: "${shape:=square}"
|
||||
@@ -0,0 +1,4 @@
|
||||
apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
resources:
|
||||
- configmap.yaml
|
||||
@@ -0,0 +1,8 @@
|
||||
apiVersion: kustomize.toolkit.fluxcd.io/v1
|
||||
kind: Kustomization
|
||||
metadata:
|
||||
name: test-ks
|
||||
namespace: flux-system
|
||||
spec:
|
||||
path: "./resources"
|
||||
postBuild: {}
|
||||
Reference in New Issue
Block a user