Skip to content

Commit

Permalink
feat(deploy): release namespace management v2
Browse files Browse the repository at this point in the history
Signed-off-by: Ilya Lesikov <ilya@lesikov.com>
  • Loading branch information
ilya-lesikov committed Jun 14, 2024
1 parent 959180e commit 88757cf
Show file tree
Hide file tree
Showing 15 changed files with 266 additions and 210 deletions.
44 changes: 37 additions & 7 deletions cmd/werf/bundle/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
helm_v3 "helm.sh/helm/v3/cmd/helm"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart/loader"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"github.com/werf/kubedog/pkg/kube"
Expand Down Expand Up @@ -229,7 +230,7 @@ func runApply(ctx context.Context) error {
}

var lockManager *lock_manager.LockManager
if m, err := lock_manager.NewLockManager(namespace); err != nil {
if m, err := lock_manager.NewLockManager(namespace, false); err != nil {
return fmt.Errorf("unable to create lock manager: %w", err)
} else {
lockManager = m
Expand Down Expand Up @@ -329,6 +330,10 @@ func runApply(ctx context.Context) error {

actionConfig.Releases.MaxHistory = *commonCmdData.ReleasesHistoryMax

if err := createReleaseNamespace(ctx, clientFactory.KubeClient(), releaseNamespace); err != nil {
return fmt.Errorf("create release namespace: %w", err)
}

return command_helpers.LockReleaseWrapper(ctx, releaseName, lockManager, func() error {
log.Default.Info(ctx, color.Style{color.Bold, color.Green}.Render("Starting release")+" %q (namespace: %q)", releaseName, releaseNamespace.Name())

Expand Down Expand Up @@ -402,7 +407,7 @@ func runApply(ctx context.Context) error {
resProcessor := resrcprocssr.NewDeployableResourcesProcessor(
deployType,
releaseName,
releaseNamespace,
releaseNamespace.Name(),
chartTree.StandaloneCRDs(),
chartTree.HookResources(),
chartTree.GeneralResources(),
Expand Down Expand Up @@ -451,10 +456,10 @@ func runApply(ctx context.Context) error {

log.Default.Info(ctx, "Constructing new deploy plan")
deployPlanBuilder := plnbuilder.NewDeployPlanBuilder(
releaseNamespace.Name(),
deployType,
taskStore,
logStore,
resProcessor.DeployableReleaseNamespaceInfo(),
resProcessor.DeployableStandaloneCRDsInfos(),
resProcessor.DeployableHookResourcesInfos(),
resProcessor.DeployableGeneralResourcesInfos(),
Expand Down Expand Up @@ -615,6 +620,7 @@ func runApply(ctx context.Context) error {
if planExecutionErr != nil && pendingReleaseCreated {
wcompops, wfailops, wcancops, criterrs, noncriterrs := runFailureDeployPlan(
ctx,
releaseNamespace.Name(),
plan,
taskStore,
resProcessor,
Expand Down Expand Up @@ -695,12 +701,35 @@ func runApply(ctx context.Context) error {
})
}

func runFailureDeployPlan(ctx context.Context, failedPlan *pln.Plan, taskStore *statestore.TaskStore, resProcessor *resrcprocssr.DeployableResourcesProcessor, newRel, prevRelease *rls.Release, history *rlshistor.History, clientFactory *kubeclnt.ClientFactory, networkParallelism int) (worthyCompletedOps, worthyFailedOps, worthyCanceledOps []opertn.Operation, criticalErrs, nonCriticalErrs []error) {
func createReleaseNamespace(ctx context.Context, kubeClient kubeclnt.KubeClienter, releaseNamespace *resrc.ReleaseNamespace) error {
if _, err := kubeClient.Get(ctx, releaseNamespace.ResourceID, kubeclnt.KubeClientGetOptions{
TryCache: true,
}); err != nil {
if errors.IsNotFound(err) {
log.Default.Info(ctx, "Creating release namespace %q", releaseNamespace.Name())

createOp := opertn.NewCreateResourceOperation(releaseNamespace.ResourceID, releaseNamespace.Unstructured(), kubeClient, opertn.CreateResourceOperationOptions{
ManageableBy: resrc.ManageableByAnyone,
})

if err := createOp.Execute(ctx); err != nil {
return fmt.Errorf("create release namespace: %w", err)
}
} else if errors.IsForbidden(err) {
} else {
return fmt.Errorf("get release namespace: %w", err)
}
}

return nil
}

func runFailureDeployPlan(ctx context.Context, releaseNamespace string, failedPlan *pln.Plan, taskStore *statestore.TaskStore, resProcessor *resrcprocssr.DeployableResourcesProcessor, newRel, prevRelease *rls.Release, history *rlshistor.History, clientFactory *kubeclnt.ClientFactory, networkParallelism int) (worthyCompletedOps, worthyFailedOps, worthyCanceledOps []opertn.Operation, criticalErrs, nonCriticalErrs []error) {
log.Default.Info(ctx, "Building failure deploy plan")
failurePlanBuilder := plnbuilder.NewDeployFailurePlanBuilder(
releaseNamespace,
failedPlan,
taskStore,
resProcessor.DeployableReleaseNamespaceInfo(),
resProcessor.DeployableHookResourcesInfos(),
resProcessor.DeployableGeneralResourcesInfos(),
newRel,
Expand Down Expand Up @@ -777,7 +806,7 @@ func runRollbackPlan(
resProcessor := resrcprocssr.NewDeployableResourcesProcessor(
helmcommon.DeployTypeRollback,
releaseName,
releaseNamespace,
releaseNamespace.Name(),
nil,
prevDeployedRelease.HookResources(),
prevDeployedRelease.GeneralResources(),
Expand Down Expand Up @@ -833,10 +862,10 @@ func runRollbackPlan(

log.Default.Info(ctx, "Constructing rollback plan")
rollbackPlanBuilder := plnbuilder.NewDeployPlanBuilder(
releaseNamespace.Name(),
helmcommon.DeployTypeRollback,
taskStore,
logStore,
resProcessor.DeployableReleaseNamespaceInfo(),
nil,
resProcessor.DeployableHookResourcesInfos(),
resProcessor.DeployableGeneralResourcesInfos(),
Expand Down Expand Up @@ -915,6 +944,7 @@ func runRollbackPlan(
if rollbackPlanExecutionErr != nil && pendingRollbackReleaseCreated {
wcompops, wfailops, wcancops, criterrs, noncriterrs := runFailureDeployPlan(
ctx,
releaseNamespace.Name(),
rollbackPlan,
taskStore,
resProcessor,
Expand Down
2 changes: 1 addition & 1 deletion cmd/werf/bundle/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ func runRender(ctx context.Context) error {
resProcessor := resrcprocssr.NewDeployableResourcesProcessor(
deployType,
releaseName,
releaseNamespace,
releaseNamespace.Name(),
chartTree.StandaloneCRDs(),
chartTree.HookResources(),
chartTree.GeneralResources(),
Expand Down
44 changes: 37 additions & 7 deletions cmd/werf/converge/converge.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"github.com/werf/kubedog/pkg/trackers/dyntracker/logstore"
Expand Down Expand Up @@ -442,7 +443,7 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
}

var lockManager *lock_manager.LockManager
if m, err := lock_manager.NewLockManager(namespace); err != nil {
if m, err := lock_manager.NewLockManager(namespace, false); err != nil {
return fmt.Errorf("unable to create lock manager: %w", err)
} else {
lockManager = m
Expand Down Expand Up @@ -567,6 +568,10 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
// actionConfig.Releases = storage.Init(mem)
// }

if err := createReleaseNamespace(ctx, clientFactory.KubeClient(), releaseNamespace); err != nil {
return fmt.Errorf("create release namespace: %w", err)
}

return command_helpers.LockReleaseWrapper(ctx, releaseName, lockManager, func() error {
log.Default.Info(ctx, color.Style{color.Bold, color.Green}.Render("Starting release")+" %q (namespace: %q)", releaseName, releaseNamespace.Name())

Expand Down Expand Up @@ -640,7 +645,7 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
resProcessor := resrcprocssr.NewDeployableResourcesProcessor(
deployType,
releaseName,
releaseNamespace,
releaseNamespace.Name(),
chartTree.StandaloneCRDs(),
chartTree.HookResources(),
chartTree.GeneralResources(),
Expand Down Expand Up @@ -689,10 +694,10 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken

log.Default.Info(ctx, "Constructing new deploy plan")
deployPlanBuilder := plnbuilder.NewDeployPlanBuilder(
releaseNamespace.Name(),
deployType,
taskStore,
logStore,
resProcessor.DeployableReleaseNamespaceInfo(),
resProcessor.DeployableStandaloneCRDsInfos(),
resProcessor.DeployableHookResourcesInfos(),
resProcessor.DeployableGeneralResourcesInfos(),
Expand Down Expand Up @@ -853,6 +858,7 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
if planExecutionErr != nil && pendingReleaseCreated {
wcompops, wfailops, wcancops, criterrs, noncriterrs := runFailureDeployPlan(
ctx,
releaseNamespace.Name(),
plan,
taskStore,
resProcessor,
Expand Down Expand Up @@ -933,12 +939,35 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
})
}

func runFailureDeployPlan(ctx context.Context, failedPlan *pln.Plan, taskStore *statestore.TaskStore, resProcessor *resrcprocssr.DeployableResourcesProcessor, newRel, prevRelease *rls.Release, history *rlshistor.History, clientFactory *kubeclnt.ClientFactory, networkParallelism int) (worthyCompletedOps, worthyFailedOps, worthyCanceledOps []opertn.Operation, criticalErrs, nonCriticalErrs []error) {
func createReleaseNamespace(ctx context.Context, kubeClient kubeclnt.KubeClienter, releaseNamespace *resrc.ReleaseNamespace) error {
if _, err := kubeClient.Get(ctx, releaseNamespace.ResourceID, kubeclnt.KubeClientGetOptions{
TryCache: true,
}); err != nil {
if errors.IsNotFound(err) {
log.Default.Info(ctx, "Creating release namespace %q", releaseNamespace.Name())

createOp := opertn.NewCreateResourceOperation(releaseNamespace.ResourceID, releaseNamespace.Unstructured(), kubeClient, opertn.CreateResourceOperationOptions{
ManageableBy: resrc.ManageableByAnyone,
})

if err := createOp.Execute(ctx); err != nil {
return fmt.Errorf("create release namespace: %w", err)
}
} else if errors.IsForbidden(err) {
} else {
return fmt.Errorf("get release namespace: %w", err)
}
}

return nil
}

func runFailureDeployPlan(ctx context.Context, releaseNamespace string, failedPlan *pln.Plan, taskStore *statestore.TaskStore, resProcessor *resrcprocssr.DeployableResourcesProcessor, newRel, prevRelease *rls.Release, history *rlshistor.History, clientFactory *kubeclnt.ClientFactory, networkParallelism int) (worthyCompletedOps, worthyFailedOps, worthyCanceledOps []opertn.Operation, criticalErrs, nonCriticalErrs []error) {
log.Default.Info(ctx, "Building failure deploy plan")
failurePlanBuilder := plnbuilder.NewDeployFailurePlanBuilder(
releaseNamespace,
failedPlan,
taskStore,
resProcessor.DeployableReleaseNamespaceInfo(),
resProcessor.DeployableHookResourcesInfos(),
resProcessor.DeployableGeneralResourcesInfos(),
newRel,
Expand Down Expand Up @@ -1015,7 +1044,7 @@ func runRollbackPlan(
resProcessor := resrcprocssr.NewDeployableResourcesProcessor(
helmcommon.DeployTypeRollback,
releaseName,
releaseNamespace,
releaseNamespace.Name(),
nil,
prevDeployedRelease.HookResources(),
prevDeployedRelease.GeneralResources(),
Expand Down Expand Up @@ -1071,10 +1100,10 @@ func runRollbackPlan(

log.Default.Info(ctx, "Constructing rollback plan")
rollbackPlanBuilder := plnbuilder.NewDeployPlanBuilder(
releaseNamespace.Name(),
helmcommon.DeployTypeRollback,
taskStore,
logStore,
resProcessor.DeployableReleaseNamespaceInfo(),
nil,
resProcessor.DeployableHookResourcesInfos(),
resProcessor.DeployableGeneralResourcesInfos(),
Expand Down Expand Up @@ -1153,6 +1182,7 @@ func runRollbackPlan(
if rollbackPlanExecutionErr != nil && pendingRollbackReleaseCreated {
wcompops, wfailops, wcancops, criterrs, noncriterrs := runFailureDeployPlan(
ctx,
releaseNamespace.Name(),
rollbackPlan,
taskStore,
resProcessor,
Expand Down
2 changes: 1 addition & 1 deletion cmd/werf/dismiss/dismiss.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func runDismiss(ctx context.Context) error {

var lockManager *lock_manager.LockManager
if !cmdData.WithNamespace {
if m, err := lock_manager.NewLockManager(namespace); err != nil {
if m, err := lock_manager.NewLockManager(namespace, false); err != nil {
return fmt.Errorf("unable to create lock manager: %w", err)
} else {
lockManager = m
Expand Down
2 changes: 1 addition & 1 deletion cmd/werf/helm/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewInstallCmd(actionConfig *action.Configuration, wc *chart_extender.WerfCh
return fmt.Errorf("unable to init werf chart: %w", err)
}

if m, err := lock_manager.NewLockManager(helm_v3.Settings.Namespace()); err != nil {
if m, err := lock_manager.NewLockManager(helm_v3.Settings.Namespace(), true); err != nil {
return fmt.Errorf("unable to create lock manager: %w", err)
} else {
return command_helpers.LockReleaseWrapper(ctx, releaseName, m, func() error {
Expand Down
2 changes: 1 addition & 1 deletion cmd/werf/helm/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewUpgradeCmd(actionConfig *action.Configuration, wc *chart_extender.WerfCh
return fmt.Errorf("unable to init werf chart: %w", err)
}

if m, err := lock_manager.NewLockManager(helm_v3.Settings.Namespace()); err != nil {
if m, err := lock_manager.NewLockManager(helm_v3.Settings.Namespace(), true); err != nil {
return fmt.Errorf("unable to create lock manager: %w", err)
} else {
return command_helpers.LockReleaseWrapper(ctx, releaseName, m, func() error {
Expand Down
Loading

0 comments on commit 88757cf

Please sign in to comment.