• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

noironetworks / aci-containers / 11012

25 Sep 2025 05:18PM UTC coverage: 66.048% (+0.4%) from 65.647%
11012

Pull #1578

travis-pro

vlella
Adopt /usr/local approach for iptables to pass Preflight
Pull Request #1578: Adopt /usr/local approach for iptables to pass Preflight

13353 of 20217 relevant lines covered (66.05%)

0.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/pkg/controller/aaepmonitor.go
1
// Copyright 2019 Cisco Systems, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRATIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
// Handlers for AaepMonitor CR updates.
16

17
package controller
18

19
import (
20
        "context"
21
        "encoding/json"
22
        "fmt"
23
        "regexp"
24
        "strconv"
25
        "strings"
26

27
        amv1 "github.com/noironetworks/aci-containers/pkg/aaepmonitor/apis/aci.attachmentmonitor/v1"
28
        aaepmonitorclientset "github.com/noironetworks/aci-containers/pkg/aaepmonitor/clientset/versioned"
29
        "github.com/noironetworks/aci-containers/pkg/apicapi"
30

31
        nadapi "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
32
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33
        "k8s.io/apimachinery/pkg/runtime"
34
        "k8s.io/apimachinery/pkg/watch"
35
        "k8s.io/client-go/tools/cache"
36
        "k8s.io/kubernetes/pkg/controller"
37
)
38

39
const (
40
        aaepMonitorCRDName = "aaepmonitors.aci.attachmentmonitor"
41
)
42

43
func (cont *AciController) queueAaepMonitorConfigByKey(key string) {
×
44
        cont.aaepMonitorConfigQueue.Add(key)
×
45
}
×
46

47
func aaepMonitorInit(cont *AciController, stopCh <-chan struct{}) {
×
48
        restconfig := cont.env.RESTConfig()
×
49
        aaepMonitorClient, err := aaepmonitorclientset.NewForConfig(restconfig)
×
50
        if err != nil {
×
51
                cont.log.Errorf("Failed to intialize aaepMonitorClient")
×
52
                return
×
53
        }
×
54

55
        cont.initAaepMonitorInformerFromClient(aaepMonitorClient)
×
56
        go cont.aaepMonitorInformer.Run(stopCh)
×
57
        go cont.processQueue(cont.aaepMonitorConfigQueue, cont.aaepMonitorInformer.GetIndexer(),
×
58
                func(obj interface{}) bool {
×
59
                        return cont.handleAaepMonitorConfigurationUpdate(obj)
×
60
                }, func(key string) bool {
×
61
                        return cont.handleAaepMonitorConfigurationDelete(key)
×
62
                }, nil, stopCh)
×
63
        cache.WaitForCacheSync(stopCh, cont.aaepMonitorInformer.HasSynced)
×
64
}
65

66
func (cont *AciController) initAaepMonitorInformerFromClient(
67
        aaepMonitorClient *aaepmonitorclientset.Clientset) {
×
68
        cont.initAaepMonitorInformerBase(
×
69
                &cache.ListWatch{
×
70
                        ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
×
71
                                return aaepMonitorClient.AciV1().AaepMonitors().List(context.TODO(), options)
×
72
                        },
×
73
                        WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
×
74
                                return aaepMonitorClient.AciV1().AaepMonitors().Watch(context.TODO(), options)
×
75
                        },
×
76
                })
77
}
78

79
func (cont *AciController) initAaepMonitorInformerBase(listWatch *cache.ListWatch) {
×
80
        cont.aaepMonitorInformer = cache.NewSharedIndexInformer(
×
81
                listWatch,
×
82
                &amv1.AaepMonitor{},
×
83
                controller.NoResyncPeriodFunc(),
×
84
                cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
×
85
        )
×
86
        cont.aaepMonitorInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
×
87
                AddFunc: func(obj interface{}) {
×
88
                        cont.aaepMonitorConfAdded(obj)
×
89
                },
×
90
                UpdateFunc: func(oldobj interface{}, newobj interface{}) {
×
91
                        cont.aaepMonitorConfUpdate(oldobj, newobj)
×
92
                },
×
93
                DeleteFunc: func(obj interface{}) {
×
94
                        cont.aaepMonitorConfDelete(obj)
×
95
                },
×
96
        })
97
}
98

99
func (cont *AciController) aaepMonitorConfAdded(obj interface{}) {
×
100
        aaepMonitorConfig, ok := obj.(*amv1.AaepMonitor)
×
101
        if !ok {
×
102
                cont.log.Error("aaepMonitorConfAdded: Bad object type")
×
103
                return
×
104
        }
×
105
        key, err := cache.MetaNamespaceKeyFunc(aaepMonitorConfig)
×
106
        if err != nil {
×
107
                return
×
108
        }
×
109
        cont.queueAaepMonitorConfigByKey(key)
×
110
}
111

112
func (cont *AciController) aaepMonitorConfUpdate(oldobj interface{}, newobj interface{}) {
×
113
        newAaepMonitorConfig := newobj.(*amv1.AaepMonitor)
×
114

×
115
        key, err := cache.MetaNamespaceKeyFunc(newAaepMonitorConfig)
×
116
        if err != nil {
×
117
                return
×
118
        }
×
119
        cont.queueAaepMonitorConfigByKey(key)
×
120
}
121

