Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: added options to disable usage of default values (and secret va…
…lues)

`--disable-default-values` and `--disable-default-secret-values` options supported for:
* werf bundle publish
* werf converge
* werf render

Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed Oct 17, 2022
1 parent 68c096f commit 49425ee
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 12 deletions.
4 changes: 4 additions & 0 deletions cmd/werf/bundle/export/export.go
Expand Up @@ -122,6 +122,8 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupSetFile(&commonCmdData, cmd)
common.SetupValues(&commonCmdData, cmd)

commonCmdData.SetupDisableDefaultValues(cmd)

common.SetupReportPath(&commonCmdData, cmd)
common.SetupReportFormat(&commonCmdData, cmd)

Expand Down Expand Up @@ -322,6 +324,8 @@ func runExport(ctx context.Context, imagesToProcess build.ImagesToProcess) error
ExtraAnnotations: userExtraAnnotations,
ExtraLabels: userExtraLabels,
IgnoreInvalidAnnotationsAndLabels: true,
DisableDefaultValues: *commonCmdData.DisableDefaultValues,
DisableDefaultSecretValues: true,
})

if err := wc.SetEnv(*commonCmdData.Environment); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions cmd/werf/bundle/publish/publish.go
Expand Up @@ -111,6 +111,8 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupSetFile(&commonCmdData, cmd)
common.SetupValues(&commonCmdData, cmd)

commonCmdData.SetupDisableDefaultValues(cmd)

common.SetupReportPath(&commonCmdData, cmd)
common.SetupReportFormat(&commonCmdData, cmd)

Expand Down Expand Up @@ -329,6 +331,8 @@ func runPublish(ctx context.Context, imagesToProcess build.ImagesToProcess) erro
ExtraAnnotations: userExtraAnnotations,
ExtraLabels: userExtraLabels,
IgnoreInvalidAnnotationsAndLabels: true,
DisableDefaultValues: *commonCmdData.DisableDefaultValues,
DisableDefaultSecretValues: true,
})

