Skip to content

Commit

Permalink
feat: deploy in multiple stages; improve 3way merge
Browse files Browse the repository at this point in the history
* 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 9a8d3ee
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 7 deletions.
1 change: 1 addition & 0 deletions cmd/werf/bundle/apply/apply.go
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions cmd/werf/bundle/render/render.go
Expand Up @@ -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),
Expand Down
3 changes: 3 additions & 0 deletions cmd/werf/converge/converge.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions cmd/werf/dismiss/dismiss.go
Expand Up @@ -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,
})
Expand Down
8 changes: 6 additions & 2 deletions cmd/werf/helm/helm.go
Expand Up @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions cmd/werf/helm/install.go
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions cmd/werf/helm/template.go
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions cmd/werf/helm/upgrade.go
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions cmd/werf/render/render.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion pkg/buildah/native_linux.go
Expand Up @@ -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
Expand Down
Expand Up @@ -6,7 +6,6 @@ import (
"os"

"github.com/werf/logboek"

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

Expand Down
2 changes: 2 additions & 0 deletions pkg/deploy/helm/annotations.go
Expand Up @@ -19,4 +19,6 @@ const (
ShowEventsAnnoName = "werf.io/show-service-messages"

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

StageWeightAnnoName = "werf.io/weight"
)
5 changes: 2 additions & 3 deletions pkg/deploy/helm/chart_extender/bundle_test.go
Expand Up @@ -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() {
Expand Down
57 changes: 57 additions & 0 deletions pkg/deploy/helm/stages_splitter.go
@@ -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.