Skip to content

Commit 24280ad

Browse files
authored
Merge pull request #1616 from pwschuurman/automated-cherry-pick-of-#1593-upstream-release-1.13
Automated cherry pick of #1593: Add support for a multi-zone volumeHandle
2 parents 722430e + 5ba2f82 commit 24280ad

File tree

15 files changed

+432
-56
lines changed

15 files changed

+432
-56
lines changed

cmd/gce-pd-csi-driver/main.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,16 @@ var (
7070
maxConcurrentFormatAndMount = flag.Int("max-concurrent-format-and-mount", 1, "If set then format and mount operations are serialized on each node. This is stronger than max-concurrent-format as it includes fsck and other mount operations")
7171
formatAndMountTimeout = flag.Duration("format-and-mount-timeout", 1*time.Minute, "The maximum duration of a format and mount operation before another such operation will be started. Used only if --serialize-format-and-mount")
7272
fallbackRequisiteZonesFlag = flag.String("fallback-requisite-zones", "", "Comma separated list of requisite zones that will be used if there are not sufficient zones present in requisite topologies when provisioning a disk")
73+
enableStoragePoolsFlag = flag.Bool("enable-storage-pools", false, "If set to true, the CSI Driver will allow volumes to be provisioned in Storage Pools")
74+
75+
multiZoneVolumeHandleDiskTypesFlag = flag.String("multi-zone-volume-handle-disk-types", "", "Comma separated list of allowed disk types that can use the multi-zone volumeHandle. Used only if --multi-zone-volume-handle-enable")
76+
multiZoneVolumeHandleEnableFlag = flag.Bool("multi-zone-volume-handle-enable", false, "If set to true, the multi-zone volumeHandle feature will be enabled")
7377

74-
enableStoragePoolsFlag = flag.Bool("enable-storage-pools", false, "If set to true, the CSI Driver will allow volumes to be provisioned in Storage Pools")
7578
computeEnvironment gce.Environment = gce.EnvironmentProduction
7679
computeEndpoint *url.URL
77-
version string
7880
allowedComputeEnvironment = []gce.Environment{gce.EnvironmentStaging, gce.EnvironmentProduction}
81+
82+
version string
7983
)
8084

8185
const (
@@ -156,9 +160,16 @@ func handle() {
156160
// Initialize identity server
157161
identityServer := driver.NewIdentityServer(gceDriver)
158162

159-
// Initilaize requisite zones
163+
// Initialize requisite zones
160164
fallbackRequisiteZones := strings.Split(*fallbackRequisiteZonesFlag, ",")
161165

166+
// Initialize multi-zone disk types
167+
multiZoneVolumeHandleDiskTypes := strings.Split(*multiZoneVolumeHandleDiskTypesFlag, ",")
168+
multiZoneVolumeHandleConfig := driver.MultiZoneVolumeHandleConfig{
169+
Enable: *multiZoneVolumeHandleEnableFlag,
170+
DiskTypes: multiZoneVolumeHandleDiskTypes,
171+
}
172+
162173
// Initialize requirements for the controller service
163174
var controllerServer *driver.GCEControllerServer
164175
if *runControllerService {
@@ -168,7 +179,7 @@ func handle() {
168179
}
169180
initialBackoffDuration := time.Duration(*errorBackoffInitialDurationMs) * time.Millisecond
170181
maxBackoffDuration := time.Duration(*errorBackoffMaxDurationMs) * time.Millisecond
171-
controllerServer = driver.NewControllerServer(gceDriver, cloudProvider, initialBackoffDuration, maxBackoffDuration, fallbackRequisiteZones, *enableStoragePoolsFlag)
182+
controllerServer = driver.NewControllerServer(gceDriver, cloudProvider, initialBackoffDuration, maxBackoffDuration, fallbackRequisiteZones, *enableStoragePoolsFlag, multiZoneVolumeHandleConfig)
172183
} else if *cloudConfigFilePath != "" {
173184
klog.Warningf("controller service is disabled but cloud config given - it has no effect")
174185
}

pkg/common/constants.go

+8
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,12 @@ const (
2424
VolumeAttributePartition = "partition"
2525

2626
UnspecifiedValue = "UNSPECIFIED"
27+
28+
// Keyword indicating a 'multi-zone' volumeHandle. Replaces "zones" in the volumeHandle:
29+
// eg: projects/{project}/zones/multi-zone/disks/{name} vs.
30+
// projects/{project}/zones/{zone}/disks/{name}
31+
MultiZoneValue = "multi-zone"
32+
33+
// Label that is set on a disk when it is used by a 'multi-zone' VolumeHandle
34+
MultiZoneLabel = "goog-gke-multi-zone"
2735
)

pkg/common/utils.go

+12
Original file line numberDiff line numberDiff line change
@@ -478,3 +478,15 @@ func UnorderedSlicesEqual(slice1 []string, slice2 []string) bool {
478478
}
479479
return true
480480
}
481+
482+
func VolumeIdAsMultiZone(volumeId string) (string, error) {
483+
splitId := strings.Split(volumeId, "/")
484+
if len(splitId) != volIDTotalElements {
485+
return "", fmt.Errorf("failed to get id components. Expected projects/{project}/zones/{zone}/disks/{name}. Got: %s", volumeId)
486+
}
487+
if splitId[volIDToplogyKey] != "zones" {
488+
return "", fmt.Errorf("expected id to be zonal. Got: %s", volumeId)
489+
}
490+
splitId[volIDToplogyValue] = MultiZoneValue
491+
return strings.Join(splitId, "/"), nil
492+
}

pkg/gce-cloud-provider/compute/cloud-disk.go

+13
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,16 @@ func (d *CloudDisk) GetEnableStoragePools() bool {
290290
return false
291291
}
292292
}
293+
294+
func (d *CloudDisk) GetLabels() map[string]string {
295+
switch {
296+
case d.disk != nil:
297+
return d.disk.Labels
298+
case d.betaDisk != nil:
299+
return d.betaDisk.Labels
300+
case d.alphaDisk != nil:
301+
return d.alphaDisk.Labels
302+
default:
303+
return nil
304+
}
305+
}

