Skip to content

Commit

Permalink
Use output params instead of manifest.txt in volume snapshot function…
Browse files Browse the repository at this point in the history
…s (#4152)

* use output params instead of manifest.txt

* address review comments
  • Loading branch information
SupriyaKasten authored and Ilya Kislenko committed Oct 17, 2018
1 parent 76ba064 commit 6fde382
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 59 deletions.
12 changes: 6 additions & 6 deletions examples/picture-gallery-pvc-snapshot/blueprint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ actions:
backup:
type: Deployment
outputArtifacts:
backupLocation:
backupInfo:
keyValue:
path: "{{ .Phases.backupVolume.Output.backupLocation }}"
manifest: "{{ .Phases.backupVolume.Output.volumeSnapshotInfo }}"
phases:
- func: CreateVolumeSnapshot
name: backupVolume
Expand All @@ -18,7 +18,7 @@ actions:
restore:
type: Deployment
inputArtifactNames:
- backupLocation
- backupInfo
phases:
- func: ScaleWorkload
name: shutdownPod
Expand All @@ -31,7 +31,7 @@ actions:
name: restoreVolume
args:
namespace: "{{ .Deployment.Namespace }}"
snapshots: "{{ .ArtifactsIn.backupLocation.KeyValue.path }}"
snapshots: "{{ .ArtifactsIn.backupInfo.KeyValue.manifest }}"
- func: ScaleWorkload
name: bringupPod
args:
Expand All @@ -42,10 +42,10 @@ actions:
delete:
type: Deployment
inputArtifactNames:
- backupLocation
- backupInfo
phases:
- func: DeleteVolumeSnapshot
name: deleteVolumeSnapshot
args:
namespace: "{{ .Deployment.Namespace }}"
snapshots: "{{ .ArtifactsIn.backupLocation.KeyValue.path }}"
snapshots: "{{ .ArtifactsIn.backupInfo.KeyValue.manifest }}"
19 changes: 7 additions & 12 deletions pkg/function/create_volume_from_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

kanister "github.com/kanisterio/kanister/pkg"
"github.com/kanisterio/kanister/pkg/kube"
"github.com/kanisterio/kanister/pkg/objectstore"
"github.com/kanisterio/kanister/pkg/param"
)

