-
Notifications
You must be signed in to change notification settings - Fork 346
/
ip_scheduler.go
403 lines (357 loc) · 12.8 KB
/
ip_scheduler.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
// Copyright 2023 Antrea 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 egress
import (
"sort"
"strconv"
"sync"
"sync/atomic"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/sets"
corev1informers "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"
"antrea.io/antrea/pkg/agent/memberlist"
"antrea.io/antrea/pkg/agent/types"
crdv1a2 "antrea.io/antrea/pkg/apis/crd/v1alpha2"
crdinformers "antrea.io/antrea/pkg/client/informers/externalversions/crd/v1alpha2"
crdlisters "antrea.io/antrea/pkg/client/listers/crd/v1alpha2"
)
const (
// workItem is the only item that will be enqueued, used to trigger Egress IP scheduling.
workItem = "key"
)
// scheduleEventHandler is a callback when an Egress is rescheduled.
type scheduleEventHandler func(egress string)
// scheduleResult is the schedule result of an Egress, including the effective Egress IP and Node.
type scheduleResult struct {
ip string
node string
}
// egressIPScheduler is responsible for scheduling Egress IPs to appropriate Nodes according to the Node selector of the
// IP pool, taking Node's capacity into consideration.
type egressIPScheduler struct {
// cluster is responsible for selecting a Node for a given IP and pool.
cluster memberlist.Interface
egressLister crdlisters.EgressLister
egressListerSynced cache.InformerSynced
// queue is used to trigger scheduling. Triggering multiple times before the item is consumed will only cause one
// execution of scheduling.
queue workqueue.Interface
// mutex is used to protect scheduleResults.
mutex sync.RWMutex
scheduleResults map[string]*scheduleResult
// scheduledOnce indicates whether scheduling has been executed at lease once.
scheduledOnce *atomic.Bool
// eventHandlers is the registered callbacks.
eventHandlers []scheduleEventHandler
// The default maximum number of Egress IPs a Node can accommodate.
maxEgressIPsPerNode int
// nodeToMaxEgressIPs caches the maximum number of Egress IPs of each Node gotten from Node annotation.
// It takes precedence over the default value.
nodeToMaxEgressIPs map[string]int
nodeToMaxEgressIPsMutex sync.RWMutex
}
func NewEgressIPScheduler(cluster memberlist.Interface, egressInformer crdinformers.EgressInformer, nodeInformer corev1informers.NodeInformer, maxEgressIPsPerNode int) *egressIPScheduler {
s := &egressIPScheduler{
cluster: cluster,
egressLister: egressInformer.Lister(),
egressListerSynced: egressInformer.Informer().HasSynced,
scheduleResults: map[string]*scheduleResult{},
scheduledOnce: &atomic.Bool{},
maxEgressIPsPerNode: maxEgressIPsPerNode,
nodeToMaxEgressIPs: map[string]int{},
queue: workqueue.New(),
}
egressInformer.Informer().AddEventHandlerWithResyncPeriod(
cache.ResourceEventHandlerFuncs{
AddFunc: s.addEgress,
UpdateFunc: s.updateEgress,
DeleteFunc: s.deleteEgress,
},
resyncPeriod,
)
nodeInformer.Informer().AddEventHandlerWithResyncPeriod(
cache.ResourceEventHandlerFuncs{
AddFunc: s.updateNode,
UpdateFunc: func(_, newObj interface{}) {
s.updateNode(newObj)
},
DeleteFunc: s.deleteNode,
},
resyncPeriod,
)
s.cluster.AddClusterEventHandler(func(poolName string) {
// Trigger scheduling regardless of which pool is changed.
s.queue.Add(workItem)
})
return s
}
func getMaxEgressIPsFromAnnotation(node *corev1.Node) (int, bool, error) {
maxEgressIPsStr, exists := node.Annotations[types.NodeMaxEgressIPsAnnotationKey]
if !exists {
return 0, false, nil
}
maxEgressIPs, err := strconv.Atoi(maxEgressIPsStr)
if err != nil {
return 0, false, err
}
return maxEgressIPs, true, nil
}
// updateNode processes Node ADD and UPDATE events.
func (s *egressIPScheduler) updateNode(obj interface{}) {
node := obj.(*corev1.Node)
maxEgressIPs, found, err := getMaxEgressIPsFromAnnotation(node)
if err != nil {
klog.ErrorS(err, "The Node's max-egress-ips annotation was invalid", "node", node.Name)
if s.deleteMaxEgressIPsByNode(node.Name) {
s.queue.Add(workItem)
}
return
}
if !found {
if s.deleteMaxEgressIPsByNode(node.Name) {
s.queue.Add(workItem)
}
return
}
if s.updateMaxEgressIPsByNode(node.Name, maxEgressIPs) {
s.queue.Add(workItem)
}
}
// deleteNode processes Node DELETE events.
func (s *egressIPScheduler) deleteNode(obj interface{}) {
node, ok := obj.(*corev1.Node)
if !ok {
deletedState, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
klog.Errorf("Received unexpected object: %v", obj)
return
}
node, ok = deletedState.Obj.(*corev1.Node)
if !ok {
klog.Errorf("DeletedFinalStateUnknown contains non-Node object: %v", deletedState.Obj)
return
}
}
s.deleteMaxEgressIPsByNode(node.Name)
}
// addEgress processes Egress ADD events.
func (s *egressIPScheduler) addEgress(obj interface{}) {
egress := obj.(*crdv1a2.Egress)
if !isEgressSchedulable(egress) {
return
}
s.queue.Add(workItem)
klog.V(2).InfoS("Egress ADD event triggered Egress IP scheduling", "egress", klog.KObj(egress))
}
// updateEgress processes Egress UPDATE events.
func (s *egressIPScheduler) updateEgress(old, cur interface{}) {
oldEgress := old.(*crdv1a2.Egress)
curEgress := cur.(*crdv1a2.Egress)
if !isEgressSchedulable(oldEgress) && !isEgressSchedulable(curEgress) {
return
}
if oldEgress.Spec.EgressIP == curEgress.Spec.EgressIP && oldEgress.Spec.ExternalIPPool == curEgress.Spec.ExternalIPPool {
return
}
s.queue.Add(workItem)
klog.V(2).InfoS("Egress UPDATE event triggered Egress IP scheduling", "egress", klog.KObj(curEgress))
}
// deleteEgress processes Egress DELETE events.
func (s *egressIPScheduler) deleteEgress(obj interface{}) {
egress, ok := obj.(*crdv1a2.Egress)
if !ok {
deletedState, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
klog.Errorf("Received unexpected object: %v", obj)
return
}
egress, ok = deletedState.Obj.(*crdv1a2.Egress)
if !ok {
klog.Errorf("DeletedFinalStateUnknown contains non-Egress object: %v", deletedState.Obj)
return
}
}
if !isEgressSchedulable(egress) {
return
}
s.queue.Add(workItem)
klog.V(2).InfoS("Egress DELETE event triggered Egress IP scheduling", "egress", klog.KObj(egress))
}
func (s *egressIPScheduler) Run(stopCh <-chan struct{}) {
klog.InfoS("Starting Egress IP scheduler")
defer klog.InfoS("Shutting down Egress IP scheduler")
defer s.queue.ShutDown()
if !cache.WaitForCacheSync(stopCh, s.egressListerSynced) {
return
}
// Schedule at least once even if there is no Egress to unblock clients waiting for HasScheduled to return true.
s.queue.Add(workItem)
go func() {
for {
obj, quit := s.queue.Get()
if quit {
return
}
s.schedule()
s.queue.Done(obj)
}
}()
<-stopCh
}
func (s *egressIPScheduler) HasScheduled() bool {
return s.scheduledOnce.Load()
}
func (s *egressIPScheduler) AddEventHandler(handler scheduleEventHandler) {
s.eventHandlers = append(s.eventHandlers, handler)
}
func (s *egressIPScheduler) GetEgressIPAndNode(egress string) (string, string, bool) {
s.mutex.RLock()
defer s.mutex.RUnlock()
result, exists := s.scheduleResults[egress]
if !exists {
return "", "", false
}
return result.ip, result.node, true
}
// EgressesByCreationTimestamp sorts a list of Egresses by creation timestamp.
type EgressesByCreationTimestamp []*crdv1a2.Egress
func (o EgressesByCreationTimestamp) Len() int { return len(o) }
func (o EgressesByCreationTimestamp) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
func (o EgressesByCreationTimestamp) Less(i, j int) bool {
if o[i].CreationTimestamp.Equal(&o[j].CreationTimestamp) {
return o[i].Name < o[j].Name
}
return o[i].CreationTimestamp.Before(&o[j].CreationTimestamp)
}
// updateMaxEgressIPsByNode updates the maxEgressIPs for a given Node in the cache.
// It returns whether there is a real change, which indicates if rescheduling is required.
func (s *egressIPScheduler) updateMaxEgressIPsByNode(nodeName string, maxEgressIPs int) bool {
s.nodeToMaxEgressIPsMutex.Lock()
defer s.nodeToMaxEgressIPsMutex.Unlock()
oldMaxEgressIPs, exists := s.nodeToMaxEgressIPs[nodeName]
if exists && oldMaxEgressIPs == maxEgressIPs {
return false
}
// If the value equals to the default value, no need to cache it and trigger rescheduling.
if !exists && s.maxEgressIPsPerNode == maxEgressIPs {
return false
}
s.nodeToMaxEgressIPs[nodeName] = maxEgressIPs
return true
}
// deleteMaxEgressIPsByNode deletes the maxEgressIPs for a given Node in the cache.
// It returns whether there is a real change, which indicates if rescheduling is required.
func (s *egressIPScheduler) deleteMaxEgressIPsByNode(nodeName string) bool {
s.nodeToMaxEgressIPsMutex.Lock()
defer s.nodeToMaxEgressIPsMutex.Unlock()
_, exists := s.nodeToMaxEgressIPs[nodeName]
if !exists {
return false
}
delete(s.nodeToMaxEgressIPs, nodeName)
return true
}
// getMaxEgressIPsByNode gets the maxEgressIPs for a given Node.
// If there isn't a value for the Node, the default value will be returned.
func (s *egressIPScheduler) getMaxEgressIPsByNode(nodeName string) int {
s.nodeToMaxEgressIPsMutex.RLock()
defer s.nodeToMaxEgressIPsMutex.RUnlock()
maxEgressIPs, exists := s.nodeToMaxEgressIPs[nodeName]
if exists {
return maxEgressIPs
}
return s.maxEgressIPsPerNode
}
// schedule takes the spec of Egress and ExternalIPPool and the state of memberlist cluster as inputs, generates
// scheduling results deterministically. When every Node's capacity is sufficient, each Egress's schedule is independent
// and is only determined by the consistent hash map. When any Node's capacity is insufficient, one Egress's schedule
// may be affected by Egresses created before it. It will be triggerred when any schedulable Egress changes or the state
// of memberlist cluster changes, and will notify Egress schedule event subscribers of Egresses that are rescheduled.
//
// Note that it's possible that different agents decide different IP - Node assignment because their caches of Egress or
// the states of memberlist cluster are inconsistent at a moment. But all agents should get the same schedule results
// and correct IP assignment when their caches converge.
func (s *egressIPScheduler) schedule() {
var egressesToUpdate []string
newResults := map[string]*scheduleResult{}
nodeToIPs := map[string]sets.String{}
egresses, _ := s.egressLister.List(labels.Everything())
// Sort Egresses by creation timestamp to make the result deterministic and prioritize objected created earlier
// when the total capacity is insufficient.
sort.Sort(EgressesByCreationTimestamp(egresses))
for _, egress := range egresses {
// Ignore Egresses that shouldn't be scheduled.
if !isEgressSchedulable(egress) {
continue
}
maxEgressIPsFilter := func(node string) bool {
// Count the Egress IPs that are already assigned to this Node.
ipsOnNode, _ := nodeToIPs[node]
numIPs := ipsOnNode.Len()
// Check if this Node can accommodate the new Egress IP.
if !ipsOnNode.Has(egress.Spec.EgressIP) {
numIPs += 1
}
return numIPs <= s.getMaxEgressIPsByNode(node)
}
node, err := s.cluster.SelectNodeForIP(egress.Spec.EgressIP, egress.Spec.ExternalIPPool, maxEgressIPsFilter)
if err != nil {
if err == memberlist.ErrNoNodeAvailable {
klog.InfoS("No Node is eligible for Egress", "egress", klog.KObj(egress))
} else {
klog.ErrorS(err, "Failed to select Node for Egress", "egress", klog.KObj(egress))
}
continue
}
result := &scheduleResult{
ip: egress.Spec.EgressIP,
node: node,
}
newResults[egress.Name] = result
ips, exists := nodeToIPs[node]
if !exists {
ips = sets.NewString()
nodeToIPs[node] = ips
}
ips.Insert(egress.Spec.EgressIP)
}
func() {
s.mutex.Lock()
defer s.mutex.Unlock()
// Identify Egresses whose schedule results are updated.
prevResults := s.scheduleResults
for egress, result := range newResults {
prevResult, exists := prevResults[egress]
if !exists || prevResult.ip != result.ip || prevResult.node != result.node {
egressesToUpdate = append(egressesToUpdate, egress)
}
delete(prevResults, egress)
}
for egress := range prevResults {
egressesToUpdate = append(egressesToUpdate, egress)
}
// Record the new results.
s.scheduleResults = newResults
}()
for _, egress := range egressesToUpdate {
for _, handler := range s.eventHandlers {
handler(egress)
}
}
s.scheduledOnce.Store(true)
}