pkg/gce-cloud-provider/compute/cloud-disk_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package gcecloudprovider
2020
import (
2121
"testing"
2222

23+
"github.com/google/go-cmp/cmp"
2324
computealpha "google.golang.org/api/compute/v0.alpha"
2425
computebeta "google.golang.org/api/compute/v0.beta"
2526
computev1 "google.golang.org/api/compute/v1"
@@ -129,3 +130,47 @@ func TestGetEnableStoragePools(t *testing.T) {
129130
}
130131
}
131132
}
133+
134+
func TestGetLabels(t *testing.T) {
135+
testCases := []struct {
136+
name string
137+
cloudDisk *CloudDisk
138+
wantLabels map[string]string
139+
}{
140+
{
141+
name: "v1 disk labels",
142+
cloudDisk: &CloudDisk{
143+
disk: &computev1.Disk{
144+
Labels: map[string]string{"foo": "v1", "goog-gke-multi-zone": "true"},
145+
},
146+
},
147+
wantLabels: map[string]string{"foo": "v1", "goog-gke-multi-zone": "true"},
148+
},
149+
{
150+
name: "beta disk labels",
151+
cloudDisk: &CloudDisk{
152+
betaDisk: &computebeta.Disk{
153+
Labels: map[string]string{"bar": "beta", "goog-gke-multi-zone": "true"},
154+
},
155+
},
156+
wantLabels: map[string]string{"bar": "beta", "goog-gke-multi-zone": "true"},
157+
},
158+
{
159+
name: "alpha disk without storage pool returns false",
160+
cloudDisk: &CloudDisk{
161+
alphaDisk: &computealpha.Disk{
162+
Labels: map[string]string{"baz": "alpha", "goog-gke-multi-zone": "true"},
163+
},
164+
},
165+
wantLabels: map[string]string{"baz": "alpha", "goog-gke-multi-zone": "true"},
166+
},
167+
}
168+
169+
for _, tc := range testCases {
170+
t.Logf("Running test: %v", tc.name)
171+
gotLabels := tc.cloudDisk.GetLabels()
172+
if diff := cmp.Diff(tc.wantLabels, gotLabels); diff != "" {
173+
t.Errorf("GetLabels() returned unexpected difference (-want +got):\n%s", diff)
174+
}
175+
}
176+
}

0 commit comments

Comments
 (0)