Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow variables to be set as interfaces through the library API #2900

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/api/v1alpha1/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ type Constant struct {
type SetVariable struct {
Variable `json:",inline"`
// The value the variable is currently set with
Value string `json:"value"`
Value interface{} `json:"value"`
}

// Validate runs all validation checks on a package constant.
Expand Down
18 changes: 12 additions & 6 deletions src/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ var devDeployCmd = &cobra.Command{
pkgConfig.CreateOpts.SetVariables = helpers.TransformAndMergeMap(
v.GetStringMapString(common.VPkgCreateSet), pkgConfig.CreateOpts.SetVariables, strings.ToUpper)

pkgConfig.PkgOpts.SetVariables = helpers.TransformAndMergeMap(
v.GetStringMapString(common.VPkgDeploySet), pkgConfig.PkgOpts.SetVariables, strings.ToUpper)
pkgCLISetVariables = helpers.TransformAndMergeMap(
v.GetStringMapString(common.VPkgDeploySet), pkgCLISetVariables, strings.ToUpper)
for k, v := range pkgCLISetVariables {
pkgConfig.PkgOpts.SetVariables[k] = v
}

pkgClient, err := packager.New(&pkgConfig)
if err != nil {
Expand Down Expand Up @@ -227,8 +230,11 @@ var devFindImagesCmd = &cobra.Command{

pkgConfig.CreateOpts.SetVariables = helpers.TransformAndMergeMap(
v.GetStringMapString(common.VPkgCreateSet), pkgConfig.CreateOpts.SetVariables, strings.ToUpper)
pkgConfig.PkgOpts.SetVariables = helpers.TransformAndMergeMap(
v.GetStringMapString(common.VPkgDeploySet), pkgConfig.PkgOpts.SetVariables, strings.ToUpper)
pkgCLISetVariables = helpers.TransformAndMergeMap(
v.GetStringMapString(common.VPkgDeploySet), pkgCLISetVariables, strings.ToUpper)
for k, v := range pkgCLISetVariables {
pkgConfig.PkgOpts.SetVariables[k] = v
}
pkgClient, err := packager.New(&pkgConfig)
if err != nil {
return err
Expand Down Expand Up @@ -311,7 +317,7 @@ func init() {
devFindImagesCmd.Flags().MarkHidden("set")
devFindImagesCmd.Flags().StringVarP(&pkgConfig.CreateOpts.Flavor, "flavor", "f", v.GetString(common.VPkgCreateFlavor), lang.CmdPackageCreateFlagFlavor)
devFindImagesCmd.Flags().StringToStringVar(&pkgConfig.CreateOpts.SetVariables, "create-set", v.GetStringMapString(common.VPkgCreateSet), lang.CmdDevFlagSet)
devFindImagesCmd.Flags().StringToStringVar(&pkgConfig.PkgOpts.SetVariables, "deploy-set", v.GetStringMapString(common.VPkgDeploySet), lang.CmdPackageDeployFlagSet)
devFindImagesCmd.Flags().StringToStringVar(&pkgCLISetVariables, "deploy-set", v.GetStringMapString(common.VPkgDeploySet), lang.CmdPackageDeployFlagSet)
// allow for the override of the default helm KubeVersion
devFindImagesCmd.Flags().StringVar(&pkgConfig.FindImagesOpts.KubeVersionOverride, "kube-version", "", lang.CmdDevFlagKubeVersion)
// check which manifests are using this particular image
Expand All @@ -334,7 +340,7 @@ func bindDevDeployFlags(v *viper.Viper) {
devDeployFlags.StringToStringVar(&pkgConfig.CreateOpts.RegistryOverrides, "registry-override", v.GetStringMapString(common.VPkgCreateRegistryOverride), lang.CmdPackageCreateFlagRegistryOverride)
devDeployFlags.StringVarP(&pkgConfig.CreateOpts.Flavor, "flavor", "f", v.GetString(common.VPkgCreateFlavor), lang.CmdPackageCreateFlagFlavor)

devDeployFlags.StringToStringVar(&pkgConfig.PkgOpts.SetVariables, "deploy-set", v.GetStringMapString(common.VPkgDeploySet), lang.CmdPackageDeployFlagSet)
devDeployFlags.StringToStringVar(&pkgCLISetVariables, "deploy-set", v.GetStringMapString(common.VPkgDeploySet), lang.CmdPackageDeployFlagSet)

// Always require adopt-existing-resources flag (no viper)
devDeployFlags.BoolVar(&pkgConfig.DeployOpts.AdoptExistingResources, "adopt-existing-resources", false, lang.CmdPackageDeployFlagAdoptExistingResources)
Expand Down
9 changes: 6 additions & 3 deletions src/cmd/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ var initCmd = &cobra.Command{
}

v := common.GetViper()
pkgConfig.PkgOpts.SetVariables = helpers.TransformAndMergeMap(
v.GetStringMapString(common.VPkgDeploySet), pkgConfig.PkgOpts.SetVariables, strings.ToUpper)
pkgCLISetVariables = helpers.TransformAndMergeMap(
v.GetStringMapString(common.VPkgDeploySet), pkgCLISetVariables, strings.ToUpper)
for k, v := range pkgCLISetVariables {
pkgConfig.PkgOpts.SetVariables[k] = v
}

pkgClient, err := packager.New(&pkgConfig, packager.WithSource(src))
if err != nil {
Expand Down Expand Up @@ -187,7 +190,7 @@ func init() {
v.SetDefault(common.VInitRegistryPushUser, types.ZarfRegistryPushUser)

// Init package set variable flags
initCmd.Flags().StringToStringVar(&pkgConfig.PkgOpts.SetVariables, "set", v.GetStringMapString(common.VPkgDeploySet), lang.CmdInitFlagSet)
initCmd.Flags().StringToStringVar(&pkgCLISetVariables, "set", v.GetStringMapString(common.VPkgDeploySet), lang.CmdInitFlagSet)

// Continue to require --confirm flag for init command to avoid accidental deployments
initCmd.Flags().BoolVar(&config.CommonOptions.Confirm, "confirm", false, lang.CmdInitFlagConfirm)
Expand Down
11 changes: 8 additions & 3 deletions src/cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"github.com/zarf-dev/zarf/src/pkg/packager"
)

var pkgCLISetVariables = map[string]string{}

var packageCmd = &cobra.Command{
Use: "package",
Aliases: []string{"p"},
Expand Down Expand Up @@ -81,8 +83,11 @@ var packageDeployCmd = &cobra.Command{
pkgConfig.PkgOpts.PackageSource = packageSource

v := common.GetViper()
pkgConfig.PkgOpts.SetVariables = helpers.TransformAndMergeMap(
v.GetStringMapString(common.VPkgDeploySet), pkgConfig.PkgOpts.SetVariables, strings.ToUpper)
pkgCLISetVariables = helpers.TransformAndMergeMap(
v.GetStringMapString(common.VPkgDeploySet), pkgCLISetVariables, strings.ToUpper)
for k, v := range pkgCLISetVariables {
pkgConfig.PkgOpts.SetVariables[k] = v
}

pkgClient, err := packager.New(&pkgConfig)
if err != nil {
Expand Down Expand Up @@ -420,7 +425,7 @@ func bindDeployFlags(v *viper.Viper) {
deployFlags.DurationVar(&pkgConfig.DeployOpts.Timeout, "timeout", v.GetDuration(common.VPkgDeployTimeout), lang.CmdPackageDeployFlagTimeout)

deployFlags.IntVar(&pkgConfig.PkgOpts.Retries, "retries", v.GetInt(common.VPkgRetries), lang.CmdPackageFlagRetries)
deployFlags.StringToStringVar(&pkgConfig.PkgOpts.SetVariables, "set", v.GetStringMapString(common.VPkgDeploySet), lang.CmdPackageDeployFlagSet)
deployFlags.StringToStringVar(&pkgCLISetVariables, "set", v.GetStringMapString(common.VPkgDeploySet), lang.CmdPackageDeployFlagSet)
deployFlags.StringVar(&pkgConfig.PkgOpts.OptionalComponents, "components", v.GetString(common.VPkgDeployComponents), lang.CmdPackageDeployFlagComponents)
deployFlags.StringVar(&pkgConfig.PkgOpts.Shasum, "shasum", v.GetString(common.VPkgDeployShasum), lang.CmdPackageDeployFlagShasum)
deployFlags.StringVar(&pkgConfig.PkgOpts.SGetKeyPath, "sget", v.GetString(common.VPkgDeploySget), lang.CmdPackageDeployFlagSget)
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/packager/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (p *Packager) getPackageYAMLHints(stage string) map[string]string {
if !present {
value = fmt.Sprintf("'%s' (default)", helpers.Truncate(variable.Default, 20, false))
} else {
value = fmt.Sprintf("'%s'", helpers.Truncate(value, 20, false))
value = fmt.Sprintf("'%s'", helpers.Truncate(fmt.Sprint(value), 20, false))
}
if variable.Sensitive {
value = "'**sanitized**'"
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/variables/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type TextTemplate struct {
Sensitive bool
AutoIndent bool
Type v1alpha1.VariableType
Value string
Value interface{}
}

// GetAllTemplates gets all of the current templates stored in the VariableConfig
Expand Down Expand Up @@ -95,7 +95,7 @@ func (vc *VariableConfig) ReplaceTextTemplate(path string) error {
// Check if the template is nil (present), use the original templateKey if not (so that it is not replaced).
value := templateKey
if template != nil {
value = template.Value
value = fmt.Sprint(template.Value)

// Check if the value is a file type and load the value contents from the file
if template.Type == v1alpha1.FileVariableType && value != "" {
Expand Down
6 changes: 3 additions & 3 deletions src/pkg/variables/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (vc *VariableConfig) GetSetVariable(name string) (variable *v1alpha1.SetVar
}

// PopulateVariables handles setting the active variables within a VariableConfig's SetVariableMap
func (vc *VariableConfig) PopulateVariables(variables []v1alpha1.InteractiveVariable, presetVariables map[string]string) error {
func (vc *VariableConfig) PopulateVariables(variables []v1alpha1.InteractiveVariable, presetVariables map[string]interface{}) error {
for name, value := range presetVariables {
vc.SetVariable(name, value, false, false, "")
}
Expand Down Expand Up @@ -64,7 +64,7 @@ func (vc *VariableConfig) PopulateVariables(variables []v1alpha1.InteractiveVari
}

// SetVariable sets a variable in a VariableConfig's SetVariableMap
func (vc *VariableConfig) SetVariable(name, value string, sensitive bool, autoIndent bool, varType v1alpha1.VariableType) {
func (vc *VariableConfig) SetVariable(name string, value interface{}, sensitive bool, autoIndent bool, varType v1alpha1.VariableType) {
vc.setVariableMap[name] = &v1alpha1.SetVariable{
Variable: v1alpha1.Variable{
Name: name,
Expand All @@ -84,7 +84,7 @@ func (vc *VariableConfig) CheckVariablePattern(name, pattern string) error {
return err
}

if r.MatchString(variable.Value) {
if r.MatchString(fmt.Sprint(variable.Value)) {
return nil
}

Expand Down
18 changes: 9 additions & 9 deletions src/pkg/variables/variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestPopulateVariables(t *testing.T) {
type test struct {
vc VariableConfig
vars []v1alpha1.InteractiveVariable
presets map[string]string
presets map[string]interface{}
wantErr bool
wantVars SetVariableMap
}
Expand All @@ -26,15 +26,15 @@ func TestPopulateVariables(t *testing.T) {
{
vc: VariableConfig{setVariableMap: SetVariableMap{}},
vars: []v1alpha1.InteractiveVariable{{Variable: v1alpha1.Variable{Name: "NAME"}}},
presets: map[string]string{},
presets: map[string]interface{}{},
wantVars: SetVariableMap{"NAME": {Variable: v1alpha1.Variable{Name: "NAME"}}},
},
{
vc: VariableConfig{setVariableMap: SetVariableMap{}},
vars: []v1alpha1.InteractiveVariable{
{Variable: v1alpha1.Variable{Name: "NAME"}, Default: "Default"},
},
presets: map[string]string{},
presets: map[string]interface{}{},
wantVars: SetVariableMap{
"NAME": {Variable: v1alpha1.Variable{Name: "NAME"}, Value: "Default"},
},
Expand All @@ -44,7 +44,7 @@ func TestPopulateVariables(t *testing.T) {
vars: []v1alpha1.InteractiveVariable{
{Variable: v1alpha1.Variable{Name: "NAME"}, Default: "Default"},
},
presets: map[string]string{"NAME": "Set"},
presets: map[string]interface{}{"NAME": "Set"},
wantVars: SetVariableMap{
"NAME": {Variable: v1alpha1.Variable{Name: "NAME"}, Value: "Set"},
},
Expand All @@ -54,7 +54,7 @@ func TestPopulateVariables(t *testing.T) {
vars: []v1alpha1.InteractiveVariable{
{Variable: v1alpha1.Variable{Name: "NAME", Sensitive: true, AutoIndent: true, Type: v1alpha1.FileVariableType}},
},
presets: map[string]string{},
presets: map[string]interface{}{},
wantVars: SetVariableMap{
"NAME": {Variable: v1alpha1.Variable{Name: "NAME", Sensitive: true, AutoIndent: true, Type: v1alpha1.FileVariableType}},
},
Expand All @@ -64,7 +64,7 @@ func TestPopulateVariables(t *testing.T) {
vars: []v1alpha1.InteractiveVariable{
{Variable: v1alpha1.Variable{Name: "NAME", Sensitive: true, AutoIndent: true, Type: v1alpha1.FileVariableType}},
},
presets: map[string]string{"NAME": "Set"},
presets: map[string]interface{}{"NAME": "Set"},
wantVars: SetVariableMap{
"NAME": {Variable: v1alpha1.Variable{Name: "NAME", Sensitive: true, AutoIndent: true, Type: v1alpha1.FileVariableType}, Value: "Set"},
},
Expand All @@ -74,7 +74,7 @@ func TestPopulateVariables(t *testing.T) {
vars: []v1alpha1.InteractiveVariable{
{Variable: v1alpha1.Variable{Name: "NAME"}, Prompt: true},
},
presets: map[string]string{},
presets: map[string]interface{}{},
wantVars: SetVariableMap{
"NAME": {Variable: v1alpha1.Variable{Name: "NAME"}, Value: "Prompt"},
},
Expand All @@ -84,7 +84,7 @@ func TestPopulateVariables(t *testing.T) {
vars: []v1alpha1.InteractiveVariable{
{Variable: v1alpha1.Variable{Name: "NAME"}, Default: "Default", Prompt: true},
},
presets: map[string]string{},
presets: map[string]interface{}{},
wantVars: SetVariableMap{
"NAME": {Variable: v1alpha1.Variable{Name: "NAME"}, Value: "Prompt"},
},
Expand All @@ -94,7 +94,7 @@ func TestPopulateVariables(t *testing.T) {
vars: []v1alpha1.InteractiveVariable{
{Variable: v1alpha1.Variable{Name: "NAME"}, Prompt: true},
},
presets: map[string]string{"NAME": "Set"},
presets: map[string]interface{}{"NAME": "Set"},
wantVars: SetVariableMap{
"NAME": {Variable: v1alpha1.Variable{Name: "NAME"}, Value: "Set"},
},
Expand Down
2 changes: 1 addition & 1 deletion src/types/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type ZarfPackageOptions struct {
// Location where the public key component of a cosign key-pair can be found
SGetKeyPath string
// Key-Value map of variable names and their corresponding values that will be used to template manifests and files in the Zarf package
SetVariables map[string]string
SetVariables map[string]interface{}
// Location where the public key component of a cosign key-pair can be found
PublicKeyPath string
// The number of retries to perform for Zarf deploy operations like image pushes or Helm installs
Expand Down
Loading