if err := wc.SetEnv(*commonCmdData.Environment); err != nil {
Expand Down
26 changes: 19 additions & 7 deletions cmd/werf/common/cmd_data.go
Expand Up @@ -30,13 +30,15 @@ type CmdData struct {
HooksStatusProgressPeriodSeconds *int64
ReleasesHistoryMax *int

SetDockerConfigJsonValue *bool
Set *[]string
SetString *[]string
Values *[]string
SetFile *[]string
SecretValues *[]string
IgnoreSecretKey *bool
SetDockerConfigJsonValue *bool
Set *[]string
SetString *[]string
Values *[]string
SetFile *[]string
SecretValues *[]string
IgnoreSecretKey *bool
DisableDefaultValues *bool
DisableDefaultSecretValues *bool

WithoutImages *bool
Repo *RepoData
Expand Down Expand Up @@ -104,3 +106,13 @@ func (cmdData *CmdData) SetupWithoutImages(cmd *cobra.Command) {
cmdData.WithoutImages = new(bool)
cmd.Flags().BoolVarP(cmdData.WithoutImages, "without-images", "", util.GetBoolEnvironmentDefaultFalse("WERF_WITHOUT_IMAGES"), "Disable building of images defined in the werf.yaml (if any) and usage of such images in the .helm/templates ($WERF_WITHOUT_IMAGES or false by default — e.g. enable all images defined in the werf.yaml by default)")
}

func (cmdData *CmdData) SetupDisableDefaultValues(cmd *cobra.Command) {
cmdData.DisableDefaultValues = new(bool)
cmd.Flags().BoolVarP(cmdData.DisableDefaultValues, "disable-default-values", "", false, `Do not use values from the default .helm/values.yaml file`)
}

func (cmdData *CmdData) SetupDisableDefaultSecretValues(cmd *cobra.Command) {
cmdData.DisableDefaultSecretValues = new(bool)
cmd.Flags().BoolVarP(cmdData.DisableDefaultSecretValues, "disable-default-secret-values", "", false, `Do not use secret values from the default .helm/secret-values.yaml file`)
}
5 changes: 5 additions & 0 deletions cmd/werf/converge/converge.go
Expand Up @@ -155,6 +155,9 @@ werf converge --repo registry.mydomain.com/web --env production`,
common.SetupSecretValues(&commonCmdData, cmd)
common.SetupIgnoreSecretKey(&commonCmdData, cmd)

commonCmdData.SetupDisableDefaultValues(cmd)
commonCmdData.SetupDisableDefaultSecretValues(cmd)

common.SetupReportPath(&commonCmdData, cmd)
common.SetupReportFormat(&commonCmdData, cmd)

Expand Down Expand Up @@ -405,6 +408,8 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
ExtraAnnotations: userExtraAnnotations,
ExtraLabels: userExtraLabels,
IgnoreInvalidAnnotationsAndLabels: true,
DisableDefaultValues: *commonCmdData.DisableDefaultValues,
DisableDefaultSecretValues: *commonCmdData.DisableDefaultSecretValues,
})

if err := wc.SetEnv(*commonCmdData.Environment); err != nil {
Expand Down
5 changes: 5 additions & 0 deletions cmd/werf/render/render.go
Expand Up @@ -132,6 +132,9 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupSecretValues(&commonCmdData, cmd)
common.SetupIgnoreSecretKey(&commonCmdData, cmd)

commonCmdData.SetupDisableDefaultValues(cmd)
commonCmdData.SetupDisableDefaultSecretValues(cmd)

common.SetupReportPath(&commonCmdData, cmd)
common.SetupReportFormat(&commonCmdData, cmd)

Expand Down Expand Up @@ -374,6 +377,8 @@ func runRender(ctx context.Context, imagesToProcess build.ImagesToProcess) error
ExtraAnnotations: userExtraAnnotations,
ExtraLabels: userExtraLabels,
IgnoreInvalidAnnotationsAndLabels: false,
DisableDefaultValues: *commonCmdData.DisableDefaultValues,
DisableDefaultSecretValues: *commonCmdData.DisableDefaultSecretValues,
})

if err := wc.SetEnv(*commonCmdData.Environment); err != nil {
Expand Down
9 changes: 9 additions & 0 deletions pkg/deploy/helm/chart_extender/bundle.go
Expand Up @@ -32,6 +32,7 @@ type BundleOptions struct {
ExtraAnnotations map[string]string
ExtraLabels map[string]string
IgnoreInvalidAnnotationsAndLabels bool
DisableDefaultValues bool
}

func NewBundle(ctx context.Context, dir string, helmEnvSettings *cli.EnvSettings, registryClient *registry.Client, secretsManager *secrets_manager.SecretsManager, opts BundleOptions) (*Bundle, error) {
Expand All @@ -44,6 +45,7 @@ func NewBundle(ctx context.Context, dir string, helmEnvSettings *cli.EnvSettings
ChartExtenderServiceValuesData: helpers.NewChartExtenderServiceValuesData(),
ChartExtenderContextData: helpers.NewChartExtenderContextData(ctx),
secretsManager: secretsManager,
DisableDefaultValues: opts.DisableDefaultValues,
}

extraAnnotationsAndLabelsPostRenderer := helm.NewExtraAnnotationsAndLabelsPostRenderer(nil, nil, opts.IgnoreInvalidAnnotationsAndLabels)
Expand Down Expand Up @@ -78,6 +80,7 @@ type Bundle struct {
HelmEnvSettings *cli.EnvSettings
RegistryClient *registry.Client
BuildChartDependenciesOpts command_helpers.BuildChartDependenciesOptions
DisableDefaultValues bool

extraAnnotationsAndLabelsPostRenderer *helm.ExtraAnnotationsAndLabelsPostRenderer
secretsManager *secrets_manager.SecretsManager
Expand Down Expand Up @@ -123,6 +126,12 @@ func (bundle *Bundle) ChartLoaded(files []*chart.ChartExtenderBufferedFile) erro
}
}

bundle.HelmChart.Values = nil
if bundle.DisableDefaultValues {
logboek.Context(bundle.ChartExtenderContext).Info().LogF("Disable default werf chart values\n")
bundle.HelmChart.Values = nil
}

return nil
}

Expand Down
25 changes: 20 additions & 5 deletions pkg/deploy/helm/chart_extender/werf_chart.go
Expand Up @@ -34,8 +34,9 @@ type WerfChartOptions struct {
ExtraAnnotations map[string]string
ExtraLabels map[string]string
BuildChartDependenciesOpts command_helpers.BuildChartDependenciesOptions
DisableSecrets bool
IgnoreInvalidAnnotationsAndLabels bool
DisableDefaultValues bool
DisableDefaultSecretValues bool
}

func NewWerfChart(ctx context.Context, giterminismManager giterminism_manager.Interface, secretsManager *secrets_manager.SecretsManager, chartDir string, helmEnvSettings *cli.EnvSettings, registryClient *registry.Client, opts WerfChartOptions) *WerfChart {
Expand All @@ -44,7 +45,6 @@ func NewWerfChart(ctx context.Context, giterminismManager giterminism_manager.In
SecretValueFiles: opts.SecretValueFiles,
HelmEnvSettings: helmEnvSettings,
RegistryClient: registryClient,
DisableSecrets: opts.DisableSecrets,

GiterminismManager: giterminismManager,
SecretsManager: secretsManager,
Expand All @@ -53,6 +53,9 @@ func NewWerfChart(ctx context.Context, giterminismManager giterminism_manager.In

ChartExtenderServiceValuesData: helpers.NewChartExtenderServiceValuesData(),
ChartExtenderContextData: helpers.NewChartExtenderContextData(ctx),

DisableDefaultValues: opts.DisableDefaultValues,
DisableDefaultSecretValues: opts.DisableDefaultSecretValues,
}

wc.extraAnnotationsAndLabelsPostRenderer.Add(opts.ExtraAnnotations, opts.ExtraLabels)
Expand All @@ -74,7 +77,8 @@ type WerfChart struct {
HelmEnvSettings *cli.EnvSettings
RegistryClient *registry.Client
BuildChartDependenciesOpts command_helpers.BuildChartDependenciesOptions
DisableSecrets bool
DisableDefaultValues bool
DisableDefaultSecretValues bool

GiterminismManager giterminism_manager.Interface
SecretsManager *secrets_manager.SecretsManager
Expand All @@ -97,9 +101,14 @@ func (wc *WerfChart) ChartCreated(c *chart.Chart) error {
// ChartLoaded method for the chart.Extender interface
func (wc *WerfChart) ChartLoaded(files []*chart.ChartExtenderBufferedFile) error {
if wc.SecretsManager != nil {
if wc.DisableDefaultSecretValues {
logboek.Context(wc.ChartExtenderContext).Info().LogF("Disable default werf chart secret values\n")
}

if err := wc.SecretsRuntimeData.DecodeAndLoadSecrets(wc.ChartExtenderContext, files, wc.ChartDir, wc.GiterminismManager.ProjectDir(), wc.SecretsManager, secrets.DecodeAndLoadSecretsOptions{
GiterminismManager: wc.GiterminismManager,
CustomSecretValueFiles: wc.SecretValueFiles,
GiterminismManager: wc.GiterminismManager,
CustomSecretValueFiles: wc.SecretValueFiles,
WithoutDefaultSecretValues: wc.DisableDefaultSecretValues,
}); err != nil {
return fmt.Errorf("error decoding secrets: %w", err)
}
Expand All @@ -117,6 +126,11 @@ func (wc *WerfChart) ChartLoaded(files []*chart.ChartExtenderBufferedFile) error
Data: []byte(helpers.ChartTemplateHelpers),
})

if wc.DisableDefaultValues {
logboek.Context(wc.ChartExtenderContext).Info().LogF("Disable default werf chart values\n")
wc.HelmChart.Values = nil
}

return nil
}

Expand Down Expand Up @@ -390,6 +404,7 @@ func (wc *WerfChart) CreateNewBundle(ctx context.Context, destDir, chartVersion
return NewBundle(ctx, destDir, wc.HelmEnvSettings, wc.RegistryClient, wc.SecretsManager, BundleOptions{
BuildChartDependenciesOpts: wc.BuildChartDependenciesOpts,
IgnoreInvalidAnnotationsAndLabels: wc.extraAnnotationsAndLabelsPostRenderer.IgnoreInvalidAnnotationsAndLabels,
DisableDefaultValues: wc.DisableDefaultValues,
})
}

Expand Down

0 comments on commit 49425ee

Please sign in to comment.