122
func (cont *AciController) aaepMonitorConfDelete(obj interface{}) {
×
123
        cont.indexMutex.Lock()
×
124
        defer cont.indexMutex.Unlock()
×
125
        aaepMonitorConfig, ok := obj.(*amv1.AaepMonitor)
×
126

×
127
        if !ok {
×
128
                deletedState, ok := obj.(cache.DeletedFinalStateUnknown)
×
129
                if !ok {
×
130
                        cont.log.Errorf("Received unexpected object: ")
×
131
                        return
×
132
                }
×
133
                aaepMonitorConfig, ok = deletedState.Obj.(*amv1.AaepMonitor)
×
134
                if !ok {
×
135
                        cont.log.Errorf("DeletedFinalStateUnknown contained non-aaepmonitorconfiguration object: %v", deletedState.Obj)
×
136
                        return
×
137
                }
×
138
        }
139

140
        key, err := cache.MetaNamespaceKeyFunc(aaepMonitorConfig)
×
141
        if err != nil {
×
142
                return
×
143
        }
×
144
        cont.queueAaepMonitorConfigByKey("DELETED_" + key)
×
145
}
146

147
func (cont *AciController) handleAaepMonitorConfigurationUpdate(obj interface{}) bool {
×
148
        aaepMonitorConfig, ok := obj.(*amv1.AaepMonitor)
×
149
        if !ok {
×
150
                cont.log.Error("handleAaepMonitorConfigurationUpdate: Bad object type")
×
151
                return false
×
152
        }
×
153

154
        addedAaeps, removedAaeps := cont.getAaepDiff(aaepMonitorConfig.Spec.Aaeps)
×
155
        for _, aaepName := range addedAaeps {
×
156
                cont.reconcileNadData(aaepName)
×
157
        }
×
158

159
        for _, aaepName := range removedAaeps {
×
160
                cont.cleanAnnotationSubscriptions(aaepName)
×
161

×
162
                cont.indexMutex.Lock()
×
163
                aaepEpgDataList := cont.sharedAaepMonitor[aaepName]
×
164
                delete(cont.sharedAaepMonitor, aaepName)
×
165

×
166
                for _, aaepEpgData := range aaepEpgDataList {
×
167
                        cont.deleteNetworkAttachmentDefinition(aaepEpgData, "AaepRemovedFromCR")
×
168
                }
×
169
                cont.indexMutex.Unlock()
×
170
        }
171

172
        return false
×
173
}
174

175
func (cont *AciController) handleAaepMonitorConfigurationDelete(key string) bool {
×
176
        cont.indexMutex.Lock()
×
177
        defer cont.indexMutex.Unlock()
×
178
        for aaepName, aaepMonitorDataList := range cont.sharedAaepMonitor {
×
179
                cont.cleanAnnotationSubscriptions(aaepName)
×
180

×
181
                for _, aaepMonitorData := range aaepMonitorDataList {
×
182
                        cont.deleteNetworkAttachmentDefinition(aaepMonitorData, "CRDeleted")
×
183
                }
×
184
        }
185

186
        cont.sharedAaepMonitor = make(map[string][]*AaepMonitoringData)
×
187
        return false
×
188
}
189

190
func (cont *AciController) handleAaepEpgAttach(infraRsObj apicapi.ApicObject) {
×
191
        infraRsObjDn := infraRsObj.GetDn()
×
192
        aaepName := cont.matchesAEPFilter(infraRsObjDn)
×
193
        if aaepName == "" {
×
194
                cont.log.Debugf("Unable to find AAEP from %s in monitoring list", infraRsObjDn)
×
195
                return
×
196
        }
×
197

198
        state := infraRsObj.GetAttrStr("state")
×
199
        if state != "formed" {
×
200
                cont.log.Debugf("[AAEP-HANDLER] Skipping NAD creation: %s is with state: %s", infraRsObjDn, state)
×
201
                return
×
202
        }
×
203

204
        epgDn := infraRsObj.GetAttrStr("tDn")
×
205

×
206
        cont.apicConn.AddImmediateSubscriptionDnLocked(epgDn, []string{"tagAnnotation"}, cont.handleAnnotationAdded,
×
207
                cont.handleAnnotationDeleted)
×
208

×
209
        encap := infraRsObj.GetAttrStr("encap")
×
210
        vlanID := cont.getVlanId(encap)
×
211

×
212
        aaepEpgData := &AaepEpgAttachData{
×
213
                epgDn:     epgDn,
×
214
                encapVlan: vlanID,
×
215
        }
×
216

×
217
        aaepMonitorData := cont.collectNadData(aaepEpgData)
×
218
        if aaepMonitorData == nil {
×
219
                return
×
220
        }
×
221

222
        cont.indexMutex.Lock()
×
223
        oldAaepMonitorData, dataIndex := cont.getAaepEpgAttachDataLocked(aaepName, epgDn)
×
224
        cont.indexMutex.Unlock()
×
225

×
226
        cont.syncNADsWithAciState(aaepName, dataIndex, oldAaepMonitorData, aaepMonitorData)
×
227
}
228

