Skip to content

Commit

Permalink
feat: add flag --helm-timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
utkuozdemir committed May 28, 2022
1 parent 82c6f0c commit 0a6a3fb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions USAGE.md
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions internal/app/migrate.go
Expand Up @@ -3,6 +3,7 @@ package app
import (
"fmt"
"strings"
"time"

"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
Expand Down Expand Up @@ -34,6 +35,7 @@ const (
FlagStrategies = "strategies"
FlagSSHKeyAlgorithm = "ssh-key-algorithm"

FlagHelmTimeout = "helm-timeout"
FlagHelmValues = "helm-values"
FlagHelmSet = "helm-set"
FlagHelmSetString = "helm-set-string"
Expand Down Expand Up @@ -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 "+
Expand All @@ -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)
Expand All @@ -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,
Expand Down
16 changes: 10 additions & 6 deletions internal/strategy/strategy.go
Expand Up @@ -99,14 +99,15 @@ 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")

var result *multierror.Error

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)
}
Expand All @@ -123,15 +124,17 @@ 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
}

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) {
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions 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"
Expand All @@ -23,6 +25,7 @@ type Request struct {
NoProgressBar bool
SourceMountReadOnly bool
KeyAlgorithm string
HelmTimeout time.Duration
HelmValuesFiles []string
HelmValues []string
HelmFileValues []string
Expand Down

0 comments on commit 0a6a3fb

Please sign in to comment.