-
Notifications
You must be signed in to change notification settings - Fork 346
/
external_node_controller.go
686 lines (642 loc) · 24.8 KB
/
external_node_controller.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
// Copyright 2022 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 externalnode
import (
"fmt"
"net"
"reflect"
"strings"
"time"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"
"antrea.io/antrea/pkg/agent/config"
"antrea.io/antrea/pkg/agent/interfacestore"
"antrea.io/antrea/pkg/agent/openflow"
"antrea.io/antrea/pkg/agent/util"
"antrea.io/antrea/pkg/apis/controlplane/v1beta2"
"antrea.io/antrea/pkg/apis/crd/v1alpha1"
enlister "antrea.io/antrea/pkg/client/listers/crd/v1alpha1"
agentConfig "antrea.io/antrea/pkg/config/agent"
binding "antrea.io/antrea/pkg/ovs/openflow"
"antrea.io/antrea/pkg/ovs/ovsconfig"
"antrea.io/antrea/pkg/ovs/ovsctl"
"antrea.io/antrea/pkg/util/channel"
"antrea.io/antrea/pkg/util/env"
"antrea.io/antrea/pkg/util/externalnode"
"antrea.io/antrea/pkg/util/ip"
"antrea.io/antrea/pkg/util/k8s"
)
const (
controllerName = "ExternalNodeController"
// How long to wait before retrying the processing of an ExternalNode change.
minRetryDelay = 5 * time.Second
maxRetryDelay = 300 * time.Second
// Disable resyncing.
resyncPeriod time.Duration = 0
ovsExternalIDUplinkName = "uplink-name"
ovsExternalIDUplinkPort = "uplink-port"
ovsExternalIDEntityName = "entity-name"
ovsExternalIDEntityNamespace = "entity-namespace"
ovsExternalIDIPs = "ip-address"
ipsSplitter = ","
)
var (
keyFunc = cache.MetaNamespaceKeyFunc
splitKeyFunc = cache.SplitMetaNamespaceKey
renameInterface = util.RenameInterface
getInterfaceConfig = util.GetInterfaceConfig
getIPNetDeviceFromIP = util.GetIPNetDeviceFromIP
hostInterfaceExists = util.HostInterfaceExists
)
type ExternalNodeController struct {
ovsBridgeClient ovsconfig.OVSBridgeClient
ovsctlClient ovsctl.OVSCtlClient
ofClient openflow.Client
externalNodeInformer cache.SharedIndexInformer
externalNodeLister enlister.ExternalNodeLister
externalNodeListerSynced cache.InformerSynced
queue workqueue.RateLimitingInterface
ifaceStore interfacestore.InterfaceStore
syncedExternalNode *v1alpha1.ExternalNode
// externalEntityUpdateNotifier is used for notifying ExternalEntity updates to NetworkPolicyController.
externalEntityUpdateNotifier channel.Notifier
nodeName string
externalNodeNamespace string
policyBypassRules []agentConfig.PolicyBypassRule
}
func NewExternalNodeController(ovsBridgeClient ovsconfig.OVSBridgeClient, ofClient openflow.Client, externalNodeInformer cache.SharedIndexInformer,
ifaceStore interfacestore.InterfaceStore, externalEntityUpdateNotifier channel.Notifier, externalNodeNamespace string, policyBypassRules []agentConfig.PolicyBypassRule) (*ExternalNodeController, error) {
c := &ExternalNodeController{
ovsBridgeClient: ovsBridgeClient,
ovsctlClient: ovsctl.NewClient(ovsBridgeClient.GetBridgeName()),
ofClient: ofClient,
externalNodeInformer: externalNodeInformer,
externalNodeLister: enlister.NewExternalNodeLister(externalNodeInformer.GetIndexer()),
externalNodeListerSynced: externalNodeInformer.HasSynced,
queue: workqueue.NewNamedRateLimitingQueue(workqueue.NewItemExponentialFailureRateLimiter(minRetryDelay, maxRetryDelay), "externalNode"),
ifaceStore: ifaceStore,
externalEntityUpdateNotifier: externalEntityUpdateNotifier,
policyBypassRules: policyBypassRules,
}
nodeName, err := env.GetNodeName()
if err != nil {
return nil, err
}
c.nodeName = nodeName
c.externalNodeNamespace = externalNodeNamespace
c.externalNodeInformer.AddEventHandlerWithResyncPeriod(
cache.ResourceEventHandlerFuncs{
AddFunc: c.enqueueExternalNodeAdd,
UpdateFunc: c.enqueueExternalNodeUpdate,
DeleteFunc: c.enqueueExternalNodeDelete,
},
resyncPeriod)
return c, nil
}
// Run will create a worker (goroutine) which will process the ExternalNode events from the work queue.
func (c *ExternalNodeController) Run(stopCh <-chan struct{}) {
defer c.queue.ShutDown()
klog.InfoS("Starting controller", "name", controllerName)
defer klog.InfoS("Shutting down controller", "name", controllerName)
if err := wait.PollImmediateUntil(5*time.Second, func() (done bool, err error) {
if err = c.reconcile(); err != nil {
klog.ErrorS(err, "ExternalNodeController failed during reconciliation")
return false, nil
}
return true, nil
}, stopCh); err != nil {
klog.Info("Stopped ExternalNodeController reconciliation")
return
}
if !cache.WaitForNamedCacheSync(controllerName, stopCh, c.externalNodeListerSynced) {
klog.Error("Failed to wait for syncing ExternalNodes cache")
return
}
c.queue.Add(k8s.NamespacedName(c.externalNodeNamespace, c.nodeName))
go wait.Until(c.worker, time.Second, stopCh)
<-stopCh
}
func (c *ExternalNodeController) enqueueExternalNodeAdd(obj interface{}) {
en := obj.(*v1alpha1.ExternalNode)
key, _ := keyFunc(en)
c.queue.Add(key)
klog.InfoS("Enqueued ExternalNode ADD event", "ExternalNode", klog.KObj(en))
}
func (c *ExternalNodeController) enqueueExternalNodeUpdate(oldObj interface{}, newObj interface{}) {
oldEN := oldObj.(*v1alpha1.ExternalNode)
newEN := newObj.(*v1alpha1.ExternalNode)
if reflect.DeepEqual(oldEN.Spec.Interfaces, newEN.Spec.Interfaces) {
klog.InfoS("Skip enqueuing ExternalNode UPDATE event as no changes for interfaces", "ExternalNode", klog.KObj(newEN))
return
}
key, _ := keyFunc(newEN)
c.queue.Add(key)
klog.InfoS("Enqueued ExternalNode UPDATE event", "ExternalNode", klog.KObj(newEN))
}
func (c *ExternalNodeController) enqueueExternalNodeDelete(obj interface{}) {
en := obj.(*v1alpha1.ExternalNode)
key, _ := keyFunc(en)
c.queue.Add(key)
klog.InfoS("Enqueued ExternalNode DELETE event", "ExternalNode", klog.KObj(en))
}
func (c *ExternalNodeController) reconcile() error {
klog.InfoS("Reconciling for controller", "name", controllerName)
if err := c.reconcileHostUplinkFlows(); err != nil {
return fmt.Errorf("failed to reconcile host uplink flows %v", err)
}
if err := c.reconcilePolicyBypassFlows(); err != nil {
return fmt.Errorf("failed to reconcile reserved flows %v", err)
}
klog.InfoS("Reconciled for controller", "name", controllerName)
return nil
}
func (c *ExternalNodeController) reconcileHostUplinkFlows() error {
hostIfaces := c.ifaceStore.GetInterfacesByType(interfacestore.ExternalEntityInterface)
for _, hostIface := range hostIfaces {
if err := c.ofClient.InstallVMUplinkFlows(hostIface.InterfaceName, hostIface.OVSPortConfig.OFPort, hostIface.UplinkPort.OFPort); err != nil {
return err
}
klog.InfoS("Reconciled host uplink flow for ExternalEntityInterface", "ifName", hostIface.InterfaceName)
}
return nil
}
func (c *ExternalNodeController) reconcilePolicyBypassFlows() error {
for _, rule := range c.policyBypassRules {
klog.V(2).InfoS("Installing policy bypass flows", "protocol", rule.Protocol, "CIDR", rule.CIDR, "port", rule.Port, "direction", rule.Direction)
protocol := parseProtocol(rule.Protocol)
_, ipNet, _ := net.ParseCIDR(rule.CIDR)
if err := c.ofClient.InstallPolicyBypassFlows(protocol, ipNet, uint16(rule.Port), rule.Direction == "ingress"); err != nil {
return err
}
}
klog.InfoS("Installed policy bypass flows", "RuleCount", len(c.policyBypassRules))
return nil
}
// worker is a long-running function that will continuously call the processNextWorkItem function in
// order to read and process a message on the work queue.
func (c *ExternalNodeController) worker() {
for c.processNextWorkItem() {
}
}
func (c *ExternalNodeController) processNextWorkItem() bool {
obj, quit := c.queue.Get()
if quit {
return false
}
defer c.queue.Done(obj)
if key, ok := obj.(string); !ok {
c.queue.Forget(obj)
klog.Errorf("Expected string type in work queue but got %#v", obj)
return true
} else if err := c.syncExternalNode(key); err == nil {
// If no error occurs, then forget this item so it does not get queued again until
// another change happens.
c.queue.Forget(key)
} else {
// Put the item back on the work queue to handle any transient errors.
c.queue.AddRateLimited(key)
klog.ErrorS(err, "Error syncing ExternalNode", "ExternalNode", key)
}
return true
}
func (c *ExternalNodeController) syncExternalNode(key string) error {
_, name, err := splitKeyFunc(key)
if err != nil {
// This err should not occur.
return err
}
en, err := c.externalNodeLister.ExternalNodes(c.externalNodeNamespace).Get(name)
if errors.IsNotFound(err) {
return c.deleteExternalNode()
}
if c.syncedExternalNode == nil {
return c.addExternalNode(en)
} else {
return c.updateExternalNode(c.syncedExternalNode, en)
}
}
func (c *ExternalNodeController) addExternalNode(en *v1alpha1.ExternalNode) error {
klog.InfoS("Adding ExternalNode", "ExternalNode", klog.KObj(en))
eeName, err := externalnode.GenExternalEntityName(en)
if err != nil {
return err
}
ifName, ips, err := getHostInterfaceName(en.Spec.Interfaces[0])
if err != nil {
return err
}
if err := c.addInterface(ifName, en.Namespace, eeName, ips); err != nil {
return err
}
c.syncedExternalNode = en
// Notify the ExternalEntity event to NetworkPolicyController.
c.externalEntityUpdateNotifier.Notify(v1beta2.ExternalEntityReference{
Name: eeName,
Namespace: en.Namespace,
})
return nil
}
func (c *ExternalNodeController) addInterface(ifName string, eeNamespace string, eeName string, ips []string) error {
hostIface, ifaceExists := c.ifaceStore.GetInterfaceByName(ifName)
if !ifaceExists {
klog.InfoS("Creating OVS ports and flows for ExternalEntityInterface", "ifName", ifName, "externalEntity", eeName, "ips", ips)
uplinkName := util.GenerateUplinkInterfaceName(ifName)
iface, err := c.createOVSPortsAndFlows(uplinkName, ifName, eeNamespace, eeName, ips)
if err != nil {
return err
}
c.ifaceStore.AddInterface(iface)
return nil
}
klog.InfoS("Updating OVS port data", "ifName", ifName, "externalEntity", eeName, "ips", ips)
portUUID := hostIface.PortUUID
portName := hostIface.InterfaceName
portData, ovsErr := c.ovsBridgeClient.GetPortData(portUUID, portName)
if ovsErr != nil {
return ovsErr
}
preEEName := portData.ExternalIDs[ovsExternalIDEntityName]
preIPs := sets.New[string](strings.Split(portData.ExternalIDs[ovsExternalIDIPs], ipsSplitter)...)
if preEEName == eeName && sets.New[string](ips...).Equal(preIPs) {
klog.InfoS("Skipping updating OVS port data as both entity name and ip are not changed", "ifName", ifName)
return nil
}
iface, err := c.updateOVSPortsData(hostIface, portData, eeName, ips)
if err != nil {
return err
}
c.ifaceStore.AddInterface(iface)
return nil
}
func (c *ExternalNodeController) updateExternalNode(preEN *v1alpha1.ExternalNode, curEN *v1alpha1.ExternalNode) error {
klog.InfoS("Updating ExternalNode", "ExternalNode", klog.KObj(curEN))
if reflect.DeepEqual(preEN.Spec.Interfaces[0], curEN.Spec.Interfaces[0]) {
klog.InfoS("Skip processing ExternalNode update as no changes for Interface[0]", "ExternalNode", klog.KObj(curEN))
return nil
}
preEEName, err := externalnode.GenExternalEntityName(preEN)
if err != nil {
return err
}
preIfName, preIPs, err := getHostInterfaceName(preEN.Spec.Interfaces[0])
if err != nil {
return err
}
curEEName, err := externalnode.GenExternalEntityName(curEN)
if err != nil {
return err
}
curIfName, curIPs, err := getHostInterfaceName(curEN.Spec.Interfaces[0])
if err != nil {
return err
}
if preIfName != curIfName {
klog.InfoS("Found interface name is changed", "preName", preIfName, "curName", curIfName)
if err = c.addInterface(curIfName, curEN.Namespace, curEEName, curIPs); err != nil {
return err
}
ifaceConfig, ifaceExists := c.ifaceStore.GetInterfaceByName(preIfName)
if ifaceExists {
if err = c.deleteInterface(ifaceConfig); err != nil {
return err
}
}
} else if !reflect.DeepEqual(preIPs, curIPs) || preEEName != curEEName {
klog.InfoS("Found interface configuration is changed", "preIPs", preIPs, "preExternalEntity", preEEName,
"curIPs", curIPs, "curExternalEntity", curEEName)
if err = c.addInterface(curIfName, curEN.Namespace, curEEName, curIPs); err != nil {
return err
}
}
c.syncedExternalNode = curEN
// Notify the ExternalEntity event to NetworkPolicyController.
c.externalEntityUpdateNotifier.Notify(v1beta2.ExternalEntityReference{
Name: curEEName,
Namespace: curEN.Namespace,
})
return nil
}
func (c *ExternalNodeController) deleteExternalNode() error {
if err := c.deleteInterfaces(); err != nil {
return err
}
c.syncedExternalNode = nil
// Remove any stale configuration that is related to the deleted ExternalNode
// and terminate the process if required.
return c.removeExternalNodeConfig()
}
func (c *ExternalNodeController) deleteInterfaces() error {
hostIfaces := c.ifaceStore.GetInterfacesByType(interfacestore.ExternalEntityInterface)
for _, hostIface := range hostIfaces {
if err := c.deleteInterface(hostIface); err != nil {
return err
}
}
return nil
}
func (c *ExternalNodeController) deleteInterface(interfaceConfig *interfacestore.InterfaceConfig) error {
klog.InfoS("Deleting interface", "ifName", interfaceConfig.InterfaceName)
if err := c.removeOVSPortsAndFlows(interfaceConfig); err != nil {
return err
}
c.ifaceStore.DeleteInterface(interfaceConfig)
return nil
}
func (c *ExternalNodeController) createOVSPortsAndFlows(uplinkName, hostIFName, eeNamespace, eeName string, ips []string) (*interfacestore.InterfaceConfig, error) {
iface, addrs, routes, err := getInterfaceConfig(hostIFName)
if err != nil {
return nil, err
}
adapterConfig := &config.AdapterNetConfig{
Name: hostIFName,
Index: iface.Index,
MAC: iface.HardwareAddr,
IPs: addrs,
Routes: routes,
MTU: iface.MTU,
}
if err = renameInterface(hostIFName, uplinkName); err != nil {
return nil, err
}
success := false
defer func() {
if !success {
if err = renameInterface(uplinkName, hostIFName); err != nil {
klog.ErrorS(err, "Failed to restore uplink name back to host interface name. Manual cleanup is required", "uplinkName", uplinkName, "hostIFName", hostIFName)
}
}
}()
// Create uplink port in OVS.
uplinkExternalIDs := map[string]interface{}{
interfacestore.AntreaInterfaceTypeKey: interfacestore.AntreaUplink,
}
uplinkUUID, ovsErr := c.ovsBridgeClient.CreatePort(uplinkName, uplinkName, uplinkExternalIDs)
if ovsErr != nil {
return nil, fmt.Errorf("failed to create uplink port %s in OVS, err %v", uplinkName, ovsErr)
}
defer func() {
if !success {
if ovsErr = c.ovsBridgeClient.DeletePort(uplinkUUID); ovsErr != nil {
klog.ErrorS(err, "Failed to delete uplink port. Manual cleanup is required", "portUUID", uplinkUUID, "uplinkName", uplinkName)
}
}
}()
uplinkOFPort, ovsErr := c.ovsBridgeClient.GetOFPort(uplinkName, false)
if ovsErr != nil {
return nil, ovsErr
}
klog.InfoS("Added uplink port in OVS", "port", uplinkOFPort, "uplinkName", uplinkName)
// Create host port in OVS.
attachInfo := GetOVSAttachInfo(uplinkName, uplinkUUID, eeName, eeNamespace, ips)
hostIfUUID, ovsErr := c.ovsBridgeClient.CreateInternalPort(hostIFName, 0, adapterConfig.MAC.String(), attachInfo)
if ovsErr != nil {
return nil, fmt.Errorf("failed to create OVS internal port for host interface %s, err %v", hostIFName, ovsErr)
}
defer func() {
if !success {
if ovsErr = c.ovsBridgeClient.DeletePort(hostIfUUID); ovsErr != nil {
klog.ErrorS(err, "Failed to delete host interface port. Manual cleanup is required", "portUUID", hostIfUUID, "hostIFName", hostIFName)
}
}
}()
hostOFPort, ovsErr := c.ovsBridgeClient.GetOFPort(hostIFName, false)
if ovsErr != nil {
return nil, ovsErr
}
klog.InfoS("Created an OVS internal port for host interface", "ofPort", hostOFPort, "interfaceName", hostIFName)
// Move configurations from the uplink to host port
if err = c.moveIFConfigurations(adapterConfig, uplinkName, hostIFName); err != nil {
return nil, err
}
klog.InfoS("Moved configurations to the host interface", "hostInterface", hostIFName)
if err = c.ofClient.InstallVMUplinkFlows(hostIFName, hostOFPort, uplinkOFPort); err != nil {
return nil, err
}
klog.InfoS("Added uplink and host port in OVS and installed openflow entries", "uplink", uplinkName, "hostInterface", hostIFName)
success = true
ifIPs := make([]net.IP, 0)
for _, ip := range ips {
ifIPs = append(ifIPs, net.ParseIP(ip))
}
hostIFConfig := &interfacestore.InterfaceConfig{
Type: interfacestore.ExternalEntityInterface,
InterfaceName: hostIFName,
IPs: ifIPs,
OVSPortConfig: &interfacestore.OVSPortConfig{
PortUUID: hostIfUUID,
OFPort: hostOFPort,
},
EntityInterfaceConfig: &interfacestore.EntityInterfaceConfig{
EntityName: eeName,
EntityNamespace: eeNamespace,
UplinkPort: &interfacestore.OVSPortConfig{
PortUUID: uplinkUUID,
OFPort: uplinkOFPort,
},
},
}
return hostIFConfig, nil
}
func GetOVSAttachInfo(uplinkName, uplinkUUID, entityName, entityNamespace string, ips []string) map[string]interface{} {
attachInfo := map[string]interface{}{
interfacestore.AntreaInterfaceTypeKey: interfacestore.AntreaHost,
}
if uplinkName != "" {
attachInfo[ovsExternalIDUplinkName] = uplinkName
}
if uplinkUUID != "" {
attachInfo[ovsExternalIDUplinkPort] = uplinkUUID
}
if entityName != "" {
attachInfo[ovsExternalIDEntityName] = entityName
}
if entityNamespace != "" {
attachInfo[ovsExternalIDEntityNamespace] = entityNamespace
}
if len(ips) != 0 {
attachInfo[ovsExternalIDIPs] = strings.Join(ips, ipsSplitter)
}
return attachInfo
}
func (c *ExternalNodeController) updateOVSPortsData(interfaceConfig *interfacestore.InterfaceConfig, portData *ovsconfig.OVSPortData, eeName string, ips []string) (*interfacestore.InterfaceConfig, error) {
attachInfo := map[string]interface{}{
ovsExternalIDUplinkName: portData.ExternalIDs[ovsExternalIDUplinkName],
ovsExternalIDUplinkPort: portData.ExternalIDs[ovsExternalIDUplinkPort],
ovsExternalIDEntityName: eeName,
ovsExternalIDEntityNamespace: portData.ExternalIDs[ovsExternalIDEntityNamespace],
ovsExternalIDIPs: strings.Join(ips, ipsSplitter),
interfacestore.AntreaInterfaceTypeKey: interfacestore.AntreaHost,
}
err := c.ovsBridgeClient.SetPortExternalIDs(interfaceConfig.InterfaceName, attachInfo)
if err != nil {
return nil, err
}
ifIPs := make([]net.IP, 0)
for _, ip := range ips {
ifIPs = append(ifIPs, net.ParseIP(ip))
}
iface := &interfacestore.InterfaceConfig{
InterfaceName: interfaceConfig.InterfaceName,
Type: interfacestore.ExternalEntityInterface,
OVSPortConfig: &interfacestore.OVSPortConfig{
PortUUID: interfaceConfig.PortUUID,
OFPort: interfaceConfig.OFPort,
},
EntityInterfaceConfig: &interfacestore.EntityInterfaceConfig{
EntityName: eeName,
EntityNamespace: interfaceConfig.EntityNamespace,
UplinkPort: &interfacestore.OVSPortConfig{
PortUUID: interfaceConfig.UplinkPort.PortUUID,
OFPort: interfaceConfig.UplinkPort.OFPort,
},
},
IPs: ifIPs,
}
return iface, nil
}
func (c *ExternalNodeController) removeOVSPortsAndFlows(interfaceConfig *interfacestore.InterfaceConfig) error {
portUUID := interfaceConfig.PortUUID
portName := interfaceConfig.InterfaceName
if err := c.ofClient.UninstallVMUplinkFlows(portName); err != nil {
return fmt.Errorf("failed to uninstall uplink and host port openflow entries, portName %s, err %v", portName, err)
}
klog.InfoS("Removed the flows installed to forward packet between uplinkPort and hostPort", "hostInterface", portName)
hostIFName := interfaceConfig.InterfaceName
uplinkIfName := util.GenerateUplinkInterfaceName(portName)
uplinkPortID := interfaceConfig.UplinkPort.PortUUID
iface, addrs, routes, err := getInterfaceConfig(hostIFName)
if err != nil {
return err
}
adapterConfig := &config.AdapterNetConfig{
Name: hostIFName,
Index: iface.Index,
MAC: iface.HardwareAddr,
IPs: addrs,
Routes: routes,
MTU: iface.MTU,
}
if ovsErr := c.ovsBridgeClient.DeletePort(portUUID); ovsErr != nil {
return fmt.Errorf("failed to delete host port %s, err %v", hostIFName, ovsErr)
}
klog.InfoS("Deleted host port in OVS", "hostInterface", hostIFName)
if ovsErr := c.ovsBridgeClient.DeletePort(uplinkPortID); ovsErr != nil {
return fmt.Errorf("failed to delete uplink port %s, err %v", uplinkIfName, ovsErr)
}
klog.InfoS("Deleted uplink port in OVS", "uplinkIfName", uplinkIfName)
defer func() {
// Delete host interface from OVS datapath if it exists.
// This is to resolve an issue that OVS fails to remove the interface from datapath. It might happen because the interface
// is busy when OVS tries to remove it with the OVSDB interface deletion event.
if err := c.ovsctlClient.DeleteDPInterface(hostIFName); err != nil {
klog.ErrorS(err, "Failed to delete host interface from OVS datapath", "interface", hostIFName)
}
}()
// Wait until the host interface created by OVS is removed.
if err = wait.PollImmediate(50*time.Millisecond, 2*time.Second, func() (bool, error) {
return !hostInterfaceExists(hostIFName), nil
}); err != nil {
return fmt.Errorf("failed to wait for host interface %s deletion in 2s, err %v", hostIFName, err)
}
// Recover the uplink interface's name.
if err = renameInterface(uplinkIfName, hostIFName); err != nil {
return err
}
klog.InfoS("Recovered uplink name to the host interface name", "uplinkIfName", uplinkIfName, "hostInterface", hostIFName)
// Move the IP configurations back to the host interface.
if err = c.moveIFConfigurations(adapterConfig, "", hostIFName); err != nil {
return err
}
klog.InfoS("Moved back configuration to the host interface", "hostInterface", hostIFName)
return nil
}
func getHostInterfaceName(iface v1alpha1.NetworkInterface) (string, []string, error) {
ifName := ""
ips := sets.New[string]()
for _, ipStr := range iface.IPs {
var ipFilter *ip.DualStackIPs
ifIP := net.ParseIP(ipStr)
if ifIP.To4() != nil {
ipFilter = &ip.DualStackIPs{IPv4: ifIP}
} else {
ipFilter = &ip.DualStackIPs{IPv6: ifIP}
}
_, _, link, err := getIPNetDeviceFromIP(ipFilter, sets.New[string]())
if err == nil {
klog.InfoS("Using the interface", "linkName", link.Name, "IP", ipStr)
ips.Insert(ipStr)
if ifName == "" {
ifName = link.Name
} else if ifName != link.Name {
return "", sets.List(ips), fmt.Errorf("find different interfaces by IPs, ifName %s, linkName %s", ifName, link.Name)
}
} else {
klog.ErrorS(err, "Failed to get device from IP", "ip", ifIP)
}
}
if ifName == "" {
return "", sets.List(ips), fmt.Errorf("cannot find interface via IPs %v", iface.IPs)
}
return ifName, sets.List(ips), nil
}
func ParseHostInterfaceConfig(ovsBridgeClient ovsconfig.OVSBridgeClient, portData *ovsconfig.OVSPortData, portConfig *interfacestore.OVSPortConfig) (*interfacestore.InterfaceConfig, error) {
interfaceConfig := &interfacestore.InterfaceConfig{
InterfaceName: portData.Name,
Type: interfacestore.ExternalEntityInterface,
OVSPortConfig: portConfig,
}
var hostUplinkConfig *interfacestore.EntityInterfaceConfig
entityIPArr := strings.Split(portData.ExternalIDs[ovsExternalIDIPs], ipsSplitter)
var entityIPs []net.IP
for _, ipStr := range entityIPArr {
entityIPs = append(entityIPs, net.ParseIP(ipStr))
}
interfaceConfig.IPs = entityIPs
uplinkName, _ := portData.ExternalIDs[ovsExternalIDUplinkName]
uplinkPortUUID, _ := portData.ExternalIDs[ovsExternalIDUplinkPort]
uplinkPortData, ovsErr := ovsBridgeClient.GetPortData(uplinkPortUUID, uplinkName)
if ovsErr != nil {
return nil, ovsErr
}
entityName, _ := portData.ExternalIDs[ovsExternalIDEntityName]
entityNamespace, _ := portData.ExternalIDs[ovsExternalIDEntityNamespace]
hostUplinkConfig = &interfacestore.EntityInterfaceConfig{
EntityName: entityName,
EntityNamespace: entityNamespace,
UplinkPort: &interfacestore.OVSPortConfig{
PortUUID: uplinkPortUUID,
OFPort: uplinkPortData.OFPort,
},
}
interfaceConfig.EntityInterfaceConfig = hostUplinkConfig
return interfaceConfig, nil
}
func parseProtocol(protocol string) binding.Protocol {
var proto binding.Protocol
switch protocol {
case "tcp":
proto = binding.ProtocolTCP
case "udp":
proto = binding.ProtocolUDP
case "icmp":
proto = binding.ProtocolICMP
case "ip":
proto = binding.ProtocolIP
}
return proto
}