229
func (cont *AciController) handleAaepEpgDetach(infraRsObjDn string) {
×
230
        aaepName := cont.matchesAEPFilter(infraRsObjDn)
×
231
        if aaepName == "" {
×
232
                cont.log.Debugf("Unable to find AAEP from %s in monitoring list", infraRsObjDn)
×
233
                return
×
234
        }
×
235

236
        epgDn := cont.getEpgDnFromInfraRsDn(infraRsObjDn)
×
237

×
238
        cont.apicConn.UnsubscribeImmediateDnLocked(epgDn, []string{"tagAnnotation"})
×
239

×
240
        cont.indexMutex.Lock()
×
241
        aaepMonitorData, dataIndex := cont.getAaepEpgAttachDataLocked(aaepName, epgDn)
×
242
        cont.indexMutex.Unlock()
×
243

×
244
        if aaepMonitorData == nil || !cont.namespaceChecks(aaepMonitorData.namespaceName, epgDn) {
×
245
                return
×
246
        }
×
247

248
        cont.indexMutex.Lock()
×
249
        cont.sharedAaepMonitor[aaepName] = append(cont.sharedAaepMonitor[aaepName][:dataIndex], cont.sharedAaepMonitor[aaepName][dataIndex+1:]...)
×
250
        cont.deleteNetworkAttachmentDefinition(aaepMonitorData, "AaepEpgDetached")
×
251
        cont.indexMutex.Unlock()
×
252
}
253

254
func (cont *AciController) handleAnnotationAdded(obj apicapi.ApicObject) bool {
×
255
        annotationDn := obj.GetDn()
×
256
        epgDn := annotationDn[:strings.Index(annotationDn, "/annotationKey-")]
×
257
        aaepName, aaepMonitorData := cont.getAaepMonitoringDataForEpg(epgDn)
×
258

×
259
        if aaepMonitorData == nil || !cont.namespaceChecks(aaepMonitorData.namespaceName, epgDn) {
×
260
                cont.log.Debugf("Insufficient data for NAD creation: Either namespace not exist or monitoring data not found")
×
261
                return true
×
262
        }
×
263

264
        cont.indexMutex.Lock()
×
265
        oldAaepMonitorData, dataIndex := cont.getAaepEpgAttachDataLocked(aaepName, epgDn)
×
266
        cont.indexMutex.Unlock()
×
267

×
268
        cont.syncNADsWithAciState(aaepName, dataIndex, oldAaepMonitorData, aaepMonitorData)
×
269

×
270
        return true
×
271
}
272

273
func (cont *AciController) handleAnnotationDeleted(annotationDn string) {
×
274
        epgDn := annotationDn[:strings.Index(annotationDn, "/annotationKey-")]
×
275

×
276
        aaepName, aaepMonitorData := cont.getAaepMonitoringDataForEpg(epgDn)
×
277

×
278
        oldAaepMonitorData, dataIndex := cont.findAaepEpgAttachDataInCache(epgDn)
×
279

×
280
        if oldAaepMonitorData == nil {
×
281
                return
×
282
        }
×
283

284
        if aaepMonitorData == nil {
×
285
                cont.indexMutex.Lock()
×
286
                cont.sharedAaepMonitor[aaepName] = append(cont.sharedAaepMonitor[aaepName][:dataIndex],
×
287
                        cont.sharedAaepMonitor[aaepName][dataIndex+1:]...)
×
288
                cont.deleteNetworkAttachmentDefinition(oldAaepMonitorData, "NamespaceAnnotationRemoved")
×
289
                cont.indexMutex.Unlock()
×
290
                return
×
291
        }
×
292

293
        cont.syncNADsWithAciState(aaepName, dataIndex, oldAaepMonitorData, aaepMonitorData)
×
294
}
295

296
func (cont *AciController) collectNadData(aaepEpgData *AaepEpgAttachData) *AaepMonitoringData {
×
297
        epgDn := aaepEpgData.epgDn
×
298
        epgAnnotations := cont.getEpgAnnotations(epgDn)
×
299
        namespaceName, nadName := cont.getSpecificEPGAnnotation(epgAnnotations)
×
300

×
301
        if !cont.namespaceChecks(namespaceName, epgDn) {
×
302
                return nil
×
303
        }
×
304

305
        aaepMonitoringData := &AaepMonitoringData{
×
306
                aaepEpgData:   *aaepEpgData,
×
307
                nadName:       nadName,
×
308
                namespaceName: namespaceName,
×
309
        }
×
310

×
311
        return aaepMonitoringData
×
312
}
313

314
func (cont *AciController) findAaepEpgAttachDataInCache(epgDn string) (*AaepMonitoringData, int) {
×
315
        cont.indexMutex.Lock()
×
316
        defer cont.indexMutex.Unlock()
×
317
        for aaepName := range cont.sharedAaepMonitor {
×
318
                aaepEpgData, dataIndex := cont.getAaepEpgAttachDataLocked(aaepName, epgDn)
×
319
                if aaepEpgData != nil {
×
320
                        return aaepEpgData, dataIndex
×
321
                }
×
322
        }
323

324
        return nil, -1
×
325
}
326

327
func (cont *AciController) getAaepEpgAttachDataLocked(aaepName string, epgDn string) (*AaepMonitoringData, int) {
×
328
        aaepEpgDataList, exists := cont.sharedAaepMonitor[aaepName]
×
329
        if !exists || len(aaepEpgDataList) == 0 {
×
330
                cont.log.Debugf("AAEP EPG attachment data not found for AAEP: %s", aaepName)
×
331
                return nil, -1
×
332
        }
×
333

334
        for dataIndex, aaepEpgData := range aaepEpgDataList {
×
335
                if aaepEpgData.aaepEpgData.epgDn == epgDn {
×
336
                        cont.log.Infof("Found Attach data: %v EPG : %s AAEP: %s", aaepEpgData, epgDn, aaepName)
×
337
                        return aaepEpgData, dataIndex
×
338
                }
×
339
        }
340
        return nil, -1
×
341
}
342

