Skip to content

Commit

Permalink
feat: Allow nested defaults for args
Browse files Browse the repository at this point in the history
  • Loading branch information
codablock committed Sep 20, 2022
1 parent ad45352 commit 42e5a45
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
20 changes: 14 additions & 6 deletions pkg/deployment/external_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func ConvertArgsToVars(args map[string]string) (*uo.UnstructuredObject, error) {
return vars, nil
}

func CheckRequiredDeployArgs(dir string, varsCtx *vars.VarsCtx, deployArgs *uo.UnstructuredObject) error {
func LoadDeploymentArgs(dir string, varsCtx *vars.VarsCtx, deployArgs *uo.UnstructuredObject) error {
// First try to load the config without templating to avoid getting errors while rendering because required
// args were not set. Otherwise we won't be able to iterator through the 'args' array in the deployment.yml
// when the rendering error is actually args related.
Expand All @@ -69,6 +69,19 @@ func CheckRequiredDeployArgs(dir string, varsCtx *vars.VarsCtx, deployArgs *uo.U
return nil
}

// load defaults
defaults := uo.New()
for _, a := range conf.Args {
if a.Default != nil {
a2 := uo.FromMap(map[string]interface{}{
a.Name: a.Default,
})
defaults.Merge(a2)
}
}
defaults.Merge(deployArgs)
*deployArgs = *defaults

err = checkRequiredArgs(conf.Args, deployArgs)
if err != nil {
return err
Expand All @@ -86,11 +99,6 @@ func checkRequiredArgs(argsDef []*types.DeploymentArg, args *uo.UnstructuredObje
if !found {
if a.Default == nil {
return fmt.Errorf("required argument %s not set", a.Name)
} else {
err := args.SetNestedField(a.Default, p...)
if err != nil {
return err
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kluctl_project/target_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (p *LoadedKluctlProject) buildVars(target *types.Target, clusterName *strin
}
}

err = deployment.CheckRequiredDeployArgs(p.DeploymentDir, varsCtx, allArgs)
err = deployment.LoadDeploymentArgs(p.DeploymentDir, varsCtx, allArgs)
if err != nil {
return doError(err)
}
Expand Down

0 comments on commit 42e5a45

Please sign in to comment.