Skip to content

Commit

Permalink
feat: add `--skip-cleanup flag, improve integration tests
Browse files Browse the repository at this point in the history
Signed-off-by: Utku Ozdemir <uoz@protonmail.com>
  • Loading branch information
utkuozdemir committed Sep 25, 2023
1 parent d88127c commit 8467b78
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 34 deletions.
1 change: 1 addition & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Flags:
--lbsvc-timeout duration timeout for the load balancer service to receive an external IP. Only used by the lbsvc strategy (default 2m0s)
-o, --no-chown omit chown on rsync
-b, --no-progress-bar do not display a progress bar
-x, --skip-cleanup skip cleanup of the migration
-c, --source-context string context in the kubeconfig file of the source PVC
-k, --source-kubeconfig string path of the kubeconfig file of the source PVC
-R, --source-mount-read-only mount the source PVC in ReadOnly mode (default true)
Expand Down
4 changes: 4 additions & 0 deletions app/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
FlagDestDeleteExtraneousFiles = "dest-delete-extraneous-files"
FlagIgnoreMounted = "ignore-mounted"
FlagNoChown = "no-chown"
FlagSkipCleanup = "skip-cleanup"
FlagNoProgressBar = "no-progress-bar"
FlagSourceMountReadOnly = "source-mount-read-only"
FlagStrategies = "strategies"
Expand Down Expand Up @@ -107,6 +108,7 @@ func setMigrateCmdFlags(cmd *cobra.Command) {
flags.BoolP(FlagIgnoreMounted, "i", false,
"do not fail if the source or destination PVC is mounted")
flags.BoolP(FlagNoChown, "o", false, "omit chown on rsync")
flags.BoolP(FlagSkipCleanup, "x", false, "skip cleanup of the migration")
flags.BoolP(FlagNoProgressBar, "b", false, "do not display a progress bar")
flags.BoolP(FlagSourceMountReadOnly, "R", true, "mount the source PVC in ReadOnly mode")
flags.StringSliceP(FlagStrategies, "s", strategy.DefaultStrategies,
Expand Down Expand Up @@ -139,6 +141,7 @@ func runMigration(cmd *cobra.Command, args []string) error {
ignoreMounted, _ := flags.GetBool(FlagIgnoreMounted)
srcMountReadOnly, _ := flags.GetBool(FlagSourceMountReadOnly)
noChown, _ := flags.GetBool(FlagNoChown)
skipCleanup, _ := flags.GetBool(FlagSkipCleanup)
noProgressBar, _ := flags.GetBool(FlagNoProgressBar)
sshKeyAlg, _ := flags.GetString(FlagSSHKeyAlgorithm)
helmTimeout, _ := flags.GetDuration(FlagHelmTimeout)
Expand All @@ -158,6 +161,7 @@ func runMigration(cmd *cobra.Command, args []string) error {
IgnoreMounted: ignoreMounted,
SourceMountReadOnly: srcMountReadOnly,
NoChown: noChown,
SkipCleanup: skipCleanup,
NoProgressBar: noProgressBar,
KeyAlgorithm: sshKeyAlg,
HelmTimeout: helmTimeout,
Expand Down
1 change: 1 addition & 0 deletions helm/pv-migrate/templates/rsync/job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ spec:
- sh
- -c
- |
set -x
n=0
rc=1
retries=10
Expand Down
53 changes: 22 additions & 31 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"strings"
"testing"
"time"
"unicode"

"github.com/hashicorp/go-multierror"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -74,6 +73,10 @@ var (
checkExtraDataShellCommand = "ls " + extraDataFilePath
clearDataShellCommand = "find /volume -mindepth 1 -delete"

resourceLabels = map[string]string{
"pv-migrate-test": "true",
}

ErrPodExecStderr = errors.New("pod exec stderr")
ErrUnexpectedTypePVCWatch = errors.New("unexpected type while watching PVC")
ErrUnexpectedTypePodWatch = errors.New("unexpected type while watching pod")
Expand Down Expand Up @@ -142,8 +145,10 @@ func TestCustomRsyncArgs(t *testing.T) {
_, err := execInPod(ctx, mainClusterCli, ns1, "dest", generateExtraDataShellCommand)
assert.NoError(t, err)

cmd := fmt.Sprintf(`%s -i -n %s -N %s --helm-set rsync.extraArgs="--partial --inplace --sparse" source dest`, migrateCmdlineWithNetpols, ns1, ns1)
assert.NoError(t, runCliApp(ctx, cmd))
cmdArgs := strings.Fields(fmt.Sprintf("%s -i -n %s -N %s", migrateCmdlineWithNetpols, ns1, ns1))
cmdArgs = append(cmdArgs, "--helm-set", "rsync.extraArgs=--partial --inplace --sparse", "source", "dest")

assert.NoError(t, runCliAppWithArgs(ctx, cmdArgs...))

stdout, err := execInPod(ctx, mainClusterCli, ns1, "dest", printDataUIDGIDContentShellCommand)
assert.NoError(t, err)
Expand Down Expand Up @@ -322,7 +327,8 @@ func TestLbSvcDestHostOverride(t *testing.T) {
_, err := mainClusterCli.KubeClient.CoreV1().Services(ns1).Create(context.Background(),
&corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: svcName,
Name: svcName,
Labels: resourceLabels,
},
Spec: corev1.ServiceSpec{
Selector: map[string]string{
Expand Down Expand Up @@ -701,6 +707,7 @@ func createPod(ctx context.Context, cli *k8s.ClusterClient, namespace string, na
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: resourceLabels,
},
Spec: corev1.PodSpec{
TerminationGracePeriodSeconds: &terminationGracePeriodSeconds,
Expand Down Expand Up @@ -739,12 +746,16 @@ func createPod(ctx context.Context, cli *k8s.ClusterClient, namespace string, na
}

func createPVC(ctx context.Context, cli *k8s.ClusterClient, namespace string, name string) error {
storageClass := os.Getenv("PVMIG_TEST_STORAGE_CLASS")

pvc := corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: resourceLabels,
},
Spec: corev1.PersistentVolumeClaimSpec{
StorageClassName: &storageClass,
AccessModes: []corev1.PersistentVolumeAccessMode{
corev1.ReadWriteOnce,
},
Expand Down Expand Up @@ -926,7 +937,8 @@ func createNS(ctx context.Context, cli *k8s.ClusterClient, name string) error {
if _, err := cli.KubeClient.CoreV1().
Namespaces().Create(ctx, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Name: name,
Labels: resourceLabels,
},
}, metav1.CreateOptions{}); err != nil {
return fmt.Errorf("failed to create namespace %s: %w", name, err)
Expand All @@ -946,42 +958,21 @@ func deleteNS(ctx context.Context, cli *k8s.ClusterClient, name string) error {
}

func runCliApp(ctx context.Context, cmd string) error {
// args := []string{os.Args[0]}
// args = append(args, strings.Fields(cmd)...)
return runCliAppWithArgs(ctx, strings.Fields(cmd)...)
}

func runCliAppWithArgs(ctx context.Context, args ...string) error {
logger, err := applog.New(ctx)
if err != nil {
return fmt.Errorf("failed to create logger: %w", err)
}

cliApp := app.New(logger, "", "", "")
cliApp.SetArgs(cmdToArgs(cmd))
cliApp.SetArgs(args)

if err = cliApp.Execute(); err != nil {
return fmt.Errorf("failed to execute command: %w", err)
}

return nil
}

func cmdToArgs(cmd string) []string {
inQuote := false

cmdFields := strings.FieldsFunc(cmd, func(r rune) bool {
if r == '"' {
inQuote = !inQuote
}

return unicode.IsSpace(r) && !inQuote
})

trimmedFields := make([]string, 0, len(cmdFields))

for _, field := range cmdFields {
trimmed := strings.TrimSpace(field)
if trimmed != "" {
trimmedFields = append(trimmedFields, trimmed)
}
}

return trimmedFields
}
1 change: 1 addition & 0 deletions migration/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Request struct {
DeleteExtraneousFiles bool
IgnoreMounted bool
NoChown bool
SkipCleanup bool
NoProgressBar bool
SourceMountReadOnly bool
KeyAlgorithm string
Expand Down
Binary file modified migrator/helm-chart.tgz
Binary file not shown.
12 changes: 9 additions & 3 deletions strategy/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,16 @@ func cleanupAndReleaseHook(a *migration.Attempt, releaseNames []string, doneCh c
doneCh <- true
}

func cleanup(a *migration.Attempt, releaseNames []string) {
mig := a.Migration
func cleanup(attempt *migration.Attempt, releaseNames []string) {
if attempt.Migration.Request.SkipCleanup {
attempt.Logger.Info("🧹 Cleanup skipped")

return
}

mig := attempt.Migration
req := mig.Request
logger := a.Logger
logger := attempt.Logger
logger.Info("🧹 Cleaning up")

var result *multierror.Error
Expand Down

0 comments on commit 8467b78

Please sign in to comment.