343
func (cont *AciController) matchesAEPFilter(infraRsObjDn string) string {
×
344
        cont.indexMutex.Lock()
×
345
        defer cont.indexMutex.Unlock()
×
346
        var aaepName string
×
347
        for aaepName = range cont.sharedAaepMonitor {
×
348
                expectedPrefix := fmt.Sprintf("uni/infra/attentp-%s", aaepName)
×
349
                if strings.HasPrefix(infraRsObjDn, expectedPrefix) {
×
350
                        return aaepName
×
351
                }
×
352
        }
353
        return ""
×
354
}
355

356
func (cont *AciController) getEpgDnFromInfraRsDn(infraRsObjDn string) string {
×
357
        re := regexp.MustCompile(`\[(.*?)\]`)
×
358
        match := re.FindStringSubmatch(infraRsObjDn)
×
359

×
360
        var epgDn string
×
361
        if len(match) > 1 {
×
362
                epgDn = match[1]
×
363
                return epgDn
×
364
        }
×
365

366
        return epgDn
×
367
}
368

369
func (cont *AciController) getAaepMonitoringDataForEpg(epgDn string) (string, *AaepMonitoringData) {
×
370
        var aaepMonitorData *AaepMonitoringData
×
371
        var aaepName string
×
372

×
373
        cont.indexMutex.Lock()
×
374
        defer cont.indexMutex.Unlock()
×
375
        for aaepName = range cont.sharedAaepMonitor {
×
376
                encap := cont.getEncapFromAaepEpgAttachObj(aaepName, epgDn)
×
377

×
378
                if encap != "" {
×
379
                        vlanID := cont.getVlanId(encap)
×
380
                        aaepEpgData := &AaepEpgAttachData{
×
381
                                epgDn:     epgDn,
×
382
                                encapVlan: vlanID,
×
383
                        }
×
384

×
385
                        aaepMonitorData = cont.collectNadData(aaepEpgData)
×
386
                        return aaepName, aaepMonitorData
×
387
                }
×
388
        }
389

390
        return "", nil
×
391
}
392

393
func (cont *AciController) cleanAnnotationSubscriptions(aaepName string) {
×
394
        aaepEpgDataList := cont.getAaepEpgAttObjDetails(aaepName)
×
395
        if aaepEpgDataList == nil {
×
396
                return
×
397
        }
×
398

399
        for _, aaepEpgData := range aaepEpgDataList {
×
400
                cont.apicConn.UnsubscribeImmediateDnLocked(aaepEpgData.epgDn, []string{"tagAnnotation"})
×
401
        }
×
402
}
403

404
func (cont *AciController) syncNADsWithAciState(aaepName string, dataIndex int, oldAaepMonitorData, aaepMonitorData *AaepMonitoringData) {
×
405
        if oldAaepMonitorData == nil {
×
406
                cont.indexMutex.Lock()
×
407
                needCacheChange := cont.createNetworkAttachmentDefinition(aaepMonitorData)
×
408
                if needCacheChange {
×
409
                        cont.sharedAaepMonitor[aaepName] = append(cont.sharedAaepMonitor[aaepName], aaepMonitorData)
×
410
                }
×
411
                cont.indexMutex.Unlock()
×
412
        } else {
×
413
                if oldAaepMonitorData.namespaceName != aaepMonitorData.namespaceName {
×
414
                        cont.indexMutex.Lock()
×
415
                        cont.sharedAaepMonitor[aaepName] = append(cont.sharedAaepMonitor[aaepName][:dataIndex], cont.sharedAaepMonitor[aaepName][dataIndex+1:]...)
×
416
                        cont.deleteNetworkAttachmentDefinition(oldAaepMonitorData, "NamespaceAnnotationChanged")
×
417
                        cont.indexMutex.Unlock()
×
418

×
419
                        cont.indexMutex.Lock()
×
420
                        needCacheChange := cont.createNetworkAttachmentDefinition(aaepMonitorData)
×
421
                        if needCacheChange {
×
422
                                cont.sharedAaepMonitor[aaepName] = append(cont.sharedAaepMonitor[aaepName], aaepMonitorData)
×
423
                        }
×
424
                        cont.indexMutex.Unlock()
×
425
                        return
×
426
                }
427

428
                if oldAaepMonitorData.nadName != aaepMonitorData.nadName || oldAaepMonitorData.aaepEpgData.encapVlan != aaepMonitorData.aaepEpgData.encapVlan {
×
429
                        cont.indexMutex.Lock()
×
430
                        needCacheChange := cont.createNetworkAttachmentDefinition(aaepMonitorData)
×
431
                        if needCacheChange {
×
432
                                cont.sharedAaepMonitor[aaepName] = append(cont.sharedAaepMonitor[aaepName][:dataIndex], cont.sharedAaepMonitor[aaepName][dataIndex+1:]...)
×
433
                                cont.sharedAaepMonitor[aaepName] = append(cont.sharedAaepMonitor[aaepName], aaepMonitorData)
×
434
                        }
×
435
                        cont.indexMutex.Unlock()
×
436
                }
437
        }
438
}
439

