Skip to content

Commit

Permalink
Add support for CSI driver gcp.csi.confidential.cloud
Browse files Browse the repository at this point in the history
Signed-off-by: Pujan Shah <pujan.shah@occrp.org>
  • Loading branch information
ps-occrp committed Jul 6, 2023
1 parent fef6f6d commit bece7b4
Show file tree
Hide file tree
Showing 2 changed files with 18 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 @@ -374,11 +381,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 @@ -404,12 +411,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

0 comments on commit bece7b4

Please sign in to comment.