Skip to content

Commit ba2bb17

Browse files
committed
feat: Provide hostname for zonal disk insert API
CL contains changes to pass in hostname for zonal disk.insert API as a part of location hint field. Code changes for regional disk API will be part of future CLs.
1 parent cf8da66 commit ba2bb17

File tree

250 files changed

+28602
-24
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

250 files changed

+28602
-24
lines changed

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

+17-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
"strings"
2828
"time"
2929

30+
"k8s.io/client-go/kubernetes"
31+
"k8s.io/client-go/rest"
3032
"k8s.io/klog/v2"
3133
"k8s.io/utils/strings/slices"
3234

@@ -72,6 +74,7 @@ var (
7274
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")
7375
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")
7476
enableStoragePoolsFlag = flag.Bool("enable-storage-pools", false, "If set to true, the CSI Driver will allow volumes to be provisioned in Storage Pools")
77+
enableVMLocationHint = flag.Bool("enable-vm-location-hint", false, "If set to true, the location hint field for create volume request will have hostname set")
7578

7679
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")
7780
multiZoneVolumeHandleEnableFlag = flag.Bool("multi-zone-volume-handle-enable", false, "If set to true, the multi-zone volumeHandle feature will be enabled")
@@ -244,7 +247,20 @@ func handle() {
244247
}
245248
}
246249