440
func (cont *AciController) addDeferredNADs(namespaceName string) {
×
441
        cont.indexMutex.Lock()
×
442
        defer cont.indexMutex.Unlock()
×
443
        for aaepName := range cont.sharedAaepMonitor {
×
444
                aaepEpgDataList := cont.getAaepEpgAttObjDetails(aaepName)
×
445

×
446
                if aaepEpgDataList == nil {
×
447
                        continue
×
448
                }
449

450
                for _, aaepEpgData := range aaepEpgDataList {
×
451
                        aaepMonitoringData := cont.collectNadData(&aaepEpgData)
×
452
                        if aaepMonitoringData == nil || aaepMonitoringData.namespaceName != namespaceName {
×
453
                                continue
×
454
                        }
455

456
                        needCacheChange := cont.createNetworkAttachmentDefinition(aaepMonitoringData)
×
457
                        if needCacheChange {
×
458
                                cont.sharedAaepMonitor[aaepName] = append(cont.sharedAaepMonitor[aaepName], aaepMonitoringData)
×
459
                        }
×
460
                }
461
        }
462
}
463

464
func (cont *AciController) cleanNADs(namespaceName string) {
×
465
        cont.indexMutex.Lock()
×
466
        defer cont.indexMutex.Unlock()
×
467
        for aaepName := range cont.sharedAaepMonitor {
×
468
                aaepEpgDataList, exists := cont.sharedAaepMonitor[aaepName]
×
469
                if !exists || len(aaepEpgDataList) == 0 {
×
470
                        continue
×
471
                }
472
                newAaepEpgDataList := []*AaepMonitoringData{}
×
473
                for _, aaepEpgData := range aaepEpgDataList {
×
474
                        if aaepEpgData.namespaceName != namespaceName {
×
475
                                newAaepEpgDataList = append(newAaepEpgDataList, aaepEpgData)
×
476
                        }
×
477
                }
478
                cont.sharedAaepMonitor[aaepName] = newAaepEpgDataList
×
479
        }
480
}
481

482
func (cont *AciController) getAaepEpgAttObjDetails(aaepName string) []AaepEpgAttachData {
×
483
        uri := fmt.Sprintf("/api/node/mo/uni/infra/attentp-%s.json?query-target=subtree&target-subtree-class=infraRsFuncToEpg", aaepName)
×
484

×
485
        resp, err := cont.apicConn.GetApicResponse(uri)
×
486
        if err != nil {
×
487
                cont.log.Errorf("Failed to get response from APIC: %v", err)
×
488
                return nil
×
489
        }
×
490

491
        if len(resp.Imdata) == 0 {
×
492
                cont.log.Debugf("Skipping NAD creation: Can't find EPGs attached with AAEP %s", aaepName)
×
493
                return nil
×
494
        }
×
495

496
        aaepEpgAttchDetails := make([]AaepEpgAttachData, 0)
×
497
        for _, respImdata := range resp.Imdata {
×
498
                aaepEpgAttachObj, ok := respImdata["infraRsFuncToEpg"]
×
499
                if !ok {
×
500
                        cont.log.Debugf("Skipping NAD creation: Empty AAEP EPG attachment object")
×
501
                        continue
×
502
                }
503

504
                if state, hasState := aaepEpgAttachObj.Attributes["state"].(string); hasState {
×
505
                        if state != "formed" {
×
506
                                aaepEpgAttchDn := aaepEpgAttachObj.Attributes["dn"].(string)
×
507
                                cont.log.Debugf("Skipping NAD creation: %s is with state: %s", aaepEpgAttchDn, state)
×
508
                                continue
×
509
                        }
510
                }
511
                vlanID := 0
×
512
                if encap, hasEncap := aaepEpgAttachObj.Attributes["encap"].(string); hasEncap {
×
513
                        vlanID = cont.getVlanId(encap)
×
514
                }
×
515

516
                aaepEpgData := AaepEpgAttachData{
×
517
                        epgDn:     aaepEpgAttachObj.Attributes["tDn"].(string),
×
518
                        encapVlan: vlanID,
×
519
                }
×
520
                aaepEpgAttchDetails = append(aaepEpgAttchDetails, aaepEpgData)
×
521
        }
522

523
        return aaepEpgAttchDetails
×
524
}
525

526
func (cont *AciController) getEpgAnnotations(epgDn string) map[string]string {
×
527
        uri := fmt.Sprintf("/api/node/mo/%s.json?query-target=subtree&target-subtree-class=tagAnnotation", epgDn)
×
528
        resp, err := cont.apicConn.GetApicResponse(uri)
×
529
        if err != nil {
×
530
                cont.log.Errorf("Failed to get response from APIC: %v", err)
×
531
                return nil
×
532
        }
×
533

534
        annotationsMap := make(map[string]string)
×
535
        for _, respImdata := range resp.Imdata {
×
536
                annotationObj, ok := respImdata["tagAnnotation"]
×
537
                if !ok {
×
538
                        cont.log.Debugf("Skipping NAD creation: Empty tag annotation of EPG %s", epgDn)
×
539
                        continue
×
540
                }
541

542
                key := annotationObj.Attributes["key"].(string)
×
543
                annotationsMap[key] = annotationObj.Attributes["value"].(string)
×
544
        }
545

546
        return annotationsMap
×
547
}
548

