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

noironetworks / aci-containers / 11007

23 Sep 2025 10:38AM UTC coverage: 65.721% (-0.1%) from 65.826%
11007

push

travis-pro

web-flow
Merge pull request #1572 from noironetworks/vmm_lite_review_fix

Addressed demo review comments

0 of 55 new or added lines in 1 file covered. (0.0%)

7 existing lines in 2 files now uncovered.

13363 of 20333 relevant lines covered (65.72%)

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

×
NEW
206
        defer 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

×
NEW
226
        cont.syncNADsWithAciState(aaepName, dataIndex, oldAaepMonitorData, aaepMonitorData, "AaepEpgAttached")
×
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

×
NEW
268
        cont.syncNADsWithAciState(aaepName, dataIndex, oldAaepMonitorData, aaepMonitorData, "NamespaceAnnotationAdded")
×
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

NEW
293
        cont.syncNADsWithAciState(aaepName, dataIndex, oldAaepMonitorData, aaepMonitorData, "NamespaceAnnotationRemoved")
×
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,
NEW
405
        aaepMonitorData *AaepMonitoringData, syncReason string) {
×
406
        if oldAaepMonitorData == nil {
×
407
                cont.indexMutex.Lock()
×
NEW
408
                needCacheChange := cont.createNetworkAttachmentDefinition(aaepMonitorData, syncReason)
×
409
                if needCacheChange {
×
410
                        cont.sharedAaepMonitor[aaepName] = append(cont.sharedAaepMonitor[aaepName], aaepMonitorData)
×
411
                }
×
412
                cont.indexMutex.Unlock()
×
413
        } else {
×
414
                if oldAaepMonitorData.namespaceName != aaepMonitorData.namespaceName {
×
415
                        cont.indexMutex.Lock()
×
416
                        cont.sharedAaepMonitor[aaepName] = append(cont.sharedAaepMonitor[aaepName][:dataIndex], cont.sharedAaepMonitor[aaepName][dataIndex+1:]...)
×
NEW
417
                        cont.deleteNetworkAttachmentDefinition(oldAaepMonitorData, syncReason)
×
418
                        cont.indexMutex.Unlock()
×
419

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

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

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

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

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

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

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

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

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

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

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

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

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

524
        return aaepEpgAttchDetails
×
525
}
526

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

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

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

547
        return annotationsMap
×
548
}
549

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

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

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

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

579
        return true
×
580
}
581

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

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

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

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

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

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

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

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

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

654
        return true
×
655
}
656

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

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

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

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

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

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

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

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

×
722
        if nadExists {
×
723
                nad.ObjectMeta.ResourceVersion = existingNAD.ObjectMeta.ResourceVersion
×
724

×
NEW
725
                updatedNad, err := nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Update(context.TODO(), nad, metav1.UpdateOptions{})
×
726

×
727
                if err != nil {
×
728
                        cont.log.Errorf("Failed to update NetworkAttachmentDefinition %s from namespace %s : %v", customNadName, namespaceName, err)
×
729
                        return false
×
730
                }
×
731

NEW
732
                cont.log.Debugf("Existing NAD Annotations: %v, %s", existingNAD.ObjectMeta.Annotations, createReason)
×
NEW
733
                if existingNAD.ObjectMeta.Annotations["aci-sync-status"] == "out-of-sync" {
×
NEW
734
                        cont.submitEvent(updatedNad, createReason, cont.getNADRevampMessage(createReason))
×
NEW
735
                }
×
736
                cont.log.Infof("Updated NetworkAttachmentDefinition %s from namespace %s", defaultNadName, namespaceName)
×
737
        } else {
×
738
                _, err = nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Create(context.TODO(), nad, metav1.CreateOptions{})
×
739
                if err != nil {
×
740
                        cont.log.Errorf("Failed to create NetworkAttachmentDefinition %s in namespace %s : %v", customNadName, namespaceName, err)
×
741
                        return false
×
742
                }
×
743
                cont.log.Infof("Created NetworkAttachmentDefinition %s in namespace %s", defaultNadName, namespaceName)
×
744
        }
