Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(bundles): update helm to 3.7.1, provide compatibility with old p…
…ublished bundles

Refs #3874
  • Loading branch information
distorhead committed Nov 1, 2021
1 parent 1643aaa commit 9dc215c
Show file tree
Hide file tree
Showing 30 changed files with 2,014 additions and 198 deletions.
40 changes: 12 additions & 28 deletions cmd/werf/bundle/apply/apply.go
Expand Up @@ -6,6 +6,8 @@ import (
"path/filepath"
"time"

"github.com/werf/werf/pkg/deploy/bundles"

"github.com/werf/werf/pkg/deploy/helm/chart_extender/helpers"
"github.com/werf/werf/pkg/deploy/helm/command_helpers"

Expand Down Expand Up @@ -142,13 +144,18 @@ func runApply() error {

cmd_helm.Settings.Debug = *commonCmdData.LogDebug

registryClientHandle, err := common.NewHelmRegistryClientHandle(ctx, &commonCmdData)
helmRegistryClientHandle, err := common.NewHelmRegistryClientHandle(ctx, &commonCmdData)
if err != nil {
return fmt.Errorf("unable to create helm registry client: %s", err)
}

bundlesRegistryClient, err := common.NewBundlesRegistryClient(ctx, &commonCmdData)
if err != nil {
return err
}

actionConfig := new(action.Configuration)
if err := helm.InitActionConfig(ctx, common.GetOndemandKubeInitializer(), *commonCmdData.Namespace, cmd_helm.Settings, registryClientHandle, actionConfig, helm.InitActionConfigOptions{
if err := helm.InitActionConfig(ctx, common.GetOndemandKubeInitializer(), *commonCmdData.Namespace, cmd_helm.Settings, helmRegistryClientHandle, actionConfig, helm.InitActionConfigOptions{
StatusProgressPeriod: time.Duration(*commonCmdData.StatusProgressPeriodSeconds) * time.Second,
HooksStatusProgressPeriod: time.Duration(*commonCmdData.HooksStatusProgressPeriodSeconds) * time.Second,
KubeConfigOptions: kube.KubeConfigOptions{
Expand All @@ -161,34 +168,11 @@ func runApply() error {
return err
}

loader.GlobalLoadOptions = &loader.LoadOptions{}

// FIXME: support semver-pattern
bundleRef := fmt.Sprintf("%s:%s", repoAddress, cmdData.Tag)

bundleTmpDir := filepath.Join(werf.GetServiceDir(), "tmp", "bundles", uuid.NewV4().String())
defer os.RemoveAll(bundleTmpDir)

if err := logboek.Context(ctx).LogProcess("Pulling bundle %q", bundleRef).DoError(func() error {
if cmd := cmd_helm.NewChartPullCmd(actionConfig, logboek.Context(ctx).OutStream()); cmd != nil {
if err := cmd.RunE(cmd, []string{bundleRef}); err != nil {
return fmt.Errorf("error saving bundle to the local chart helm cache: %s", err)
}
}
return nil
}); err != nil {
return err
}

if err := logboek.Context(ctx).LogProcess("Exporting bundle %q", bundleRef).DoError(func() error {
if cmd := cmd_helm.NewChartExportCmd(actionConfig, logboek.Context(ctx).OutStream(), cmd_helm.ChartExportCmdOptions{Destination: bundleTmpDir}); cmd != nil {
if err := cmd.RunE(cmd, []string{bundleRef}); err != nil {
return fmt.Errorf("error pushing bundle %q: %s", bundleRef, err)
}
}
return nil
}); err != nil {
return err
if err := bundles.Pull(ctx, fmt.Sprintf("%s:%s", repoAddress, cmdData.Tag), bundleTmpDir, bundlesRegistryClient); err != nil {
return fmt.Errorf("unable to pull bundle: %s", err)
}

namespace := common.GetNamespace(&commonCmdData)
Expand All @@ -208,7 +192,7 @@ func runApply() error {
userExtraAnnotations["project.werf.io/env"] = *commonCmdData.Environment
}

bundle, err := chart_extender.NewBundle(ctx, bundleTmpDir, cmd_helm.Settings, registryClientHandle, chart_extender.BundleOptions{
bundle, err := chart_extender.NewBundle(ctx, bundleTmpDir, cmd_helm.Settings, helmRegistryClientHandle, chart_extender.BundleOptions{
ExtraAnnotations: userExtraAnnotations,
ExtraLabels: userExtraLabels,
})
Expand Down
48 changes: 6 additions & 42 deletions cmd/werf/bundle/download/download.go
Expand Up @@ -4,18 +4,14 @@ import (
"fmt"
"os"

"github.com/werf/werf/pkg/werf/global_warnings"
"github.com/werf/werf/pkg/deploy/bundles"

"github.com/werf/werf/pkg/deploy/helm"
"github.com/werf/werf/pkg/werf/global_warnings"

cmd_helm "helm.sh/helm/v3/cmd/helm"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart/loader"

"github.com/spf13/cobra"

"github.com/werf/logboek"

"github.com/werf/werf/cmd/werf/common"
"github.com/werf/werf/pkg/werf"
)
Expand Down Expand Up @@ -47,7 +43,7 @@ func NewCmd() *cobra.Command {
common.LogVersion()

return common.LogRunningTime(func() error {
return runApply()
return runDownload()
})
},
}
Expand Down Expand Up @@ -75,7 +71,7 @@ func NewCmd() *cobra.Command {
return cmd
}

func runApply() error {
func runDownload() error {
ctx := common.BackgroundContext()

if err := werf.Init(*commonCmdData.TmpDir, *commonCmdData.HomeDir); err != nil {
Expand All @@ -93,42 +89,10 @@ func runApply() error {

cmd_helm.Settings.Debug = *commonCmdData.LogDebug

registryClientHandle, err := common.NewHelmRegistryClientHandle(ctx, &commonCmdData)
bundlesRegistryClient, err := common.NewBundlesRegistryClient(ctx, &commonCmdData)
if err != nil {
return fmt.Errorf("unable to create helm registry client: %s", err)
}

actionConfig := new(action.Configuration)
if err := helm.InitActionConfig(ctx, nil, "", cmd_helm.Settings, registryClientHandle, actionConfig, helm.InitActionConfigOptions{}); err != nil {
return err
}

loader.GlobalLoadOptions = &loader.LoadOptions{}

// FIXME: support semver-pattern
bundleRef := fmt.Sprintf("%s:%s", repoAddress, cmdData.Tag)

if err := logboek.Context(ctx).LogProcess("Pulling bundle %q", bundleRef).DoError(func() error {
if cmd := cmd_helm.NewChartPullCmd(actionConfig, logboek.Context(ctx).OutStream()); cmd != nil {
if err := cmd.RunE(cmd, []string{bundleRef}); err != nil {
return fmt.Errorf("error saving bundle to the local chart helm cache: %s", err)
}
}
return nil
}); err != nil {
return err
}

if err := logboek.Context(ctx).LogProcess("Saving bundle into directory").DoError(func() error {
if cmd := cmd_helm.NewChartExportCmd(actionConfig, logboek.Context(ctx).OutStream(), cmd_helm.ChartExportCmdOptions{Destination: cmdData.Destination}); cmd != nil {
if err := cmd.RunE(cmd, []string{bundleRef}); err != nil {
return fmt.Errorf("error saving bundle to the directory: %s", err)
}
}
return nil
}); err != nil {
return err
}

return nil
return bundles.Pull(ctx, fmt.Sprintf("%s:%s", repoAddress, cmdData.Tag), cmdData.Destination, bundlesRegistryClient)
}
14 changes: 10 additions & 4 deletions cmd/werf/bundle/export/export.go
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"time"

"github.com/spf13/cobra"
"github.com/werf/logboek"
Expand Down Expand Up @@ -296,12 +297,12 @@ func runExport(ctx context.Context) error {
logboek.LogOptionalLn()
}

registryClientHandle, err := common.NewHelmRegistryClientHandle(ctx, &commonCmdData)
helmRegistryClientHandle, err := common.NewHelmRegistryClientHandle(ctx, &commonCmdData)
if err != nil {
return fmt.Errorf("unable to create helm registry client: %s", err)
}

wc := chart_extender.NewWerfChart(ctx, giterminismManager, nil, chartDir, cmd_helm.Settings, registryClientHandle, chart_extender.WerfChartOptions{
wc := chart_extender.NewWerfChart(ctx, giterminismManager, nil, chartDir, cmd_helm.Settings, helmRegistryClientHandle, chart_extender.WerfChartOptions{
ExtraAnnotations: userExtraAnnotations,
ExtraLabels: userExtraLabels,
})
Expand Down Expand Up @@ -338,10 +339,15 @@ func runExport(ctx context.Context) error {
FileValues: common.GetSetFile(&commonCmdData),
}

chartVersion := fmt.Sprintf("0.0.0-%d", time.Now().Unix())

p := getter.All(cmd_helm.Settings)
if vals, err := valueOpts.MergeValues(p, wc); err != nil {
vals, err := valueOpts.MergeValues(p, wc)
if err != nil {
return err
} else if _, err := wc.CreateNewBundle(ctx, cmdData.Destination, vals); err != nil {
}

if _, err := wc.CreateNewBundle(ctx, cmdData.Destination, chartVersion, vals); err != nil {
return fmt.Errorf("unable to create bundle: %s", err)
}

Expand Down
67 changes: 24 additions & 43 deletions cmd/werf/bundle/publish/publish.go
Expand Up @@ -5,6 +5,9 @@ import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/Masterminds/semver"

"helm.sh/helm/v3/pkg/getter"

Expand All @@ -15,12 +18,10 @@ import (
"github.com/werf/werf/pkg/git_repo/gitdata"
"github.com/werf/werf/pkg/werf/global_warnings"

"github.com/werf/werf/pkg/deploy/helm"

"github.com/werf/werf/pkg/deploy/bundles"
"github.com/werf/werf/pkg/deploy/helm/chart_extender"
"github.com/werf/werf/pkg/deploy/helm/chart_extender/helpers"
cmd_helm "helm.sh/helm/v3/cmd/helm"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/cli/values"
Expand Down Expand Up @@ -320,12 +321,17 @@ func runPublish(ctx context.Context) error {
logboek.LogOptionalLn()
}

registryClientHandle, err := common.NewHelmRegistryClientHandle(ctx, &commonCmdData)
helmRegistryClientHandle, err := common.NewHelmRegistryClientHandle(ctx, &commonCmdData)
if err != nil {
return fmt.Errorf("unable to create helm registry client: %s", err)
return err
}

wc := chart_extender.NewWerfChart(ctx, giterminismManager, nil, chartDir, cmd_helm.Settings, registryClientHandle, chart_extender.WerfChartOptions{
bundlesRegistryClient, err := common.NewBundlesRegistryClient(ctx, &commonCmdData)
if err != nil {
return err
}

wc := chart_extender.NewWerfChart(ctx, giterminismManager, nil, chartDir, cmd_helm.Settings, helmRegistryClientHandle, chart_extender.WerfChartOptions{
ExtraAnnotations: userExtraAnnotations,
ExtraLabels: userExtraLabels,
})
Expand Down Expand Up @@ -362,49 +368,24 @@ func runPublish(ctx context.Context) error {
FileValues: *commonCmdData.SetFile,
}

chartVersion := cmdData.Tag
if _, err := semver.NewVersion(chartVersion); err != nil {
chartVersion = fmt.Sprintf("0.0.0-%d-%s", time.Now().Unix(), chartVersion)
}

bundleTmpDir := filepath.Join(werf.GetServiceDir(), "tmp", "bundles", uuid.NewV4().String())
defer os.RemoveAll(bundleTmpDir)

p := getter.All(cmd_helm.Settings)
if vals, err := valueOpts.MergeValues(p, wc); err != nil {
vals, err := valueOpts.MergeValues(p, wc)
if err != nil {
return err
} else if bundle, err := wc.CreateNewBundle(ctx, bundleTmpDir, vals); err != nil {
return fmt.Errorf("unable to create bundle: %s", err)
} else {
loader.GlobalLoadOptions = &loader.LoadOptions{}

bundleRef := fmt.Sprintf("%s:%s", repoAddress, cmdData.Tag)

if err := logboek.Context(ctx).LogProcess("Saving bundle to the local chart helm cache").DoError(func() error {
actionConfig := new(action.Configuration)
if err := helm.InitActionConfig(ctx, nil, "", cmd_helm.Settings, registryClientHandle, actionConfig, helm.InitActionConfigOptions{}); err != nil {
return err
}

helmChartSaveCmd := cmd_helm.NewChartSaveCmd(actionConfig, logboek.Context(ctx).OutStream())
if err := helmChartSaveCmd.RunE(helmChartSaveCmd, []string{bundle.Dir, bundleRef}); err != nil {
return fmt.Errorf("error saving bundle to the local chart helm cache: %s", err)
}
return nil
}); err != nil {
return err
}

if err := logboek.Context(ctx).LogProcess("Pushing bundle %q", bundleRef).DoError(func() error {
actionConfig := new(action.Configuration)
if err := helm.InitActionConfig(ctx, nil, "", cmd_helm.Settings, registryClientHandle, actionConfig, helm.InitActionConfigOptions{}); err != nil {
return err
}
}

helmChartPushCmd := cmd_helm.NewChartPushCmd(actionConfig, logboek.Context(ctx).OutStream())
if err := helmChartPushCmd.RunE(helmChartPushCmd, []string{bundleRef}); err != nil {
return fmt.Errorf("error pushing bundle %q: %s", bundleRef, err)
}
return nil
}); err != nil {
return err
}
bundle, err := wc.CreateNewBundle(ctx, bundleTmpDir, chartVersion, vals)
if err != nil {
return fmt.Errorf("unable to create bundle: %s", err)
}

return nil
return bundles.Publish(ctx, bundle, fmt.Sprintf("%s:%s", repoAddress, cmdData.Tag), bundlesRegistryClient)
}
18 changes: 15 additions & 3 deletions cmd/werf/common/helm.go
Expand Up @@ -6,9 +6,9 @@ import (

"github.com/werf/kubedog/pkg/kube"
"github.com/werf/logboek"
"github.com/werf/werf/pkg/deploy/bundles/registry"

"github.com/werf/werf/pkg/deploy/helm"
cmd_helm "helm.sh/helm/v3/cmd/helm"
helm_v3 "helm.sh/helm/v3/cmd/helm"
"helm.sh/helm/v3/pkg/action"
)
Expand All @@ -21,10 +21,22 @@ func NewHelmRegistryClientHandle(ctx context.Context, commonCmdData *CmdData) (*
}
}

func NewActionConfig(ctx context.Context, kubeInitializer helm.KubeInitializer, namespace string, commonCmdData *CmdData, registryClientHandle *helm_v3.RegistryClientHandle) (*action.Configuration, error) {
func NewBundlesRegistryClient(ctx context.Context, commonCmdData *CmdData) (*registry.Client, error) {
debug := logboek.Context(ctx).Debug().IsAccepted()
insecure := *commonCmdData.InsecureHelmDependencies
out := logboek.Context(ctx).OutStream()

return registry.NewClient(
registry.ClientOptDebug(debug),
registry.ClientOptInsecure(insecure),
registry.ClientOptWriter(out),
)
}

func NewActionConfig(ctx context.Context, kubeInitializer helm.KubeInitializer, namespace string, commonCmdData *CmdData, registryClient *helm_v3.RegistryClientHandle) (*action.Configuration, error) {
actionConfig := new(action.Configuration)

if err := helm.InitActionConfig(ctx, kubeInitializer, namespace, cmd_helm.Settings, registryClientHandle, actionConfig, helm.InitActionConfigOptions{
if err := helm.InitActionConfig(ctx, kubeInitializer, namespace, helm_v3.Settings, registryClient, actionConfig, helm.InitActionConfigOptions{
StatusProgressPeriod: time.Duration(*commonCmdData.StatusProgressPeriodSeconds) * time.Second,
HooksStatusProgressPeriod: time.Duration(*commonCmdData.HooksStatusProgressPeriodSeconds) * time.Second,
KubeConfigOptions: kube.KubeConfigOptions{
Expand Down
14 changes: 7 additions & 7 deletions cmd/werf/converge/converge.go
Expand Up @@ -376,12 +376,12 @@ func run(ctx context.Context, containerRuntime container_runtime.ContainerRuntim
lockManager = m
}

registryClientHandle, err := common.NewHelmRegistryClientHandle(ctx, &commonCmdData)
helmRegistryClientHandle, err := common.NewHelmRegistryClientHandle(ctx, &commonCmdData)
if err != nil {
return fmt.Errorf("unable to create helm registry client: %s", err)
}

wc := chart_extender.NewWerfChart(ctx, giterminismManager, secretsManager, chartDir, cmd_helm.Settings, registryClientHandle, chart_extender.WerfChartOptions{
wc := chart_extender.NewWerfChart(ctx, giterminismManager, secretsManager, chartDir, cmd_helm.Settings, helmRegistryClientHandle, chart_extender.WerfChartOptions{
SecretValueFiles: common.GetSecretValues(&commonCmdData),
ExtraAnnotations: userExtraAnnotations,
ExtraLabels: userExtraLabels,
Expand Down Expand Up @@ -423,17 +423,17 @@ func run(ctx context.Context, containerRuntime container_runtime.ContainerRuntim
FileValues: common.GetSetFile(&commonCmdData),
}

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

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

actionConfig, err = common.NewActionConfig(ctx, common.GetOndemandKubeInitializer(), namespace, &commonCmdData, registryClientHandle)
actionConfig, err = common.NewActionConfig(ctx, common.GetOndemandKubeInitializer(), namespace, &commonCmdData, helmRegistryClientHandle)
if err != nil {
return err
}
Expand Down Expand Up @@ -485,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, chainPostRenderer func(postrender.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, helmRegistryClientHandle *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 {
Expand All @@ -512,7 +512,7 @@ func migrateHelm2ToHelm3(ctx context.Context, releaseName, namespace string, mai

logboek.Context(ctx).Default().LogOptionalLn()
if err := logboek.Context(ctx).LogProcess("Rendering helm 3 templates for the current project state").DoError(func() error {
actionConfig, err := common.NewActionConfig(ctx, common.GetOndemandKubeInitializer(), namespace, &commonCmdData, registryClientHandle)
actionConfig, err := common.NewActionConfig(ctx, common.GetOndemandKubeInitializer(), namespace, &commonCmdData, helmRegistryClientHandle)
if err != nil {
return err
}
Expand Down

0 comments on commit 9dc215c

Please sign in to comment.