Skip to content

Commit 172c91c

Browse files
committed
Regional PD Implementation
1 parent 14ab26f commit 172c91c

File tree

18 files changed

+1023
-383
lines changed

18 files changed

+1023
-383
lines changed

Diff for: cmd/main.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ func init() {
3535
var (
3636
endpoint = flag.String("endpoint", "unix:/tmp/csi.sock", "CSI endpoint")
3737
driverName = flag.String("drivername", "com.google.csi.gcepd", "name of the driver")
38-
nodeID = flag.String("nodeid", "", "node id")
3938
vendorVersion string
4039
)
4140

@@ -50,7 +49,7 @@ func handle() {
5049
if vendorVersion == "" {
5150
glog.Fatalf("vendorVersion must be set at compile time")
5251
}
53-
glog.Infof("Driver vendor version %v", vendorVersion)
52+
glog.V(4).Infof("Driver vendor version %v", vendorVersion)
5453

5554
gceDriver := driver.GetGCEDriver()
5655

@@ -68,7 +67,7 @@ func handle() {
6867
glog.Fatalf("Failed to set up metadata service: %v", err)
6968
}
7069

71-
err = gceDriver.SetupGCEDriver(cloudProvider, mounter, deviceUtils, ms, *driverName, *nodeID, vendorVersion)
70+
err = gceDriver.SetupGCEDriver(cloudProvider, mounter, deviceUtils, ms, *driverName, vendorVersion)
7271
if err != nil {
7372
glog.Fatalf("Failed to initialize GCE CSI Driver: %v", err)
7473
}

Diff for: pkg/common/constants.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ package common
1818

1919
const (
2020
// Keys for Storage Class Parameters
21-
ParameterKeyZone = "zone"
22-
ParameterKeyType = "type"
21+
ParameterKeyType = "type"
22+
ParameterKeyReplicationType = "replication-type"
2323

2424
// Keys for Topology. This key will be shared amonst drivers from GCP
2525
TopologyKeyZone = "com.google.topology/zone"

Diff for: pkg/common/utils.go

+46-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ package common
1919
import (
2020
"fmt"
2121
"strings"
22+
23+
"k8s.io/kubernetes/pkg/cloudprovider/providers/gce/cloud/meta"
24+
)
25+
26+
const (
27+
volIDFmt = "projects/%s/zones/%s/disks/{}"
28+
29+
volIDToplogyKey = 2
30+
volIDToplogyValue = 3
31+
volIDDiskNameValue = 5
32+
volIDTotalElements = 6
33+
34+
nodeIDZoneValue = 0
35+
nodeIDNameValue = 1
36+
nodeIDTotalElements = 2
2237
)
2338

2439
func BytesToGb(bytes int64) int64 {
@@ -31,14 +46,41 @@ func GbToBytes(Gb int64) int64 {
3146
return Gb * 1024 * 1024 * 1024
3247
}
3348

34-
func SplitZoneNameId(id string) (string, string, error) {
49+
func VolumeIDToKey(id string) (*meta.Key, error) {
50+
splitId := strings.Split(id, "/")
51+
if len(splitId) != volIDTotalElements {
52+
return nil, fmt.Errorf("failed to get id components. Expected projects/{project}/zones/{zone}/disks/{name}. Got: %s", id)
53+
}
54+
if splitId[volIDToplogyKey] == "zones" {
55+
return meta.ZonalKey(splitId[volIDDiskNameValue], splitId[volIDToplogyValue]), nil
56+
} else if splitId[volIDToplogyKey] == "regions" {
57+
return meta.RegionalKey(splitId[volIDDiskNameValue], splitId[volIDToplogyValue]), nil
58+
} else {
59+
return nil, fmt.Errorf("could not get id components, expected either zones or regions, got: %v", splitId[2])
60+
}
61+
}
62+
63+
func NodeIDToZoneAndName(id string) (string, string, error) {
3564
splitId := strings.Split(id, "/")
36-
if len(splitId) != 2 {
37-
return "", "", fmt.Errorf("Failed to get id components. Expected {zone}/{name}. Got: %s", id)
65+
if len(splitId) != nodeIDTotalElements {
66+
return "", "", fmt.Errorf("failed to get id components. expected {zone}/{name}. Got: %s", id)
3867
}
39-
return splitId[0], splitId[1], nil
68+
return splitId[nodeIDZoneValue], splitId[nodeIDNameValue], nil
4069
}
4170

4271
func CombineVolumeId(zone, name string) string {
4372
return fmt.Sprintf("%s/%s", zone, name)
4473
}
74+
75+
func GetRegionFromZones(zones []string) (string, error) {
76+
if len(zones) < 1 {
77+
return "", fmt.Errorf("no zones specified")
78+
}
79+
zone := zones[0]
80+
// Zone expected format {locale}-{region}-{zone}
81+
splitZone := strings.Split(zone, "-")
82+
if len(splitZone) != 3 {
83+
return "", fmt.Errorf("zone in unexpected format, expected: {locale}-{region}-{zone}, got: %v", zone)
84+
}
85+
return strings.Join(splitZone[0:2], "-"), nil
86+
}

Diff for: pkg/gce-cloud-provider/compute/cloud-disk.go

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
*/
14+
15+
package gcecloudprovider
16+
17+
import (
18+
computebeta "google.golang.org/api/compute/v0.beta"
19+
compute "google.golang.org/api/compute/v1"
20+
)
21+
22+
type CloudDisk struct {
23+
ZonalDisk *compute.Disk
24+
RegionalDisk *computebeta.Disk
25+
}
26+
27+
type CloudDiskType string
28+
29+
const (
30+
// Zonal key type.
31+
Zonal = "zonal"
32+
// Regional key type.
33+
Regional = "regional"
34+
// Global key type.
35+
Global = "global"
36+
)
37+
38+
func ZonalCloudDisk(disk *compute.Disk) *CloudDisk {
39+
return &CloudDisk{
40+
ZonalDisk: disk,
41+
}
42+
}
43+
44+
func RegionalCloudDisk(disk *computebeta.Disk) *CloudDisk {
45+
return &CloudDisk{
46+
RegionalDisk: disk,
47+
}
48+
}
49+
50+
func (d *CloudDisk) Type() CloudDiskType {
51+
switch {
52+
case d.ZonalDisk != nil:
53+
return Zonal
54+
case d.RegionalDisk != nil:
55+
return Regional
56+
default:
57+
return Global
58+
}
59+
}
60+
61+
func (d *CloudDisk) GetUsers() []string {
62+
switch d.Type() {
63+
case Zonal:
64+
return d.ZonalDisk.Users
65+
case Regional:
66+
return d.RegionalDisk.Users
67+
default:
68+
return nil
69+
}
70+
}
71+
72+
func (d *CloudDisk) GetName() string {
73+
switch d.Type() {
74+
case Zonal:
75+
return d.ZonalDisk.Name
76+
case Regional:
77+
return d.RegionalDisk.Name
78+
default:
79+
return ""
80+
}
81+
}
82+
83+
func (d *CloudDisk) GetKind() string {
84+
switch d.Type() {
85+
case Zonal:
86+
return d.ZonalDisk.Kind
87+
case Regional:
88+
return d.RegionalDisk.Kind
89+
default:
90+
return ""
91+
}
92+
}
93+
94+
func (d *CloudDisk) GetType() string {
95+
switch d.Type() {
96+
case Zonal:
97+
return d.ZonalDisk.Type
98+
case Regional:
99+
return d.RegionalDisk.Type
100+
default:
101+
return ""
102+
}
103+
}
104+
105+
func (d *CloudDisk) GetSelfLink() string {
106+
switch d.Type() {
107+
case Zonal:
108+
return d.ZonalDisk.SelfLink
109+
case Regional:
110+
return d.RegionalDisk.SelfLink
111+
default:
112+
return ""
113+
}
114+
}
115+
116+
func (d *CloudDisk) GetSizeGb() int64 {
117+
switch d.Type() {
118+
case Zonal:
119+
return d.ZonalDisk.SizeGb
120+
case Regional:
121+
return d.RegionalDisk.SizeGb
122+
default:
123+
return -1
124+
}
125+
}

0 commit comments

Comments
 (0)