forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmock.go
More file actions
332 lines (268 loc) · 7.72 KB
/
Copy pathmock.go
File metadata and controls
332 lines (268 loc) · 7.72 KB
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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package mock
import (
"context"
"fmt"
"net"
"time"
"golang.org/x/time/rate"
"github.com/cilium/cilium/pkg/api/helpers"
"github.com/cilium/cilium/pkg/azure/types"
"github.com/cilium/cilium/pkg/ipam/service/ipallocator"
ipamTypes "github.com/cilium/cilium/pkg/ipam/types"
"github.com/cilium/cilium/pkg/lock"
)
// Operation is an Azure API operation that this mock API supports
type Operation int
const (
AllOperations Operation = iota
GetInstance
GetInstances
GetVpcsAndSubnets
AssignPrivateIpAddressesVMSS
UnassignPrivateIpAddressesVMSS
MaxOperation
)
type subnet struct {
subnet *ipamTypes.Subnet
allocator *ipallocator.Range
}
type API struct {
mutex lock.RWMutex
subnets map[string]*subnet
instances *ipamTypes.InstanceMap
vnets map[string]*ipamTypes.VirtualNetwork
errors map[Operation]error
delaySim *helpers.DelaySimulator
limiter *rate.Limiter
}
func NewAPI(subnets []*ipamTypes.Subnet, vnets []*ipamTypes.VirtualNetwork) *API {
api := &API{
instances: ipamTypes.NewInstanceMap(),
subnets: map[string]*subnet{},
vnets: map[string]*ipamTypes.VirtualNetwork{},
errors: map[Operation]error{},
delaySim: helpers.NewDelaySimulator(),
}
api.UpdateSubnets(subnets)
for _, v := range vnets {
api.vnets[v.ID] = v
}
return api
}
func (a *API) UpdateSubnets(subnets []*ipamTypes.Subnet) {
a.mutex.Lock()
a.subnets = map[string]*subnet{}
for _, s := range subnets {
_, cidr, _ := net.ParseCIDR(s.CIDR.String())
a.subnets[s.ID] = &subnet{
subnet: s.DeepCopy(),
allocator: ipallocator.NewCIDRRange(cidr),
}
}
a.mutex.Unlock()
}
func (a *API) UpdateInstances(instances *ipamTypes.InstanceMap) {
a.mutex.Lock()
a.updateInstancesLocked(instances)
a.mutex.Unlock()
}
func (a *API) updateInstancesLocked(instances *ipamTypes.InstanceMap) {
a.instances = instances.DeepCopy()
}
// SetMockError modifies the mock API to return an error for a particular
// operation
func (a *API) SetMockError(op Operation, err error) {
a.mutex.Lock()
a.errors[op] = err
a.mutex.Unlock()
}
// SetDelay specifies the delay which should be simulated for an individual
// Azure API operation
func (a *API) SetDelay(op Operation, delay time.Duration) {
if op == AllOperations {
for op := AllOperations + 1; op < MaxOperation; op++ {
a.delaySim.SetDelay(op, delay)
}
} else {
a.delaySim.SetDelay(op, delay)
}
}
// SetLimiter adds a rate limiter to all simulated API calls
func (a *API) SetLimiter(limit float64, burst int) {
a.limiter = rate.NewLimiter(rate.Limit(limit), burst)
}
func (a *API) rateLimit() {
a.mutex.RLock()
if a.limiter == nil {
a.mutex.RUnlock()
return
}
r := a.limiter.Reserve()
a.mutex.RUnlock()
if delay := r.Delay(); delay != time.Duration(0) && delay != rate.InfDuration {
time.Sleep(delay)
}
}
func (a *API) GetInstance(ctx context.Context, subnets ipamTypes.SubnetMap, instanceID string) (*ipamTypes.Instance, error) {
a.rateLimit()
a.delaySim.Delay(GetInstance)
a.mutex.RLock()
defer a.mutex.RUnlock()
if err, ok := a.errors[GetInstance]; ok {
return nil, err
}
instance := ipamTypes.Instance{}
instance.Interfaces = map[string]ipamTypes.InterfaceRevision{}
if err := a.instances.ForeachInterface(instanceID, func(instanceID, interfaceID string, iface ipamTypes.InterfaceRevision) error {
instance.Interfaces[interfaceID] = iface
return nil
}); err != nil {
return nil, err
}
return instance.DeepCopy(), nil
}
func (a *API) GetInstances(ctx context.Context, subnets ipamTypes.SubnetMap) (*ipamTypes.InstanceMap, error) {
a.rateLimit()
a.delaySim.Delay(GetInstances)
a.mutex.RLock()
defer a.mutex.RUnlock()
if err, ok := a.errors[GetInstances]; ok {
return nil, err
}
return a.instances.DeepCopy(), nil
}
func (a *API) GetVpcsAndSubnets(ctx context.Context) (ipamTypes.VirtualNetworkMap, ipamTypes.SubnetMap, error) {
a.rateLimit()
a.delaySim.Delay(GetVpcsAndSubnets)
a.mutex.RLock()
defer a.mutex.RUnlock()
if err, ok := a.errors[GetVpcsAndSubnets]; ok {
return nil, nil, err
}
vnets := ipamTypes.VirtualNetworkMap{}
subnets := ipamTypes.SubnetMap{}
for _, s := range a.subnets {
sd := s.subnet.DeepCopy()
sd.AvailableAddresses = s.allocator.Free()
subnets[sd.ID] = sd
}
for _, v := range a.vnets {
vnets[v.ID] = v.DeepCopy()
}
return vnets, subnets, nil
}
func (a *API) AssignPrivateIpAddressesVM(ctx context.Context, subnetID, interfaceName string, addresses int) error {
return nil
}
func (a *API) AssignPrivateIpAddressesVMSS(ctx context.Context, vmName, vmssName, subnetID, interfaceName string, addresses int) error {
a.rateLimit()
a.delaySim.Delay(AssignPrivateIpAddressesVMSS)
a.mutex.Lock()
defer a.mutex.Unlock()
if err, ok := a.errors[AssignPrivateIpAddressesVMSS]; ok {
return err
}
foundInterface := false
instances := a.instances.DeepCopy()
err := instances.ForeachInterface("", func(id, _ string, iface ipamTypes.InterfaceRevision) error {
intf, ok := iface.Resource.(*types.AzureInterface)
if !ok {
return fmt.Errorf("invalid interface object")
}
if intf.Name != interfaceName || intf.GetVMID() != vmName {
return nil
}
if len(intf.Addresses)+addresses > types.InterfaceAddressLimit {
return fmt.Errorf("exceeded interface limit")
}
s, ok := a.subnets[subnetID]
if !ok {
return fmt.Errorf("subnet %s does not exist", subnetID)
}
for range addresses {
ip, err := s.allocator.AllocateNext()
if err != nil {
panic("Unable to allocate IP from allocator")
}
intf.Addresses = append(intf.Addresses, types.AzureAddress{
IP: ip.String(),
Subnet: subnetID,
State: types.StateSucceeded,
})
}
foundInterface = true
return nil
})
if err != nil {
return err
}
a.updateInstancesLocked(instances)
if !foundInterface {
return fmt.Errorf("interface %s not found", interfaceName)
}
return nil
}
func (a *API) UnassignPrivateIpAddressesVM(ctx context.Context, interfaceName string, addresses []string) error {
return nil
}
func (a *API) UnassignPrivateIpAddressesVMSS(ctx context.Context, vmName, vmssName, interfaceName string, addresses []string) error {
a.rateLimit()
a.delaySim.Delay(UnassignPrivateIpAddressesVMSS)
a.mutex.Lock()
defer a.mutex.Unlock()
if err, ok := a.errors[UnassignPrivateIpAddressesVMSS]; ok {
return err
}
if len(addresses) == 0 {
return nil
}
releaseSet := make(map[string]struct{}, len(addresses))
for _, ip := range addresses {
releaseSet[ip] = struct{}{}
}
foundInterface := false
instances := a.instances.DeepCopy()
err := instances.ForeachInterface("", func(id, _ string, iface ipamTypes.InterfaceRevision) error {
intf, ok := iface.Resource.(*types.AzureInterface)
if !ok {
return fmt.Errorf("invalid interface object")
}
if intf.Name != interfaceName || intf.GetVMID() != vmName {
return nil
}
kept := intf.Addresses[:0]
for _, addr := range intf.Addresses {
if _, drop := releaseSet[addr.IP]; drop {
if s, ok := a.subnets[addr.Subnet]; ok {
_, ipNet, err := net.ParseCIDR(addr.IP + "/32")
if err == nil {
s.allocator.Release(ipNet.IP)
}
}
continue
}
kept = append(kept, addr)
}
intf.Addresses = kept
foundInterface = true
return nil
})
if err != nil {
return err
}
a.updateInstancesLocked(instances)
if !foundInterface {
return fmt.Errorf("interface %s not found", interfaceName)
}
return nil
}
func (a *API) AssignPublicIPAddressesVMSS(ctx context.Context, instanceID, vmssName string, publicIpTags ipamTypes.Tags) (string, error) {
a.rateLimit()
return "mock-public-ip-prefix-id", nil
}
func (a *API) AssignPublicIPAddressesVM(ctx context.Context, instanceID string, publicIpTags ipamTypes.Tags) (string, error) {
a.rateLimit()
return "mock-public-ip-prefix-id", nil
}