Honor ks.spec.postBuild.substituteStrategy
Signed-off-by: Matheus Pimenta <matheuscscp@gmail.com>
This commit is contained in:
@@ -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