549
func (cont *AciController) getSpecificEPGAnnotation(annotations map[string]string) (string, string) {
×
550
        namespaceNameAnnotationKey := cont.config.CnoIdentifier + "-namespace"
×
551
        namespaceName, exists := annotations[namespaceNameAnnotationKey]
×
552
        if !exists {
×
553
                cont.log.Errorf("Annotation with key '%s' not found", namespaceNameAnnotationKey)
×
554
        }
×
555

556
        nadNameAnnotationKey := cont.config.CnoIdentifier + "-nad"
×
557
        nadName, exists := annotations[nadNameAnnotationKey]
×
558
        if !exists {
×
559
                cont.log.Errorf("Annotation with key '%s' not found", nadNameAnnotationKey)
×
560
        }
×
561
        return namespaceName, nadName
×
562
}
563

564
func (cont *AciController) namespaceChecks(namespaceName string, epgDn string) bool {
×
565
        if namespaceName == "" {
×
566
                cont.log.Debugf("Defering NAD operation for EPG %s: Namespace name not provided in EPG annotation", epgDn)
×
567
                return false
×
568
        }
×
569

570
        kubeClient := cont.env.(*K8sEnvironment).kubeClient
×
571
        _, err := kubeClient.CoreV1().Namespaces().Get(context.TODO(), namespaceName, metav1.GetOptions{})
×
572
        namespaceExists := err == nil
×
573
        if !namespaceExists {
×
574
                cont.log.Debugf("Defering NAD operation for EPG %s: Namespace %s not exists", epgDn, namespaceName)
×
575
                return false
×
576
        }
×
577

578
        return true
×
579
}
580

581
func (cont *AciController) reconcileNadData(aaepName string) {
×
582
        aaepEpgDataList := cont.getAaepEpgAttObjDetails(aaepName)
×
583

×
584
        for _, aaepEpgData := range aaepEpgDataList {
×
585
                aaepMonitoringData := cont.collectNadData(&aaepEpgData)
×
586
                if aaepMonitoringData == nil {
×
587
                        cont.apicConn.AddImmediateSubscriptionDnLocked(aaepEpgData.epgDn,
×
588
                                []string{"tagAnnotation"}, cont.handleAnnotationAdded,
×
589
                                cont.handleAnnotationDeleted)
×
590
                        continue
×
591
                }
592

593
                cont.indexMutex.Lock()
×
594
                needCacheChange := cont.createNetworkAttachmentDefinition(aaepMonitoringData)
×
595
                if needCacheChange {
×
596
                        cont.sharedAaepMonitor[aaepName] = append(cont.sharedAaepMonitor[aaepName], aaepMonitoringData)
×
597
                }
×
598
                cont.indexMutex.Unlock()
×
599

×
600
                cont.apicConn.AddImmediateSubscriptionDnLocked(aaepEpgData.epgDn,
×
601
                        []string{"tagAnnotation"}, cont.handleAnnotationAdded,
×
602
                        cont.handleAnnotationDeleted)
×
603
        }
604

605
        cont.indexMutex.Lock()
×
606
        if _, ok := cont.sharedAaepMonitor[aaepName]; !ok {
×
607
                cont.sharedAaepMonitor[aaepName] = []*AaepMonitoringData{}
×
608
        }
×
609
        cont.indexMutex.Unlock()
×
610
}
611

612
func (cont *AciController) generateDefaultNadName(epgDn string) string {
×
613
        parts := strings.Split(epgDn, "/")
×
614

×
615
        tenant := parts[1][3:]
×
616
        appProfile := parts[2][3:]
×
617
        epgName := parts[3][4:]
×
618

×
619
        return fmt.Sprintf("%s-%s-%s", tenant, appProfile, epgName)
×
620
}
×
621

622
func (cont *AciController) isNADUpdateRequired(nadData *AaepMonitoringData, existingNAD *nadapi.NetworkAttachmentDefinition) bool {
×
623
        vlanID := nadData.aaepEpgData.encapVlan
×
624
        namespaceName := nadData.namespaceName
×
625
        customNadName := nadData.nadName
×
626
        defaultNadName := cont.generateDefaultNadName(nadData.aaepEpgData.epgDn)
×
627
        existingAnnotaions := existingNAD.ObjectMeta.Annotations
×
628
        if existingAnnotaions != nil {
×
629
                if existingNAD.ObjectMeta.Annotations["aci-sync-status"] == "out-of-sync" || existingNAD.ObjectMeta.Annotations["cno-name"] != customNadName {
×
630
                        return true
×
631
                }
×
632
        } else {
×
633
                // NAD exists, check if VLAN needs to be updated
×
634
                existingConfig := existingNAD.Spec.Config
×
635
                if existingConfig != "" {
×
636
                        var existingCNVConfig map[string]interface{}
×
637
                        if json.Unmarshal([]byte(existingConfig), &existingCNVConfig) == nil {
×
638
                                if existingVLAN, ok := existingCNVConfig["vlan"].(float64); ok {
×
639
                                        if int(existingVLAN) == vlanID {
×
640
                                                // VLAN hasn't changed, no update needed
×
641
                                                cont.log.Infof("NetworkAttachmentDefinition %s already exists with correct VLAN %d in namespace %s", defaultNadName, vlanID, namespaceName)
×
642
                                                return false
×
643
                                        }
×
644
                                } else if vlanID == 0 {
×
645
                                        // Both existing and new have no VLAN, no update needed
×
646
                                        cont.log.Infof("NetworkAttachmentDefinition %s already exists with no VLAN in namespace %s", defaultNadName, namespaceName)
×
647
                                        return false
×
648
                                }
×
649
                        }
650
                }
651
        }
652

653
        return true
×
654
}
655

