Skip to content
Permalink
Browse files
feat: deploy in multiple stages; improve 3way merge
* Resource deployment now can happen in multiple stages.
* 3-way merge improved for install/upgrade/uninstall/rollback: resources
  deployed in previous non-successful releases accounted for in merge.

Signed-off-by: Ilya Lesikov <ilya@lesikov.com>
  • Loading branch information
ilya-lesikov committed Jun 3, 2022
1 parent bf1f2d0 commit 9a8d3ee4fb261bfa231aa76748667f45aa3afe9d
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 7 deletions.
@@ -216,6 +216,7 @@ func runApply() error {
}

helmUpgradeCmd, _ := helm_v3.NewUpgradeCmd(actionConfig, logboek.Context(ctx).OutStream(), helm_v3.UpgradeCmdOptions{
StagesSplitter: helm.StagesSplitter{},
ChainPostRenderer: bundle.ChainPostRenderer,
ValueOpts: &values.Options{
ValueFiles: common.GetValues(&commonCmdData),
@@ -225,6 +225,7 @@ func runRender(ctx context.Context) error {
}

helmTemplateCmd, _ := helm_v3.NewTemplateCmd(actionConfig, output, helm_v3.TemplateCmdOptions{
StagesSplitter: helm.StagesSplitter{},
ChainPostRenderer: bundle.ChainPostRenderer,
ValueOpts: &values.Options{
ValueFiles: common.GetValues(&commonCmdData),
@@ -23,6 +23,7 @@ import (
"github.com/werf/werf/pkg/build"
"github.com/werf/werf/pkg/config/deploy_params"
"github.com/werf/werf/pkg/container_backend"
"github.com/werf/werf/pkg/deploy/helm"
"github.com/werf/werf/pkg/deploy/helm/chart_extender"
"github.com/werf/werf/pkg/deploy/helm/chart_extender/helpers"
"github.com/werf/werf/pkg/deploy/helm/command_helpers"
@@ -446,6 +447,7 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
}

helmUpgradeCmd, _ := helm_v3.NewUpgradeCmd(actionConfig, logboek.OutStream(), helm_v3.UpgradeCmdOptions{
StagesSplitter: helm.StagesSplitter{},
ChainPostRenderer: wc.ChainPostRenderer,
ValueOpts: valueOpts,
CreateNamespace: common.NewBool(true),
@@ -525,6 +527,7 @@ func migrateHelm2ToHelm3(ctx context.Context, releaseName, namespace string, mai
}

helmTemplateCmd, _ := helm_v3.NewTemplateCmd(actionConfig, ioutil.Discard, helm_v3.TemplateCmdOptions{
StagesSplitter: helm.StagesSplitter{},
ChainPostRenderer: chainPostRenderer,
ValueOpts: valueOpts,
Validate: common.NewBool(true),
@@ -229,6 +229,7 @@ func runDismiss(ctx context.Context) error {
}

helmUninstallCmd := helm_v3.NewUninstallCmd(actionConfig, logboek.Context(ctx).OutStream(), helm_v3.UninstallCmdOptions{
StagesSplitter: helm.StagesSplitter{},
DeleteNamespace: &cmdData.WithNamespace,
DeleteHooks: &cmdData.WithHooks,
})
@@ -65,15 +65,19 @@ func NewCmd() *cobra.Command {
common.SetupInsecureHelmDependencies(&_commonCmdData, cmd)

cmd.AddCommand(
helm_v3.NewUninstallCmd(actionConfig, os.Stdout, helm_v3.UninstallCmdOptions{}),
helm_v3.NewUninstallCmd(actionConfig, os.Stdout, helm_v3.UninstallCmdOptions{
StagesSplitter: helm.StagesSplitter{},
}),
helm_v3.NewDependencyCmd(actionConfig, os.Stdout),
helm_v3.NewGetCmd(actionConfig, os.Stdout),
helm_v3.NewHistoryCmd(actionConfig, os.Stdout),
NewLintCmd(actionConfig, wc),
helm_v3.NewListCmd(actionConfig, os.Stdout),
NewTemplateCmd(actionConfig, wc),
helm_v3.NewRepoCmd(os.Stdout),
helm_v3.NewRollbackCmd(actionConfig, os.Stdout),
helm_v3.NewRollbackCmd(actionConfig, os.Stdout, helm_v3.RollbackCmdOptions{
StagesSplitter: helm.StagesSplitter{},
}),
NewInstallCmd(actionConfig, wc),
NewUpgradeCmd(actionConfig, wc),
helm_v3.NewCreateCmd(os.Stdout),
@@ -9,6 +9,7 @@ import (
"helm.sh/helm/v3/pkg/action"

"github.com/werf/werf/cmd/werf/common"
"github.com/werf/werf/pkg/deploy/helm"
"github.com/werf/werf/pkg/deploy/helm/chart_extender"
"github.com/werf/werf/pkg/deploy/helm/command_helpers"
"github.com/werf/werf/pkg/deploy/lock_manager"
@@ -18,6 +19,7 @@ var installCmdData common.CmdData

func NewInstallCmd(actionConfig *action.Configuration, wc *chart_extender.WerfChartStub) *cobra.Command {
cmd, helmAction := helm_v3.NewInstallCmd(actionConfig, os.Stdout, helm_v3.InstallCmdOptions{
StagesSplitter: helm.StagesSplitter{},
ChainPostRenderer: wc.ChainPostRenderer,
})
SetupRenderRelatedWerfChartParams(cmd, &installCmdData)
@@ -9,13 +9,15 @@ import (
"helm.sh/helm/v3/pkg/action"

"github.com/werf/werf/cmd/werf/common"
"github.com/werf/werf/pkg/deploy/helm"
"github.com/werf/werf/pkg/deploy/helm/chart_extender"
)

var templateCmdData common.CmdData

func NewTemplateCmd(actionConfig *action.Configuration, wc *chart_extender.WerfChartStub) *cobra.Command {
cmd, _ := helm_v3.NewTemplateCmd(actionConfig, os.Stdout, helm_v3.TemplateCmdOptions{
StagesSplitter: helm.StagesSplitter{},
ChainPostRenderer: wc.ChainPostRenderer,
})
SetupRenderRelatedWerfChartParams(cmd, &templateCmdData)
@@ -9,6 +9,7 @@ import (
"helm.sh/helm/v3/pkg/action"

"github.com/werf/werf/cmd/werf/common"
"github.com/werf/werf/pkg/deploy/helm"
"github.com/werf/werf/pkg/deploy/helm/chart_extender"
"github.com/werf/werf/pkg/deploy/helm/command_helpers"
"github.com/werf/werf/pkg/deploy/lock_manager"
@@ -18,6 +19,7 @@ var upgradeCmdData common.CmdData

func NewUpgradeCmd(actionConfig *action.Configuration, wc *chart_extender.WerfChartStub) *cobra.Command {
cmd, _ := helm_v3.NewUpgradeCmd(actionConfig, os.Stdout, helm_v3.UpgradeCmdOptions{
StagesSplitter: helm.StagesSplitter{},
ChainPostRenderer: wc.ChainPostRenderer,
})
SetupRenderRelatedWerfChartParams(cmd, &upgradeCmdData)
@@ -18,6 +18,7 @@ import (
"github.com/werf/werf/cmd/werf/common"
"github.com/werf/werf/pkg/build"
"github.com/werf/werf/pkg/config/deploy_params"
"github.com/werf/werf/pkg/deploy/helm"
"github.com/werf/werf/pkg/deploy/helm/chart_extender"
"github.com/werf/werf/pkg/deploy/helm/chart_extender/helpers"
"github.com/werf/werf/pkg/deploy/secrets_manager"
@@ -412,6 +413,7 @@ func runRender(ctx context.Context) error {
}

templateOpts := helm_v3.TemplateCmdOptions{
StagesSplitter: helm.StagesSplitter{},
ChainPostRenderer: wc.ChainPostRenderer,
ValueOpts: &values.Options{
ValueFiles: common.GetValues(&commonCmdData),
@@ -355,7 +355,7 @@ func (b *NativeBuildah) Rm(ctx context.Context, ref string, opts RmOpts) error {
func (b *NativeBuildah) Rmi(ctx context.Context, ref string, opts RmiOpts) error {
_, rmiErrors := b.Runtime.RemoveImages(ctx, []string{ref}, &libimage.RemoveImagesOptions{
Force: opts.Force,
//Filters: []string{"readonly=false", "intermediate=false", "dangling=true"},
// Filters: []string{"readonly=false", "intermediate=false", "dangling=true"},
})

var multiErr *multierror.Error
@@ -6,7 +6,6 @@ import (
"os"

"github.com/werf/logboek"

"github.com/werf/werf/pkg/container_backend"
)

@@ -19,4 +19,6 @@ const (
ShowEventsAnnoName = "werf.io/show-service-messages"

ReplicasOnCreationAnnoName = "werf.io/replicas-on-creation"

StageWeightAnnoName = "werf.io/weight"
)
@@ -7,11 +7,10 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/werf/werf/pkg/deploy/secrets_manager"

helm_v3 "helm.sh/helm/v3/cmd/helm"

"helm.sh/helm/v3/pkg/chart"

"github.com/werf/werf/pkg/deploy/secrets_manager"
)

var _ = Describe("Bundle", func() {
@@ -0,0 +1,57 @@
package helm

import (
"fmt"
"sort"
"strconv"

"helm.sh/helm/v3/pkg/kube"
"helm.sh/helm/v3/pkg/phasemanagers/stages"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/resource"
)

type StagesSplitter struct{}

func (s StagesSplitter) Split(resources kube.ResourceList) (stages.SortedStageList, error) {
var stageList stages.SortedStageList
if err := resources.Visit(func(res *resource.Info, err error) error {
if err != nil {
return err
}

unstructuredObj := unstructured.Unstructured{}
unstructuredObj.Object, err = runtime.DefaultUnstructuredConverter.ToUnstructured(res.Object)
if err != nil {
return fmt.Errorf("error converting object to unstructured type: %w", err)
}

var weight int
if w, ok := unstructuredObj.GetAnnotations()[StageWeightAnnoName]; ok {
weight, err = strconv.Atoi(w)
if err != nil {
return fmt.Errorf("error parsing annotation \"%s: %s\" — value should be an integer: %w", StageWeightAnnoName, w, err)
}
}

stage := stageList.StageByWeight(weight)

if stage == nil {
stage = &stages.Stage{
Weight: weight,
}
stageList = append(stageList, stage)
}

stage.DesiredResources.Append(res)

return nil
}); err != nil {
return nil, fmt.Errorf("error visiting resources list: %w", err)
}

sort.Sort(stageList)

return stageList, nil
}

0 comments on commit 9a8d3ee

Please sign in to comment.