Skip to content

Commit

Permalink
feat: implement svc strategy using helm
Browse files Browse the repository at this point in the history
Signed-off-by: Utku Ozdemir <uoz@protonmail.com>
  • Loading branch information
utkuozdemir committed Nov 14, 2021
1 parent d3a96e2 commit 9c37a5f
Showing 1 changed file with 68 additions and 3 deletions.
71 changes: 68 additions & 3 deletions internal/strategy/svc.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package strategy

import (
"github.com/utkuozdemir/pv-migrate/internal/k8s"
"github.com/utkuozdemir/pv-migrate/internal/rsync"
"github.com/utkuozdemir/pv-migrate/internal/task"
corev1 "k8s.io/api/core/v1"
"helm.sh/helm/v3/pkg/action"
"time"
)

type Svc struct {
Expand All @@ -17,11 +19,74 @@ func (r *Svc) canDo(t *task.Task) bool {
}

func (r *Svc) Run(e *task.Execution) (bool, error) {
if !r.canDo(e.Task) {
t := e.Task
if !r.canDo(t) {
return false, nil
}

sourceNs := t.Migration.Source.Namespace
destNs := t.Migration.Dest.Namespace

s := e.Task.SourceInfo
d := e.Task.DestInfo

helmActionConfig, err := initHelmActionConfig(e.Logger, e.Task.DestInfo)
if err != nil {
return true, err
}

install := action.NewInstall(helmActionConfig)
install.Namespace = destNs
install.ReleaseName = e.HelmReleaseName
install.Wait = true
install.Timeout = 1 * time.Minute

t.Logger.Info(":key: Generating SSH key pair")
keyAlgorithm := t.Migration.Options.KeyAlgorithm
publicKey, privateKey, err := rsync.CreateSSHKeyPair(keyAlgorithm)
privateKeyMountPath := "/root/.ssh/id_" + keyAlgorithm
if err != nil {
return true, err
}

opts := t.Migration.Options
vals := map[string]interface{}{
"rsync": map[string]interface{}{
"enabled": true,
"deleteExtraneousFiles": opts.DeleteExtraneousFiles,
"noChown": opts.NoChown,
"privateKeyMount": true,
"privateKey": privateKey,
"privateKeyMountPath": privateKeyMountPath,
},
"sshd": map[string]interface{}{
"enabled": true,
"publicKey": publicKey,
},
"source": map[string]interface{}{
"namespace": sourceNs,
"pvcName": s.Claim.Name,
"pvcMountReadOnly": opts.SourceMountReadOnly,
"path": t.Migration.Source.Path,
},
"dest": map[string]interface{}{
"namespace": destNs,
"pvcName": d.Claim.Name,
"path": t.Migration.Dest.Path,
},
}

_, err = install.Run(t.Chart, vals)
if err != nil {
return true, err
}

doneCh := registerCleanupHook(e)
defer cleanupAndReleaseHook(e, doneCh)
return true, rsync.RunRsyncJobOverSSH(e, corev1.ServiceTypeClusterIP)

showProgressBar := !opts.NoProgressBar
kubeClient := t.SourceInfo.ClusterClient.KubeClient
jobName := e.HelmReleaseName + "-rsync"
err = k8s.WaitUntilJobIsCompleted(e.Logger, kubeClient, destNs, jobName, showProgressBar)
return true, err
}

0 comments on commit 9c37a5f

Please sign in to comment.