From d5b1d54f9f83819bf151e3f489b70f56838774d6 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Boll Date: Thu, 5 Dec 2024 09:36:23 +0100 Subject: [PATCH] Review remarks --- tooling/templatize/pkg/pipeline/arm.go | 6 +++++- tooling/templatize/pkg/pipeline/run.go | 13 +++++++------ tooling/templatize/pkg/pipeline/run_test.go | 10 +++++++++- tooling/templatize/pkg/pipeline/shell.go | 6 +++++- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/tooling/templatize/pkg/pipeline/arm.go b/tooling/templatize/pkg/pipeline/arm.go index e6efb8c22..3bf221237 100644 --- a/tooling/templatize/pkg/pipeline/arm.go +++ b/tooling/templatize/pkg/pipeline/arm.go @@ -31,8 +31,12 @@ func newArmClient(subscriptionID, region string) *armClient { func (a *armClient) runArmStep(ctx context.Context, options *PipelineRunOptions, rgName string, step *Step, input map[string]output) (output, error) { logger := logr.FromContextOrDiscard(ctx) + inputValues, err := getInputValues(step.Inputs, input) + if err != nil { + return nil, fmt.Errorf("failed to get input values: %w", err) + } // Transform Bicep to ARM - deploymentProperties, err := transformBicepToARM(ctx, step.Parameters, options.Vars, addInputVars(step.Inputs, input)) + deploymentProperties, err := transformBicepToARM(ctx, step.Parameters, options.Vars, inputValues) if err != nil { return nil, fmt.Errorf("failed to transform Bicep to ARM: %w", err) } diff --git a/tooling/templatize/pkg/pipeline/run.go b/tooling/templatize/pkg/pipeline/run.go index a0ed8aed9..3b3d99a4f 100644 --- a/tooling/templatize/pkg/pipeline/run.go +++ b/tooling/templatize/pkg/pipeline/run.go @@ -3,7 +3,6 @@ package pipeline import ( "context" "fmt" - "log" "os" "path/filepath" "strings" @@ -243,16 +242,18 @@ func (rg *ResourceGroup) Validate() error { return nil } -func addInputVars(configuredInputs []Input, inputs map[string]output) map[string]any { - envVars := make(map[string]any) +func getInputValues(configuredInputs []Input, inputs map[string]output) (map[string]any, error) { + values := make(map[string]any) for _, i := range configuredInputs { if v, found := inputs[i.Step]; found { value, err := v.GetValue(i.Output) if err != nil { - log.Fatal(err) + return nil, fmt.Errorf("failed to get value for input %s.%s: %w", i.Step, i.Output, err) } - envVars[i.Name] = value.Value + values[i.Name] = value.Value + } else { + return nil, fmt.Errorf("step %s not found in provided outputs", i.Step) } } - return envVars + return values, nil } diff --git a/tooling/templatize/pkg/pipeline/run_test.go b/tooling/templatize/pkg/pipeline/run_test.go index 5b9b82147..92302f8c8 100644 --- a/tooling/templatize/pkg/pipeline/run_test.go +++ b/tooling/templatize/pkg/pipeline/run_test.go @@ -313,6 +313,14 @@ func TestAddInputVars(t *testing.T) { }, } - envVars := addInputVars(s.Inputs, mapOutput) + envVars, err := getInputValues(s.Inputs, mapOutput) + assert.NilError(t, err) assert.DeepEqual(t, envVars, map[string]any{"input1": "value1"}) + + _, err = getInputValues([]Input{ + { + Step: "foobar", + }, + }, mapOutput) + assert.Error(t, err, "step foobar not found in provided outputs") } diff --git a/tooling/templatize/pkg/pipeline/shell.go b/tooling/templatize/pkg/pipeline/shell.go index 3bf1a8716..c3943d170 100644 --- a/tooling/templatize/pkg/pipeline/shell.go +++ b/tooling/templatize/pkg/pipeline/shell.go @@ -53,7 +53,11 @@ func (s *Step) runShellStep(ctx context.Context, kubeconfigFile string, options maps.Copy(envVars, stepVars) - for k, v := range addInputVars(s.Inputs, inputs) { + inputValues, err := getInputValues(s.Inputs, inputs) + if err != nil { + return fmt.Errorf("failed to get input values: %w", err) + } + for k, v := range inputValues { envVars[k] = utils.AnyToString(v) } // execute the command