Skip to content
Permalink
Browse files
feat(helm): werf-helm-* commands now fully support --post-renderer param
Chain werfs extra-annotations-and-labels-post-renderer with user specified post-renderer.

Refs werf/helm#108
  • Loading branch information
distorhead committed Oct 26, 2021
1 parent 077cd28 commit eb8208efbcae5e0b9779a79551f43c22e4e3fc0c
@@ -204,18 +204,18 @@ func runApply() error {
lockManager = m
}

bundle := chart_extender.NewBundle(ctx, bundleTmpDir, cmd_helm.Settings, registryClientHandle, chart_extender.BundleOptions{})
if *commonCmdData.Environment != "" {
userExtraAnnotations["project.werf.io/env"] = *commonCmdData.Environment
}

postRenderer, err := bundle.GetPostRenderer()
bundle, err := chart_extender.NewBundle(ctx, bundleTmpDir, cmd_helm.Settings, registryClientHandle, chart_extender.BundleOptions{
ExtraAnnotations: userExtraAnnotations,
ExtraLabels: userExtraLabels,
})
if err != nil {
return err
}

postRenderer.Add(userExtraAnnotations, userExtraLabels)
if *commonCmdData.Environment != "" {
postRenderer.Add(map[string]string{"project.werf.io/env": *commonCmdData.Environment}, nil)
}