656
func (cont *AciController) createNetworkAttachmentDefinition(nadData *AaepMonitoringData) bool {
×
657
        bridge := cont.config.BridgeName
×
658
        if bridge == "" {
×
659
                cont.log.Errorf("Linux bridge name must be specified when creating NetworkAttachmentDefinitions")
×
660
                return false
×
661
        }
×
662

663
        vlanID := nadData.aaepEpgData.encapVlan
×
664
        namespaceName := nadData.namespaceName
×
665
        customNadName := nadData.nadName
×
666
        defaultNadName := cont.generateDefaultNadName(nadData.aaepEpgData.epgDn)
×
667
        nadClient := cont.env.(*K8sEnvironment).nadClient
×
668
        mtu := 1500
×
669

×
670
        // Check if NAD already exists
×
671
        existingNAD, err := nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Get(context.TODO(), defaultNadName, metav1.GetOptions{})
×
672
        nadExists := err == nil
×
673

×
674
        if nadExists && !cont.isNADUpdateRequired(nadData, existingNAD) {
×
675
                return true
×
676
        }
×
677

678
        cnvBridgeConfig := map[string]any{
×
679
                "cniVersion":       "0.3.1",
×
680
                "name":             defaultNadName,
×
681
                "type":             "bridge",
×
682
                "isDefaultGateway": true,
×
683
                "bridge":           bridge,
×
684
                "mtu":              mtu,
×
685
        }
×
686

×
687
        if vlanID > 0 {
×
688
                cnvBridgeConfig["vlan"] = vlanID
×
689
        }
×
690

691
        configJSON, err := json.Marshal(cnvBridgeConfig)
×
692
        if err != nil {
×
693
                cont.log.Errorf("Failed to marshal CNV bridge config: %v", err)
×
694
                return false
×
695
        }
×
696

697
        nad := &nadapi.NetworkAttachmentDefinition{
×
698
                TypeMeta: metav1.TypeMeta{
×
699
                        APIVersion: "k8s.cni.cncf.io/v1",
×
700
                        Kind:       "NetworkAttachmentDefinition",
×
701
                },
×
702
                ObjectMeta: metav1.ObjectMeta{
×
703
                        Name:      defaultNadName,
×
704
                        Namespace: namespaceName,
×
705
                        Labels: map[string]string{
×
706
                                "managed-by": "cisco-network-operator",
×
707
                                "vlan":       strconv.Itoa(vlanID),
×
708
                        },
×
709
                        Annotations: map[string]string{
×
710
                                "managed-by": "cisco-network-operator",
×
711
                                "vlan":       strconv.Itoa(vlanID),
×
712
                                "cno-name":   customNadName,
×
713
                        },
×
714
                },
×
715
                Spec: nadapi.NetworkAttachmentDefinitionSpec{
×
716
                        Config: string(configJSON),
×
717
                },
×
718
        }
×
719

×
720
        if nadExists {
×
721
                nad.ObjectMeta.ResourceVersion = existingNAD.ObjectMeta.ResourceVersion
×
722

×
723
                _, err = nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Update(context.TODO(), nad, metav1.UpdateOptions{})
×
724

×
725
                if err != nil {
×
726
                        cont.log.Errorf("Failed to update NetworkAttachmentDefinition %s from namespace %s : %v", customNadName, namespaceName, err)
×
727
                        return false
×
728
                }
×
729
                cont.log.Infof("Updated NetworkAttachmentDefinition %s from namespace %s", defaultNadName, namespaceName)
×
730
        } else {
×
731
                _, err = nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Create(context.TODO(), nad, metav1.CreateOptions{})
×
732
                if err != nil {
×
733
                        cont.log.Errorf("Failed to create NetworkAttachmentDefinition %s in namespace %s : %v", customNadName, namespaceName, err)
×
734
                        return false
×
735
                }
×
736
                cont.log.Infof("Created NetworkAttachmentDefinition %s in namespace %s", defaultNadName, namespaceName)
×
737
        }
738

739
        return true
×
740
}
741

