-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
mock_gce_cloud.go
190 lines (157 loc) · 6.03 KB
/
mock_gce_cloud.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
/*
Copyright 2017 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 gce
import (
"fmt"
"google.golang.org/api/cloudresourcemanager/v1"
compute "google.golang.org/api/compute/v1"
"google.golang.org/api/storage/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
"k8s.io/kops/cloudmock/gce/mockcloudresourcemanager"
mockcompute "k8s.io/kops/cloudmock/gce/mockcompute"
"k8s.io/kops/cloudmock/gce/mockdns"
"k8s.io/kops/cloudmock/gce/mockiam"
"k8s.io/kops/cloudmock/gce/mockstorage"
"k8s.io/kops/dnsprovider/pkg/dnsprovider"
dnsproviderclouddns "k8s.io/kops/dnsprovider/pkg/dnsprovider/providers/google/clouddns"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/cloudinstances"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/gce"
)
// MockGCECloud is a mock implementation of GCECloud for testing
type MockGCECloud struct {
project string
region string
labels map[string]string
computeClient *mockcompute.MockClient
dnsClient *mockdns.MockClient
iamClient *mockiam.MockClient
storageClient *storage.Service
cloudResourceManagerClient *cloudresourcemanager.Service
}
var _ gce.GCECloud = &MockGCECloud{}
// InstallMockGCECloud registers a MockGCECloud implementation for the specified region & project
func InstallMockGCECloud(region string, project string) *MockGCECloud {
c := &MockGCECloud{
project: project,
region: region,
computeClient: mockcompute.NewMockClient(project),
dnsClient: mockdns.NewMockClient(),
iamClient: mockiam.NewMockClient(project),
storageClient: mockstorage.New(),
cloudResourceManagerClient: mockcloudresourcemanager.New(),
}
gce.CacheGCECloudInstance(region, project, c)
return c
}
func (c *MockGCECloud) AllResources() map[string]interface{} {
return c.computeClient.AllResources()
}
// GetCloudGroups is not implemented yet
func (c *MockGCECloud) GetCloudGroups(cluster *kops.Cluster, instancegroups []*kops.InstanceGroup, warnUnmatched bool, nodes []v1.Node) (map[string]*cloudinstances.CloudInstanceGroup, error) {
klog.V(8).Infof("MockGCECloud cloud provider GetCloudGroups not implemented yet")
return nil, fmt.Errorf("MockGCECloud cloud provider does not support getting cloud groups at this time")
}
// Zones is not implemented yet
func (c *MockGCECloud) Zones() ([]string, error) {
return nil, fmt.Errorf("not yet implemented")
}
// WithLabels returns a copy of the MockGCECloud bound to the specified labels
func (c *MockGCECloud) WithLabels(labels map[string]string) gce.GCECloud {
i := &MockGCECloud{}
*i = *c
i.labels = labels
return i
}
// ProviderID implements fi.Cloud::ProviderID
func (c *MockGCECloud) ProviderID() kops.CloudProviderID {
return kops.CloudProviderGCE
}
// FindVPCInfo implements fi.Cloud::FindVPCInfo
func (c *MockGCECloud) FindVPCInfo(id string) (*fi.VPCInfo, error) {
return nil, fmt.Errorf("MockGCECloud::MockGCECloud not implemented")
}
// DNS implements fi.Cloud::DNS
func (c *MockGCECloud) DNS() (dnsprovider.Interface, error) {
return dnsproviderclouddns.NewFakeInterface()
}
// Compute implements GCECloud::Compute
func (c *MockGCECloud) Compute() gce.ComputeClient {
return c.computeClient
}
// Storage implements GCECloud::Storage
func (c *MockGCECloud) Storage() *storage.Service {
return c.storageClient
}
// IAM returns the IAM client
func (c *MockGCECloud) IAM() gce.IamClient {
return c.iamClient
}
// CloudResourceManager returns the client for the cloudresourcemanager API
func (c *MockGCECloud) CloudResourceManager() *cloudresourcemanager.Service {
return c.cloudResourceManagerClient
}
// CloudDNS returns the DNS client
func (c *MockGCECloud) CloudDNS() gce.DNSClient {
return c.dnsClient
}
// WaitForOp implements GCECloud::WaitForOp
func (c *MockGCECloud) WaitForOp(op *compute.Operation) error {
if op.Status != "DONE" {
return fmt.Errorf("unexpected operation: %+v", op)
}
return nil
}
// FindClusterStatus implements GCECloud::FindClusterStatus
func (c *MockGCECloud) FindClusterStatus(cluster *kops.Cluster) (*kops.ClusterStatus, error) {
return nil, fmt.Errorf("MockGCECloud::FindClusterStatus not implemented")
}
// GetApiIngressStatus implements GCECloud::GetApiIngressStatus
func (c *MockGCECloud) GetApiIngressStatus(cluster *kops.Cluster) ([]fi.ApiIngressStatus, error) {
return nil, fmt.Errorf("MockGCECloud::GetApiIngressStatus not implemented")
}
// Region implements GCECloud::Region
func (c *MockGCECloud) Region() string {
return c.region
}
// Project implements GCECloud::Project
func (c *MockGCECloud) Project() string {
return c.project
}
// ServiceAccount implements GCECloud::ServiceAccount
func (c *MockGCECloud) ServiceAccount() (string, error) {
return "12345678-compute@developer.gserviceaccount.com", nil
}
// Labels implements GCECloud::Labels
func (c *MockGCECloud) Labels() map[string]string {
return c.labels
}
// DeleteGroup implements fi.Cloud::DeleteGroup
func (c *MockGCECloud) DeleteGroup(g *cloudinstances.CloudInstanceGroup) error {
return nil
// return deleteCloudInstanceGroup(c, g)
}
// DeleteInstance deletes a GCE instance
func (c *MockGCECloud) DeleteInstance(i *cloudinstances.CloudInstance) error {
return nil
// return recreateCloudInstance(c, i)
}
func (c *MockGCECloud) DeregisterInstance(i *cloudinstances.CloudInstance) error {
return nil
}
// DetachInstance is not implemented yet. It needs to cause a cloud instance to no longer be counted against the group's size limits.
func (c *MockGCECloud) DetachInstance(i *cloudinstances.CloudInstance) error {
return nil
}