247-
err = gceDriver.SetupGCEDriver(driverName, version, extraVolumeLabels, extraTags, identityServer, controllerServer, nodeServer)
250+
var kubeClient *kubernetes.Clientset
251+
if *enableVMLocationHint {
252+
cfg, err := rest.InClusterConfig()
253+
if err != nil {
254+
klog.Fatalf("Could not fetch in-cluster config: %v", err.Error())
255+
}
256+
257+
kubeClient, err = kubernetes.NewForConfig(cfg)
258+
if err != nil {
259+
klog.Fatalf("Could not fetch in-cluster client: %v", err.Error())
260+
}
261+
}
262+
263+
err = gceDriver.SetupGCEDriver(driverName, version, extraVolumeLabels, extraTags, kubeClient, identityServer, controllerServer, nodeServer)
248264
if err != nil {
249265
klog.Fatalf("Failed to initialize GCE CSI Driver: %v", err.Error())
250266
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ require (
5656
github.com/cespare/xxhash/v2 v2.3.0 // indirect
5757
github.com/davecgh/go-spew v1.1.1 // indirect
5858
github.com/emicklei/go-restful v2.9.5+incompatible // indirect
59+
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
5960
github.com/felixge/httpsnoop v1.0.4 // indirect
6061
github.com/fsnotify/fsnotify v1.5.4 // indirect
6162
github.com/go-logr/logr v1.4.2 // indirect

go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,7 @@ github.com/evanphx/json-patch v4.2.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLi
10081008
github.com/evanphx/json-patch v4.5.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
10091009
github.com/evanphx/json-patch v4.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
10101010
github.com/evanphx/json-patch v4.11.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
1011+
github.com/evanphx/json-patch v4.12.0+incompatible h1:4onqiflcdA9EOZ4RxV643DvftH5pOlLGNtQ5lPWQu84=
10111012
github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
10121013
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d/go.mod h1:ZZMPRZwes7CROmyNKgQzC3XPs6L/G2EJLHddWejkmf4=
10131014
github.com/fatih/camelcase v1.0.0/go.mod h1:yN2Sb0lFhZJUdVvtELVWefmrXpuZESvPmqwoZc+/fpc=

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func (cloud *FakeCloudProvider) ValidateExistingDisk(ctx context.Context, resp *
221221
return ValidateDiskParameters(resp, params)
222222
}
223223

224-
func (cloud *FakeCloudProvider) InsertDisk(ctx context.Context, project string, volKey *meta.Key, params common.DiskParameters, capBytes int64, capacityRange *csi.CapacityRange, replicaZones []string, snapshotID string, volumeContentSourceVolumeID string, multiWriter bool, accessMode string) error {
224+
func (cloud *FakeCloudProvider) InsertDisk(ctx context.Context, project string, volKey *meta.Key, params common.DiskParameters, capBytes int64, capacityRange *csi.CapacityRange, replicaZones []string, snapshotID string, volumeContentSourceVolumeID string, multiWriter bool, accessMode, hostName string) error {
225225
if disk, ok := cloud.disks[volKey.String()]; ok {
226226
err := cloud.ValidateExistingDisk(ctx, disk, params,
227227
int64(capacityRange.GetRequiredBytes()),
@@ -242,6 +242,7 @@ func (cloud *FakeCloudProvider) InsertDisk(ctx context.Context, project string,
242242
Labels: params.Labels,
243243
ProvisionedIops: params.ProvisionedIOPSOnCreate,
244244
ProvisionedThroughput: params.ProvisionedThroughputOnCreate,
245+
LocationHint: cloud.GetLocationHintURI(project, volKey.Zone, hostName),
245246
}
246247

247248
if snapshotID != "" {
@@ -382,6 +383,10 @@ func (cloud *FakeCloudProvider) GetDiskTypeURI(project string, volKey *meta.Key,
382383
}
383384
}
384385

386+
func (cloud *FakeCloudProvider) GetLocationHintURI(project, zone, hostName string) string {
387+
return fmt.Sprintf(locationHintURITemplate, project, zone, hostName)
388+
}
389+
385390
func (cloud *FakeCloudProvider) getZonalDiskTypeURI(project, zone, diskType string) string {
386391
return fmt.Sprintf(diskTypeURITemplateSingleZone, project, zone, diskType)
387392
}

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

+9-4
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ type GCECompute interface {
102102
GetDisk(ctx context.Context, project string, volumeKey *meta.Key, gceAPIVersion GCEAPIVersion) (*CloudDisk, error)
103103
RepairUnderspecifiedVolumeKey(ctx context.Context, project string, volumeKey *meta.Key) (string, *meta.Key, error)
104104
ValidateExistingDisk(ctx context.Context, disk *CloudDisk, params common.DiskParameters, reqBytes, limBytes int64, multiWriter bool) error
105-
InsertDisk(ctx context.Context, project string, volKey *meta.Key, params common.DiskParameters, capBytes int64, capacityRange *csi.CapacityRange, replicaZones []string, snapshotID string, volumeContentSourceVolumeID string, multiWriter bool, accessMode string) error
105+
InsertDisk(ctx context.Context, project string, volKey *meta.Key, params common.DiskParameters, capBytes int64, capacityRange *csi.CapacityRange, replicaZones []string, snapshotID string, volumeContentSourceVolumeID string, multiWriter bool, accessMode, hostName string) error
106106
DeleteDisk(ctx context.Context, project string, volumeKey *meta.Key) error
107107
UpdateDisk(ctx context.Context, project string, volKey *meta.Key, existingDisk *CloudDisk, params common.ModifyVolumeParameters) error
108108
AttachDisk(ctx context.Context, project string, volKey *meta.Key, readWrite, diskType, instanceZone, instanceName string, forceAttach bool) error
@@ -436,7 +436,7 @@ func ValidateDiskParameters(disk *CloudDisk, params common.DiskParameters) error
436436
return nil
437437
}
438438

439-
func (cloud *CloudProvider) InsertDisk(ctx context.Context, project string, volKey *meta.Key, params common.DiskParameters, capBytes int64, capacityRange *csi.CapacityRange, replicaZones []string, snapshotID string, volumeContentSourceVolumeID string, multiWriter bool, accessMode string) error {
439+
func (cloud *CloudProvider) InsertDisk(ctx context.Context, project string, volKey *meta.Key, params common.DiskParameters, capBytes int64, capacityRange *csi.CapacityRange, replicaZones []string, snapshotID string, volumeContentSourceVolumeID string, multiWriter bool, accessMode, hostName string) error {
440440
klog.V(5).Infof("Inserting disk %v", volKey)
441441

442442
description, err := encodeTags(params.Tags)
@@ -449,7 +449,7 @@ func (cloud *CloudProvider) InsertDisk(ctx context.Context, project string, volK
449449
if description == "" {
450450
description = "Disk created by GCE-PD CSI Driver"
451451
}
452-
return cloud.insertZonalDisk(ctx, project, volKey, params, capBytes, capacityRange, snapshotID, volumeContentSourceVolumeID, description, multiWriter, accessMode)
452+
return cloud.insertZonalDisk(ctx, project, volKey, params, capBytes, capacityRange, snapshotID, volumeContentSourceVolumeID, description, multiWriter, accessMode, hostName)
453453
case meta.Regional:
454454
if description == "" {
455455
description = "Regional disk created by GCE-PD CSI Driver"
@@ -772,7 +772,7 @@ func (cloud *CloudProvider) insertZonalDisk(
772772
volumeContentSourceVolumeID string,
773773
description string,
774774
multiWriter bool,
775-
accessMode string) error {
775+
accessMode, hostName string) error {
776776
var (
777777
err error
778778
opName string
@@ -788,6 +788,7 @@ func (cloud *CloudProvider) insertZonalDisk(
788788
Description: description,
789789
Type: cloud.GetDiskTypeURI(project, volKey, params.DiskType),
790790
Labels: params.Labels,
791+
LocationHint: cloud.GetLocationHintURI(project, volKey.Zone, hostName),
791792
}
792793

793794
if params.ProvisionedIOPSOnCreate > 0 {
@@ -1107,6 +1108,10 @@ func (cloud *CloudProvider) GetDiskTypeURI(project string, volKey *meta.Key, dis
11071108
}
11081109
}
11091110

1111+
func (cloud *CloudProvider) GetLocationHintURI(project, zone, hostName string) string {
1112+
return fmt.Sprintf(locationHintURITemplate, project, zone, hostName)
1113+
}
1114+
11101115
func (cloud *CloudProvider) getZonalDiskTypeURI(project string, zone, diskType string) string {
11111116
return cloud.service.BasePath + fmt.Sprintf(diskTypeURITemplateSingleZone, project, zone, diskType)
11121117
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const (
5151
diskTypeURITemplateRegional = "projects/%s/regions/%s/diskTypes/%s" // {gce.projectID}/regions/{disk.Region}/diskTypes/{disk.Type}"
5252

5353
regionURITemplate = "projects/%s/regions/%s"
54+
locationHintURITemplate = "projects/%s/zone/%s/instance/%s"
5455

5556
replicaZoneURITemplateSingleZone = "projects/%s/zones/%s" // {gce.projectID}/zones/{disk.Zone}
5657
EnvironmentStaging Environment = "staging"

pkg/gce-pd-csi-driver/controller.go

+31-6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import (
3838
"k8s.io/klog/v2"
3939
"k8s.io/utils/strings/slices"
4040

41+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
42+
"k8s.io/client-go/kubernetes"
4143
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/common"
4244
gce "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider/compute"
4345
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/metrics"
@@ -208,6 +210,8 @@ const (
208210
listDisksUsersField = googleapi.Field("items/users")
209211

210212
readOnlyManyAccessMode = "READ_ONLY_MANY"
213+
214+
annSelectedNode = "volume.kubernetes.io/selected-node"
211215
)
212216

213217
var (
@@ -720,21 +724,23 @@ func (gceCS *GCEControllerServer) createSingleDisk(ctx context.Context, req *csi
720724
// Create the disk
721725
var disk *gce.CloudDisk
722726
name := req.GetName()
727+
pvcName, pvcNamespace := req.Parameters[common.ParameterKeyPVCName], req.Parameters[common.ParameterKeyPVCNamespace]
728+
hostName := getHostNameFromPVC(ctx, pvcName, pvcNamespace, gceCS.Driver.kubeClient)
723729

724730
switch params.ReplicationType {
725731
case replicationTypeNone:
726732
if len(zones) != 1 {
727733
return nil, status.Errorf(codes.Internal, "CreateVolume failed to get a single zone for creating zonal disk, instead got: %v", zones)
728734
}
729-
disk, err = createSingleZoneDisk(ctx, gceCS.CloudProvider, name, zones, params, capacityRange, capBytes, snapshotID, volumeContentSourceVolumeID, multiWriter, accessMode)
735+
disk, err = createSingleZoneDisk(ctx, gceCS.CloudProvider, name, zones, params, capacityRange, capBytes, snapshotID, volumeContentSourceVolumeID, multiWriter, accessMode, hostName)
730736
if err != nil {
731737
return nil, common.LoggedError("CreateVolume failed to create single zonal disk "+name+": ", err)
732738
}
733739
case replicationTypeRegionalPD:
734740
if len(zones) != 2 {
735741
return nil, status.Errorf(codes.Internal, "CreateVolume failed to get a 2 zones for creating regional disk, instead got: %v", zones)
736742
}
737-
disk, err = createRegionalDisk(ctx, gceCS.CloudProvider, name, zones, params, capacityRange, capBytes, snapshotID, volumeContentSourceVolumeID, multiWriter, accessMode)
743+
disk, err = createRegionalDisk(ctx, gceCS.CloudProvider, name, zones, params, capacityRange, capBytes, snapshotID, volumeContentSourceVolumeID, multiWriter, accessMode, "")
738744
if err != nil {
739745
return nil, common.LoggedError("CreateVolume failed to create regional disk "+name+": ", err)
740746
}
@@ -872,6 +878,25 @@ func (gceCS *GCEControllerServer) DeleteVolume(ctx context.Context, req *csi.Del
872878
return gceCS.deleteSingleDeviceDisk(ctx, req, project, volKey)
873879
}
874880

881+
func getHostNameFromPVC(ctx context.Context, pvcName, pvcNamespace string, client kubernetes.Interface) string {
882+
if client == nil {
883+
// Client is initialized only when location hint is enabled
884+
return ""
885+
}
886+
887+
pvc, err := client.CoreV1().PersistentVolumeClaims(pvcNamespace).Get(ctx, pvcName, metav1.GetOptions{})
888+
if err != nil {
889+
klog.Errorf("Failed to get persistent volume claim %s: %v", pvcName, err)
890+
// Don't return the error as create volume request can be continued without hostname
891+
return ""
892+
}
893+
if hostName, ok := pvc.Annotations[annSelectedNode]; ok {
894+
klog.V(4).Infof("Retrieved hostname %q from PVC %v", hostName, pvcName)
895+
return hostName
896+
}
897+
return ""
898+
}
899+
875900
func getGCEApiVersion(multiWriter bool) gce.GCEAPIVersion {
876901
if multiWriter {
877902
return gce.GCEAPIVersionBeta
@@ -2482,7 +2507,7 @@ func getResourceId(resourceLink string) (string, error) {
24822507
return strings.Join(elts[3:], "/"), nil
24832508
}
24842509

2485-
func createRegionalDisk(ctx context.Context, cloudProvider gce.GCECompute, name string, zones []string, params common.DiskParameters, capacityRange *csi.CapacityRange, capBytes int64, snapshotID string, volumeContentSourceVolumeID string, multiWriter bool, accessMode string) (*gce.CloudDisk, error) {
2510+
func createRegionalDisk(ctx context.Context, cloudProvider gce.GCECompute, name string, zones []string, params common.DiskParameters, capacityRange *csi.CapacityRange, capBytes int64, snapshotID string, volumeContentSourceVolumeID string, multiWriter bool, accessMode, hostName string) (*gce.CloudDisk, error) {
24862511
project := cloudProvider.GetDefaultProject()
24872512
region, err := common.GetRegionFromZones(zones)
24882513
if err != nil {
@@ -2495,7 +2520,7 @@ func createRegionalDisk(ctx context.Context, cloudProvider gce.GCECompute, name
24952520
fullyQualifiedReplicaZones, cloudProvider.GetReplicaZoneURI(project, replicaZone))
24962521
}
24972522

2498-
err = cloudProvider.InsertDisk(ctx, project, meta.RegionalKey(name, region), params, capBytes, capacityRange, fullyQualifiedReplicaZones, snapshotID, volumeContentSourceVolumeID, multiWriter, accessMode)
2523+
err = cloudProvider.InsertDisk(ctx, project, meta.RegionalKey(name, region), params, capBytes, capacityRange, fullyQualifiedReplicaZones, snapshotID, volumeContentSourceVolumeID, multiWriter, accessMode, hostName)
24992524
if err != nil {
25002525
return nil, fmt.Errorf("failed to insert regional disk: %w", err)
25012526
}
@@ -2512,13 +2537,13 @@ func createRegionalDisk(ctx context.Context, cloudProvider gce.GCECompute, name
25122537
return disk, nil
25132538
}
25142539

2515-
func createSingleZoneDisk(ctx context.Context, cloudProvider gce.GCECompute, name string, zones []string, params common.DiskParameters, capacityRange *csi.CapacityRange, capBytes int64, snapshotID string, volumeContentSourceVolumeID string, multiWriter bool, accessMode string) (*gce.CloudDisk, error) {
2540+
func createSingleZoneDisk(ctx context.Context, cloudProvider gce.GCECompute, name string, zones []string, params common.DiskParameters, capacityRange *csi.CapacityRange, capBytes int64, snapshotID string, volumeContentSourceVolumeID string, multiWriter bool, accessMode, hostName string) (*gce.CloudDisk, error) {
25162541
project := cloudProvider.GetDefaultProject()
25172542
if len(zones) != 1 {
25182543
return nil, fmt.Errorf("got wrong number of zones for zonal create volume: %v", len(zones))
25192544
}
25202545
diskZone := zones[0]
2521-
err := cloudProvider.InsertDisk(ctx, project, meta.ZonalKey(name, diskZone), params, capBytes, capacityRange, nil, snapshotID, volumeContentSourceVolumeID, multiWriter, accessMode)
2546+
err := cloudProvider.InsertDisk(ctx, project, meta.ZonalKey(name, diskZone), params, capBytes, capacityRange, nil, snapshotID, volumeContentSourceVolumeID, multiWriter, accessMode, hostName)
25222547
if err != nil {
25232548
return nil, fmt.Errorf("failed to insert zonal disk: %w", err)
25242549
}

0 commit comments

Comments
 (0)