Skip to content

Commit

Permalink
chore: incorporate Result changes
Browse files Browse the repository at this point in the history
Signed-off-by: Hidde Beydals <[email protected]>
  • Loading branch information
hiddeco committed Sep 10, 2024
1 parent 55b40a4 commit df87d55
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 36 deletions.
20 changes: 11 additions & 9 deletions internal/directives/helm_template_directive.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,21 @@ func (d *helmTemplateDirective) Name() string {

// Run implements the Directive interface.
func (d *helmTemplateDirective) Run(ctx context.Context, stepCtx *StepContext) (Result, error) {
failure := Result{Status: StatusFailure}

// Validate the configuration against the JSON Schema
if err := validate(
d.schemaLoader,
gojsonschema.NewGoLoader(stepCtx.Config),
d.Name(),
); err != nil {
return ResultFailure, err
return failure, err
}

// Convert the configuration into a typed struct
cfg, err := configToStruct[HelmTemplateConfig](stepCtx.Config)
if err != nil {
return ResultFailure, fmt.Errorf("could not convert config into %s config: %w", d.Name(), err)
return failure, fmt.Errorf("could not convert config into %s config: %w", d.Name(), err)
}

return d.run(ctx, stepCtx, cfg)
Expand All @@ -67,32 +69,32 @@ func (d *helmTemplateDirective) run(
) (Result, error) {
composedValues, err := d.composeValues(stepCtx.WorkDir, cfg.ValuesFiles)
if err != nil {
return ResultFailure, fmt.Errorf("failed to compose values: %w", err)
return Result{Status: StatusFailure}, fmt.Errorf("failed to compose values: %w", err)
}

chartRequested, err := d.loadChart(stepCtx.WorkDir, cfg.Path)
if err != nil {
return ResultFailure, fmt.Errorf("failed to load chart from %q: %w", cfg.Path, err)
return Result{Status: StatusFailure}, fmt.Errorf("failed to load chart from %q: %w", cfg.Path, err)
}

if err = d.checkDependencies(chartRequested); err != nil {
return ResultFailure, fmt.Errorf("missing chart dependencies: %w", err)
return Result{Status: StatusFailure}, fmt.Errorf("missing chart dependencies: %w", err)
}

install, err := d.newInstallAction(cfg, stepCtx.Project)
if err != nil {
return ResultFailure, fmt.Errorf("failed to initialize Helm action config: %w", err)
return Result{Status: StatusFailure}, fmt.Errorf("failed to initialize Helm action config: %w", err)
}

rls, err := install.RunWithContext(ctx, chartRequested, composedValues)
if err != nil {
return ResultFailure, fmt.Errorf("failed to render chart: %w", err)
return Result{Status: StatusFailure}, fmt.Errorf("failed to render chart: %w", err)
}

if err = d.writeOutput(stepCtx.WorkDir, cfg.OutPath, rls.Manifest); err != nil {
return ResultFailure, fmt.Errorf("failed to write rendered chart: %w", err)
return Result{Status: StatusFailure}, fmt.Errorf("failed to write rendered chart: %w", err)
}
return ResultSuccess, nil
return Result{Status: StatusSuccess}, nil
}

// composeValues composes the values from the given values files. It merges the
Expand Down
16 changes: 8 additions & 8 deletions internal/directives/helm_template_directive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ data:
},
assertions: func(t *testing.T, workDir string, result Result, err error) {
require.NoError(t, err)
assert.Equal(t, ResultSuccess, result)
assert.Equal(t, Result{Status: StatusSuccess}, result)

outPath := filepath.Join(workDir, "output.yaml")
require.FileExists(t, outPath)
Expand Down Expand Up @@ -92,7 +92,7 @@ data:
},
assertions: func(t *testing.T, workDir string, result Result, err error) {
require.NoError(t, err)
assert.Equal(t, ResultSuccess, result)
assert.Equal(t, Result{Status: StatusSuccess}, result)

outPath := filepath.Join(workDir, "output.yaml")
require.FileExists(t, outPath)
Expand All @@ -118,7 +118,7 @@ data:
},
assertions: func(t *testing.T, workDir string, result Result, err error) {
require.ErrorContains(t, err, "failed to compose values")
assert.Equal(t, ResultFailure, result)
assert.Equal(t, Result{Status: StatusFailure}, result)

require.NoFileExists(t, filepath.Join(workDir, "output.yaml"))
},
Expand All @@ -130,7 +130,7 @@ data:
},
assertions: func(t *testing.T, workDir string, result Result, err error) {
require.ErrorContains(t, err, "failed to load chart")
assert.Equal(t, ResultFailure, result)
assert.Equal(t, Result{Status: StatusFailure}, result)

require.NoFileExists(t, filepath.Join(workDir, "output.yaml"))
},
Expand All @@ -153,7 +153,7 @@ dependencies:
},
assertions: func(t *testing.T, workDir string, result Result, err error) {
require.ErrorContains(t, err, "missing chart dependencies")
assert.Equal(t, ResultFailure, result)
assert.Equal(t, Result{Status: StatusFailure}, result)

require.NoFileExists(t, filepath.Join(workDir, "output.yaml"))
},
Expand All @@ -171,7 +171,7 @@ version: 0.1.0`,
},
assertions: func(t *testing.T, workDir string, result Result, err error) {
require.ErrorContains(t, err, "failed to initialize Helm action config")
assert.Equal(t, ResultFailure, result)
assert.Equal(t, Result{Status: StatusFailure}, result)

require.NoFileExists(t, filepath.Join(workDir, "output.yaml"))
},
Expand All @@ -197,7 +197,7 @@ data:
},
assertions: func(t *testing.T, workDir string, result Result, err error) {
require.ErrorContains(t, err, "failed to render chart")
assert.Equal(t, ResultFailure, result)
assert.Equal(t, Result{Status: StatusFailure}, result)

require.NoFileExists(t, filepath.Join(workDir, "output.yaml"))
},
Expand All @@ -222,7 +222,7 @@ metadata:
},
assertions: func(t *testing.T, workDir string, result Result, err error) {
require.ErrorContains(t, err, "failed to write rendered chart")
assert.Equal(t, ResultFailure, result)
assert.Equal(t, Result{Status: StatusFailure}, result)

require.NoFileExists(t, filepath.Join(workDir, "output.yaml"))
},
Expand Down
24 changes: 15 additions & 9 deletions internal/directives/helm_update_chart_directive.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,21 @@ func (d *helmUpdateChartDirective) Name() string {

// Run implements the Directive interface.
func (d *helmUpdateChartDirective) Run(ctx context.Context, stepCtx *StepContext) (Result, error) {
failure := Result{Status: StatusFailure}

// Validate the configuration against the JSON Schema
if err := validate(
d.schemaLoader,
gojsonschema.NewGoLoader(stepCtx.Config),
d.Name(),
); err != nil {
return ResultFailure, err
return failure, err
}

// Convert the configuration into a typed struct
cfg, err := configToStruct[HelmUpdateChartConfig](stepCtx.Config)
if err != nil {
return ResultFailure, fmt.Errorf("could not convert config into %s config: %w", d.Name(), err)
return failure, fmt.Errorf("could not convert config into %s config: %w", d.Name(), err)
}

return d.run(ctx, stepCtx, cfg)
Expand All @@ -76,35 +78,39 @@ func (d *helmUpdateChartDirective) run(
) (Result, error) {
absChartPath, err := securejoin.SecureJoin(stepCtx.WorkDir, cfg.Path)
if err != nil {
return ResultFailure, fmt.Errorf("failed to join path %q: %w", cfg.Path, err)
return Result{Status: StatusFailure}, fmt.Errorf("failed to join path %q: %w", cfg.Path, err)
}

chartFilePath := filepath.Join(absChartPath, "Chart.yaml")
chartDependencies, err := loadChartDependencies(chartFilePath)
if err != nil {
return ResultFailure, fmt.Errorf("failed to load chart dependencies from %q: %w", chartFilePath, err)
return Result{
Status: StatusFailure,
}, fmt.Errorf("failed to load chart dependencies from %q: %w", chartFilePath, err)
}

changes, err := d.processChartUpdates(ctx, stepCtx, cfg, chartDependencies)
if err != nil {
return ResultFailure, err
return Result{Status: StatusFailure}, err
}

if err = intyaml.SetStringsInFile(chartFilePath, changes); err != nil {
return ResultFailure, fmt.Errorf("failed to update chart dependencies in %q: %w", chartFilePath, err)
return Result{
Status: StatusFailure,
}, fmt.Errorf("failed to update chart dependencies in %q: %w", chartFilePath, err)
}

helmHome, err := os.MkdirTemp("", "helm-chart-update-")
if err != nil {
return ResultFailure, fmt.Errorf("failed to create temporary Helm home directory: %w", err)
return Result{Status: StatusFailure}, fmt.Errorf("failed to create temporary Helm home directory: %w", err)
}
defer os.RemoveAll(helmHome)

if err := d.updateDependencies(ctx, stepCtx, helmHome, absChartPath, chartDependencies); err != nil {
return ResultFailure, err
return Result{Status: StatusFailure}, err
}

return ResultSuccess, nil
return Result{Status: StatusSuccess}, nil
}

func (d *helmUpdateChartDirective) processChartUpdates(
Expand Down
2 changes: 1 addition & 1 deletion internal/directives/helm_update_chart_directive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func Test_helmUpdateChartDirective_run(t *testing.T) {
},
assertions: func(t *testing.T, tempDir string, result Result, err error) {
assert.NoError(t, err)
assert.Equal(t, ResultSuccess, result)
assert.Equal(t, Result{Status: StatusSuccess}, result)

// Check if Chart.yaml was updated correctly
updatedChartYaml, err := os.ReadFile(filepath.Join(tempDir, "testchart", "Chart.yaml"))
Expand Down
12 changes: 7 additions & 5 deletions internal/directives/helm_update_image_directive.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,21 @@ func (d *helmUpdateImageDirective) Name() string {

// Run implements the Directive interface.
func (d *helmUpdateImageDirective) Run(ctx context.Context, stepCtx *StepContext) (Result, error) {
failure := Result{Status: StatusFailure}

// Validate the configuration against the JSON Schema
if err := validate(
d.schemaLoader,
gojsonschema.NewGoLoader(stepCtx.Config),
d.Name(),
); err != nil {
return ResultFailure, err
return failure, err
}

// Convert the configuration into a typed struct
cfg, err := configToStruct[HelmUpdateImageConfig](stepCtx.Config)
if err != nil {
return ResultFailure, fmt.Errorf("could not convert config into %s config: %w", d.Name(), err)
return failure, fmt.Errorf("could not convert config into %s config: %w", d.Name(), err)
}

return d.run(ctx, stepCtx, cfg)
Expand All @@ -62,16 +64,16 @@ func (d *helmUpdateImageDirective) run(
) (Result, error) {
changes, err := d.generateImageUpdates(ctx, stepCtx, cfg)
if err != nil {
return ResultFailure, fmt.Errorf("failed to generate image updates: %w", err)
return Result{Status: StatusFailure}, fmt.Errorf("failed to generate image updates: %w", err)
}

if len(changes) > 0 {
if err := d.updateValuesFile(stepCtx.WorkDir, cfg.Path, changes); err != nil {
return ResultFailure, fmt.Errorf("values file update failed: %w", err)
return Result{Status: StatusFailure}, fmt.Errorf("values file update failed: %w", err)
}
}

return ResultSuccess, nil
return Result{Status: StatusSuccess}, nil
}

func (d *helmUpdateImageDirective) generateImageUpdates(
Expand Down
8 changes: 4 additions & 4 deletions internal/directives/helm_update_image_directive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func Test_helmUpdateImageDirective_run(t *testing.T) {
},
assertions: func(t *testing.T, workDir string, result Result, err error) {
assert.NoError(t, err)
assert.Equal(t, ResultSuccess, result)
assert.Equal(t, Result{Status: StatusSuccess}, result)
content, err := os.ReadFile(path.Join(workDir, "values.yaml"))
require.NoError(t, err)
assert.Contains(t, string(content), "tag: 1.19.0")
Expand All @@ -99,7 +99,7 @@ func Test_helmUpdateImageDirective_run(t *testing.T) {
},
assertions: func(t *testing.T, workDir string, result Result, err error) {
assert.NoError(t, err)
assert.Equal(t, ResultSuccess, result)
assert.Equal(t, Result{Status: StatusSuccess}, result)
content, err := os.ReadFile(path.Join(workDir, "values.yaml"))
require.NoError(t, err)
assert.Contains(t, string(content), "tag: oldtag")
Expand Down Expand Up @@ -140,7 +140,7 @@ func Test_helmUpdateImageDirective_run(t *testing.T) {
assertions: func(t *testing.T, _ string, result Result, err error) {
require.ErrorContains(t, err, "failed to generate image updates")
require.Errorf(t, err, "something went wrong")
assert.Equal(t, ResultFailure, result)
assert.Equal(t, Result{Status: StatusFailure}, result)
},
},
{
Expand Down Expand Up @@ -188,7 +188,7 @@ func Test_helmUpdateImageDirective_run(t *testing.T) {
},
assertions: func(t *testing.T, _ string, result Result, err error) {
assert.Error(t, err)
assert.Equal(t, ResultFailure, result)
assert.Equal(t, Result{Status: StatusFailure}, result)
assert.Contains(t, err.Error(), "values file update failed")
},
},
Expand Down

0 comments on commit df87d55

Please sign in to comment.