From 2bb7f3860338dc039e45a1f9fc198c15b797b3c9 Mon Sep 17 00:00:00 2001 From: Ramasai Venkatsitarambhaskar Tadepalli Date: Sat, 4 Oct 2025 21:54:01 -0400 Subject: [PATCH] refactor: convert `Kustomization` resource into unstructured map only once during variable substitution Signed-off-by: Ramasai Venkatsitarambhaskar Tadepalli --- internal/build/build.go | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/internal/build/build.go b/internal/build/build.go index aa041955..7deca47c 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -520,30 +520,32 @@ func (b *Builder) do(ctx context.Context, kustomization kustomizev1.Kustomizatio return nil, fmt.Errorf("kustomize build failed: %w", err) } + if kustomization.Spec.PostBuild == nil { + return m, nil + } + + data, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&kustomization) + if err != nil { + return nil, err + } for _, res := range m.Resources() { // run variable substitutions - if kustomization.Spec.PostBuild != nil { - data, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&kustomization) + outRes, err := kustomize.SubstituteVariables(ctx, + b.client, + unstructured.Unstructured{Object: data}, + res, + kustomize.SubstituteWithDryRun(b.dryRun), + kustomize.SubstituteWithStrict(b.strictSubst), + ) + if err != nil { + return nil, fmt.Errorf("var substitution failed for '%s': %w", res.GetName(), err) + } + + if outRes != nil { + _, err = m.Replace(res) if err != nil { return nil, err } - outRes, err := kustomize.SubstituteVariables(ctx, - b.client, - unstructured.Unstructured{Object: data}, - res, - kustomize.SubstituteWithDryRun(b.dryRun), - kustomize.SubstituteWithStrict(b.strictSubst), - ) - if err != nil { - return nil, fmt.Errorf("var substitution failed for '%s': %w", res.GetName(), err) - } - - if outRes != nil { - _, err = m.Replace(res) - if err != nil { - return nil, err - } - } } }