742
func (cont *AciController) deleteNetworkAttachmentDefinition(nadData *AaepMonitoringData, deleteReason string) {
×
743
        namespaceName := nadData.namespaceName
×
744
        epgDn := nadData.aaepEpgData.epgDn
×
745

×
746
        kubeClient := cont.env.(*K8sEnvironment).kubeClient
×
747
        _, err := kubeClient.CoreV1().Namespaces().Get(context.TODO(), namespaceName, metav1.GetOptions{})
×
748
        namespaceExists := err == nil
×
749
        if !namespaceExists {
×
750
                cont.log.Debugf("Defering NAD deletion for EPG %s: Namespace %s not exists", epgDn, namespaceName)
×
751
                return
×
752
        }
×
753

754
        nadName := cont.generateDefaultNadName(nadData.aaepEpgData.epgDn)
×
755
        nadClient := cont.env.(*K8sEnvironment).nadClient
×
756

×
757
        nadDetails, err := nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Get(context.TODO(), nadName, metav1.GetOptions{})
×
758
        nadExists := err == nil
×
759

×
760
        if nadExists {
×
761
                if !cont.isVmmLiteNAD(nadDetails) {
×
762
                        return
×
763
                }
×
764

765
                if cont.isNADinUse(namespaceName, nadName) {
×
766
                        nadDetails.ObjectMeta.Annotations["aci-sync-status"] = "out-of-sync"
×
767
                        _, err = nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Update(context.TODO(), nadDetails, metav1.UpdateOptions{})
×
768
                        if err != nil {
×
769
                                cont.log.Errorf("Failed to add out-of-sync annotation to the NAD %s from namespace %s : %v", nadName, namespaceName, err)
×
770
                                return
×
771
                        }
×
772
                        cont.submitEvent(nadDetails, deleteReason, cont.getNADDeleteMessage(deleteReason))
×
773
                        cont.log.Infof("Added annotation out-of-sync for NAD %s from namespace %s", nadName, namespaceName)
×
774
                        return
×
775
                }
776

777
                delete(nadDetails.ObjectMeta.Annotations, "managed-by")
×
778
                _, err = nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Update(context.TODO(), nadDetails, metav1.UpdateOptions{})
×
779
                if err != nil {
×
780
                        cont.log.Errorf("Failed to remove VMM lite annotation from NetworkAttachmentDefinition %s from namespace %s: %v", nadName, namespaceName, err)
×
781
                        return
×
782
                }
×
783

784
                nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Delete(context.TODO(), nadName, metav1.DeleteOptions{})
×
785
                cont.log.Infof("Deleted NAD %s from %s namespace", nadName, namespaceName)
×
786
        } else {
×
787
                cont.log.Debugf("NAD %s not there to delete in namespace %s", nadName, namespaceName)
×
788
        }
×
789
}
790

791
func (cont *AciController) getVlanId(encap string) int {
×
792
        if after, ok := strings.CutPrefix(encap, "vlan-"); ok {
×
793
                vlanStr := after
×
794
                if vlanID, err := strconv.Atoi(vlanStr); err == nil && vlanID > 0 {
×
795
                        return vlanID
×
796
                }
×
797
        } else if after, ok := strings.CutPrefix(encap, "vlan"); ok {
×
798
                vlanStr := after
×
799
                if vlanID, err := strconv.Atoi(vlanStr); err == nil && vlanID > 0 {
×
800
                        return vlanID
×
801
                }
×
802
        }
803

804
        return 0
×
805
}
806

807
func (cont *AciController) getAaepDiff(crAaeps []string) (addedAaeps, removedAaeps []string) {
×
808
        crAaepMap := make(map[string]bool)
×
809
        for _, crAaep := range crAaeps {
×
810
                crAaepMap[crAaep] = true
×
811
        }
×
812

813
        cont.indexMutex.Lock()
×
814
        for _, crAaep := range crAaeps {
×
815
                if _, ok := cont.sharedAaepMonitor[crAaep]; !ok {
×
816
                        addedAaeps = append(addedAaeps, crAaep)
×
817
                }
×
818
        }
819
        cont.indexMutex.Unlock()
×
820

×
821
        cont.indexMutex.Lock()
×
822
        for cachedAaep := range cont.sharedAaepMonitor {
×
823
                if !crAaepMap[cachedAaep] {
×
824
                        removedAaeps = append(removedAaeps, cachedAaep)
×
825
                }
×
826
        }
827
        cont.indexMutex.Unlock()
×
828

×
829
        return
×
830
}
831

832
func (cont *AciController) getEncapFromAaepEpgAttachObj(aaepName, epgDn string) string {
×
833
        uri := fmt.Sprintf("/api/node/mo/uni/infra/attentp-%s/gen-default/rsfuncToEpg-[%s].json?query-target=self", aaepName, epgDn)
×
834
        resp, err := cont.apicConn.GetApicResponse(uri)
×
835
        if err != nil {
×
836
                cont.log.Errorf("Failed to get response from APIC: AAEP %s and EPG %s ERROR: %v", aaepName, epgDn, err)
×
837
                return ""
×
838
        }
×
839

840
        for _, obj := range resp.Imdata {
×
841
                lresp, ok := obj["infraRsFuncToEpg"]
×
842
                if !ok {
×
843
                        cont.log.Errorf("InfraRsFuncToEpg object not found in response for %s", uri)
×
844
                        break
×
845
                }
846
                if val, ok := lresp.Attributes["encap"]; ok {
×
847
                        encap := val.(string)
×
848
                        return encap
×
849
                } else {
×
850
                        cont.log.Errorf("Encap missing for infraRsFuncToEpg object for %s: %v", uri, err)
×
851
                        break
×
852
                }
853
        }
854

855
        return ""
×
856
}
857

858
func (cont *AciController) isVmmLiteNAD(nadDetails *nadapi.NetworkAttachmentDefinition) bool {
×
859
        return nadDetails.ObjectMeta.Annotations["managed-by"] == "cisco-network-operator"
×
860
}
×
861

862
func (cont *AciController) isNADinUse(namespaceName string, nadName string) bool {
×
863
        kubeClient := cont.env.(*K8sEnvironment).kubeClient
×
864
        pods, err := kubeClient.CoreV1().Pods(namespaceName).List(context.TODO(), metav1.ListOptions{})
×
865
        if err == nil {
×
866
                for _, pod := range pods.Items {
×
867
                        networksAnn, ok := pod.Annotations["k8s.v1.cni.cncf.io/networks"]
×
868
                        if ok && (networksAnn == nadName) {
×
869
                                cont.log.Infof("NAD %s is still used by Pod %s/%s", nadName, namespaceName, pod.Name)
×
870
                                return true
×
871
                        }
×
872
                }
873
        }
874
        return false
×
875
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc