Skip to content

Commit f11eada

Browse files
committed
Added topology support to driver. Updated CSI Test dep to v0.3.0.
Refactor common utils and added metadata service. Refactor of e2e testing framework to accomidate parallel tests.
1 parent f5069ef commit f11eada

32 files changed

+1830
-364
lines changed

Gopkg.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
non-go = true
7777

7878
[[constraint]]
79-
branch = "master"
79+
branch = "v0.3.0"
8080
name = "github.com/kubernetes-csi/csi-test"
8181

8282
[[constraint]]

cmd/main.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ package main
1616

1717
import (
1818
"flag"
19+
"math/rand"
1920
"os"
21+
"time"
2022

2123
"github.com/golang/glog"
2224

23-
gce "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider"
25+
gce "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider/compute"
26+
metadataservice "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider/metadata"
2427
driver "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-pd-csi-driver"
2528
mountmanager "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/mount-manager"
2629
)
@@ -38,6 +41,7 @@ var (
3841

3942
func main() {
4043
flag.Parse()
44+
rand.Seed(time.Now().UnixNano())
4145
handle()
4246
os.Exit(0)
4347
}
@@ -59,7 +63,12 @@ func handle() {
5963
mounter := mountmanager.NewSafeMounter()
6064
deviceUtils := mountmanager.NewDeviceUtils()
6165

62-
err = gceDriver.SetupGCEDriver(cloudProvider, mounter, deviceUtils, *driverName, *nodeID, vendorVersion)
66+
ms, err := metadataservice.NewMetadataService()
67+
if err != nil {
68+
glog.Fatalf("Failed to set up metadata service: %v", err)
69+
}
70+
71+
err = gceDriver.SetupGCEDriver(cloudProvider, mounter, deviceUtils, ms, *driverName, *nodeID, vendorVersion)
6372
if err != nil {
6473
glog.Fatalf("Failed to initialize GCE CSI Driver: %v", err)
6574
}

pkg/common/constants.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package common
18+
19+
const (
20+
// Keys for Storage Class Parameters
21+
ParameterKeyZone = "zone"
22+
ParameterKeyType = "type"
23+
24+
// Keys for Topology. This key will be shared amonst drivers from GCP
25+
TopologyKeyZone = "com.google.topology/zone"
26+
)

pkg/utils/utils.go renamed to pkg/common/utils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package utils
17+
package common
1818

1919
import (
2020
"fmt"

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

+17-6
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ import (
2121
"github.com/golang/glog"
2222
"golang.org/x/net/context"
2323
compute "google.golang.org/api/compute/v1"
24+
"google.golang.org/api/googleapi"
2425
"google.golang.org/grpc/codes"
2526
"google.golang.org/grpc/status"
26-
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/utils"
27+
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/common"
2728
)
2829

2930
type FakeCloudProvider struct {
@@ -58,7 +59,7 @@ func (cloud *FakeCloudProvider) GetZone() string {
5859
func (cloud *FakeCloudProvider) GetDiskOrError(ctx context.Context, volumeZone, volumeName string) (*compute.Disk, error) {
5960
disk, ok := cloud.disks[volumeName]
6061
if !ok {
61-
return nil, fmt.Errorf("Disk %v not found", volumeName)
62+
return nil, notFoundError()
6263
}
6364
return disk, nil
6465
}
@@ -71,12 +72,12 @@ func (cloud *FakeCloudProvider) GetAndValidateExistingDisk(ctx context.Context,
7172
}
7273
if disk != nil {
7374
// Check that disk is the same
74-
requestValid := utils.GbToBytes(disk.SizeGb) >= reqBytes || reqBytes == 0
75-
responseValid := utils.GbToBytes(disk.SizeGb) <= limBytes || limBytes == 0
75+
requestValid := common.GbToBytes(disk.SizeGb) >= reqBytes || reqBytes == 0
76+
responseValid := common.GbToBytes(disk.SizeGb) <= limBytes || limBytes == 0
7677
if !requestValid || !responseValid {
7778
return true, status.Error(codes.AlreadyExists, fmt.Sprintf(
7879
"Disk already exists with incompatible capacity. Need %v (Required) < %v (Existing) < %v (Limit)",
79-
reqBytes, utils.GbToBytes(disk.SizeGb), limBytes))
80+
reqBytes, common.GbToBytes(disk.SizeGb), limBytes))
8081
}
8182

8283
respType := strings.Split(disk.Type, "/")
@@ -171,7 +172,7 @@ func (cloud *FakeCloudProvider) InsertInstance(instance *compute.Instance, insta
171172
func (cloud *FakeCloudProvider) GetInstanceOrError(ctx context.Context, instanceZone, instanceName string) (*compute.Instance, error) {
172173
instance, ok := cloud.instances[instanceName]
173174
if !ok {
174-
return nil, fmt.Errorf("Could not find instance %v", instanceName)
175+
return nil, notFoundError()
175176
}
176177
return instance, nil
177178
}
@@ -180,3 +181,13 @@ func (cloud *FakeCloudProvider) GetInstanceOrError(ctx context.Context, instance
180181
func (cloud *FakeCloudProvider) WaitForOp(ctx context.Context, op *compute.Operation, zone string) error {
181182
return nil
182183
}
184+
185+
func notFoundError() *googleapi.Error {
186+
return &googleapi.Error{
187+
Errors: []googleapi.ErrorItem{
188+
{
189+
Reason: "notFound",
190+
},
191+
},
192+
}
193+
}

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

+6-14
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"google.golang.org/grpc/codes"
2626
"google.golang.org/grpc/status"
2727
"k8s.io/apimachinery/pkg/util/wait"
28-
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/utils"
28+
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/common"
2929
)
3030

3131
type GCECompute interface {
@@ -62,11 +62,7 @@ func (cloud *CloudProvider) GetDiskOrError(ctx context.Context, volumeZone, volu
6262
glog.Infof("Getting disk %v from zone %v", volumeName, volumeZone)
6363
disk, err := svc.Disks.Get(project, volumeZone, volumeName).Context(ctx).Do()
6464
if err != nil {
65-
if IsGCEError(err, "notFound") {
66-
return nil, status.Error(codes.NotFound, fmt.Sprintf("disk %v does not exist", volumeName))
67-
}
68-
69-
return nil, status.Error(codes.Internal, fmt.Sprintf("unknown disk GET error: %v", err))
65+
return nil, err
7066
}
7167
glog.Infof("Got disk %v from zone %v", volumeName, volumeZone)
7268
return disk, nil
@@ -86,12 +82,12 @@ func (cloud *CloudProvider) GetAndValidateExistingDisk(ctx context.Context, conf
8682

8783
if resp != nil {
8884
// Disk already exists
89-
requestValid := utils.GbToBytes(resp.SizeGb) >= reqBytes && reqBytes != 0
90-
responseValid := utils.GbToBytes(resp.SizeGb) <= limBytes && limBytes != 0
85+
requestValid := common.GbToBytes(resp.SizeGb) >= reqBytes && reqBytes != 0
86+
responseValid := common.GbToBytes(resp.SizeGb) <= limBytes && limBytes != 0
9187
if !requestValid || !responseValid {
9288
return true, status.Error(codes.AlreadyExists, fmt.Sprintf(
9389
"Disk already exists with incompatible capacity. Need %v (Required) < %v (Existing) < %v (Limit)",
94-
reqBytes, utils.GbToBytes(resp.SizeGb), limBytes))
90+
reqBytes, common.GbToBytes(resp.SizeGb), limBytes))
9591
}
9692

9793
respType := strings.Split(resp.Type, "/")
@@ -190,11 +186,7 @@ func (cloud *CloudProvider) GetInstanceOrError(ctx context.Context, instanceZone
190186
glog.Infof("Getting instance %v from zone %v", instanceName, instanceZone)
191187
instance, err := svc.Instances.Get(project, instanceZone, instanceName).Do()
192188
if err != nil {
193-
if IsGCEError(err, "notFound") {
194-
return nil, status.Error(codes.NotFound, fmt.Sprintf("instance %v does not exist", instanceName))
195-
}
196-
197-
return nil, status.Error(codes.Internal, fmt.Sprintf("unknown instance GET error: %v", err))
189+
return nil, err
198190
}
199191
glog.Infof("Got instance %v from zone %v", instanceName, instanceZone)
200192
return instance, nil

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

-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ func CreateCloudProvider(vendorVersion string) (*CloudProvider, error) {
5151
if err != nil {
5252
return nil, err
5353
}
54-
// TODO: Use metadata server or flags to retrieve project and zone. Fallback on flag if necessary
5554

5655
project, zone, err := getProjectAndZoneFromMetadata()
5756
if err != nil {
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package metadata
18+
19+
type fakeServiceManager struct{}
20+
21+
var _ MetadataService = &fakeServiceManager{}
22+
23+
func NewFakeService() MetadataService {
24+
return &fakeServiceManager{}
25+
}
26+
27+
func (manager *fakeServiceManager) GetZone() string {
28+
return "test-location"
29+
}
30+
31+
func (manager *fakeServiceManager) GetProject() string {
32+
return "test-project"
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package metadata
18+
19+
import (
20+
"fmt"
21+
22+
"cloud.google.com/go/compute/metadata"
23+
)
24+
25+
// MetadataService is a fakeable interface exposing necessary data
26+
// from the GCE Metadata service
27+
type MetadataService interface {
28+
GetZone() string
29+
GetProject() string
30+
}
31+
32+
type metadataServiceManager struct {
33+
// Current zone the driver is running in
34+
zone string
35+
project string
36+
}
37+
38+
var _ MetadataService = &metadataServiceManager{}
39+
40+
func NewMetadataService() (MetadataService, error) {
41+
zone, err := metadata.Zone()
42+
if err != nil {
43+
return nil, fmt.Errorf("failed to get current zone: %v", err)
44+
}
45+
projectID, err := metadata.ProjectID()
46+
if err != nil {
47+
return nil, fmt.Errorf("failed to get project: %v", err)
48+
}
49+
50+
return &metadataServiceManager{
51+
project: projectID,
52+
zone: zone,
53+
}, nil
54+
}
55+
56+
func (manager *metadataServiceManager) GetZone() string {
57+
return manager.zone
58+
}
59+
60+
func (manager *metadataServiceManager) GetProject() string {
61+
return manager.project
62+
}

0 commit comments

Comments
 (0)