if vals, err := helpers.GetBundleServiceValues(ctx, helpers.ServiceValuesOptions{
Env: *commonCmdData.Environment,
Namespace: namespace,
@@ -232,7 +232,7 @@ func runApply() error {
}

helmUpgradeCmd, _ := cmd_helm.NewUpgradeCmd(actionConfig, logboek.Context(ctx).OutStream(), cmd_helm.UpgradeCmdOptions{
PostRenderer: postRenderer,
ChainPostRenderer: bundle.ChainPostRenderer,
ValueOpts: &values.Options{
ValueFiles: common.GetValues(&commonCmdData),
StringValues: common.GetSetString(&commonCmdData),
@@ -423,18 +423,13 @@ func run(ctx context.Context, containerRuntime container_runtime.ContainerRuntim
FileValues: common.GetSetFile(&commonCmdData),
}

postRenderer, err := wc.GetPostRenderer()
if err != nil {
return err
}

actionConfig, err := common.NewActionConfig(ctx, common.GetOndemandKubeInitializer(), namespace, &commonCmdData, registryClientHandle)
if err != nil {
return err
}
maintenanceHelper := createMaintenanceHelper(ctx, actionConfig, kubeConfigOptions)

if err := migrateHelm2ToHelm3(ctx, releaseName, namespace, maintenanceHelper, postRenderer, valueOpts, filepath.Join(giterminismManager.ProjectDir(), chartDir), registryClientHandle); err != nil {
if err := migrateHelm2ToHelm3(ctx, releaseName, namespace, maintenanceHelper, wc.ChainPostRenderer, valueOpts, filepath.Join(giterminismManager.ProjectDir(), chartDir), registryClientHandle); err != nil {
return err
}

@@ -444,13 +439,13 @@ func run(ctx context.Context, containerRuntime container_runtime.ContainerRuntim
}

helmUpgradeCmd, _ := cmd_helm.NewUpgradeCmd(actionConfig, logboek.OutStream(), cmd_helm.UpgradeCmdOptions{
PostRenderer: postRenderer,
ValueOpts: valueOpts,
CreateNamespace: common.NewBool(true),
Install: common.NewBool(true),
Wait: common.NewBool(true),
Atomic: common.NewBool(cmdData.AutoRollback),
Timeout: common.NewDuration(time.Duration(cmdData.Timeout) * time.Second),
ChainPostRenderer: wc.ChainPostRenderer,
ValueOpts: valueOpts,
CreateNamespace: common.NewBool(true),
Install: common.NewBool(true),
Wait: common.NewBool(true),
Atomic: common.NewBool(cmdData.AutoRollback),
Timeout: common.NewDuration(time.Duration(cmdData.Timeout) * time.Second),
})

return command_helpers.LockReleaseWrapper(ctx, releaseName, lockManager, func() error {
@@ -490,7 +485,7 @@ func createMaintenanceHelper(ctx context.Context, actionConfig *action.Configura
return maintenance_helper.NewMaintenanceHelper(actionConfig, maintenanceOpts)
}

func migrateHelm2ToHelm3(ctx context.Context, releaseName, namespace string, maintenanceHelper *maintenance_helper.MaintenanceHelper, postRenderer postrender.PostRenderer, valueOpts *values.Options, fullChartDir string, registryClientHandle *helm_v3.RegistryClientHandle) error {
func migrateHelm2ToHelm3(ctx context.Context, releaseName, namespace string, maintenanceHelper *maintenance_helper.MaintenanceHelper, chainPostRenderer func(postrender.PostRenderer) postrender.PostRenderer, valueOpts *values.Options, fullChartDir string, registryClientHandle *helm_v3.RegistryClientHandle) error {
if helm2Exists, err := checkHelm2AvailableAndReleaseExists(ctx, releaseName, namespace, maintenanceHelper); err != nil {
return fmt.Errorf("error checking availability of helm 2 and existence of helm 2 release %q: %s", releaseName, err)
} else if !helm2Exists {
@@ -523,11 +518,11 @@ func migrateHelm2ToHelm3(ctx context.Context, releaseName, namespace string, mai
}

helmTemplateCmd, _ := cmd_helm.NewTemplateCmd(actionConfig, ioutil.Discard, cmd_helm.TemplateCmdOptions{
PostRenderer: postRenderer,
ValueOpts: valueOpts,
Validate: common.NewBool(true),
IncludeCrds: common.NewBool(true),
IsUpgrade: common.NewBool(true),
ChainPostRenderer: chainPostRenderer,
ValueOpts: valueOpts,
Validate: common.NewBool(true),
IncludeCrds: common.NewBool(true),
IsUpgrade: common.NewBool(true),
})
return helmTemplateCmd.RunE(helmTemplateCmd, []string{releaseName, fullChartDir})
}); err != nil {
@@ -22,13 +22,8 @@ import (
var installCmdData cmd_werf_common.CmdData

func NewInstallCmd(actionConfig *action.Configuration, wc *chart_extender.WerfChartStub) *cobra.Command {
postRenderer, err := wc.GetPostRenderer()
if err != nil {
panic(err.Error())
}

cmd, helmAction := cmd_helm.NewInstallCmd(actionConfig, os.Stdout, cmd_helm.InstallCmdOptions{
PostRenderer: postRenderer,
ChainPostRenderer: wc.ChainPostRenderer,
})
SetupRenderRelatedWerfChartParams(cmd, &installCmdData)

@@ -18,13 +18,8 @@ import (
var templateCmdData cmd_werf_common.CmdData

func NewTemplateCmd(actionConfig *action.Configuration, wc *chart_extender.WerfChartStub) *cobra.Command {
postRenderer, err := wc.GetPostRenderer()
if err != nil {
panic(err.Error())
}

cmd, _ := cmd_helm.NewTemplateCmd(actionConfig, os.Stdout, cmd_helm.TemplateCmdOptions{
PostRenderer: postRenderer,
ChainPostRenderer: wc.ChainPostRenderer,
})
SetupRenderRelatedWerfChartParams(cmd, &templateCmdData)

@@ -19,13 +19,8 @@ import (
var upgradeCmdData cmd_werf_common.CmdData

func NewUpgradeCmd(actionConfig *action.Configuration, wc *chart_extender.WerfChartStub) *cobra.Command {
postRenderer, err := wc.GetPostRenderer()
if err != nil {
panic(err.Error())
}

cmd, _ := cmd_helm.NewUpgradeCmd(actionConfig, os.Stdout, cmd_helm.UpgradeCmdOptions{
PostRenderer: postRenderer,
ChainPostRenderer: wc.ChainPostRenderer,
})
SetupRenderRelatedWerfChartParams(cmd, &upgradeCmdData)

@@ -382,13 +382,8 @@ func runRender(ctx context.Context) error {
SubchartExtenderFactoryFunc: func() chart.ChartExtender { return chart_extender.NewWerfSubchart() },
}

postRenderer, err := wc.GetPostRenderer()
if err != nil {
return err
}

helmTemplateCmd, _ := cmd_helm.NewTemplateCmd(actionConfig, output, cmd_helm.TemplateCmdOptions{
PostRenderer: postRenderer,
ChainPostRenderer: wc.ChainPostRenderer,
ValueOpts: &values.Options{
ValueFiles: common.GetValues(&commonCmdData),
StringValues: common.GetSetString(&commonCmdData),
2 go.mod
@@ -99,6 +99,6 @@ replace github.com/jaguilar/vt100 => github.com/tonistiigi/vt100 v0.0.0-20190402

replace k8s.io/helm => github.com/werf/helm v0.0.0-20210202111118-81e74d46da0f

replace helm.sh/helm/v3 => github.com/werf/helm/v3 v3.0.0-20210831144243-7dcadcc35336
replace helm.sh/helm/v3 => github.com/werf/helm/v3 v3.0.0-20211026074935-16d6048b0f73

replace github.com/deislabs/oras => github.com/werf/third-party-oras v0.9.1-0.20210927171747-6d045506f4c8
2 go.sum
@@ -1840,6 +1840,8 @@ github.com/werf/helm v0.0.0-20210202111118-81e74d46da0f h1:81YscYTF9mmTf0ULOsCmm
github.com/werf/helm v0.0.0-20210202111118-81e74d46da0f/go.mod h1:OMONwLWU9zEENgaVjWEX+M+xik2QakejzKHG1+6mnUo=
github.com/werf/helm/v3 v3.0.0-20210831144243-7dcadcc35336 h1:jNpooQx5IR/oFGDqLAzoQcrlOcq1/yd2u8/x9bGAttE=
github.com/werf/helm/v3 v3.0.0-20210831144243-7dcadcc35336/go.mod h1:mIIus8EOqj+obtycw3sidsR4ORr2aFDmXMSI3k+oeVY=
github.com/werf/helm/v3 v3.0.0-20211026074935-16d6048b0f73 h1:ZEzBUf6e0Qbg7oKG0cvEgPf6C6qLZpCk8uf/QgfyrjA=
github.com/werf/helm/v3 v3.0.0-20211026074935-16d6048b0f73/go.mod h1:mIIus8EOqj+obtycw3sidsR4ORr2aFDmXMSI3k+oeVY=
github.com/werf/kubedog v0.6.3-0.20210917123541-e7a881ef7261 h1:EygQrvgiNi9QqnhHETAf1XnERiILQ0cZYju2TV69tdM=
github.com/werf/kubedog v0.6.3-0.20210917123541-e7a881ef7261/go.mod h1:QQZtZEKQf9HMKjMkbkrrwX9VDf5XKsn4TVmbWjsHn7M=
github.com/werf/kubedog v0.6.3-0.20211012155517-f9f20ac58fad h1:teC4uNgoT7KQx+EIHNS/jijWGkg/EJruU9p3vf3gGhw=
@@ -9,6 +9,8 @@ import (
"path/filepath"
"text/template"

"helm.sh/helm/v3/pkg/postrender"

"github.com/werf/logboek"
"sigs.k8s.io/yaml"

@@ -29,17 +31,39 @@ import (

type BundleOptions struct {
BuildChartDependenciesOpts command_helpers.BuildChartDependenciesOptions
ExtraAnnotations map[string]string
ExtraLabels map[string]string
}

func NewBundle(ctx context.Context, dir string, helmEnvSettings *cli.EnvSettings, registryClientHandle *helm_v3.RegistryClientHandle, opts BundleOptions) *Bundle {
return &Bundle{
func NewBundle(ctx context.Context, dir string, helmEnvSettings *cli.EnvSettings, registryClientHandle *helm_v3.RegistryClientHandle, opts BundleOptions) (*Bundle, error) {
bundle := &Bundle{
Dir: dir,
HelmEnvSettings: helmEnvSettings,
RegistryClientHandle: registryClientHandle,
BuildChartDependenciesOpts: opts.BuildChartDependenciesOpts,
ChartExtenderServiceValuesData: helpers.NewChartExtenderServiceValuesData(),
ChartExtenderContextData: helpers.NewChartExtenderContextData(ctx),
}

extraAnnotationsAndLabelsPostRenderer := helm.NewExtraAnnotationsAndLabelsPostRenderer(nil, nil)

if dataMap, err := readBundleJsonMap(filepath.Join(bundle.Dir, "extra_annotations.json")); err != nil {
return nil, err
} else {
extraAnnotationsAndLabelsPostRenderer.Add(dataMap, nil)
}

if dataMap, err := readBundleJsonMap(filepath.Join(bundle.Dir, "extra_labels.json")); err != nil {
return nil, err
} else {
extraAnnotationsAndLabelsPostRenderer.Add(nil, dataMap)
}

extraAnnotationsAndLabelsPostRenderer.Add(opts.ExtraAnnotations, opts.ExtraLabels)

bundle.extraAnnotationsAndLabelsPostRenderer = extraAnnotationsAndLabelsPostRenderer

return bundle, nil
}

/*
@@ -53,26 +77,22 @@ type Bundle struct {
RegistryClientHandle *helm_v3.RegistryClientHandle
BuildChartDependenciesOpts command_helpers.BuildChartDependenciesOptions

extraAnnotationsAndLabelsPostRenderer *helm.ExtraAnnotationsAndLabelsPostRenderer

*helpers.ChartExtenderServiceValuesData
*helpers.ChartExtenderContextData
}

func (bundle *Bundle) GetPostRenderer() (*helm.ExtraAnnotationsAndLabelsPostRenderer, error) {
postRenderer := helm.NewExtraAnnotationsAndLabelsPostRenderer(nil, nil)
func (bundle *Bundle) ChainPostRenderer(postRenderer postrender.PostRenderer) postrender.PostRenderer {
var chain []postrender.PostRenderer

if dataMap, err := readBundleJsonMap(filepath.Join(bundle.Dir, "extra_annotations.json")); err != nil {
return nil, err
} else {
postRenderer.Add(dataMap, nil)
if postRenderer != nil {
chain = append(chain, postRenderer)
}

if dataMap, err := readBundleJsonMap(filepath.Join(bundle.Dir, "extra_labels.json")); err != nil {
return nil, err
} else {
postRenderer.Add(nil, dataMap)
}
chain = append(chain, bundle.extraAnnotationsAndLabelsPostRenderer)

return postRenderer, nil
return helm.NewPostRendererChain(chain...)
}

// ChartCreated method for the chart.Extender interface
@@ -11,6 +11,8 @@ import (
"strings"
"text/template"

"helm.sh/helm/v3/pkg/postrender"

"github.com/mitchellh/copystructure"
"github.com/werf/werf/pkg/deploy/secrets_manager"

@@ -226,8 +228,16 @@ func (wc *WerfChart) ReadFile(filePath string) (bool, []byte, error) {
return true, res, err
}

func (wc *WerfChart) GetPostRenderer() (*helm.ExtraAnnotationsAndLabelsPostRenderer, error) {
return wc.extraAnnotationsAndLabelsPostRenderer, nil
func (wc *WerfChart) ChainPostRenderer(postRenderer postrender.PostRenderer) postrender.PostRenderer {
var chain []postrender.PostRenderer

if postRenderer != nil {
chain = append(chain, postRenderer)
}

chain = append(chain, wc.extraAnnotationsAndLabelsPostRenderer)

return helm.NewPostRendererChain(chain...)
}

func (wc *WerfChart) SetWerfConfig(werfConfig *config.WerfConfig) error {
@@ -366,5 +376,5 @@ func (wc *WerfChart) CreateNewBundle(ctx context.Context, destDir string, inputV
}
}

return NewBundle(ctx, destDir, wc.HelmEnvSettings, wc.RegistryClientHandle, BundleOptions{BuildChartDependenciesOpts: wc.BuildChartDependenciesOpts}), nil
return NewBundle(ctx, destDir, wc.HelmEnvSettings, wc.RegistryClientHandle, BundleOptions{BuildChartDependenciesOpts: wc.BuildChartDependenciesOpts})
}
@@ -52,8 +52,16 @@ func (wc *WerfChartStub) SetupSecretValueFiles(secretValueFiles []string) {
wc.SecretValueFiles = secretValueFiles
}

func (wc *WerfChartStub) GetPostRenderer() (postrender.PostRenderer, error) {
return wc.extraAnnotationsAndLabelsPostRenderer, nil
func (wc *WerfChartStub) ChainPostRenderer(postRenderer postrender.PostRenderer) postrender.PostRenderer {
var chain []postrender.PostRenderer

if postRenderer != nil {
chain = append(chain, postRenderer)
}

chain = append(chain, wc.extraAnnotationsAndLabelsPostRenderer)

return helm.NewPostRendererChain(chain...)
}

// ChartCreated method for the chart.Extender interface
@@ -0,0 +1,31 @@
package helm

import (
"bytes"

"helm.sh/helm/v3/pkg/postrender"
)

type PostRendererChain struct {
PostRenderers []postrender.PostRenderer
}

func NewPostRendererChain(postRenderers ...postrender.PostRenderer) *PostRendererChain {
return &PostRendererChain{
PostRenderers: postRenderers,
}
}

func (chain *PostRendererChain) Run(renderedManifests *bytes.Buffer) (*bytes.Buffer, error) {
newManifests := renderedManifests

for _, pr := range chain.PostRenderers {
manifests, err := pr.Run(newManifests)
if err != nil {
return manifests, err
}
newManifests = manifests
}

return newManifests, nil
}

0 comments on commit eb8208e

Please sign in to comment.