Skip to content

Commit

Permalink
Skip writing VolumeSnapshotClass.deletionPolicy
Browse files Browse the repository at this point in the history
  • Loading branch information
Timo Reimann committed Jun 10, 2020
1 parent d59d0b8 commit ede7d28
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 60 deletions.
14 changes: 0 additions & 14 deletions cmd/snapshots-a2b/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,6 @@ func writeVolumeSnapshotObjects(ctx context.Context, client dynamic.Interface, v

fmt.Printf("Found VolumeSnapshotClass for DigitalOcean Block Storage under name %q\n", dobsSnapClassName)

deletionPolicy, found, err := unstructured.NestedString(dobsAlphaSnapClass.Object, "deletionPolicy")
if err != nil {
return fmt.Errorf("failed to get v1alpha1/VolumeSnapshotClass.deletionPolicy: %s", err)
}
if !found {
deletionPolicy = "Delete"
}

fmt.Printf("--- Writing deletion policy %q for VolumeSnapshotClass %q\n", deletionPolicy, dobsSnapClassName)
err = volWriter.writeDeletionPolicy(deletionPolicy)
if err != nil {
return fmt.Errorf("failed to write deletion policy %q for for VolumeSnapshotClass %q: %s", deletionPolicy, dobsSnapClassName, err)
}

// VolumeSnapshotContent

alphaSnapContents, err := client.Resource(alphaSnapContentRes).List(ctx, metav1.ListOptions{})
Expand Down
39 changes: 5 additions & 34 deletions cmd/snapshots-a2b/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@ import (
const dobsStorageClassName = "do-block-storage"

type createVolSnapshotClassOpts struct {
name string
snapshotter string
isDefault bool
deletionPolicy string
snapshotter string
isDefault bool
}

func createVolSnapshotClass(opts createVolSnapshotClassOpts) *unstructured.Unstructured {
if opts.name == "" {
opts.name = dobsStorageClassName
}
if opts.snapshotter == "" {
opts.snapshotter = dobsDriverNewStyle
}
Expand All @@ -42,17 +37,13 @@ func createVolSnapshotClass(opts createVolSnapshotClassOpts) *unstructured.Unstr
"apiVersion": "snapshot.storage.k8s.io/v1alpha1",
"kind": "VolumeSnapshotClass",
"metadata": map[string]interface{}{
"name": opts.name,
"name": dobsStorageClassName,
"annotations": annotations,
},
"snapshotter": opts.snapshotter,
},
}

if opts.deletionPolicy != "" {
class.Object["deletionPolicy"] = opts.deletionPolicy
}

return class
}

Expand All @@ -73,25 +64,14 @@ func TestWriteVolumeSnapshotObjectsClass(t *testing.T) {
inVolumeSnapClass: createVolSnapshotClass(createVolSnapshotClassOpts{
snapshotter: dobsDriverNewStyle,
}),
wantErr: nil,
wantDeletePolicy: "Delete",
wantErr: nil,
},
{
name: "old style snapshotter",
inVolumeSnapClass: createVolSnapshotClass(createVolSnapshotClassOpts{
snapshotter: dobsDriverOldStyle,
}),
wantErr: nil,
wantDeletePolicy: "Delete",
},
{
name: "retain deletion policy",
inVolumeSnapClass: createVolSnapshotClass(createVolSnapshotClassOpts{
snapshotter: dobsDriverNewStyle,
deletionPolicy: "Retain",
}),
wantErr: nil,
wantDeletePolicy: "Retain",
wantErr: nil,
},
}

Expand All @@ -116,15 +96,6 @@ func TestWriteVolumeSnapshotObjectsClass(t *testing.T) {
if err != test.wantErr {
t.Fatalf("got error %q, want error: %q", err, test.wantErr)
}

if err != nil {
return
}

gotDeletionPolicy := volWriter.objBuffer.classDeletionPolicy
if gotDeletionPolicy != test.wantDeletePolicy {
t.Fatalf("got class deletion policy %q, want %q", gotDeletionPolicy, test.wantDeletePolicy)
}
})
}
}
Expand Down
14 changes: 2 additions & 12 deletions cmd/snapshots-a2b/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,6 @@ func newVolumeSnapshotWriter(directory string) (*volumeSnapshotWriter, error) {
}, nil
}

func (w *volumeSnapshotWriter) writeDeletionPolicy(policy string) error {
if w.directory == "" {
w.objBuffer.classDeletionPolicy = policy
fmt.Println(policy)
return nil
}

return w.writeFile(w.directory, fileVolumeSnapshotClassDeletionPolicy, []byte(policy))
}

func (w *volumeSnapshotWriter) writeVolumeSnapshotContent(obj v1beta1snapshot.VolumeSnapshotContent) error {
data, err := yaml.Marshal(obj)
if err != nil {
Expand All @@ -63,7 +53,7 @@ func (w *volumeSnapshotWriter) writeVolumeSnapshotContent(obj v1beta1snapshot.Vo
return nil
}

dir := filepath.Join(w.directory, "manifests", subDirectoryVolumeSnapshotContents)
dir := filepath.Join(w.directory, subDirectoryVolumeSnapshotContents)
return w.writeFile(dir, obj.ObjectMeta.Name, data)
}

Expand All @@ -79,7 +69,7 @@ func (w *volumeSnapshotWriter) writeVolumeSnapshot(obj v1beta1snapshot.VolumeSna
return nil
}

dir := filepath.Join(w.directory, "manifests", subDirectoryVolumeSnapshots)
dir := filepath.Join(w.directory, subDirectoryVolumeSnapshots)
return w.writeFile(dir, obj.ObjectMeta.Name, data)
}

Expand Down

0 comments on commit ede7d28

Please sign in to comment.