745

746
        return true
×
747
}
748

749
func (cont *AciController) deleteNetworkAttachmentDefinition(nadData *AaepMonitoringData, deleteReason string) {
×
750
        namespaceName := nadData.namespaceName
×
751
        epgDn := nadData.aaepEpgData.epgDn
×
752

×
753
        kubeClient := cont.env.(*K8sEnvironment).kubeClient
×
754
        _, err := kubeClient.CoreV1().Namespaces().Get(context.TODO(), namespaceName, metav1.GetOptions{})
×
755
        namespaceExists := err == nil
×
756
        if !namespaceExists {
×
757
                cont.log.Debugf("Defering NAD deletion for EPG %s: Namespace %s not exists", epgDn, namespaceName)
×
758
                return
×
759
        }
×
760

761
        nadName := cont.generateDefaultNadName(nadData.aaepEpgData.epgDn)
×
762
        nadClient := cont.env.(*K8sEnvironment).nadClient
×
763

×
764
        nadDetails, err := nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Get(context.TODO(), nadName, metav1.GetOptions{})
×
765
        nadExists := err == nil
×
766

×
767
        if nadExists {
×
768
                if !cont.isVmmLiteNAD(nadDetails) {
×
769
                        return
×
770
                }
×
771

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

784
                delete(nadDetails.ObjectMeta.Annotations, "managed-by")
×
785
                _, err = nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Update(context.TODO(), nadDetails, metav1.UpdateOptions{})
×
786
                if err != nil {
×
787
                        cont.log.Errorf("Failed to remove VMM lite annotation from NetworkAttachmentDefinition %s from namespace %s: %v", nadName, namespaceName, err)
×
788
                        return
×
789
                }
×
790

791
                nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Delete(context.TODO(), nadName, metav1.DeleteOptions{})
×
792
                cont.log.Infof("Deleted NAD %s from %s namespace", nadName, namespaceName)
×
793
        } else {
×
794
                cont.log.Debugf("NAD %s not there to delete in namespace %s", nadName, namespaceName)
×
795
        }
×
796
}
797

798
func (cont *AciController) getVlanId(encap string) int {
×
799
        if after, ok := strings.CutPrefix(encap, "vlan-"); ok {
×
800
                vlanStr := after
×
801
                if vlanID, err := strconv.Atoi(vlanStr); err == nil && vlanID > 0 {
×
802
                        return vlanID
×
803
                }
×
804
        } else if after, ok := strings.CutPrefix(encap, "vlan"); ok {
×
805
                vlanStr := after
×
806
                if vlanID, err := strconv.Atoi(vlanStr); err == nil && vlanID > 0 {
×
807
                        return vlanID
×
808
                }
×
809
        }
810

811
        return 0
×
812
}
813

814
func (cont *AciController) getAaepDiff(crAaeps []string) (addedAaeps, removedAaeps []string) {
×
815
        crAaepMap := make(map[string]bool)
×
816
        for _, crAaep := range crAaeps {
×
817
                crAaepMap[crAaep] = true
×
818
        }
×
819

820
        cont.indexMutex.Lock()
×
821
        for _, crAaep := range crAaeps {
×
822
                if _, ok := cont.sharedAaepMonitor[crAaep]; !ok {
×
823
                        addedAaeps = append(addedAaeps, crAaep)
×
824
                }
×
825
        }
826
        cont.indexMutex.Unlock()
×
827

×
828
        cont.indexMutex.Lock()
×
829
        for cachedAaep := range cont.sharedAaepMonitor {
×
830
                if !crAaepMap[cachedAaep] {
×
831
                        removedAaeps = append(removedAaeps, cachedAaep)
×
832
                }
×
833
        }
834
        cont.indexMutex.Unlock()
×
835

×
836
        return
×
837
}
838

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

