Skip to content
Permalink
Browse files
fix(dismiss): rework uninstall-with-namespace procedure
* Better logging when checking existance of release and namespace.
* Fix case when release not exists, but namespace exists and --with-namespace option specified (previously namespace was not removed in such case).
* Do not create namespace when it was not existed before running werf-dismiss.

Refs https://github.com/werf/3p-helm/pull/218/commits

Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed Sep 14, 2022
1 parent b8d862b commit 865744926b9669f794114355944223373fad9951
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
@@ -2,12 +2,14 @@ package dismiss

import (
"context"
"errors"
"fmt"
"time"

"github.com/spf13/cobra"
helm_v3 "helm.sh/helm/v3/cmd/helm"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/storage/driver"

"github.com/werf/kubedog/pkg/kube"
"github.com/werf/logboek"
@@ -240,10 +242,18 @@ func runDismiss(ctx context.Context) error {
DontFailIfNoRelease: &dontFailIfNoRelease,
})

logboek.Context(ctx).Default().LogFDetails("Using namespace: %s\n", namespace)
logboek.Context(ctx).Default().LogFDetails("Using release: %s\n", releaseName)

if cmdData.WithNamespace {
// TODO: solve lock release + delete-namespace case
return helmUninstallCmd.RunE(helmUninstallCmd, []string{releaseName})
} else {
if _, err := actionConfig.Releases.History(releaseName); errors.Is(err, driver.ErrReleaseNotFound) {
logboek.Context(ctx).Default().LogFDetails("No such release %q\n", releaseName)
return nil
}

return command_helpers.LockReleaseWrapper(ctx, releaseName, lockManager, func() error {
return helmUninstallCmd.RunE(helmUninstallCmd, []string{releaseName})
})
2 go.mod
@@ -314,6 +314,6 @@ replace k8s.io/helm => github.com/werf/helm v0.0.0-20210202111118-81e74d46da0f

replace github.com/deislabs/oras => github.com/werf/third-party-oras v0.9.1-0.20210927171747-6d045506f4c8

replace helm.sh/helm/v3 => github.com/werf/3p-helm/v3 v3.0.0-20220902145201-6265178e3c32
replace helm.sh/helm/v3 => github.com/werf/3p-helm/v3 v3.0.0-20220914162629-ff5881b99e90

replace github.com/go-git/go-git/v5 => github.com/ZauberNerd/go-git/v5 v5.4.3-0.20220315170230-29ec1bc1e5db
4 go.sum
@@ -2050,8 +2050,8 @@ github.com/vmware/govmomi v0.20.3/go.mod h1:URlwyTFZX72RmxtxuaFL2Uj3fD1JTvZdx59b
github.com/weppos/publicsuffix-go v0.4.0/go.mod h1:z3LCPQ38eedDQSwmsSRW4Y7t2L8Ln16JPQ02lHAdn5k=
github.com/weppos/publicsuffix-go v0.5.0 h1:rutRtjBJViU/YjcI5d80t4JAVvDltS6bciJg2K1HrLU=
github.com/weppos/publicsuffix-go v0.5.0/go.mod h1:z3LCPQ38eedDQSwmsSRW4Y7t2L8Ln16JPQ02lHAdn5k=
github.com/werf/3p-helm/v3 v3.0.0-20220902145201-6265178e3c32 h1:Z3XL0banUFF7IkIzY82onwWA2qiTvuQ0pBMERA/scis=
github.com/werf/3p-helm/v3 v3.0.0-20220902145201-6265178e3c32/go.mod h1:NxtE2KObf2PrzDl6SIamPFPKyAqWi10iWuvKlQn/Yao=
github.com/werf/3p-helm/v3 v3.0.0-20220914162629-ff5881b99e90 h1:Gsp+PghpTAj8vVIt68f6kEr+4qtbQsMi2/rCCSuV4ic=
github.com/werf/3p-helm/v3 v3.0.0-20220914162629-ff5881b99e90/go.mod h1:NxtE2KObf2PrzDl6SIamPFPKyAqWi10iWuvKlQn/Yao=
github.com/werf/copy-recurse v0.2.4 h1:kEyGUKhgS8WdEOjInNQKgk4lqPWzP2AgR27F3dcGsVc=
github.com/werf/copy-recurse v0.2.4/go.mod h1:KVHSQ90p19xflWW0B7BJhLBwmSbEtuxIaBnjlUYRPhk=
github.com/werf/helm v0.0.0-20210202111118-81e74d46da0f h1:81YscYTF9mmTf0ULOsCmm42YWQp+qWDzWi1HjWniZrg=
@@ -64,6 +64,9 @@ func InitActionConfig(ctx context.Context, kubeInitializer KubeInitializer, name
kubeClient.Extender = NewHelmKubeClientExtender()

actionConfig.RegistryClient = registryClient
actionConfig.Log = func(f string, a ...interface{}) {
logboek.Context(ctx).Default().LogFDetails(fmt.Sprintf("%s\n", f), a...)
}

return nil
}
@@ -20,12 +20,33 @@ type LockManager struct {
LockerWithRetry *locker_with_retry.LockerWithRetry
}

func NewLockManager(namespace string) (*LockManager, error) {
configMapName := "werf-synchronization"
type ConfigMapLocker struct {
ConfigMapName, Namespace string

Locker lockgate.Locker
}

func NewConfigMapLocker(configMapName, namespace string, locker lockgate.Locker) *ConfigMapLocker {
return &ConfigMapLocker{
ConfigMapName: configMapName,
Namespace: namespace,
Locker: locker,
}
}

if _, err := kubeutils.GetOrCreateConfigMapWithNamespaceIfNotExists(kube.Client, namespace, configMapName); err != nil {
return nil, err
func (locker *ConfigMapLocker) Acquire(lockName string, opts lockgate.AcquireOptions) (bool, lockgate.LockHandle, error) {
if _, err := kubeutils.GetOrCreateConfigMapWithNamespaceIfNotExists(kube.Client, locker.Namespace, locker.ConfigMapName); err != nil {
return false, lockgate.LockHandle{}, fmt.Errorf("unable to prepare kubernetes cm/%s in ns/%s: %w", locker.Namespace, locker.ConfigMapName, err)
}
return locker.Locker.Acquire(lockName, opts)
}

func (locker *ConfigMapLocker) Release(lock lockgate.LockHandle) error {
return locker.Locker.Release(lock)
}

func NewLockManager(namespace string) (*LockManager, error) {
configMapName := "werf-synchronization"

locker := distributed_locker.NewKubernetesLocker(
kube.DynamicClient, schema.GroupVersionResource{
@@ -34,7 +55,8 @@ func NewLockManager(namespace string) (*LockManager, error) {
Resource: "configmaps",
}, configMapName, namespace,
)
lockerWithRetry := locker_with_retry.NewLockerWithRetry(context.Background(), locker, locker_with_retry.LockerWithRetryOptions{MaxAcquireAttempts: 10, MaxReleaseAttempts: 10})
cmLocker := NewConfigMapLocker(configMapName, namespace, locker)
lockerWithRetry := locker_with_retry.NewLockerWithRetry(context.Background(), cmLocker, locker_with_retry.LockerWithRetryOptions{MaxAcquireAttempts: 10, MaxReleaseAttempts: 10})

return &LockManager{
Namespace: namespace,

0 comments on commit 8657449

Please sign in to comment.