-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfake-gce.go
193 lines (165 loc) · 5.7 KB
/
fake-gce.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gcecloudprovider
import (
"fmt"
"strings"
"github.com/golang/glog"
"golang.org/x/net/context"
compute "google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/common"
)
type FakeCloudProvider struct {
project string
zone string
disks map[string]*compute.Disk
instances map[string]*compute.Instance
}
var _ GCECompute = &FakeCloudProvider{}
func FakeCreateCloudProvider(project, zone string) (*FakeCloudProvider, error) {
return &FakeCloudProvider{
project: project,
zone: zone,
disks: map[string]*compute.Disk{},
instances: map[string]*compute.Instance{},
}, nil
}
// Getters
func (cloud *FakeCloudProvider) GetProject() string {
return cloud.project
}
func (cloud *FakeCloudProvider) GetZone() string {
return cloud.zone
}
// Disk Methods
func (cloud *FakeCloudProvider) GetDiskOrError(ctx context.Context, volumeZone, volumeName string) (*compute.Disk, error) {
disk, ok := cloud.disks[volumeName]
if !ok {
return nil, notFoundError()
}
return disk, nil
}
func (cloud *FakeCloudProvider) GetAndValidateExistingDisk(ctx context.Context, configuredZone, name, diskType string, reqBytes, limBytes int64) (exists bool, err error) {
disk, ok := cloud.disks[name]
if !ok {
// Disk doesn't exist
return false, nil
}
if disk != nil {
// Check that disk is the same
requestValid := common.GbToBytes(disk.SizeGb) >= reqBytes || reqBytes == 0
responseValid := common.GbToBytes(disk.SizeGb) <= limBytes || limBytes == 0
if !requestValid || !responseValid {
return true, status.Error(codes.AlreadyExists, fmt.Sprintf(
"Disk already exists with incompatible capacity. Need %v (Required) < %v (Existing) < %v (Limit)",
reqBytes, common.GbToBytes(disk.SizeGb), limBytes))
}
respType := strings.Split(disk.Type, "/")
typeMatch := respType[len(respType)-1] != diskType
typeDefault := diskType == "" && respType[len(respType)-1] == "pd-standard"
if !typeMatch && !typeDefault {
return true, status.Error(codes.AlreadyExists, fmt.Sprintf(
"Disk already exists with incompatible type. Need %v. Got %v",
diskType, respType[len(respType)-1]))
}
// Volume exists with matching name, capacity, type.
glog.Infof("Compatible disk already exists. Reusing existing.")
return true, nil
}
return false, nil
}
func (cloud *FakeCloudProvider) InsertDisk(ctx context.Context, zone string, diskToCreate *compute.Disk) (*compute.Operation, error) {
cloud.disks[diskToCreate.Name] = diskToCreate
return &compute.Operation{}, nil
}
func (cloud *FakeCloudProvider) DeleteDisk(ctx context.Context, zone, name string) (*compute.Operation, error) {
delete(cloud.disks, name)
return &compute.Operation{}, nil
}
func (cloud *FakeCloudProvider) AttachDisk(ctx context.Context, zone, instanceName string, attachedDisk *compute.AttachedDisk) (*compute.Operation, error) {
instance, ok := cloud.instances[instanceName]
if !ok {
return nil, fmt.Errorf("Failed to get instance %v", instanceName)
}
instance.Disks = append(instance.Disks, attachedDisk)
return nil, nil
}
func (cloud *FakeCloudProvider) DetachDisk(ctx context.Context, volumeZone, instanceName, volumeName string) (*compute.Operation, error) {
instance, ok := cloud.instances[instanceName]
if !ok {
return nil, fmt.Errorf("Failed to get instance %v", instanceName)
}
found := -1
for i, disk := range instance.Disks {
if disk.DeviceName == volumeName {
found = i
break
}
}
instance.Disks[found] = instance.Disks[len(instance.Disks)-1]
instance.Disks = instance.Disks[:len(instance.Disks)-1]
return nil, nil
}
/*
func (cloud *CloudProvider) GetDiskSourceURI(disk *compute.Disk, zone string) string {
projectsApiEndpoint := gceComputeAPIEndpoint + "projects/"
if cloud.service != nil {
projectsApiEndpoint = cloud.service.BasePath
}
return projectsApiEndpoint + fmt.Sprintf(
diskSourceURITemplateSingleZone,
cloud.project,
zone,
disk.Name)
}
func (cloud *CloudProvider) GetDiskTypeURI(zone, diskType string) string {
return fmt.Sprintf(diskTypeURITemplateSingleZone, cloud.project, zone, diskType)
}
*/
func (cloud *FakeCloudProvider) GetDiskSourceURI(disk *compute.Disk, zone string) string {
return ""
}
func (cloud *FakeCloudProvider) GetDiskTypeURI(zone, diskType string) string {
return ""
}
func (cloud *FakeCloudProvider) WaitForAttach(ctx context.Context, zone, diskName, instanceName string) error {
return nil
}
// Instance Methods
func (cloud *FakeCloudProvider) InsertInstance(instance *compute.Instance, instanceName string) {
cloud.instances[instanceName] = instance
return
}
func (cloud *FakeCloudProvider) GetInstanceOrError(ctx context.Context, instanceZone, instanceName string) (*compute.Instance, error) {
instance, ok := cloud.instances[instanceName]
if !ok {
return nil, notFoundError()
}
return instance, nil
}
// Operation Methods
func (cloud *FakeCloudProvider) WaitForOp(ctx context.Context, op *compute.Operation, zone string) error {
return nil
}
func notFoundError() *googleapi.Error {
return &googleapi.Error{
Errors: []googleapi.ErrorItem{
{
Reason: "notFound",
},
},
}
}