forked from kubernetes-csi/external-snapshotter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Link PVCs and PVs in VolumeGroupSnapshot and VolumeGroupSnapshotContent
This use the update API to set `persistentVolumeClaimRef` in `VolumeGroupSnapshot` and `persistentVolumeName` in `VolumeGroupSnapshotContent` to the corresponding objects. This makes restoring volumes from a VolumeGroupSnapshot easier. Related: kubernetes-csi#969
- Loading branch information
1 parent
7c8516f
commit 8cf3ea5
Showing
5 changed files
with
287 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package utils | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// SnapshotInfo contains basic information about a volume being snapshotted | ||
type SnapshotInfo struct { | ||
VolumeHandle string `json:"volumeHandle"` | ||
PVName string `json:"pvName"` | ||
PVCName string `json:"pvcName"` | ||
} | ||
|
||
// SnapshotInfoList contains basic information about a set of volumes being snapshotted | ||
type SnapshotInfoList []SnapshotInfo | ||
|
||
// ToJSON serizalizes to JSON a set of SnapshotInfo | ||
func (data SnapshotInfoList) ToJSON() (string, error) { | ||
result, err := json.Marshal(data) | ||
if err != nil { | ||
err = fmt.Errorf("while serializing SnapshotInfoList: %w", err) | ||
} | ||
return string(result), err | ||
} | ||
|
||
// SnapshotInfoFromJSON deserializes from JSON a set of snapshot info | ||
func SnapshotInfoFromJSON(content string) (SnapshotInfoList, error) { | ||
var result SnapshotInfoList | ||
|
||
err := json.Unmarshal([]byte(content), &result) | ||
if err != nil { | ||
err = fmt.Errorf("while de-serializing SnapshotInfoList: %w", err) | ||
} | ||
|
||
return result, err | ||
} | ||
|
||
// GetFromVolumeHandle gets the entry from the list corresponding to a certain | ||
// volume handle. Returns an empty SnapshotInfo if there is no such entry | ||
func (data SnapshotInfoList) GetFromVolumeHandle(volumeHandle string) SnapshotInfo { | ||
for i := range data { | ||
if data[i].VolumeHandle == volumeHandle { | ||
return data[i] | ||
} | ||
} | ||
|
||
return SnapshotInfo{} | ||
} | ||
|
||
// GetFromPVName gets the entry from the list corresponding to a certain | ||
// PV name. Returns an empty SnapshotInfo if there is no such entry | ||
func (data SnapshotInfoList) GetFromPVName(pvName string) SnapshotInfo { | ||
for i := range data { | ||
if data[i].PVName == pvName { | ||
return data[i] | ||
} | ||
} | ||
|
||
return SnapshotInfo{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package utils | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
var ( | ||
firstInfoListEntry = SnapshotInfo{ | ||
VolumeHandle: "b98b9100-fbe2-11ee-b405-b2139ff66f78", | ||
PVName: "pvc-f1718d88-e548-480b-bee8-cbfc47faaf59", | ||
PVCName: "cluster-example-1", | ||
} | ||
|
||
secondInfoListEntry = SnapshotInfo{ | ||
VolumeHandle: "b98c3e51-fbe2-11ee-b405-b2139ff66f788", | ||
PVName: "pvc-c59c9c0f-159a-43d6-9c60-61ccaf03158c", | ||
PVCName: "cluster-example-1-wal", | ||
} | ||
|
||
infoList = SnapshotInfoList{firstInfoListEntry, secondInfoListEntry} | ||
) | ||
|
||
func TestMarshalUnmarshal(t *testing.T) { | ||
jsonContent, err := infoList.ToJSON() | ||
if err != nil { | ||
t.Fatalf("JSON serialization failed: %v", err) | ||
} | ||
|
||
unmarshalledInfoList, err := SnapshotInfoFromJSON(jsonContent) | ||
if err != nil { | ||
t.Fatalf("JSON deserialization failed: %v", err) | ||
} | ||
|
||
if !reflect.DeepEqual(infoList, unmarshalledInfoList) { | ||
t.Fatalf("unexpected info loss in serialization/deserialization: %v %v", infoList, unmarshalledInfoList) | ||
} | ||
} | ||
|
||
func TestEmptyMarshalUnmarshal(t *testing.T) { | ||
var emptyInfoList SnapshotInfoList = nil | ||
jsonContent, err := emptyInfoList.ToJSON() | ||
if err != nil { | ||
t.Fatalf("JSON serialization failed: %v", err) | ||
} | ||
|
||
unmarshalledInfoList, err := SnapshotInfoFromJSON(jsonContent) | ||
if err != nil { | ||
t.Fatalf("JSON deserialization failed: %v", err) | ||
} | ||
|
||
if !reflect.DeepEqual(emptyInfoList, unmarshalledInfoList) { | ||
t.Fatalf("unexpected info loss in serialization/deserialization: %v %v", infoList, unmarshalledInfoList) | ||
} | ||
} | ||
|
||
func TestGetFromVolumeHandle(t *testing.T) { | ||
testcases := []struct { | ||
volumeHandle string | ||
result SnapshotInfo | ||
infoList SnapshotInfoList | ||
}{ | ||
{ | ||
volumeHandle: firstInfoListEntry.VolumeHandle, | ||
result: firstInfoListEntry, | ||
infoList: infoList, | ||
}, | ||
{ | ||
volumeHandle: secondInfoListEntry.VolumeHandle, | ||
result: secondInfoListEntry, | ||
infoList: infoList, | ||
}, | ||
{ | ||
volumeHandle: "<non-existing>", | ||
result: SnapshotInfo{}, | ||
infoList: infoList, | ||
}, | ||
{ | ||
volumeHandle: "<non-existing>", | ||
result: SnapshotInfo{}, | ||
infoList: nil, | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
t.Logf("looking for %s:", tc.volumeHandle) | ||
result := tc.infoList.GetFromVolumeHandle(tc.volumeHandle) | ||
if !reflect.DeepEqual(result, tc.result) { | ||
t.Fatalf("unexpected GetFromVolumeHandle result: %v %v", result, tc.result) | ||
} | ||
} | ||
} | ||
|
||
func TestGetFromPVName(t *testing.T) { | ||
testcases := []struct { | ||
pvName string | ||
result SnapshotInfo | ||
infoList SnapshotInfoList | ||
}{ | ||
{ | ||
pvName: firstInfoListEntry.PVName, | ||
result: firstInfoListEntry, | ||
infoList: infoList, | ||
}, | ||
{ | ||
pvName: secondInfoListEntry.PVName, | ||
result: secondInfoListEntry, | ||
infoList: infoList, | ||
}, | ||
{ | ||
pvName: "<non-existing>", | ||
result: SnapshotInfo{}, | ||
infoList: infoList, | ||
}, | ||
{ | ||
pvName: "<non-existing>", | ||
result: SnapshotInfo{}, | ||
infoList: nil, | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
t.Logf("looking for %s:", tc.pvName) | ||
result := tc.infoList.GetFromPVName(tc.pvName) | ||
if !reflect.DeepEqual(result, tc.result) { | ||
t.Fatalf("unexpected GetFromPVName result: %v %v", result, tc.result) | ||
} | ||
} | ||
} |
Oops, something went wrong.