-
Notifications
You must be signed in to change notification settings - Fork 14
/
mock.go
379 lines (306 loc) · 10.3 KB
/
mock.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
Copyright 2020 Elotl Inc.
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 cloud
import (
"fmt"
"time"
"github.com/elotl/kip/pkg/api"
"github.com/elotl/kip/pkg/util"
"k8s.io/apimachinery/pkg/util/sets"
)
type MockCloudClient struct {
Instances map[string]CloudInstance
ContainerInstances map[string]ContainerInstance
ControllerID string
InsideVPC bool
VPCCIDRs []string
Subnets []SubnetAttributes
Starter func(node *api.Node, image Image, metadata string) (*StartNodeResult, error)
SpotStarter func(node *api.Node, image Image, metadata string) (*StartNodeResult, error)
Stopper func(instanceID string) error
Waiter func(node *api.Node) ([]api.NetworkAddress, error)
Lister func() ([]CloudInstance, error)
Resizer func(node *api.Node, size int64) (error, bool)
ContainerAuthorizer func() (string, string, error)
ImageGetter func(BootImageSpec) (Image, error)
InstanceListerFilter func([]string) ([]CloudInstance, error)
InstanceLister func() ([]CloudInstance, error)
DNSInfoGetter func() ([]string, []string, error)
RouteRemover func(string, string) error
RouteAdder func(string, string) error
StatusKeeperGetter func() StatusKeeper
SubnetGetter func() ([]SubnetAttributes, error)
AZGetter func() ([]string, error)
AvailabilityChecker func() (bool, error)
// Container Instance Funcs
ContainerClusterEnsurer func() error
ContainerInstanceLister func() ([]ContainerInstance, error)
ContainerInstanceListerFilter func(instIDs []string) ([]ContainerInstance, error)
ContainerInstancesStatusesGetter func(instIDs []string) (map[string][]api.UnitStatus, error)
ContainerInstanceRunner func(*api.Pod) (string, error)
ContainerInstanceStopper func(string) error
ContainerInstanceWaiter func(*api.Pod) (*api.Pod, error)
}
func (m *MockCloudClient) SetBootSecurityGroupIDs([]string) {
}
func (m *MockCloudClient) GetBootSecurityGroupIDs() []string {
return nil
}
func (m *MockCloudClient) StartNode(node *api.Node, image Image, metadata string) (*StartNodeResult, error) {
return m.Starter(node, image, metadata)
}
func (m *MockCloudClient) StartSpotNode(node *api.Node, image Image, metadata string) (*StartNodeResult, error) {
return m.SpotStarter(node, image, metadata)
}
func (m *MockCloudClient) StopInstance(instanceID string) error {
return m.Stopper(instanceID)
}
func (m *MockCloudClient) WaitForRunning(node *api.Node) ([]api.NetworkAddress, error) {
return m.Waiter(node)
}
func (m *MockCloudClient) ResizeVolume(node *api.Node, size int64) (error, bool) {
return m.Resizer(node, size)
}
func (m *MockCloudClient) GetRegistryAuth() (string, string, error) {
return m.ContainerAuthorizer()
}
func (m *MockCloudClient) GetImage(spec BootImageSpec) (Image, error) {
return m.ImageGetter(spec)
}
func (m *MockCloudClient) SetSustainedCPU(n *api.Node, enabled bool) error {
return nil
}
func (m *MockCloudClient) AddInstanceTags(string, map[string]string) error {
return nil
}
func (c *MockCloudClient) CloudStatusKeeper() StatusKeeper {
return c.StatusKeeperGetter()
}
func (c *MockCloudClient) GetSubnets() ([]SubnetAttributes, error) {
return c.SubnetGetter()
}
func (c *MockCloudClient) GetAvailabilityZones() ([]string, error) {
return c.AZGetter()
}
func (c *MockCloudClient) IsAvailable() (bool, error) {
return c.AvailabilityChecker()
}
func (c *MockCloudClient) EnsureMilpaSecurityGroups([]string, []string) error {
return nil
}
func (c *MockCloudClient) ListInstancesFilterID(iid []string) ([]CloudInstance, error) {
return c.InstanceListerFilter(iid)
}
func (c *MockCloudClient) ListInstances() ([]CloudInstance, error) {
return c.InstanceLister()
}
func (e *MockCloudClient) CreateSGName(svcName string) string {
return fmt.Sprintf("%s.%s.%s", e.ControllerID, "default", svcName)
}
func (e *MockCloudClient) ConnectWithPublicIPs() bool {
return e.InsideVPC
}
func (e *MockCloudClient) ModifySourceDestinationCheck(iid string, enable bool) error {
return nil
}
func (e *MockCloudClient) GetDNSInfo() ([]string, []string, error) {
return e.DNSInfoGetter()
}
func (e *MockCloudClient) RemoveRoute(destinationCIDR, nextHop string) error {
return e.RouteRemover(destinationCIDR, nextHop)
}
func (e *MockCloudClient) AddRoute(destinationCIDR, instanceID string) error {
return e.RouteAdder(destinationCIDR, instanceID)
}
func (e *MockCloudClient) GetVPCCIDRs() []string {
return e.VPCCIDRs
}
func (e *MockCloudClient) AddInstances(insts ...CloudInstance) {
for _, inst := range insts {
e.Instances[inst.ID] = inst
}
}
func (m *MockCloudClient) GetAttributes() CloudAttributes {
return CloudAttributes{
DiskProductName: api.StorageGP2,
FixedSizeVolume: false,
Provider: ProviderAWS,
Region: "us-east-1",
Zone: m.Subnets[0].AZ,
}
}
func (m *MockCloudClient) EnsureContainerInstanceCluster() error {
return m.ContainerClusterEnsurer()
}
func (m *MockCloudClient) ListContainerInstances() ([]ContainerInstance, error) {
return m.ContainerInstanceLister()
}
func (m *MockCloudClient) ListContainerInstancesFilterID(insts []string) ([]ContainerInstance, error) {
return m.ContainerInstanceListerFilter(insts)
}
func (m *MockCloudClient) GetContainerInstancesStatuses(instIDs []string) (map[string][]api.UnitStatus, error) {
return m.ContainerInstancesStatusesGetter(instIDs)
}
func (m *MockCloudClient) StartContainerInstance(pod *api.Pod) (string, error) {
return m.ContainerInstanceRunner(pod)
}
func (m *MockCloudClient) StopContainerInstance(instID string) error {
return m.ContainerInstanceStopper(instID)
}
func (m *MockCloudClient) WaitForContainerInstanceRunning(pod *api.Pod) (*api.Pod, error) {
return m.ContainerInstanceWaiter(pod)
}
func (m *MockCloudClient) AttachSecurityGroups(node *api.Node, groups []string) error {
return nil
}
func (m *MockCloudClient) AddIAMPermissions(node *api.Node, permissions string) error {
return nil
}
func NewMockClient() *MockCloudClient {
net := &MockCloudClient{
Instances: make(map[string]CloudInstance),
ContainerInstances: make(map[string]ContainerInstance),
ControllerID: "test_cluster",
InsideVPC: true,
VPCCIDRs: []string{"172.20.0.0/16"},
Subnets: []SubnetAttributes{
{
ID: "sub-1111",
CIDR: "172.16.0.0/16",
AZ: "us-east-1a",
AddressAffinity: PublicAddress,
AvailableAddresses: 250,
},
{
ID: "sub-2222",
CIDR: "172.17.0.0/16",
AZ: "us-east-1b",
AddressAffinity: PrivateAddress,
AvailableAddresses: 250,
},
{
ID: "sub-3333",
CIDR: "172.18.0.0/16",
AZ: "us-east-1c",
AddressAffinity: PublicAddress,
AvailableAddresses: 250,
},
},
}
net.InstanceListerFilter = func(iid []string) ([]CloudInstance, error) {
insts := make([]CloudInstance, 0, len(iid))
for _, inst := range net.Instances {
if util.StringInSlice(inst.ID, iid) {
insts = append(insts, inst)
}
}
return insts, nil
}
net.InstanceLister = func() ([]CloudInstance, error) {
insts := make([]CloudInstance, 0, len(net.Instances))
for _, inst := range net.Instances {
insts = append(insts, inst)
}
return insts, nil
}
net.ContainerInstanceRunner = func(p *api.Pod) (string, error) {
id := p.Status.BoundInstanceID
inst := ContainerInstance{ID: id}
net.ContainerInstances[id] = inst
return id, nil
}
net.ContainerInstanceStopper = func(id string) error {
if _, exists := net.ContainerInstances[id]; !exists {
return fmt.Errorf("Container Instance %s does not exist", id)
}
delete(net.ContainerInstances, id)
return nil
}
net.ContainerInstanceListerFilter = func(ids []string) ([]ContainerInstance, error) {
insts := make([]ContainerInstance, 0, len(ids))
for _, inst := range net.ContainerInstances {
if util.StringInSlice(inst.ID, ids) {
insts = append(insts, inst)
}
}
return insts, nil
}
net.ContainerInstanceLister = func() ([]ContainerInstance, error) {
insts := make([]ContainerInstance, 0, len(net.ContainerInstances))
for _, inst := range net.ContainerInstances {
insts = append(insts, inst)
}
return insts, nil
}
net.ContainerInstanceWaiter = func(p *api.Pod) (*api.Pod, error) {
return p, nil
}
net.RouteRemover = func(destinationCIDR, nextHop string) error {
return nil
}
net.RouteAdder = func(destinationCIDR, nextHop string) error {
return nil
}
net.AvailabilityChecker = func() (bool, error) {
return true, nil
}
net.ImageGetter = func(BootImageSpec) (Image, error) {
t := time.Now()
img := Image{
ID: "1234",
Name: "MockImage",
RootDevice: "/dev/xvda",
CreationTime: &t,
}
return img, nil
}
net.DNSInfoGetter = func() ([]string, []string, error) {
return []string{"cloud.internal"}, []string{"1.1.1.1"}, nil
}
net.SubnetGetter = func() ([]SubnetAttributes, error) {
return net.Subnets, nil
}
net.AZGetter = func() ([]string, error) {
sns, err := net.GetSubnets()
if err != nil {
return nil, err
}
azs := sets.NewString()
for _, sn := range sns {
if len(sn.AZ) > 0 {
azs.Insert(sn.AZ)
}
}
return azs.List(), nil
}
net.StatusKeeperGetter = func() StatusKeeper {
status, _ := NewLinkedAZSubnetStatus(net)
return status
}
net.Starter = func(node *api.Node, image Image, metadata string) (*StartNodeResult, error) {
inst := CloudInstance{
ID: node.Status.InstanceID,
NodeName: node.Name,
}
net.Instances[node.Status.InstanceID] = inst
return nil, nil
}
net.Stopper = func(instID string) error {
if _, exists := net.Instances[instID]; !exists {
return fmt.Errorf("Instance %s does not exist", instID)
}
delete(net.Instances, instID)
return nil
}
return net
}