From 0a6a3fb99ca81a4e871d7060e1cf2bf12c7679c4 Mon Sep 17 00:00:00 2001 From: Utku Ozdemir Date: Fri, 27 May 2022 14:56:55 +0200 Subject: [PATCH] feat: add flag --helm-timeout --- USAGE.md | 1 + internal/app/migrate.go | 5 +++++ internal/strategy/strategy.go | 16 ++++++++++------ migration/types.go | 3 +++ 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/USAGE.md b/USAGE.md index 4879974c1..6c7c56b8c 100644 --- a/USAGE.md +++ b/USAGE.md @@ -41,6 +41,7 @@ Flags: --helm-set-file strings set additional Helm values from respective files specified via the command line (can specify multiple or separate values with commas: key1=path1,key2=path2) --helm-set-string strings set additional Helm STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2) -f, --helm-values strings set additional Helm values by a YAML file or a URL (can specify multiple) + -t, --helm-timeout duration install/uninstall timeout for helm releases (default 1m0s) -h, --help help for migrate -i, --ignore-mounted do not fail if the source or destination PVC is mounted -o, --no-chown omit chown on rsync diff --git a/internal/app/migrate.go b/internal/app/migrate.go index effe64bd0..e5ab568de 100644 --- a/internal/app/migrate.go +++ b/internal/app/migrate.go @@ -3,6 +3,7 @@ package app import ( "fmt" "strings" + "time" "github.com/spf13/cobra" flag "github.com/spf13/pflag" @@ -34,6 +35,7 @@ const ( FlagStrategies = "strategies" FlagSSHKeyAlgorithm = "ssh-key-algorithm" + FlagHelmTimeout = "helm-timeout" FlagHelmValues = "helm-values" FlagHelmSet = "helm-set" FlagHelmSetString = "helm-set-string" @@ -114,6 +116,7 @@ func setMigrateCmdFlags(cmd *cobra.Command) { "By default, it is determined by used strategy and differs across strategies. "+ "Has no effect for mnt2 and local strategies") + flags.DurationP(FlagHelmTimeout, "t", 1*time.Minute, "install/uninstall timeout for helm releases") flags.StringSliceP(FlagHelmValues, "f", nil, "set additional Helm values by a YAML file or a URL (can specify multiple)") flags.StringSlice(FlagHelmSet, nil, "set additional Helm values on the command line (can specify "+ @@ -132,6 +135,7 @@ func runMigration(cmd *cobra.Command, args []string) error { noChown, _ := flags.GetBool(FlagNoChown) noProgressBar, _ := flags.GetBool(FlagNoProgressBar) sshKeyAlg, _ := flags.GetString(FlagSSHKeyAlgorithm) + helmTimeout, _ := flags.GetDuration(FlagHelmTimeout) helmValues, _ := flags.GetStringSlice(FlagHelmValues) helmSet, _ := flags.GetStringSlice(FlagHelmSet) helmSetString, _ := flags.GetStringSlice(FlagHelmSetString) @@ -149,6 +153,7 @@ func runMigration(cmd *cobra.Command, args []string) error { NoChown: noChown, NoProgressBar: noProgressBar, KeyAlgorithm: sshKeyAlg, + HelmTimeout: helmTimeout, HelmValuesFiles: helmValues, HelmValues: helmSet, HelmStringValues: helmSetString, diff --git a/internal/strategy/strategy.go b/internal/strategy/strategy.go index 64f6389d7..d1c77aabd 100644 --- a/internal/strategy/strategy.go +++ b/internal/strategy/strategy.go @@ -99,6 +99,7 @@ func cleanupAndReleaseHook(a *migration.Attempt, releaseNames []string, doneCh c func cleanup(a *migration.Attempt, releaseNames []string) { mig := a.Migration + req := mig.Request logger := a.Logger logger.Info(":broom: Cleaning up") @@ -106,7 +107,7 @@ func cleanup(a *migration.Attempt, releaseNames []string) { for _, info := range []*pvc.Info{mig.SourceInfo, mig.DestInfo} { for _, name := range releaseNames { - err := cleanupForPVC(logger, name, info) + err := cleanupForPVC(logger, name, req.HelmTimeout, info) if err != nil { result = multierror.Append(result, err) } @@ -123,7 +124,9 @@ func cleanup(a *migration.Attempt, releaseNames []string) { logger.Info(":sparkles: Cleanup done") } -func cleanupForPVC(logger *log.Entry, helmReleaseName string, pvcInfo *pvc.Info) error { +func cleanupForPVC(logger *log.Entry, helmReleaseName string, + helmUninstallTimeout time.Duration, pvcInfo *pvc.Info, +) error { ac, err := initHelmActionConfig(logger, pvcInfo) if err != nil { return err @@ -131,7 +134,7 @@ func cleanupForPVC(logger *log.Entry, helmReleaseName string, pvcInfo *pvc.Info) uninstall := action.NewUninstall(ac) uninstall.Wait = true - uninstall.Timeout = 1 * time.Minute + uninstall.Timeout = helmUninstallTimeout _, err = uninstall.Run(helmReleaseName) if err != nil && !errors.Is(err, driver.ErrReleaseNotFound) && !apierrors.IsNotFound(err) { @@ -180,13 +183,14 @@ func installHelmChart(attempt *migration.Attempt, pvcInfo *pvc.Info, name string return err } + mig := attempt.Migration + req := mig.Request + install := action.NewInstall(helmActionConfig) install.Namespace = pvcInfo.Claim.Namespace install.ReleaseName = name install.Wait = true - install.Timeout = 1 * time.Minute - - mig := attempt.Migration + install.Timeout = req.HelmTimeout vals, err := getMergedHelmValues(helmValuesFile, mig.Request) if err != nil { diff --git a/migration/types.go b/migration/types.go index a9cd71863..a8c2b6f0e 100644 --- a/migration/types.go +++ b/migration/types.go @@ -1,6 +1,8 @@ package migration import ( + "time" + log "github.com/sirupsen/logrus" "github.com/utkuozdemir/pv-migrate/internal/pvc" "helm.sh/helm/v3/pkg/chart" @@ -23,6 +25,7 @@ type Request struct { NoProgressBar bool SourceMountReadOnly bool KeyAlgorithm string + HelmTimeout time.Duration HelmValuesFiles []string HelmValues []string HelmFileValues []string