Skip to content

Commit

Permalink
Merge pull request #146 from ps-occrp/main
Browse files Browse the repository at this point in the history
Add support for CSI driver gcp.csi.confidential.cloud
  • Loading branch information
blackpiglet authored Jul 21, 2023
2 parents 4a46a41 + 02e7c37 commit b8f6436
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/146-ps-occrp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for CSI driver gcp.csi.confidential.cloud
27 changes: 17 additions & 10 deletions velero-plugin-for-gcp/volume_snapshotter.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ const (
zoneSeparator = "__"
projectKey = "project"
snapshotLocationKey = "snapshotLocation"
pdCSIDriver = "pd.csi.storage.gke.io"
)

var pdCSIDriver = map[string]bool{
"pd.csi.storage.gke.io": true,
"gcp.csi.confidential.cloud": true,
}

var pdVolRegexp = regexp.MustCompile(`^projects\/[^\/]+\/(zones|regions)\/[^\/]+\/disks\/[^\/]+$`)

type VolumeSnapshotter struct {
Expand Down Expand Up @@ -131,15 +135,18 @@ func isMultiZone(volumeAZ string) bool {
// parseRegion parses a failure-domain tag with multiple zones
// and returns a single region. Zones are sperated by double underscores (__).
// For example
// input: us-central1-a__us-central1-b
// return: us-central1
//
// input: us-central1-a__us-central1-b
// return: us-central1
//
// When a custom storage class spans multiple geographical zones,
// such as us-central1 and us-west1 only the zone matching the cluster is used
// in the failure-domain tag.
// For example
// Cluster nodes in us-central1-c, us-central1-f
// Storage class zones us-central1-a, us-central1-f, us-east1-a, us-east1-d
// The failure-domain tag would be: us-central1-a__us-central1-f
//
// Cluster nodes in us-central1-c, us-central1-f
// Storage class zones us-central1-a, us-central1-f, us-east1-a, us-east1-d
// The failure-domain tag would be: us-central1-a__us-central1-f
func parseRegion(volumeAZ string) (string, error) {
zones := strings.Split(volumeAZ, zoneSeparator)
zone := zones[0]
Expand Down Expand Up @@ -392,11 +399,11 @@ func (b *VolumeSnapshotter) GetVolumeID(unstructuredPV runtime.Unstructured) (st

if pv.Spec.CSI != nil {
driver := pv.Spec.CSI.Driver
if driver == pdCSIDriver {
if pdCSIDriver[driver] {
handle := pv.Spec.CSI.VolumeHandle
if !pdVolRegexp.MatchString(handle) {
return "", fmt.Errorf("invalid volumeHandle for CSI driver:%s, expected projects/{project}/zones/{zone}/disks/{name}, got %s",
pdCSIDriver, handle)
driver, handle)
}
l := strings.Split(handle, "/")
return l[len(l)-1], nil
Expand All @@ -422,12 +429,12 @@ func (b *VolumeSnapshotter) SetVolumeID(unstructuredPV runtime.Unstructured, vol
if pv.Spec.CSI != nil {
// PV is provisioned by CSI driver
driver := pv.Spec.CSI.Driver
if driver == pdCSIDriver {
if pdCSIDriver[driver] {
handle := pv.Spec.CSI.VolumeHandle
// To restore in the same AZ, here we only replace the 'disk' chunk.
if !pdVolRegexp.MatchString(handle) {
return nil, fmt.Errorf("invalid volumeHandle for restore with CSI driver:%s, expected projects/{project}/zones/{zone}/disks/{name}, got %s",
pdCSIDriver, handle)
driver, handle)
}
pv.Spec.CSI.VolumeHandle = handle[:strings.LastIndex(handle, "/")+1] + volumeID
} else {
Expand Down
43 changes: 43 additions & 0 deletions velero-plugin-for-gcp/volume_snapshotter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,29 @@ func TestGetVolumeIDForCSI(t *testing.T) {
want: "",
wantErr: true,
},
{
name: "Constellation csi driver",
csiJSON: `{
"driver": "gcp.csi.confidential.cloud",
"fsType": "ext4",
"volumeAttributes": {
"storage.kubernetes.io/csiProvisionerIdentity": "1637243273131-8081-gcp.csi.confidential.cloud"
},
"volumeHandle": "projects/velero-gcp/zones/us-central1-f/disks/pvc-a970184f-6cc1-4769-85ad-61dcaf8bf51d"
}`,
want: "pvc-a970184f-6cc1-4769-85ad-61dcaf8bf51d",
wantErr: false,
},
{
name: "Constellation csi driver with invalid handle name",
csiJSON: `{
"driver": "gcp.csi.confidential.cloud",
"fsType": "ext4",
"volumeHandle": "pvc-a970184f-6cc1-4769-85ad-61dcaf8bf51d"
}`,
want: "",
wantErr: true,
},
{
name: "unknown driver",
csiJSON: `{
Expand Down Expand Up @@ -185,6 +208,26 @@ func TestSetVolumeIDForCSI(t *testing.T) {
volumeID: "restore-fd9729b5-868b-4544-9568-1c5d9121dabc",
wantErr: true,
},
{
name: "set ID to CSI with Constellation pd CSI driver",
csiJSON: `{
"driver": "gcp.csi.confidential.cloud",
"fsType": "ext4",
"volumeHandle": "projects/velero-gcp/zones/us-central1-f/disks/pvc-a970184f-6cc1-4769-85ad-61dcaf8bf51d"
}`,
volumeID: "restore-fd9729b5-868b-4544-9568-1c5d9121dabc",
wantErr: false,
},
{
name: "set ID to CSI with Constellation pd CSI driver, but the volumeHandle is invalid",
csiJSON: `{
"driver": "gcp.csi.confidential.cloud",
"fsType": "ext4",
"volumeHandle": "pvc-a970184f-6cc1-4769-85ad-61dcaf8bf51d"
}`,
volumeID: "restore-fd9729b5-868b-4544-9568-1c5d9121dabc",
wantErr: true,
},
{
name: "set ID to CSI with unknown driver",
csiJSON: `"{
Expand Down

0 comments on commit b8f6436

Please sign in to comment.