-
Notifications
You must be signed in to change notification settings - Fork 150
/
eni.go
450 lines (392 loc) · 12.9 KB
/
eni.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package daemon
import (
"context"
"fmt"
"math/rand"
"sort"
"strings"
"sync"
"time"
"github.com/AliyunContainerService/terway/deviceplugin"
"github.com/AliyunContainerService/terway/pkg/aliyun"
"github.com/AliyunContainerService/terway/pkg/ipam"
"github.com/AliyunContainerService/terway/pkg/logger"
"github.com/AliyunContainerService/terway/pkg/pool"
"github.com/AliyunContainerService/terway/pkg/tracing"
"github.com/AliyunContainerService/terway/types"
"github.com/aliyun/alibaba-cloud-sdk-go/services/vpc"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
)
var eniLog = logger.DefaultLogger
const (
// vSwitchIPCntTimeout is the duration for the vswitchIPCntMap content's effectiveness
vSwitchIPCntTimeout = 10 * time.Minute
typeNameENI = "eni"
poolNameENI = "eni"
factoryNameENI = "eni"
tracingKeyVSwitches = "vswitches"
tracingKeyVSwitchSelectionPolicy = "vswitch_selection_policy"
tracingKeyCacheExpireAt = "cache_expire_at"
)
type eniResourceManager struct {
pool pool.ObjectPool
ecs ipam.API
trunkENI *types.ENI
}
func newENIResourceManager(poolConfig *types.PoolConfig, ecs ipam.API, allocatedResources map[string]resourceManagerInitItem, ipFamily *types.IPFamily, k8s Kubernetes) (ResourceManager, error) {
eniLog.Debugf("new ENI Resource Manager, pool config: %+v, allocated resources: %+v", poolConfig, allocatedResources)
factory, err := newENIFactory(poolConfig, ecs)
if err != nil {
return nil, errors.Wrapf(err, "error create ENI factory")
}
_ = tracing.Register(tracing.ResourceTypeFactory, factoryNameENI, factory)
var capacity, memberLimit int
if !poolConfig.DisableDevicePlugin {
limit, err := aliyun.GetLimit(ecs, aliyun.GetInstanceMeta().InstanceType)
if err != nil {
return nil, fmt.Errorf("error get max eni for eni factory, %w", err)
}
capacity = limit.Adapters
capacity = int(float64(capacity)*poolConfig.EniCapRatio) + poolConfig.EniCapShift - 1
if poolConfig.MaxENI != 0 && poolConfig.MaxENI < capacity {
capacity = poolConfig.MaxENI
}
if poolConfig.MaxPoolSize > capacity {
poolConfig.MaxPoolSize = capacity
}
if poolConfig.MinENI != 0 {
poolConfig.MinPoolSize = poolConfig.MinENI
}
memberLimit = limit.MemberAdapterLimit
if poolConfig.ENICapPolicy == types.ENICapPolicyPreferTrunk {
memberLimit = limit.MaxMemberAdapterLimit
}
} else {
// NB(l1b0k): adapt DisableDevicePlugin func, will refactor latter
capacity = 1
memberLimit = 0
poolConfig.MaxPoolSize = 1
poolConfig.MinPoolSize = 0
}
var trunkENI *types.ENI
if poolConfig.WaitTrunkENI {
logger.DefaultLogger.Infof("waitting trunk eni ready")
factory.trunkOnEni, err = k8s.WaitTrunkReady()
if err != nil {
return nil, err
}
logger.DefaultLogger.Infof("trunk eni found %s", factory.trunkOnEni)
}
poolCfg := pool.Config{
Name: poolNameENI,
Type: typeNameENI,
MaxIdle: poolConfig.MaxPoolSize,
MinIdle: poolConfig.MinPoolSize,
Capacity: capacity,
Factory: factory,
Initializer: func(holder pool.ResourceHolder) error {
ctx := context.Background()
enis, err := ecs.GetAttachedENIs(ctx, false)
if err != nil {
return fmt.Errorf("error get attach ENI on pool init, %w", err)
}
if factory.enableTrunk && memberLimit > 0 {
logger.DefaultLogger.Infof("lookup trunk eni")
for _, eni := range enis {
if eni.Trunk {
logger.DefaultLogger.Infof("find trunk eni %s", eni.ID)
factory.trunkOnEni = eni.ID
}
if eni.ID == factory.trunkOnEni {
trunkENI = eni
eni.Trunk = true
}
}
if factory.trunkOnEni == "" && len(enis) < capacity {
trunkENIRes, err := factory.CreateWithIPCount(1, true)
if err != nil {
return errors.Wrapf(err, "error init trunk eni")
}
trunkENI, _ = trunkENIRes[0].(*types.ENI)
factory.trunkOnEni = trunkENI.ID
enis = append(enis, trunkENI)
}
}
for _, e := range enis {
if ipFamily.IPv6 {
_, ipv6, err := ecs.GetENIIPs(ctx, e.MAC)
if err != nil || len(ipv6) == 0 {
return errors.Wrapf(err, "error get eni ip")
}
e.PrimaryIP.IPv6 = ipv6[0]
}
if item, ok := allocatedResources[e.GetResourceID()]; ok {
holder.AddInuse(e, podInfoKey(item.podInfo.Namespace, item.podInfo.Name))
} else {
holder.AddIdle(e)
}
}
return nil
},
}
p, err := pool.NewSimpleObjectPool(poolCfg)
if err != nil {
return nil, err
}
mgr := &eniResourceManager{
pool: p,
ecs: ecs,
trunkENI: trunkENI,
}
if poolConfig.DisableDevicePlugin {
return mgr, nil
}
//init deviceplugin for ENI
realCap := 0
eniType := deviceplugin.ENITypeENI
if !poolConfig.EnableENITrunking {
realCap = capacity
}
// report only trunk is created
if poolConfig.EnableENITrunking && factory.trunkOnEni != "" {
eniType = deviceplugin.ENITypeMember
realCap = memberLimit
err = k8s.PatchTrunkInfo(factory.trunkOnEni)
if err != nil {
return nil, errors.Wrapf(err, "error patch trunk info on node")
}
}
logger.DefaultLogger.Infof("set deviceplugin cap %d", realCap)
dp := deviceplugin.NewENIDevicePlugin(realCap, eniType)
err = dp.Serve()
if err != nil {
return nil, errors.Wrapf(err, "error set deviceplugin on node")
}
return mgr, nil
}
func (m *eniResourceManager) Allocate(ctx *networkContext, prefer string) (types.NetworkResource, error) {
return m.pool.Acquire(ctx, prefer, podInfoKey(ctx.pod.Namespace, ctx.pod.Name))
}
func (m *eniResourceManager) Release(context *networkContext, resItem types.ResourceItem) error {
if context != nil && context.pod != nil {
return m.pool.ReleaseWithReservation(resItem.ID, context.pod.IPStickTime)
}
return m.pool.Release(resItem.ID)
}
func (m *eniResourceManager) GarbageCollection(inUseResSet map[string]types.ResourceItem, expireResSet map[string]types.ResourceItem) error {
for expireRes, expireItem := range expireResSet {
if _, err := m.pool.Stat(expireRes); err == nil {
err = m.Release(nil, expireItem)
if err != nil {
return err
}
}
}
return nil
}
func (m *eniResourceManager) Stat(context *networkContext, resID string) (types.NetworkResource, error) {
return m.pool.Stat(resID)
}
func (m *eniResourceManager) GetResourceMapping() (tracing.ResourcePoolStats, error) {
return m.pool.GetResourceMapping()
}
// MapSorter is a slice container for sorting
type MapSorter []Item
// Item is the element type of MapSorter, which contains the values for sorting
type Item struct {
Key string
Val int
}
func newMapSorter(m map[string]int) MapSorter {
ms := make(MapSorter, 0, len(m))
for k, v := range m {
ms = append(ms, Item{k, v})
}
return ms
}
// SortInDescendingOrder is a bubble sort per element's value
func (ms MapSorter) SortInDescendingOrder() {
eniLog.Debugf("before sorting, slice = %+v", ms)
sort.Slice(ms, func(i, j int) bool {
// reverse sorting
return ms[i].Val > ms[j].Val
})
eniLog.Debugf("after sorting, slice = %+v", ms)
}
type eniFactory struct {
name string
enableTrunk bool
trunkOnEni string
switches []string
eniTags map[string]string
securityGroups []string
instanceID string
ecs ipam.API
vswitchIPCntMap map[string]int
tsExpireAt time.Time
vswitchSelectionPolicy string
disableSecurityGroupCheck bool
sync.RWMutex
}
func newENIFactory(poolConfig *types.PoolConfig, ecs ipam.API) (*eniFactory, error) {
if len(poolConfig.SecurityGroups) == 0 {
securityGroups, err := ecs.GetAttachedSecurityGroups(context.Background(), poolConfig.InstanceID)
if err != nil {
return nil, errors.Wrapf(err, "error get security group on factory init")
}
poolConfig.SecurityGroups = securityGroups
}
return &eniFactory{
name: factoryNameENI,
switches: poolConfig.VSwitch,
eniTags: poolConfig.ENITags,
securityGroups: poolConfig.SecurityGroups,
enableTrunk: poolConfig.EnableENITrunking,
instanceID: poolConfig.InstanceID,
ecs: ecs,
vswitchIPCntMap: make(map[string]int),
vswitchSelectionPolicy: poolConfig.VSwitchSelectionPolicy,
disableSecurityGroupCheck: poolConfig.DisableSecurityGroupCheck,
}, nil
}
func (f *eniFactory) GetVSwitches() ([]string, error) {
var vSwitches []string
vswCnt := len(f.switches)
// If there is ONLY ONE vswitch, then there is no need for ordering per switches' available IP counts,
// return the slice with only this vswitch.
if vswCnt == 1 {
return f.switches, nil
}
if f.vswitchSelectionPolicy == types.VSwitchSelectionPolicyRandom {
vSwitches = make([]string, vswCnt)
copy(vSwitches, f.switches)
rand.Seed(time.Now().UnixNano())
rand.Shuffle(vswCnt, func(i, j int) { vSwitches[i], vSwitches[j] = vSwitches[j], vSwitches[i] })
return vSwitches, nil
}
if f.vswitchSelectionPolicy == types.VSwitchSelectionPolicyOrdered {
// If VSwitchSelectionPolicy is ordered, then call f.ecs.DescribeVSwitch API to get the switch's available IP count
// PS: this is only feasible for systems with RAM policy for VPC API permission.
// Use f.vswitchIPCntMap to track IP count + vswitch ID
var (
start = time.Now()
err error
)
// If f.vswitchIPCntMap is empty, then fill in the map with switch + switch's available IP count.
f.Lock()
if (len(f.vswitchIPCntMap) == 0 && f.tsExpireAt.IsZero()) || start.After(f.tsExpireAt) {
// Loop vswitch slice to get each vswitch's available IP count.
for _, vswitch := range f.switches {
var vsw *vpc.VSwitch
vsw, err = f.ecs.DescribeVSwitchByID(context.Background(), vswitch)
if err != nil {
f.vswitchIPCntMap[vswitch] = 0
} else {
f.vswitchIPCntMap[vswitch] = int(vsw.AvailableIpAddressCount)
}
}
if err == nil {
// don't cache result when error
f.tsExpireAt = time.Now().Add(vSwitchIPCntTimeout)
}
}
f.Unlock()
if len(f.vswitchIPCntMap) > 0 {
m := newMapSorter(f.vswitchIPCntMap)
//sort.Sort(sort.Reverse(m))
m.SortInDescendingOrder()
for _, item := range m {
vSwitches = append(vSwitches, item.Key)
}
} else {
vSwitches = f.switches
}
}
return vSwitches, nil
}
func (f *eniFactory) Create(int) ([]types.NetworkResource, error) {
return f.CreateWithIPCount(1, false)
}
func (f *eniFactory) CreateWithIPCount(count int, trunk bool) ([]types.NetworkResource, error) {
vSwitches, _ := f.GetVSwitches()
eniLog.Infof("adjusted vswitch slice: %+v", vSwitches)
tags := map[string]string{
types.NetworkInterfaceTagCreatorKey: types.NetworkInterfaceTagCreatorValue,
}
for k, v := range f.eniTags {
tags[k] = v
}
eni, err := f.ecs.AllocateENI(context.Background(), vSwitches[0], f.securityGroups, f.instanceID, trunk, count, tags)
if err != nil {
return nil, err
}
return []types.NetworkResource{eni}, nil
}
func (f *eniFactory) Dispose(resource types.NetworkResource) error {
eni := resource.(*types.ENI)
if f.enableTrunk && eni.Trunk {
return fmt.Errorf("trunk ENI %+v will not dispose", eni.ID)
}
return f.ecs.FreeENI(context.Background(), eni.ID, f.instanceID)
}
func (f *eniFactory) Config() []tracing.MapKeyValueEntry {
config := []tracing.MapKeyValueEntry{
{Key: tracingKeyName, Value: f.name},
{Key: tracingKeyVSwitches, Value: strings.Join(f.switches, " ")},
{Key: tracingKeyVSwitchSelectionPolicy, Value: f.vswitchSelectionPolicy},
}
return config
}
func (f *eniFactory) Trace() []tracing.MapKeyValueEntry {
trace := []tracing.MapKeyValueEntry{
{Key: tracingKeyCacheExpireAt, Value: fmt.Sprint(f.tsExpireAt)},
}
for vs, cnt := range f.vswitchIPCntMap {
key := fmt.Sprintf("vswitch/%s/ip_count", vs)
trace = append(trace, tracing.MapKeyValueEntry{
Key: key,
Value: fmt.Sprint(cnt),
})
}
return trace
}
func (f *eniFactory) Execute(cmd string, _ []string, message chan<- string) {
switch cmd {
case commandMapping:
mapping, err := f.ListResource()
message <- fmt.Sprintf("mapping: %v, err: %s\n", mapping, err)
default:
message <- "can't recognize command\n"
}
close(message)
}
func (f *eniFactory) Check(res types.NetworkResource) error {
eni, ok := res.(*types.ENI)
if !ok {
return fmt.Errorf("unsupported type %T", res)
}
_, err := f.ecs.GetENIByMac(context.Background(), eni.MAC)
return err
}
func (f *eniFactory) ListResource() (map[string]types.NetworkResource, error) {
enis, err := f.ecs.GetAttachedENIs(context.Background(), false)
if err != nil {
return nil, err
}
mapping := make(map[string]types.NetworkResource, len(enis))
for i := 0; i < len(enis); i++ {
mapping[enis[i].GetResourceID()] = enis[i]
}
return mapping, nil
}
func (f *eniFactory) Reconcile() {
// check security group
if f.disableSecurityGroupCheck {
return
}
err := f.ecs.CheckEniSecurityGroup(context.Background(), f.securityGroups)
if err != nil {
_ = tracing.RecordNodeEvent(corev1.EventTypeWarning, "ResourceInvalid", fmt.Sprintf("eni has misconfiged security group. %s", err.Error()))
}
}