Skip to content

Commit 5291315

Browse files
authored
Merge pull request #118 from davidz627/feature/nodeIDProject
Change NodeID to be fully qualified name
2 parents 779684f + 80019ad commit 5291315

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

pkg/common/utils.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,21 @@ const (
2828
// Volume ID Expected Format
2929
// "projects/{projectName}/zones/{zoneName}/disks/{diskName}"
3030
// "projects/{projectName}/regions/{regionName}/disks/{diskName}"
31-
volIDToplogyKey = 2
32-
volIDToplogyValue = 3
33-
volIDDiskNameValue = 5
34-
volIDTotalElements = 6
31+
volIDToplogyKey = 2
32+
volIDToplogyValue = 3
33+
volIDDiskNameValue = 5
34+
volIDTotalElements = 6
35+
36+
// Snapshot ID
3537
snapshotTotalElements = 5
3638
snapshotTopologyKey = 2
3739

3840
// Node ID Expected Format
39-
// "{zoneName}/{instanceName}"
40-
nodeIDZoneValue = 0
41-
nodeIDNameValue = 1
42-
nodeIDTotalElements = 2
41+
// "projects/{projectName}/zones/{zoneName}/disks/{diskName}"
42+
nodeIDFmt = "projects/%s/zones/%s/instances/%s"
43+
nodeIDZoneValue = 3
44+
nodeIDNameValue = 5
45+
nodeIDTotalElements = 6
4346

4447
regionalDeviceNameSuffix = "_regional"
4548
)
@@ -83,7 +86,7 @@ func SnapshotIDToKey(id string) (string, error) {
8386
func NodeIDToZoneAndName(id string) (string, string, error) {
8487
splitId := strings.Split(id, "/")
8588
if len(splitId) != nodeIDTotalElements {
86-
return "", "", fmt.Errorf("failed to get id components. expected {zone}/{name}. Got: %s", id)
89+
return "", "", fmt.Errorf("failed to get id components. expected projects/{project}/zones/{zone}/instances/{name}. Got: %s", id)
8790
}
8891
return splitId[nodeIDZoneValue], splitId[nodeIDNameValue], nil
8992
}
@@ -117,3 +120,7 @@ func GetDeviceName(volKey *meta.Key) (string, error) {
117120
return "", fmt.Errorf("volume key %v neither zonal nor regional", volKey.Name)
118121
}
119122
}
123+
124+
func CreateNodeID(project, zone, name string) string {
125+
return fmt.Sprintf(nodeIDFmt, project, zone, name)
126+
}

pkg/common/utils_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
const (
2828
volIDZoneFmt = "projects/%s/zones/%s/disks/%s"
2929
volIDRegionFmt = "projects/%s/regions/%s/disks/%s"
30-
nodeIDFmt = "%s/%s"
3130
)
3231

3332
func TestBytesToGb(t *testing.T) {
@@ -154,6 +153,7 @@ func TestVolumeIDToKey(t *testing.T) {
154153
}
155154

156155
func TestNodeIDToZoneAndName(t *testing.T) {
156+
testProject := "test-project"
157157
testName := "test-name"
158158
testZone := "test-zone"
159159

@@ -166,7 +166,7 @@ func TestNodeIDToZoneAndName(t *testing.T) {
166166
}{
167167
{
168168
name: "normal",
169-
nodeID: fmt.Sprintf(nodeIDFmt, testZone, testName),
169+
nodeID: CreateNodeID(testProject, testZone, testName),
170170
expZone: testZone,
171171
expName: testName,
172172
},

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package gceGCEDriver
1717
import (
1818
"fmt"
1919
"os"
20-
"strings"
2120
"sync"
2221

2322
csi "github.com/container-storage-interface/spec/lib/go/csi/v0"
@@ -271,7 +270,7 @@ func (ns *GCENodeServer) NodeUnstageVolume(ctx context.Context, req *csi.NodeUns
271270
func (ns *GCENodeServer) NodeGetId(ctx context.Context, req *csi.NodeGetIdRequest) (*csi.NodeGetIdResponse, error) {
272271
glog.V(4).Infof("NodeGetId called with req: %#v", req)
273272

274-
nodeID := strings.Join([]string{ns.MetadataService.GetZone(), ns.MetadataService.GetName()}, "/")
273+
nodeID := common.CreateNodeID(ns.MetadataService.GetProject(), ns.MetadataService.GetZone(), ns.MetadataService.GetName())
275274

276275
return &csi.NodeGetIdResponse{
277276
NodeId: nodeID,
@@ -293,7 +292,7 @@ func (ns *GCENodeServer) NodeGetInfo(ctx context.Context, req *csi.NodeGetInfoRe
293292
Segments: map[string]string{common.TopologyKeyZone: ns.MetadataService.GetZone()},
294293
}
295294

296-
nodeID := strings.Join([]string{ns.MetadataService.GetZone(), ns.MetadataService.GetName()}, "/")
295+
nodeID := common.CreateNodeID(ns.MetadataService.GetProject(), ns.MetadataService.GetZone(), ns.MetadataService.GetName())
297296

298297
resp := &csi.NodeGetInfoResponse{
299298
NodeId: nodeID,

test/remote/instance.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"google.golang.org/api/googleapi"
3333
"k8s.io/apimachinery/pkg/util/uuid"
3434
"k8s.io/apimachinery/pkg/util/wait"
35+
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/common"
3536
)
3637

3738
const (
@@ -62,7 +63,7 @@ func (i *InstanceInfo) GetName() string {
6263
}
6364

6465
func (i *InstanceInfo) GetNodeID() string {
65-
return strings.Join([]string{i.zone, i.name}, "/")
66+
return common.CreateNodeID(i.project, i.zone, i.name)
6667
}
6768

6869
func CreateInstanceInfo(project, instanceZone, name string, cs *compute.Service) (*InstanceInfo, error) {

0 commit comments

Comments
 (0)