Skip to content

Commit

Permalink
Modify delete_data to use PodRunner (#5426)
Browse files Browse the repository at this point in the history
* Modify delete_data to use PodRunner:

* Add waitForPodReady to delete_data
  • Loading branch information
DeepikaDixit authored and Ilya Kislenko committed Apr 17, 2019
1 parent cfc4885 commit 0044bfe
Showing 1 changed file with 59 additions and 51 deletions.
110 changes: 59 additions & 51 deletions pkg/function/delete_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"

"github.com/pkg/errors"
"k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"

"github.com/kanisterio/kanister/pkg"
"github.com/kanisterio/kanister/pkg/format"
Expand Down Expand Up @@ -40,6 +42,62 @@ func (*deleteDataFunc) Name() string {
return "DeleteData"
}

func deleteData(ctx context.Context, cli kubernetes.Interface, tp param.TemplateParams, reclaimSpace bool, namespace, targetPath, deleteTag, deleteIdentifier, encryptionKey string) (map[string]interface{}, error) {
options := &kube.PodOptions{
Namespace: namespace,
GenerateName: deleteDataJobPrefix,
Image: kanisterToolsImage,
Command: []string{"sh", "-c", "tail -f /dev/null"},
}
pr := kube.NewPodRunner(cli, options)
podFunc := deleteDataPodFunc(cli, tp, reclaimSpace, namespace, targetPath, deleteTag, deleteIdentifier, encryptionKey)
return pr.Run(ctx, podFunc)
}

func deleteDataPodFunc(cli kubernetes.Interface, tp param.TemplateParams, reclaimSpace bool, namespace, targetPath, deleteTag, deleteIdentifier, encryptionKey string) func(ctx context.Context, pod *v1.Pod) (map[string]interface{}, error) {
return func(ctx context.Context, pod *v1.Pod) (map[string]interface{}, error) {
// Wait for pod to reach running state
if err := kube.WaitForPodReady(ctx, cli, pod.Namespace, pod.Name); err != nil {
return nil, errors.Wrapf(err, "Failed while waiting for Pod %s to be ready", pod.Name)
}
if (deleteIdentifier != "") == (deleteTag != "") {
return nil, errors.Errorf("Require one argument: %s or %s", DeleteDataBackupIdentifierArg, DeleteDataBackupTagArg)
}
if deleteTag != "" {
cmd := restic.SnapshotsCommandByTag(tp.Profile, targetPath, deleteTag, encryptionKey)
stdout, stderr, err := kube.Exec(cli, namespace, pod.Name, pod.Spec.Containers[0].Name, cmd)
format.Log(pod.Name, pod.Spec.Containers[0].Name, stdout)
format.Log(pod.Name, pod.Spec.Containers[0].Name, stderr)
if err != nil {
return nil, errors.Wrapf(err, "Failed to forget data, could not get snapshotID from tag, Tag: %s", deleteTag)
}
deleteIdentifier, err = restic.SnapshotIDFromSnapshotLog(stdout)
if err != nil {
return nil, errors.Wrapf(err, "Failed to forget data, could not get snapshotID from tag, Tag: %s", deleteTag)
}
}
if deleteIdentifier != "" {
cmd := restic.ForgetCommandByID(tp.Profile, targetPath, deleteIdentifier, encryptionKey)
stdout, stderr, err := kube.Exec(cli, namespace, pod.Name, pod.Spec.Containers[0].Name, cmd)
format.Log(pod.Name, pod.Spec.Containers[0].Name, stdout)
format.Log(pod.Name, pod.Spec.Containers[0].Name, stderr)
if err != nil {
return nil, errors.Wrapf(err, "Failed to forget data")
}
}
if reclaimSpace {
cmd := restic.PruneCommand(tp.Profile, targetPath, encryptionKey)
stdout, stderr, err := kube.Exec(cli, namespace, pod.Name, pod.Spec.Containers[0].Name, cmd)
format.Log(pod.Name, pod.Spec.Containers[0].Name, stdout)
format.Log(pod.Name, pod.Spec.Containers[0].Name, stderr)
if err != nil {
return nil, errors.Wrapf(err, "Failed to prune data after forget")
}
}
return nil, nil
}
}

func (*deleteDataFunc) Exec(ctx context.Context, tp param.TemplateParams, args map[string]interface{}) (map[string]interface{}, error) {
var namespace, deleteArtifactPrefix, deleteIdentifier, deleteTag, encryptionKey string
var reclaimSpace bool
Expand All @@ -66,57 +124,7 @@ func (*deleteDataFunc) Exec(ctx context.Context, tp param.TemplateParams, args m
if err != nil {
return nil, errors.Wrapf(err, "Failed to create Kubernetes client")
}
pod, err := kube.CreatePod(ctx, cli, &kube.PodOptions{
Namespace: namespace,
GenerateName: deleteDataJobPrefix,
Image: kanisterToolsImage,
Command: []string{"sh", "-c", "tail -f /dev/null"},
})
if err != nil {
return nil, errors.Wrapf(err, "Failed to create pod to delete data")
}
defer kube.DeletePod(context.Background(), cli, pod)

// Wait for pod to reach running state
if err := kube.WaitForPodReady(ctx, cli, pod.Namespace, pod.Name); err != nil {
return nil, errors.Wrapf(err, "Failed while waiting for Pod %s to be ready", pod.Name)
}
if (deleteIdentifier != "") == (deleteTag != "") {
return nil, errors.Errorf("Require one argument: %s or %s", DeleteDataBackupIdentifierArg, DeleteDataBackupTagArg)
}
if deleteTag != "" {
cmd := restic.SnapshotsCommandByTag(tp.Profile, deleteArtifactPrefix, deleteTag, encryptionKey)
stdout, stderr, err := kube.Exec(cli, namespace, pod.Name, pod.Spec.Containers[0].Name, cmd)
format.Log(pod.Name, pod.Spec.Containers[0].Name, stdout)
format.Log(pod.Name, pod.Spec.Containers[0].Name, stderr)
if err != nil {
return nil, errors.Wrapf(err, "Failed to forget data, could not get snapshotID from tag, Tag: %s", deleteTag)
}
deleteIdentifier, err = restic.SnapshotIDFromSnapshotLog(stdout)
if err != nil {
return nil, errors.Wrapf(err, "Failed to forget data, could not get snapshotID from tag, Tag: %s", deleteTag)
}
}
if deleteIdentifier != "" {
cmd := restic.ForgetCommandByID(tp.Profile, deleteArtifactPrefix, deleteIdentifier, encryptionKey)
stdout, stderr, err := kube.Exec(cli, namespace, pod.Name, pod.Spec.Containers[0].Name, cmd)
format.Log(pod.Name, pod.Spec.Containers[0].Name, stdout)
format.Log(pod.Name, pod.Spec.Containers[0].Name, stderr)
if err != nil {
return nil, errors.Wrapf(err, "Failed to forget data")
}
}

if reclaimSpace {
cmd := restic.PruneCommand(tp.Profile, deleteArtifactPrefix, encryptionKey)
stdout, stderr, err := kube.Exec(cli, namespace, pod.Name, pod.Spec.Containers[0].Name, cmd)
format.Log(pod.Name, pod.Spec.Containers[0].Name, stdout)
format.Log(pod.Name, pod.Spec.Containers[0].Name, stderr)
if err != nil {
return nil, errors.Wrapf(err, "Failed to prune data after forget")
}
}
return nil, nil
return deleteData(ctx, cli, tp, reclaimSpace, namespace, deleteArtifactPrefix, deleteTag, deleteIdentifier, encryptionKey)
}

func (*deleteDataFunc) RequiredArgs() []string {
Expand Down

0 comments on commit 0044bfe

Please sign in to comment.