847
        for _, obj := range resp.Imdata {
×
848
                lresp, ok := obj["infraRsFuncToEpg"]
×
849
                if !ok {
×
850
                        cont.log.Errorf("InfraRsFuncToEpg object not found in response for %s", uri)
×
851
                        break
×
852
                }
853
                if val, ok := lresp.Attributes["encap"]; ok {
×
854
                        encap := val.(string)
×
855
                        return encap
×
856
                } else {
×
857
                        cont.log.Errorf("Encap missing for infraRsFuncToEpg object for %s: %v", uri, err)
×
858
                        break
×
859
                }
860
        }
861

862
        return ""
×
863
}
864

865
func (cont *AciController) isVmmLiteNAD(nadDetails *nadapi.NetworkAttachmentDefinition) bool {
×
866
        return nadDetails.ObjectMeta.Annotations["managed-by"] == "cisco-network-operator"
×
867
}
×
868

869
func (cont *AciController) isNADinUse(namespaceName string, nadName string) bool {
×
870
        kubeClient := cont.env.(*K8sEnvironment).kubeClient
×
871
        pods, err := kubeClient.CoreV1().Pods(namespaceName).List(context.TODO(), metav1.ListOptions{})
×
872
        if err == nil {
×
NEW
873
                var networks []map[string]string
×
874
                for _, pod := range pods.Items {
×
875
                        networksAnn, ok := pod.Annotations["k8s.v1.cni.cncf.io/networks"]
×
876
                        if ok && (networksAnn == nadName) {
×
877
                                cont.log.Infof("NAD %s is still used by Pod %s/%s", nadName, namespaceName, pod.Name)
×
878
                                return true
×
879
                        }
×
NEW
880
                        if err := json.Unmarshal([]byte(networksAnn), &networks); err != nil {
×
NEW
881
                                cont.log.Errorf("Error while getting pod annotations: %v", err)
×
NEW
882
                                return false
×
NEW
883
                        }
×
NEW
884
                        for _, network := range networks {
×
NEW
885
                                if ok && (network["name"] == nadName) {
×
NEW
886
                                        cont.log.Infof("NAD %s is still used by VM %s/%s", nadName, namespaceName, pod.Name)
×
NEW
887
                                        return true
×
NEW
888
                                }
×
889
                        }
890
                }
891
        }
892
        return false
×
893
}
894

NEW
895
func (cont *AciController) getNADDeleteMessage(deleteReason string) string {
×
NEW
896
        messagePrefix := "NAD is in use by pods: "
×
NEW
897
        switch {
×
NEW
898
        case deleteReason == "NamespaceAnnotationRemoved":
×
NEW
899
                return messagePrefix + "Namespace name EPG annotaion removed"
×
NEW
900
        case deleteReason == "AaepEpgDetached":
×
NEW
901
                return messagePrefix + "EPG detached from AAEP"
×
NEW
902
        case deleteReason == "CRDeleted":
×
NEW
903
                return messagePrefix + "aaepmonitor CR deleted"
×
NEW
904
        case deleteReason == "AaepRemovedFromCR":
×
NEW
905
                return messagePrefix + "AAEP removed from aaepmonitor CR"
×
906
        }
NEW
907
        return messagePrefix + "One or many pods are using NAD"
×
908
}
909

NEW
910
func (cont *AciController) getNADRevampMessage(createReason string) string {
×
NEW
911
        messagePrefix := "NAD is in sync: "
×
NEW
912
        switch {
×
NEW
913
        case createReason == "NamespaceAnnotationAdded":
×
NEW
914
                return messagePrefix + "Namespace name EPG annotaion added"
×
NEW
915
        case createReason == "AaepEpgAttached":
×
NEW
916
                return messagePrefix + "EPG attached with AAEP"
×
NEW
917
        case createReason == "AaepAddedInCR":
×
NEW
918
                return messagePrefix + "AAEP added back in aaepmonitor CR"
×
NEW
919
        case createReason == "NamespaceCreated":
×
NEW
920
                return messagePrefix + "Namespace created back"
×
921
        }
NEW
922
        return messagePrefix + "NAD synced with ACI"
×
923
}
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