Expand All @@ -24,7 +23,7 @@ var (

const (
CreateVolumeFromSnapshotNamespaceArg = "namespace"
CreateVolumeFromSnapshotPathArg = "snapshots"
CreateVolumeFromSnapshotManifestArg = "snapshots"
)

type createVolumeFromSnapshotFunc struct{}
Expand All @@ -33,13 +32,9 @@ func (*createVolumeFromSnapshotFunc) Name() string {
return "CreateVolumeFromSnapshot"
}

func createVolumeFromSnapshot(ctx context.Context, cli kubernetes.Interface, namespace, snapshotPath string, profile *param.Profile) error {
data, err := objectstore.GetData(ctx, profile, objectstore.ProviderTypeS3, profile.Location.S3Compliant.Bucket, snapshotPath, "manifest.txt")
if err != nil {
return err
}
func createVolumeFromSnapshot(ctx context.Context, cli kubernetes.Interface, namespace, snapshotinfo string, profile *param.Profile) error {
PVCData := []VolumeSnapshotInfo{}
err = json.Unmarshal(data, &PVCData)
err := json.Unmarshal([]byte(snapshotinfo), &PVCData)
if err != nil {
return errors.Wrapf(err, "Could not decode JSON data")
}
Expand Down Expand Up @@ -81,16 +76,16 @@ func (kef *createVolumeFromSnapshotFunc) Exec(ctx context.Context, tp param.Temp
if err != nil {
return nil, errors.Wrapf(err, "Failed to create Kubernetes client")
}
var namespace, snapshots string
var namespace, snapshotinfo string
if err = Arg(args, CreateVolumeFromSnapshotNamespaceArg, &namespace); err != nil {
return nil, err
}
if err = Arg(args, CreateVolumeFromSnapshotPathArg, &snapshots); err != nil {
if err = Arg(args, CreateVolumeFromSnapshotManifestArg, &snapshotinfo); err != nil {
return nil, err
}
return nil, createVolumeFromSnapshot(ctx, cli, namespace, snapshots, tp.Profile)
return nil, createVolumeFromSnapshot(ctx, cli, namespace, snapshotinfo, tp.Profile)
}

func (*createVolumeFromSnapshotFunc) RequiredArgs() []string {
return []string{CreateVolumeFromSnapshotNamespaceArg, CreateVolumeFromSnapshotPathArg}
return []string{CreateVolumeFromSnapshotNamespaceArg, CreateVolumeFromSnapshotManifestArg}
}
28 changes: 4 additions & 24 deletions pkg/function/create_volume_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package function
import (
"context"
"encoding/json"
"path/filepath"

"github.com/pkg/errors"
"k8s.io/api/core/v1"
Expand All @@ -12,7 +11,6 @@ import (

kanister "github.com/kanisterio/kanister/pkg"
"github.com/kanisterio/kanister/pkg/kube"
"github.com/kanisterio/kanister/pkg/objectstore"
"github.com/kanisterio/kanister/pkg/param"
)

Expand Down Expand Up @@ -51,11 +49,7 @@ type volumeInfo struct {
}

func createVolumeSnapshot(ctx context.Context, tp param.TemplateParams, cli kubernetes.Interface, namespace string, pvcs []string) (map[string]interface{}, error) {
appName, err := getAppName(tp)
if err != nil {
return nil, errors.Wrapf(err, "Failed to get App name")
}
backupLocation := filepath.Join(appName, tp.Time)

PVCData := make([]VolumeSnapshotInfo, 0, len(pvcs))
for _, pvc := range pvcs {
volInfo, err := getPVCInfo(cli, namespace, pvc)
Expand All @@ -72,14 +66,12 @@ func createVolumeSnapshot(ctx context.Context, tp param.TemplateParams, cli kube
if err != nil {
return nil, errors.Wrapf(err, "Failed to encode JSON data")
}
if err = objectstore.PutData(ctx, tp.Profile, objectstore.ProviderTypeS3, tp.Profile.Location.S3Compliant.Bucket, backupLocation, "manifest.txt", manifestData); err != nil {
return nil, errors.Wrapf(err, "Failed to upload Snapshot manifest to S3")
}
return map[string]interface{}{"backupLocation": backupLocation}, nil

return map[string]interface{}{"volumeSnapshotInfo": string(manifestData)}, nil
}

func snapshotVolume(ctx context.Context, cli kubernetes.Interface, vol *volumeInfo, namespace string) (*VolumeSnapshotInfo, error) {
return &VolumeSnapshotInfo{SnapshotID: "123", StorageType: "EBS", Region: ""}, nil
return &VolumeSnapshotInfo{SnapshotID: vol.volumeID, StorageType: vol.storageType, Region: ""}, nil
}

func getPVCInfo(kubeCli kubernetes.Interface, namespace string, name string) (*volumeInfo, error) {
Expand Down Expand Up @@ -141,18 +133,6 @@ func getPVCList(tp param.TemplateParams) ([]string, error) {
return pvcList, nil
}

func getAppName(tp param.TemplateParams) (string, error) {
var appName string
switch {
case tp.Deployment != nil:
return tp.Deployment.Name, nil
case tp.StatefulSet != nil:
return tp.StatefulSet.Name, nil
default:
return appName, errors.New("Failed to get app name")
}
}

func (kef *createVolumeSnapshotFunc) Exec(ctx context.Context, tp param.TemplateParams, args map[string]interface{}) (map[string]interface{}, error) {
cli, err := kube.NewClient()
if err != nil {
Expand Down
15 changes: 7 additions & 8 deletions pkg/function/delete_volume_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

kanister "github.com/kanisterio/kanister/pkg"
"github.com/kanisterio/kanister/pkg/kube"
"github.com/kanisterio/kanister/pkg/objectstore"
"github.com/kanisterio/kanister/pkg/param"
)

Expand All @@ -22,7 +21,7 @@ var (

const (
DeleteVolumeSnapshotNamespaceArg = "namespace"
DeleteVolumeSnapshotPathArg = "snapshots"
DeleteVolumeSnapshotManifestArg = "snapshots"
)

type deleteVolumeSnapshotFunc struct{}
Expand All @@ -31,25 +30,25 @@ func (*deleteVolumeSnapshotFunc) Name() string {
return "DeleteVolumeSnapshot"
}

func deleteVolumeSnapshot(ctx context.Context, cli kubernetes.Interface, namespace, snapshotPath string, profile *param.Profile) error {
return objectstore.DeleteData(ctx, profile, objectstore.ProviderTypeS3, profile.Location.S3Compliant.Bucket, snapshotPath)
func deleteVolumeSnapshot(ctx context.Context, cli kubernetes.Interface, namespace, snapshotinfo string, profile *param.Profile) error {
return nil
}

func (kef *deleteVolumeSnapshotFunc) Exec(ctx context.Context, tp param.TemplateParams, args map[string]interface{}) (map[string]interface{}, error) {
cli, err := kube.NewClient()
if err != nil {
return nil, errors.Wrapf(err, "Failed to create Kubernetes client")
}
var namespace, snapshots string
var namespace, snapshotinfo string
if err = Arg(args, DeleteVolumeSnapshotNamespaceArg, &namespace); err != nil {
return nil, err
}
if err = Arg(args, DeleteVolumeSnapshotPathArg, &snapshots); err != nil {
if err = Arg(args, DeleteVolumeSnapshotManifestArg, &snapshotinfo); err != nil {
return nil, err
}
return nil, deleteVolumeSnapshot(ctx, cli, namespace, snapshots, tp.Profile)
return nil, deleteVolumeSnapshot(ctx, cli, namespace, snapshotinfo, tp.Profile)
}

func (*deleteVolumeSnapshotFunc) RequiredArgs() []string {
return []string{DeleteVolumeSnapshotNamespaceArg, DeleteVolumeSnapshotPathArg}
return []string{DeleteVolumeSnapshotNamespaceArg, DeleteVolumeSnapshotManifestArg}
}
17 changes: 8 additions & 9 deletions pkg/function/volume_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ func newVolumeSnapshotBlueprint() *crv1alpha1.Blueprint {
"backup": {
Kind: param.StatefulSetKind,
OutputArtifacts: map[string]crv1alpha1.Artifact{
"backupLocation": {
"backupInfo": {
KeyValue: map[string]string{
"path": "{{ .Phases.testBackupVolume.Output.backupLocation }}",
"manifest": "{{ .Phases.testBackupVolume.Output.volumeSnapshotInfo }}",
},
},
},
Expand All @@ -88,7 +88,7 @@ func newVolumeSnapshotBlueprint() *crv1alpha1.Blueprint {
"restore": {
Kind: param.StatefulSetKind,
InputArtifactNames: []string{
"backupLocation",
"backupInfo",
},
Phases: []crv1alpha1.BlueprintPhase{
{
Expand All @@ -104,7 +104,7 @@ func newVolumeSnapshotBlueprint() *crv1alpha1.Blueprint {
Func: "CreateVolumeFromSnapshot",
Args: map[string]interface{}{
CreateVolumeFromSnapshotNamespaceArg: "{{ .StatefulSet.Namespace }}",
CreateVolumeFromSnapshotPathArg: "{{ .ArtifactsIn.backupLocation.KeyValue.path }}",
CreateVolumeFromSnapshotManifestArg: "{{ .ArtifactsIn.backupInfo.KeyValue.manifest }}",
},
},
{
Expand All @@ -120,15 +120,15 @@ func newVolumeSnapshotBlueprint() *crv1alpha1.Blueprint {
"delete": {
Kind: param.StatefulSetKind,
InputArtifactNames: []string{
"backupLocation",
"backupInfo",
},
Phases: []crv1alpha1.BlueprintPhase{
{
Name: "deleteVolumeSnapshot",
Func: "DeleteVolumeSnapshot",
Args: map[string]interface{}{
DeleteVolumeSnapshotNamespaceArg: "{{ .StatefulSet.Namespace }}",
DeleteVolumeSnapshotPathArg: "{{ .ArtifactsIn.backupLocation.KeyValue.path }}",
DeleteVolumeSnapshotManifestArg: "{{ .ArtifactsIn.backupInfo.KeyValue.manifest }}",
},
},
},
Expand Down Expand Up @@ -213,7 +213,6 @@ func (s *VolumeSnapshotTestSuite) TestVolumeSnapshot(c *C) {

tp, err := param.New(ctx, s.cli, s.crCli, as)
c.Assert(err, IsNil)
tp.Profile = testutil.ObjectStoreProfileOrSkip(c)

actions := []string{"backup", "restore", "delete"}
bp := newVolumeSnapshotBlueprint()
Expand All @@ -224,12 +223,12 @@ func (s *VolumeSnapshotTestSuite) TestVolumeSnapshot(c *C) {
output, err := p.Exec(ctx, *bp, action, *tp)
if action == "backup" {
keyval := make(map[string]string)
keyval["path"] = output["backupLocation"].(string)
keyval["manifest"] = output["volumeSnapshotInfo"].(string)
artifact := crv1alpha1.Artifact{
KeyValue: keyval,
}
tp.ArtifactsIn = make(map[string]crv1alpha1.Artifact)
tp.ArtifactsIn["backupLocation"] = artifact
tp.ArtifactsIn["backupInfo"] = artifact
}
c.Assert(err, IsNil)
}
Expand Down

0 comments on commit 6fde382

Please sign in to comment.