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

noironetworks / aci-containers / 11313

07 Nov 2025 03:28PM UTC coverage: 64.259% (-0.09%) from 64.353%
11313

push

travis-pro

web-flow
Merge pull request #1625 from noironetworks/mmr-6.1.1-fixed_event_msg_and_NAD_update_issue

Added fix for following:

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

2 existing lines in 1 file now uncovered.

13353 of 20780 relevant lines covered (64.26%)

0.73 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
        "crypto/sha256"
22
        "encoding/hex"
23
        "encoding/json"
24
        "fmt"
25
        "regexp"
26
        "strconv"
27
        "strings"
28

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

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

41
const (
42
        aaepMonitorCRDName = "aaepmonitors.aci.attachmentmonitor"
43
)
44

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

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

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

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

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

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

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

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

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

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

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

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

156
        cont.log.Infof("Started processing of aaepmonitor CR creation/modification")
×
157
        addedAaeps, removedAaeps := cont.getAaepDiff(aaepMonitorConfig.Spec.Aaeps)
×
158
        for _, aaepName := range addedAaeps {
×
159
                cont.reconcileNadData(aaepName)
×
160
        }
×
161

162
        for _, aaepName := range removedAaeps {
×
163
                cont.cleanAnnotationSubscriptions(aaepName)
×
164

×
165
                cont.indexMutex.Lock()
×
166
                aaepEpgAttachDataMap := cont.sharedAaepMonitor[aaepName]
×
167
                delete(cont.sharedAaepMonitor, aaepName)
×
168

×
169
                for epgDn, aaepEpgAttachData := range aaepEpgAttachDataMap {
×
170
                        cont.deleteNetworkAttachmentDefinition(aaepName, epgDn, aaepEpgAttachData, "AaepRemovedFromCR")
×
171
                }
×
172
                cont.indexMutex.Unlock()
×
173
        }
174
        cont.log.Infof("Completed processing of aaepmonitor CR creation/modification")
×
175
        return false
×
176
}
177

178
func (cont *AciController) handleAaepMonitorConfigurationDelete(key string) bool {
×
179
        cont.indexMutex.Lock()
×
180
        defer cont.indexMutex.Unlock()
×
181
        cont.log.Infof("Started processing of aaepmonitor CR deletion")
×
182
        for aaepName, aaepEpgAttachDataMap := range cont.sharedAaepMonitor {
×
183
                cont.cleanAnnotationSubscriptions(aaepName)
×
184

×
185
                for epgDn, aaepEpgAttachData := range aaepEpgAttachDataMap {
×
186
                        cont.deleteNetworkAttachmentDefinition(aaepName, epgDn, aaepEpgAttachData, "CRDeleted")
×
187
                }
×
188
        }
189

190
        cont.sharedAaepMonitor = make(map[string]map[string]*AaepEpgAttachData)
×
191
        cont.log.Infof("Completed processing of aaepmonitor CR deletion")
×
192
        return false
×
193
}
194

195
func (cont *AciController) handleAaepEpgAttach(infraRsObj apicapi.ApicObject) {
×
196
        infraRsObjDn := infraRsObj.GetDn()
×
197
        aaepName := cont.matchesAEPFilter(infraRsObjDn)
×
198
        cont.log.Infof("Started processing of EPG: %s attached with AAEP: %s", infraRsObj.GetAttrStr("tDn"), aaepName)
×
199
        if aaepName == "" {
×
200
                cont.log.Debugf("Unable to find AAEP from %s in monitoring list", infraRsObjDn)
×
201
                return
×
202
        }
×
203

204
        state := infraRsObj.GetAttrStr("state")
×
205
        if state != "formed" {
×
206
                cont.log.Debugf("Skipping NAD creation: %s is with state: %s", infraRsObjDn, state)
×
207
                return
×
208
        }
×
209

210
        epgDn := infraRsObj.GetAttrStr("tDn")
×
211
        encap := infraRsObj.GetAttrStr("encap")
×
212
        vlanID := cont.getVlanId(encap)
×
213

×
214
        if cont.checkDuplicateAaepEpgAttachRequest(aaepName, epgDn, vlanID) {
×
215
                cont.log.Infof("AAEP %s EPG %s attachment data already exists", aaepName, epgDn)
×
216
                return
×
217
        }
×
218

219
        cont.indexMutex.Lock()
×
220
        oldAaepMonitorData := cont.getAaepEpgAttachDataLocked(aaepName, epgDn)
×
221
        cont.indexMutex.Unlock()
×
222

×
223
        if cont.checkVlanUsedInCluster(vlanID) {
×
224
                // This is needed when user updates vlan to the vlan already used in cluster
×
225
                cont.handleOldNadDeletion(aaepName, epgDn, oldAaepMonitorData, "AaepEpgAttachedWithVlanInUse")
×
226
                cont.log.Errorf("Skipping NAD creation: VLAN %d is already used in cluster", vlanID)
×
227
                return
×
228
        }
×
229

230
        defer cont.apicConn.AddImmediateSubscriptionDnLocked(epgDn, []string{"tagAnnotation"}, cont.handleAnnotationAdded,
×
231
                cont.handleAnnotationDeleted)
×
232

×
233
        epgVlanMap := &EpgVlanMap{
×
234
                epgDn:     epgDn,
×
235
                encapVlan: vlanID,
×
236
        }
×
237

×
238
        aaepEpgAttachData := cont.collectNadData(epgVlanMap)
×
239
        if aaepEpgAttachData == nil {
×
240
                return
×
241
        }
×
242

243
        if cont.checkIfEpgWithOverlappingVlan(aaepName, vlanID, epgDn) {
×
244
                // This is needed when user updates vlan from non-overlapping to overlapping
×
245
                cont.handleOldNadDeletion(aaepName, epgDn, oldAaepMonitorData, "AaepEpgAttachedWithOverlappingVlan")
×
246

×
247
                // Add new entry with nadCreated as false
×
248
                cont.indexMutex.Lock()
×
249
                if cont.sharedAaepMonitor[aaepName] == nil {
×
250
                        cont.sharedAaepMonitor[aaepName] = make(map[string]*AaepEpgAttachData)
×
251
                }
×
252
                aaepEpgAttachData.nadCreated = false
×
253
                cont.sharedAaepMonitor[aaepName][epgDn] = aaepEpgAttachData
×
254
                cont.indexMutex.Unlock()
×
255
                cont.log.Errorf("Skipping NAD creation: EPG %s with AAEP %s has overlapping VLAN %d", epgDn, aaepName, vlanID)
×
256
                return
×
257
        }
258
        cont.syncNADsWithAciState(aaepName, epgDn, oldAaepMonitorData, aaepEpgAttachData, "AaepEpgAttached")
×
259
        cont.log.Infof("Completed processing of EPG: %s attached with AAEP: %s", epgDn, aaepName)
×
260
}
261

262
func (cont *AciController) handleAaepEpgDetach(infraRsObjDn string) {
×
263
        aaepName := cont.matchesAEPFilter(infraRsObjDn)
×
264
        if aaepName == "" {
×
265
                cont.log.Debugf("Unable to find AAEP from %s in monitoring list", infraRsObjDn)
×
266
                return
×
267
        }
×
268

269
        epgDn := cont.getEpgDnFromInfraRsDn(infraRsObjDn)
×
270

×
271
        if epgDn == "" {
×
272
                cont.log.Errorf("Unable to find EPG from %s", infraRsObjDn)
×
273
                return
×
274
        }
×
275

276
        cont.log.Infof("Started processing of EPG: %s detached from AAEP: %s", infraRsObjDn, aaepName)
×
277
        // Need to check if EPG is not attached with any other AAEP
×
278
        if !cont.isEpgAttachedWithAaep(epgDn) {
×
279
                cont.apicConn.UnsubscribeImmediateDnLocked(epgDn, []string{"tagAnnotation"})
×
280
        }
×
281

282
        cont.indexMutex.Lock()
×
283
        aaepEpgAttachData := cont.getAaepEpgAttachDataLocked(aaepName, epgDn)
×
284
        cont.indexMutex.Unlock()
×
285

×
286
        if aaepEpgAttachData == nil {
×
287
                cont.log.Debugf("Monitoring data not available for EPG %s with AAEP %s", epgDn, aaepName)
×
288
                return
×
289
        }
×
290
        if !cont.namespaceChecks(aaepEpgAttachData.namespaceName, epgDn) {
×
291
                cont.log.Debugf("Namespace %s not found", aaepEpgAttachData.namespaceName)
×
292
                return
×
293
        }
×
294

295
        cont.handleOldNadDeletion(aaepName, epgDn, aaepEpgAttachData, "AaepEpgDetached")
×
296
        cont.log.Infof("Completed processing of EPG: %s detached from AAEP: %s", epgDn, aaepName)
×
297
}
298

299
func (cont *AciController) handleAnnotationAdded(obj apicapi.ApicObject) bool {
×
300
        annotationDn := obj.GetDn()
×
301
        epgDn := annotationDn[:strings.Index(annotationDn, "/annotationKey-")]
×
302
        aaepMonitorDataMap := cont.getAaepMonitoringDataForEpg(epgDn)
×
303

×
304
        cont.log.Infof("Started processing of EPG: %s annotation %s addition/modification", epgDn, annotationDn)
×
305
        for aaepName, aaepMonitorData := range aaepMonitorDataMap {
×
306
                cont.indexMutex.Lock()
×
307
                oldAaepMonitorData := cont.getAaepEpgAttachDataLocked(aaepName, epgDn)
×
308
                cont.indexMutex.Unlock()
×
309

×
310
                if len(aaepMonitorData) == 0 {
×
311
                        // No new monitoring data available for this EPG with this AAEP but old data exists
×
312
                        // delete old NAD if exists and remove from monitoring map
×
313
                        // create NAD for next EPG with old VLAN
×
314
                        cont.handleOldNadDeletion(aaepName, epgDn, oldAaepMonitorData, "NamespaceAnnotationAdded")
×
315
                        cont.log.Debugf("Insufficient data for NAD creation: Monitoring data not available for EPG %s with AAEP %s", epgDn, aaepName)
×
316
                        continue
×
317
                }
318

319
                aaepEpgAttachData, exists := aaepMonitorData[epgDn]
×
320
                if !exists || aaepEpgAttachData == nil {
×
321
                        cont.handleOldNadDeletion(aaepName, epgDn, oldAaepMonitorData, "NamespaceAnnotationAdded")
×
322
                        cont.log.Debugf("Insufficient data for NAD creation: Monitoring data not available for EPG %s with AAEP %s", epgDn, aaepName)
×
323
                        continue
×
324
                }
325

326
                if !cont.checkMonitorDataModified(oldAaepMonitorData, aaepEpgAttachData) {
×
327
                        cont.log.Debugf("No changes in annotation for EPG %s with AAEP %s", epgDn, aaepName)
×
328
                        continue
×
329
                }
330

331
                if !cont.namespaceChecks(aaepEpgAttachData.namespaceName, epgDn) {
×
332
                        // Namespace not exist, need to delete old NAD if exists and remove from monitoring map
×
333
                        // create NAD for next EPG with old VLAN
×
334
                        cont.handleOldNadDeletion(aaepName, epgDn, oldAaepMonitorData, "NamespaceAnnotationAdded")
×
335
                        cont.log.Debugf("Insufficient data in annotation for NAD creation: Namespace not exist, in case of EPG %s with AAEP %s", epgDn, aaepName)
×
336
                        continue
×
337
                }
338

339
                // This is needed when user updates annotation and VLAN is already overlapping
340
                if cont.checkIfEpgWithOverlappingVlan(aaepName, aaepEpgAttachData.encapVlan, epgDn) {
×
341
                        // Add new entry with nadCreated as false
×
342
                        aaepEpgAttachData.nadCreated = false
×
343
                        cont.indexMutex.Lock()
×
344
                        if cont.sharedAaepMonitor[aaepName] == nil {
×
345
                                cont.sharedAaepMonitor[aaepName] = make(map[string]*AaepEpgAttachData)
×
346
                        }
×
347
                        cont.sharedAaepMonitor[aaepName][epgDn] = aaepEpgAttachData
×
348
                        cont.indexMutex.Unlock()
×
349
                        cont.log.Debugf("Skipping EPG annotation add handling: EPG %s with AAEP %s has overlapping VLAN %d", epgDn,
×
350
                                aaepName, aaepEpgAttachData.encapVlan)
×
351
                        continue
×
352
                }
353

354
                cont.syncNADsWithAciState(aaepName, epgDn, oldAaepMonitorData, aaepEpgAttachData, "NamespaceAnnotationAdded")
×
355
        }
356
        cont.log.Infof("Completed processing annotation addition/modification for EPG: %s, AnnotationDn: %s", epgDn, annotationDn)
×
357
        return true
×
358
}
359

360
func (cont *AciController) handleAnnotationDeleted(annotationDn string) {
×
361
        epgDn := annotationDn[:strings.Index(annotationDn, "/annotationKey-")]
×
362
        cont.log.Infof("Started processing of EPG: %s annotation %s deletion", epgDn, annotationDn)
×
363
        aaepMonitorDataMap := cont.getAaepMonitoringDataForEpg(epgDn)
×
364

×
NEW
365
        var reason string
×
NEW
366
        if !cont.checkIfEPGExists(epgDn) {
×
NEW
367
                reason = "EpgDeleted"
×
NEW
368
        } else {
×
NEW
369
                reason = "NamespaceAnnotationRemoved"
×
NEW
370
        }
×
371

372
        for aaepName, aaepMonitorData := range aaepMonitorDataMap {
×
373
                cont.indexMutex.Lock()
×
374
                oldAaepMonitorData := cont.getAaepEpgAttachDataLocked(aaepName, epgDn)
×
375
                cont.indexMutex.Unlock()
×
376

×
377
                if oldAaepMonitorData == nil {
×
378
                        cont.log.Debugf("Monitoring data not available for EPG %s with AAEP %s", epgDn, aaepName)
×
379
                        continue
×
380
                }
381

382
                if oldAaepMonitorData.nadCreated == false {
×
383
                        cont.indexMutex.Lock()
×
384
                        delete(cont.sharedAaepMonitor[aaepName], epgDn)
×
385
                        cont.indexMutex.Unlock()
×
386
                        continue
×
387
                }
388

389
                if aaepMonitorData == nil {
×
390
                        cont.indexMutex.Lock()
×
391
                        delete(cont.sharedAaepMonitor[aaepName], epgDn)
×
NEW
392
                        cont.deleteNetworkAttachmentDefinition(aaepName, epgDn, oldAaepMonitorData, reason)
×
393
                        cont.indexMutex.Unlock()
×
394
                        cont.createNadForNextEpg(aaepName, oldAaepMonitorData.encapVlan)
×
395
                        continue
×
396
                }
397

398
                aaepEpgAttachData, exists := aaepMonitorData[epgDn]
×
399
                if !exists || aaepEpgAttachData == nil {
×
400
                        cont.indexMutex.Lock()
×
401
                        delete(cont.sharedAaepMonitor[aaepName], epgDn)
×
NEW
402
                        cont.deleteNetworkAttachmentDefinition(aaepName, epgDn, oldAaepMonitorData, reason)
×
403
                        cont.indexMutex.Unlock()
×
404
                        cont.createNadForNextEpg(aaepName, oldAaepMonitorData.encapVlan)
×
405
                        continue
×
406
                }
407

NEW
408
                cont.syncNADsWithAciState(aaepName, epgDn, oldAaepMonitorData, aaepEpgAttachData, reason)
×
409
        }
410
        cont.log.Infof("Completed processing annotation deletion for EPG: %s, AnnotationDn: %s", epgDn, annotationDn)
×
411
}
412

413
func (cont *AciController) collectNadData(epgVlanMap *EpgVlanMap) *AaepEpgAttachData {
×
414
        epgDn := epgVlanMap.epgDn
×
415
        epgAnnotations := cont.getEpgAnnotations(epgDn)
×
NEW
416
        if epgAnnotations == nil {
×
NEW
417
                cont.log.Debugf("No annotations found for EPG %s", epgDn)
×
NEW
418
                return nil
×
NEW
419
        }
×
420
        namespaceName, nadName := cont.getSpecificEPGAnnotation(epgAnnotations)
×
421

×
422
        if !cont.namespaceChecks(namespaceName, epgDn) {
×
423
                cont.log.Debugf("Namespace not exist, in case of EPG %s", epgDn)
×
424
                return nil
×
425
        }
×
426

427
        aaepMonitoringData := &AaepEpgAttachData{
×
428
                encapVlan:     epgVlanMap.encapVlan,
×
429
                nadName:       nadName,
×
430
                namespaceName: namespaceName,
×
431
                nadCreated:    true,
×
432
        }
×
433

×
434
        return aaepMonitoringData
×
435
}
436

437
func (cont *AciController) getAaepEpgAttachDataLocked(aaepName, epgDn string) *AaepEpgAttachData {
×
438
        aaepEpgAttachDataMap, exists := cont.sharedAaepMonitor[aaepName]
×
439
        if !exists || aaepEpgAttachDataMap == nil {
×
440
                cont.log.Debugf("AAEP %s EPG %s attachment data not found", aaepName, epgDn)
×
441
                return nil
×
442
        }
×
443

444
        aaepEpgAttachData, exists := aaepEpgAttachDataMap[epgDn]
×
445
        if !exists || aaepEpgAttachData == nil {
×
446
                cont.log.Debugf("AAEP %s EPG %s attachment data not found", aaepName, epgDn)
×
447
                return nil
×
448
        }
×
449

450
        cont.log.Infof("Found attachment data: %v for EPG : %s AAEP: %s", *aaepEpgAttachData, epgDn, aaepName)
×
451
        return aaepEpgAttachData
×
452
}
453

454
func (cont *AciController) matchesAEPFilter(infraRsObjDn string) string {
×
455
        cont.indexMutex.Lock()
×
456
        defer cont.indexMutex.Unlock()
×
457
        var aaepName string
×
458
        for aaepName = range cont.sharedAaepMonitor {
×
459
                expectedPrefix := fmt.Sprintf("uni/infra/attentp-%s/", aaepName)
×
460
                if strings.HasPrefix(infraRsObjDn, expectedPrefix) {
×
461
                        return aaepName
×
462
                }
×
463
        }
464
        return ""
×
465
}
466

467
func (cont *AciController) getEpgDnFromInfraRsDn(infraRsObjDn string) string {
×
468
        re := regexp.MustCompile(`\[(.*?)\]`)
×
469
        match := re.FindStringSubmatch(infraRsObjDn)
×
470

×
471
        var epgDn string
×
472
        if len(match) > 1 {
×
473
                epgDn = match[1]
×
474
                return epgDn
×
475
        }
×
476

477
        return epgDn
×
478
}
479

480
func (cont *AciController) getAaepMonitoringDataForEpg(epgDn string) map[string]map[string]*AaepEpgAttachData {
×
481
        aaepEpgAttachData := make(map[string]map[string]*AaepEpgAttachData)
×
482

×
483
        cont.indexMutex.Lock()
×
484
        defer cont.indexMutex.Unlock()
×
485
        for aaepName := range cont.sharedAaepMonitor {
×
486
                encap := cont.getEncapFromAaepEpgAttachObj(aaepName, epgDn)
×
487

×
488
                if encap != "" {
×
489
                        vlanID := cont.getVlanId(encap)
×
490
                        epgVlanMap := EpgVlanMap{
×
491
                                epgDn:     epgDn,
×
492
                                encapVlan: vlanID,
×
493
                        }
×
494
                        if aaepEpgAttachData[aaepName] == nil {
×
495
                                aaepEpgAttachData[aaepName] = make(map[string]*AaepEpgAttachData)
×
496
                        }
×
497
                        nadData := cont.collectNadData(&epgVlanMap)
×
498
                        if nadData != nil {
×
499
                                aaepEpgAttachData[aaepName][epgDn] = nadData
×
500
                        }
×
501
                }
502
        }
503

504
        return aaepEpgAttachData
×
505
}
506

507
func (cont *AciController) cleanAnnotationSubscriptions(aaepName string) {
×
508
        epgVlanMapList := cont.getAaepEpgAttObjDetails(aaepName)
×
509
        if epgVlanMapList == nil {
×
510
                return
×
511
        }
×
512

513
        for _, epgVlanMap := range epgVlanMapList {
×
514
                cont.apicConn.UnsubscribeImmediateDnLocked(epgVlanMap.epgDn, []string{"tagAnnotation"})
×
515
        }
×
516
}
517

518
func (cont *AciController) syncNADsWithAciState(aaepName string, epgDn string, oldAaepEpgAttachData,
519
        aaepEpgAttachData *AaepEpgAttachData, syncReason string) {
×
520
        if oldAaepEpgAttachData == nil {
×
521
                cont.handleNewNadCreation(aaepName, epgDn, aaepEpgAttachData, syncReason)
×
522
        } else {
×
523
                if cont.checkAnnotationModified(oldAaepEpgAttachData, aaepEpgAttachData) {
×
524
                        if oldAaepEpgAttachData.namespaceName != aaepEpgAttachData.namespaceName {
×
525
                                cont.indexMutex.Lock()
×
526
                                if oldAaepEpgAttachData.nadCreated == true {
×
527
                                        cont.deleteNetworkAttachmentDefinition(aaepName, epgDn, oldAaepEpgAttachData, syncReason)
×
528
                                }
×
529
                                delete(cont.sharedAaepMonitor[aaepName], epgDn)
×
530
                                cont.indexMutex.Unlock()
×
531
                        }
532
                        cont.handleNewNadCreation(aaepName, epgDn, aaepEpgAttachData, syncReason)
×
533
                        return
×
534
                }
535

536
                if oldAaepEpgAttachData.encapVlan != aaepEpgAttachData.encapVlan {
×
537
                        cont.handleOldNadDeletion(aaepName, epgDn, oldAaepEpgAttachData, syncReason)
×
538
                        cont.handleNewNadCreation(aaepName, epgDn, aaepEpgAttachData, syncReason)
×
539
                }
×
540
        }
541
}
542

543
func (cont *AciController) addDeferredNADs(namespaceName string) {
×
544
        aaepEpgVlanMap := make(map[string][]EpgVlanMap)
×
545

×
546
        // Collect all AAEP EPG attachment details
×
547
        cont.indexMutex.Lock()
×
548
        for aaepName := range cont.sharedAaepMonitor {
×
549
                epgVlanMapList := cont.getAaepEpgAttObjDetails(aaepName)
×
550

×
551
                if epgVlanMapList == nil {
×
552
                        continue
×
553
                }
554

555
                aaepEpgVlanMap[aaepName] = epgVlanMapList
×
556
        }
557
        cont.indexMutex.Unlock()
×
558

×
559
        // Process each AAEP EPG attachment details
×
560
        for aaepName, epgVlanMapList := range aaepEpgVlanMap {
×
561
                for _, epgVlanMap := range epgVlanMapList {
×
562
                        aaepEpgAttachData := cont.collectNadData(&epgVlanMap)
×
563
                        if aaepEpgAttachData == nil || aaepEpgAttachData.namespaceName != namespaceName {
×
564
                                continue
×
565
                        }
566

567
                        if cont.checkIfEpgWithOverlappingVlan(aaepName, aaepEpgAttachData.encapVlan, epgVlanMap.epgDn) {
×
568
                                cont.indexMutex.Lock()
×
569
                                if cont.sharedAaepMonitor[aaepName] == nil {
×
570
                                        cont.sharedAaepMonitor[aaepName] = make(map[string]*AaepEpgAttachData)
×
571
                                }
×
572
                                aaepEpgAttachData.nadCreated = false
×
573
                                cont.sharedAaepMonitor[aaepName][epgVlanMap.epgDn] = aaepEpgAttachData
×
574
                                cont.indexMutex.Unlock()
×
575

×
576
                                cont.log.Errorf("Skipping NAD creation: EPG %s with AAEP %s has overlapping VLAN %d", epgVlanMap.epgDn,
×
577
                                        aaepName, aaepEpgAttachData.encapVlan)
×
578
                                continue
×
579
                        } else if cont.checkVlanUsedInCluster(aaepEpgAttachData.encapVlan) {
×
580
                                cont.log.Errorf("Skipping NAD creation: VLAN %d is already used in cluster", aaepEpgAttachData.encapVlan)
×
581
                                continue
×
582
                        } else {
×
583
                                epgDn := epgVlanMap.epgDn
×
584
                                cont.handleNewNadCreation(aaepName, epgDn, aaepEpgAttachData, "NamespaceCreated")
×
585
                        }
×
586
                }
587
        }
588
}
589

590
func (cont *AciController) cleanNADs(namespaceName string) {
×
591
        aaepMonitorDataToDelete := make(map[string]map[string]*AaepEpgAttachData)
×
592
        cont.indexMutex.Lock()
×
593
        for aaepName := range cont.sharedAaepMonitor {
×
594
                aaepEpgAttachDataMap, exists := cont.sharedAaepMonitor[aaepName]
×
595
                if !exists || aaepEpgAttachDataMap == nil {
×
596
                        continue
×
597
                }
598

599
                for epgDn, aaepEpgAttachData := range aaepEpgAttachDataMap {
×
600
                        if aaepEpgAttachData.namespaceName == namespaceName {
×
601
                                nadName := cont.generateDefaultNadName(aaepName, epgDn)
×
602
                                removedAnnotation := cont.removeManagedByAnnotationFromNAD(nadName, namespaceName)
×
603
                                if !removedAnnotation {
×
604
                                        continue
×
605
                                }
606
                                delete(cont.sharedAaepMonitor[aaepName], epgDn)
×
607
                                if aaepEpgAttachData.nadCreated {
×
608
                                        cont.deleteNetworkAttachmentDefinition(aaepName, epgDn, aaepEpgAttachData, "NamespaceDeleted")
×
609
                                }
×
610
                                aaepMonitorDataToDelete[aaepName] = aaepEpgAttachDataMap
×
611
                        }
612
                }
613
        }
614
        cont.indexMutex.Unlock()
×
615

×
616
        for aaepName, aaepEpgAttachDataMap := range aaepMonitorDataToDelete {
×
617
                for _, aaepEpgAttachData := range aaepEpgAttachDataMap {
×
618
                        cont.createNadForNextEpg(aaepName, aaepEpgAttachData.encapVlan)
×
619
                }
×
620
        }
621
}
622

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

×
626
        resp, err := cont.apicConn.GetApicResponse(uri)
×
627
        if err != nil {
×
628
                cont.log.Errorf("Failed to get response from APIC: %v", err)
×
629
                return nil
×
630
        }
×
631

632
        if len(resp.Imdata) == 0 {
×
633
                cont.log.Debugf("Can't find EPGs attached with AAEP %s", aaepName)
×
634
                return nil
×
635
        }
×
636

637
        epgVlanMapList := make([]EpgVlanMap, 0)
×
638
        for _, respImdata := range resp.Imdata {
×
639
                aaepEpgAttachObj, ok := respImdata["infraRsFuncToEpg"]
×
640
                if !ok {
×
641
                        cont.log.Debugf("Empty AAEP EPG attachment object")
×
642
                        continue
×
643
                }
644

645
                if state, hasState := aaepEpgAttachObj.Attributes["state"].(string); hasState {
×
646
                        if state != "formed" {
×
647
                                aaepEpgAttchDn := aaepEpgAttachObj.Attributes["dn"].(string)
×
648
                                cont.log.Debugf("%s is with state: %s", aaepEpgAttchDn, state)
×
649
                                continue
×
650
                        }
651
                }
652
                vlanID := 0
×
653
                if encap, hasEncap := aaepEpgAttachObj.Attributes["encap"].(string); hasEncap {
×
654
                        vlanID = cont.getVlanId(encap)
×
655
                }
×
656

657
                epgVlanMap := EpgVlanMap{
×
658
                        epgDn:     aaepEpgAttachObj.Attributes["tDn"].(string),
×
659
                        encapVlan: vlanID,
×
660
                }
×
661
                epgVlanMapList = append(epgVlanMapList, epgVlanMap)
×
662
        }
663

664
        return epgVlanMapList
×
665
}
666

667
func (cont *AciController) getEpgAnnotations(epgDn string) map[string]string {
×
668
        uri := fmt.Sprintf("/api/node/mo/%s.json?query-target=subtree&target-subtree-class=tagAnnotation", epgDn)
×
669
        resp, err := cont.apicConn.GetApicResponse(uri)
×
670
        if err != nil {
×
671
                cont.log.Errorf("Failed to get response from APIC: %v", err)
×
672
                return nil
×
673
        }
×
674

675
        annotationsMap := make(map[string]string)
×
676
        for _, respImdata := range resp.Imdata {
×
677
                annotationObj, ok := respImdata["tagAnnotation"]
×
678
                if !ok {
×
679
                        cont.log.Debugf("Empty tag annotation of EPG %s", epgDn)
×
680
                        continue
×
681
                }
682

683
                key := annotationObj.Attributes["key"].(string)
×
684
                annotationsMap[key] = annotationObj.Attributes["value"].(string)
×
685
        }
686

687
        return annotationsMap
×
688
}
689

NEW
690
func (cont *AciController) checkIfEPGExists(epgDn string) bool {
×
NEW
691
        uri := fmt.Sprintf("/api/node/mo/%s.json", epgDn)
×
NEW
692
        resp, err := cont.apicConn.GetApicResponse(uri)
×
NEW
693
        if err != nil {
×
NEW
694
                cont.log.Errorf("Failed to get response from APIC: %v", err)
×
NEW
695
                return false
×
NEW
696
        }
×
NEW
697
        if len(resp.Imdata) == 0 {
×
NEW
698
                cont.log.Debugf("EPG %s does not exist", epgDn)
×
NEW
699
                return false
×
NEW
700
        }
×
NEW
701
        return true
×
702
}
703

704
func (cont *AciController) getSpecificEPGAnnotation(annotations map[string]string) (string, string) {
×
705
        namespaceNameAnnotationKey := cont.config.CnoIdentifier + "-namespace"
×
706
        namespaceName, exists := annotations[namespaceNameAnnotationKey]
×
707
        if !exists {
×
708
                cont.log.Debugf("Annotation with key '%s' not found", namespaceNameAnnotationKey)
×
709
        }
×
710

711
        nadNameAnnotationKey := cont.config.CnoIdentifier + "-nad"
×
712
        nadName, exists := annotations[nadNameAnnotationKey]
×
713
        if !exists {
×
714
                cont.log.Debugf("Annotation with key '%s' not found", nadNameAnnotationKey)
×
715
        }
×
716
        return namespaceName, nadName
×
717
}
718

719
func (cont *AciController) namespaceChecks(namespaceName string, epgDn string) bool {
×
720
        if namespaceName == "" {
×
721
                cont.log.Debugf("Defering NAD operation for EPG %s: Namespace name not provided in EPG annotation", epgDn)
×
722
                return false
×
723
        }
×
724

725
        kubeClient := cont.env.(*K8sEnvironment).kubeClient
×
726
        _, err := kubeClient.CoreV1().Namespaces().Get(context.TODO(), namespaceName, metav1.GetOptions{})
×
727
        namespaceExists := err == nil
×
728
        if !namespaceExists {
×
729
                cont.log.Debugf("Defering NAD operation for EPG %s: Namespace %s not exists", epgDn, namespaceName)
×
730
                return false
×
731
        }
×
732

733
        return true
×
734
}
735

736
func (cont *AciController) reconcileNadData(aaepName string) {
×
737
        epgVlanMapList := cont.getAaepEpgAttObjDetails(aaepName)
×
738

×
739
        for _, epgVlanMap := range epgVlanMapList {
×
740
                if cont.checkVlanUsedInCluster(epgVlanMap.encapVlan) {
×
741
                        cont.log.Errorf("Skipping NAD creation: VLAN %d is already used in cluster", epgVlanMap.encapVlan)
×
742
                        continue
×
743
                }
744
                aaepEpgAttachData := cont.collectNadData(&epgVlanMap)
×
745
                if aaepEpgAttachData == nil {
×
746
                        cont.apicConn.AddImmediateSubscriptionDnLocked(epgVlanMap.epgDn,
×
747
                                []string{"tagAnnotation"}, cont.handleAnnotationAdded,
×
748
                                cont.handleAnnotationDeleted)
×
749
                        continue
×
750
                }
751

752
                epgDn := epgVlanMap.epgDn
×
753
                if cont.checkIfEpgWithOverlappingVlan(aaepName, aaepEpgAttachData.encapVlan, epgDn) {
×
754
                        aaepEpgAttachData.nadCreated = false
×
755
                        cont.indexMutex.Lock()
×
756
                        if cont.sharedAaepMonitor[aaepName] == nil {
×
757
                                cont.sharedAaepMonitor[aaepName] = make(map[string]*AaepEpgAttachData)
×
758
                        }
×
759
                        cont.sharedAaepMonitor[aaepName][epgDn] = aaepEpgAttachData
×
760
                        cont.indexMutex.Unlock()
×
761
                        cont.apicConn.AddImmediateSubscriptionDnLocked(epgDn,
×
762
                                []string{"tagAnnotation"}, cont.handleAnnotationAdded,
×
763
                                cont.handleAnnotationDeleted)
×
764
                        cont.log.Errorf("Skipping NAD creation: EPG %s with AAEP %s has overlapping VLAN %d",
×
765
                                epgDn, aaepName, aaepEpgAttachData.encapVlan)
×
766
                        continue
×
767
                }
768

769
                OldepgDn := cont.handleNewNadCreation(aaepName, epgDn, aaepEpgAttachData, "AaepAddedInCR")
×
770
                if OldepgDn != "" {
×
771
                        cont.apicConn.AddImmediateSubscriptionDnLocked(OldepgDn,
×
772
                                []string{"tagAnnotation"}, cont.handleAnnotationAdded,
×
773
                                cont.handleAnnotationDeleted)
×
774
                }
×
775

776
                cont.apicConn.AddImmediateSubscriptionDnLocked(epgDn,
×
777
                        []string{"tagAnnotation"}, cont.handleAnnotationAdded,
×
778
                        cont.handleAnnotationDeleted)
×
779
        }
780

781
        cont.indexMutex.Lock()
×
782
        if _, ok := cont.sharedAaepMonitor[aaepName]; !ok {
×
783
                cont.sharedAaepMonitor[aaepName] = make(map[string]*AaepEpgAttachData)
×
784
        }
×
785
        cont.indexMutex.Unlock()
×
786
}
787

788
// clean converts a string to lowercase, removes underscores and dots,
789
// and replaces any other invalid character with a hyphen.
790
func cleanApicResourceNames(apicResource string) string {
×
791
        apicResource = strings.ToLower(apicResource)
×
792
        var stringBuilder strings.Builder
×
793
        for _, character := range apicResource {
×
794
                switch {
×
795
                case character == '_' || character == '.':
×
796
                        continue
×
797
                case (character >= 'a' && character <= 'z') || (character >= '0' && character <= '9') || character == '-':
×
798
                        stringBuilder.WriteRune(character)
×
799
                default:
×
800
                        stringBuilder.WriteRune('-')
×
801
                }
802
        }
803
        return strings.Trim(stringBuilder.String(), "-")
×
804
}
805

806
func (cont *AciController) generateDefaultNadName(aaepName, epgDn string) string {
×
807
        parts := strings.Split(epgDn, "/")
×
808

×
809
        tenant := parts[1][3:]
×
810
        appProfile := parts[2][3:]
×
811
        epgName := parts[3][4:]
×
812

×
813
        apicResourceNames := tenant + appProfile + epgName + aaepName
×
814
        hashBytes := sha256.Sum256([]byte(apicResourceNames))
×
815
        hash := hex.EncodeToString(hashBytes[:])[:16]
×
816

×
817
        return fmt.Sprintf("%s-%s-%s-%s",
×
818
                cleanApicResourceNames(tenant), cleanApicResourceNames(appProfile), cleanApicResourceNames(epgName), hash)
×
819
}
×
820

821
func (cont *AciController) isNADUpdateRequired(aaepName string, epgDn string, nadData *AaepEpgAttachData,
822
        existingNAD *nadapi.NetworkAttachmentDefinition) bool {
×
823
        vlanID := nadData.encapVlan
×
824
        namespaceName := nadData.namespaceName
×
825
        customNadName := nadData.nadName
×
826
        defaultNadName := cont.generateDefaultNadName(aaepName, epgDn)
×
827
        existingAnnotaions := existingNAD.ObjectMeta.Annotations
×
828
        if existingAnnotaions != nil {
×
829
                if existingNAD.ObjectMeta.Annotations["aci-sync-status"] == "out-of-sync" || existingNAD.ObjectMeta.Annotations["cno-name"] != customNadName {
×
830
                        return true
×
831
                }
×
832
        } else {
×
833
                // NAD exists, check if VLAN needs to be updated
×
834
                existingConfig := existingNAD.Spec.Config
×
835
                if existingConfig != "" {
×
836
                        var existingCNVConfig map[string]interface{}
×
837
                        if json.Unmarshal([]byte(existingConfig), &existingCNVConfig) == nil {
×
838
                                if existingVLAN, ok := existingCNVConfig["vlan"].(float64); ok {
×
839
                                        if int(existingVLAN) == vlanID {
×
840
                                                // VLAN hasn't changed, no update needed
×
841
                                                cont.log.Infof("NetworkAttachmentDefinition %s already exists with correct VLAN %d in namespace %s",
×
842
                                                        defaultNadName, vlanID, namespaceName)
×
843
                                                return false
×
844
                                        }
×
845
                                } else if vlanID == 0 {
×
846
                                        // Both existing and new have no VLAN, no update needed
×
847
                                        cont.log.Infof("NetworkAttachmentDefinition %s already exists with no VLAN in namespace %s", defaultNadName, namespaceName)
×
848
                                        return false
×
849
                                }
×
850
                        }
851
                }
852
        }
853

854
        return true
×
855
}
856

857
func (cont *AciController) createNetworkAttachmentDefinition(aaepName string, epgDn string, nadData *AaepEpgAttachData, createReason string) bool {
×
858
        bridge := cont.config.BridgeName
×
859
        if bridge == "" {
×
860
                cont.log.Errorf("Linux bridge name must be specified when creating NetworkAttachmentDefinitions")
×
861
                return false
×
862
        }
×
863

864
        vlanID := nadData.encapVlan
×
865
        namespaceName := nadData.namespaceName
×
866
        customNadName := nadData.nadName
×
867
        defaultNadName := cont.generateDefaultNadName(aaepName, epgDn)
×
868
        nadClient := cont.env.(*K8sEnvironment).nadClient
×
869
        mtu := 1500
×
870

×
871
        // Check if NAD already exists
×
872
        existingNAD, err := nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Get(context.TODO(), defaultNadName, metav1.GetOptions{})
×
873
        nadExists := err == nil
×
874

×
875
        if nadExists && !cont.isNADUpdateRequired(aaepName, epgDn, nadData, existingNAD) {
×
876
                return true
×
877
        }
×
878

879
        cnvBridgeConfig := map[string]any{
×
NEW
880
                "cniVersion": "0.3.1",
×
NEW
881
                "name":       defaultNadName,
×
NEW
882
                "type":       "bridge",
×
NEW
883
                "bridge":     bridge,
×
NEW
884
        }
×
NEW
885

×
NEW
886
        if !nadExists {
×
NEW
887
                cnvBridgeConfig["mtu"] = mtu
×
NEW
888
                cnvBridgeConfig["disableContainerInterface"] = true
×
NEW
889
                // Add optional parameters from controller config if they are set
×
NEW
890
                if cont.config.IsGateway != nil {
×
NEW
891
                        cnvBridgeConfig["isGateway"] = *cont.config.IsGateway
×
NEW
892
                }
×
NEW
893
                if cont.config.IsDefaultGateway != nil {
×
NEW
894
                        cnvBridgeConfig["isDefaultGateway"] = *cont.config.IsDefaultGateway
×
NEW
895
                }
×
NEW
896
                if cont.config.ForceAddress != nil {
×
NEW
897
                        cnvBridgeConfig["forceAddress"] = *cont.config.ForceAddress
×
NEW
898
                }
×
NEW
899
                if cont.config.IpMasq != nil {
×
NEW
900
                        cnvBridgeConfig["ipMasq"] = *cont.config.IpMasq
×
NEW
901
                }
×
NEW
902
                if cont.config.IpMasqBackend != "" {
×
NEW
903
                        cnvBridgeConfig["ipMasqBackend"] = cont.config.IpMasqBackend
×
NEW
904
                }
×
NEW
905
                if cont.config.Mtu != nil {
×
NEW
906
                        cnvBridgeConfig["mtu"] = *cont.config.Mtu
×
NEW
907
                }
×
NEW
908
                if cont.config.HairpinMode != nil {
×
NEW
909
                        cnvBridgeConfig["hairpinMode"] = *cont.config.HairpinMode
×
NEW
910
                }
×
NEW
911
                if cont.config.PromiscMode != nil {
×
NEW
912
                        cnvBridgeConfig["promiscMode"] = *cont.config.PromiscMode
×
NEW
913
                }
×
NEW
914
                if cont.config.Enabledad != nil {
×
NEW
915
                        cnvBridgeConfig["enabledad"] = *cont.config.Enabledad
×
NEW
916
                }
×
NEW
917
                if cont.config.Macspoofchk != nil {
×
NEW
918
                        cnvBridgeConfig["macspoofchk"] = *cont.config.Macspoofchk
×
NEW
919
                }
×
NEW
920
                if cont.config.DisableContainerInterface != nil {
×
NEW
921
                        cnvBridgeConfig["disableContainerInterface"] = *cont.config.DisableContainerInterface
×
NEW
922
                }
×
NEW
923
                if cont.config.PortIsolation != nil {
×
NEW
924
                        cnvBridgeConfig["portIsolation"] = *cont.config.PortIsolation
×
NEW
925
                }
×
NEW
926
                if len(cont.config.Ipam) > 0 {
×
NEW
927
                        cnvBridgeConfig["ipam"] = cont.config.Ipam
×
NEW
928
                }
×
NEW
929
        } else {
×
NEW
930
                // Preserve existing optional parameters from existing NAD
×
NEW
931
                existingConfig := existingNAD.Spec.Config
×
NEW
932
                if existingConfig != "" {
×
NEW
933
                        var existingCNVConfig map[string]interface{}
×
NEW
934
                        if json.Unmarshal([]byte(existingConfig), &existingCNVConfig) == nil {
×
NEW
935
                                optionalParams := []string{"isGateway", "isDefaultGateway", "forceAddress", "ipMasq",
×
NEW
936
                                        "ipMasqBackend", "mtu", "hairpinMode", "promiscMode", "enabledad", "macspoofchk",
×
NEW
937
                                        "disableContainerInterface", "portIsolation", "ipam"}
×
NEW
938
                                for _, param := range optionalParams {
×
NEW
939
                                        if value, ok := existingCNVConfig[param]; ok {
×
NEW
940
                                                cnvBridgeConfig[param] = value
×
NEW
941
                                        }
×
942
                                }
943
                        }
944
                }
945
        }
946
        if vlanID > 0 {
×
947
                cnvBridgeConfig["vlan"] = vlanID
×
948
        }
×
949

950
        configJSON, err := json.Marshal(cnvBridgeConfig)
×
951
        if err != nil {
×
952
                cont.log.Errorf("Failed to marshal CNV bridge config: %v", err)
×
953
                return false
×
954
        }
×
955

956
        nad := &nadapi.NetworkAttachmentDefinition{
×
957
                TypeMeta: metav1.TypeMeta{
×
958
                        APIVersion: "k8s.cni.cncf.io/v1",
×
959
                        Kind:       "NetworkAttachmentDefinition",
×
960
                },
×
961
                ObjectMeta: metav1.ObjectMeta{
×
962
                        Name:      defaultNadName,
×
963
                        Namespace: namespaceName,
×
964
                        Labels: map[string]string{
×
965
                                "managed-by": "cisco-network-operator",
×
966
                                "vlan":       strconv.Itoa(vlanID),
×
967
                        },
×
968
                        Annotations: map[string]string{
×
969
                                "managed-by":      "cisco-network-operator",
×
970
                                "vlan":            strconv.Itoa(vlanID),
×
971
                                "cno-name":        customNadName,
×
972
                                "aci-sync-status": "in-sync",
×
973
                                "aaep-name":       aaepName,
×
974
                                "epg-dn":          epgDn,
×
975
                        },
×
976
                },
×
977
                Spec: nadapi.NetworkAttachmentDefinitionSpec{
×
978
                        Config: string(configJSON),
×
979
                },
×
980
        }
×
981

×
982
        if nadExists {
×
983
                nad.ObjectMeta.ResourceVersion = existingNAD.ObjectMeta.ResourceVersion
×
984

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

×
987
                if err != nil {
×
988
                        cont.log.Errorf("Failed to update NetworkAttachmentDefinition %s from namespace %s : %v", customNadName, namespaceName, err)
×
989
                        return false
×
990
                }
×
991

992
                cont.log.Debugf("Existing NAD Annotations: %v, %s", existingNAD.ObjectMeta.Annotations, createReason)
×
993
                if existingNAD.ObjectMeta.Annotations["aci-sync-status"] == "out-of-sync" {
×
994
                        cont.submitEvent(updatedNad, createReason, cont.getNADRevampMessage(createReason))
×
995
                }
×
996
                cont.log.Infof("Updated NetworkAttachmentDefinition %s from namespace %s", defaultNadName, namespaceName)
×
997
        } else {
×
998
                _, err = nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Create(context.TODO(), nad, metav1.CreateOptions{})
×
999
                if err != nil {
×
1000
                        cont.log.Errorf("Failed to create NetworkAttachmentDefinition %s in namespace %s : %v", customNadName, namespaceName, err)
×
1001
                        return false
×
1002
                }
×
1003
                cont.log.Infof("Created NetworkAttachmentDefinition %s in namespace %s", defaultNadName, namespaceName)
×
1004
        }
1005

1006
        return true
×
1007
}
1008

1009
func (cont *AciController) deleteNetworkAttachmentDefinition(aaepName string, epgDn string, nadData *AaepEpgAttachData, deleteReason string) {
×
1010
        namespaceName := nadData.namespaceName
×
1011

×
1012
        kubeClient := cont.env.(*K8sEnvironment).kubeClient
×
1013
        _, err := kubeClient.CoreV1().Namespaces().Get(context.TODO(), namespaceName, metav1.GetOptions{})
×
1014
        namespaceExists := err == nil
×
1015
        if !namespaceExists {
×
1016
                cont.log.Debugf("Defering NAD deletion for EPG %s: Namespace %s not exists", epgDn, namespaceName)
×
1017
                return
×
1018
        }
×
1019

1020
        nadName := cont.generateDefaultNadName(aaepName, epgDn)
×
1021
        nadClient := cont.env.(*K8sEnvironment).nadClient
×
1022

×
1023
        nadDetails, err := nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Get(context.TODO(), nadName, metav1.GetOptions{})
×
1024
        nadExists := err == nil
×
1025

×
1026
        if nadExists {
×
1027
                if !cont.isVmmLiteNAD(nadDetails) {
×
1028
                        return
×
1029
                }
×
1030

1031
                if cont.isNADinUse(namespaceName, nadName) {
×
1032
                        nadDetails.ObjectMeta.Annotations["aci-sync-status"] = "out-of-sync"
×
1033
                        _, err = nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Update(context.TODO(), nadDetails, metav1.UpdateOptions{})
×
1034
                        if err != nil {
×
1035
                                cont.log.Errorf("Failed to add out-of-sync annotation to the NAD %s from namespace %s : %v", nadName, namespaceName, err)
×
1036
                                return
×
1037
                        }
×
1038
                        cont.submitEvent(nadDetails, deleteReason, cont.getNADDeleteMessage(deleteReason))
×
1039
                        cont.log.Infof("Added annotation out-of-sync for NAD %s from namespace %s", nadName, namespaceName)
×
1040
                        return
×
1041
                }
1042

1043
                delete(nadDetails.ObjectMeta.Annotations, "managed-by")
×
1044
                _, err = nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Update(context.TODO(), nadDetails, metav1.UpdateOptions{})
×
1045
                if err != nil {
×
1046
                        cont.log.Errorf("Failed to remove VMM lite annotation from NetworkAttachmentDefinition %s from namespace %s: %v", nadName, namespaceName, err)
×
1047
                        return
×
1048
                }
×
1049

1050
                nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Delete(context.TODO(), nadName, metav1.DeleteOptions{})
×
1051
                cont.log.Infof("Deleted NAD %s from %s namespace", nadName, namespaceName)
×
1052
        } else {
×
1053
                cont.log.Debugf("NAD %s not there to delete in namespace %s", nadName, namespaceName)
×
1054
        }
×
1055
}
1056

1057
func (cont *AciController) removeManagedByAnnotationFromNAD(nadName, namespaceName string) bool {
×
1058
        nadClient := cont.env.(*K8sEnvironment).nadClient
×
1059
        // Check if NAD already exists
×
1060
        existingNAD, err := nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Get(context.TODO(), nadName, metav1.GetOptions{})
×
1061
        nadExists := err == nil
×
1062

×
1063
        if nadExists {
×
1064
                if !cont.isVmmLiteNAD(existingNAD) {
×
1065
                        return true
×
1066
                }
×
1067

1068
                delete(existingNAD.ObjectMeta.Annotations, "managed-by")
×
1069
                _, err = nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Update(context.TODO(), existingNAD, metav1.UpdateOptions{})
×
1070
                if err != nil {
×
1071
                        cont.log.Errorf("Failed to remove managed-by VMM lite annotation from NetworkAttachmentDefinition %s from namespace %s: %v", nadName, namespaceName, err)
×
1072
                        return false
×
1073
                }
×
1074

1075
                cont.log.Infof("Removed managed-by annotation from NAD %s in namespace %s", nadName, namespaceName)
×
1076
        } else {
×
1077
                cont.log.Debugf("NAD %s not there to remove managed-by annotation in namespace %s", nadName, namespaceName)
×
1078
        }
×
1079
        return true
×
1080
}
1081

1082
func (cont *AciController) getVlanId(encap string) int {
×
1083
        if after, ok := strings.CutPrefix(encap, "vlan-"); ok {
×
1084
                vlanStr := after
×
1085
                if vlanID, err := strconv.Atoi(vlanStr); err == nil && vlanID > 0 {
×
1086
                        return vlanID
×
1087
                }
×
1088
        } else if after, ok := strings.CutPrefix(encap, "vlan"); ok {
×
1089
                vlanStr := after
×
1090
                if vlanID, err := strconv.Atoi(vlanStr); err == nil && vlanID > 0 {
×
1091
                        return vlanID
×
1092
                }
×
1093
        }
1094

1095
        return 0
×
1096
}
1097

1098
func (cont *AciController) getAaepDiff(crAaeps []string) (addedAaeps, removedAaeps []string) {
×
1099
        crAaepMap := make(map[string]bool)
×
1100
        for _, crAaep := range crAaeps {
×
1101
                crAaepMap[crAaep] = true
×
1102
        }
×
1103

1104
        cont.indexMutex.Lock()
×
1105
        for _, crAaep := range crAaeps {
×
1106
                if _, ok := cont.sharedAaepMonitor[crAaep]; !ok {
×
1107
                        addedAaeps = append(addedAaeps, crAaep)
×
1108
                }
×
1109
        }
1110
        cont.indexMutex.Unlock()
×
1111

×
1112
        cont.indexMutex.Lock()
×
1113
        for cachedAaep := range cont.sharedAaepMonitor {
×
1114
                if !crAaepMap[cachedAaep] {
×
1115
                        removedAaeps = append(removedAaeps, cachedAaep)
×
1116
                }
×
1117
        }
1118
        cont.indexMutex.Unlock()
×
1119

×
1120
        return
×
1121
}
1122

1123
func (cont *AciController) getEncapFromAaepEpgAttachObj(aaepName, epgDn string) string {
×
1124
        uri := fmt.Sprintf("/api/node/mo/uni/infra/attentp-%s/gen-default/rsfuncToEpg-[%s].json?query-target=self", aaepName, epgDn)
×
1125
        resp, err := cont.apicConn.GetApicResponse(uri)
×
1126
        if err != nil {
×
1127
                cont.log.Errorf("Failed to get response from APIC: AAEP %s and EPG %s ERROR: %v", aaepName, epgDn, err)
×
1128
                return ""
×
1129
        }
×
1130

1131
        for _, obj := range resp.Imdata {
×
1132
                lresp, ok := obj["infraRsFuncToEpg"]
×
1133
                if !ok {
×
1134
                        cont.log.Errorf("InfraRsFuncToEpg object not found in response for %s", uri)
×
1135
                        break
×
1136
                }
1137
                if val, ok := lresp.Attributes["encap"]; ok {
×
1138
                        encap := val.(string)
×
1139
                        return encap
×
1140
                } else {
×
1141
                        cont.log.Errorf("Encap missing for infraRsFuncToEpg object for %s: %v", uri, err)
×
1142
                        break
×
1143
                }
1144
        }
1145

1146
        return ""
×
1147
}
1148

1149
func (cont *AciController) isVmmLiteNAD(nadDetails *nadapi.NetworkAttachmentDefinition) bool {
×
1150
        return nadDetails.ObjectMeta.Annotations["managed-by"] == "cisco-network-operator"
×
1151
}
×
1152

1153
func (cont *AciController) isNADinUse(namespaceName string, nadName string) bool {
×
1154
        kubeClient := cont.env.(*K8sEnvironment).kubeClient
×
1155
        pods, err := kubeClient.CoreV1().Pods(namespaceName).List(context.TODO(), metav1.ListOptions{})
×
1156
        if err == nil {
×
1157
                var networks []map[string]string
×
1158
                for _, pod := range pods.Items {
×
1159
                        networksAnn, ok := pod.Annotations["k8s.v1.cni.cncf.io/networks"]
×
1160
                        if ok && (networksAnn == nadName) {
×
1161
                                cont.log.Infof("NAD %s is still used by Pod %s/%s", nadName, namespaceName, pod.Name)
×
1162
                                return true
×
1163
                        }
×
1164
                        if err := json.Unmarshal([]byte(networksAnn), &networks); err != nil {
×
1165
                                cont.log.Errorf("Error while getting pod annotations: %v", err)
×
1166
                                return false
×
1167
                        }
×
1168
                        for _, network := range networks {
×
1169
                                if ok && (network["name"] == nadName) {
×
1170
                                        cont.log.Infof("NAD %s is still used by VM %s/%s", nadName, namespaceName, pod.Name)
×
1171
                                        return true
×
1172
                                }
×
1173
                        }
1174
                }
1175
        }
1176
        return false
×
1177
}
1178

1179
func (cont *AciController) getNADDeleteMessage(deleteReason string) string {
×
1180
        messagePrefix := "NAD is in use by pods: "
×
1181
        switch {
×
NEW
1182
        case deleteReason == "EpgDeleted":
×
NEW
1183
                return messagePrefix + "EPG deleted"
×
1184
        case deleteReason == "NamespaceAnnotationRemoved":
×
NEW
1185
                return messagePrefix + "Namespace name EPG annotaion removed"
×
1186
        case deleteReason == "AaepEpgDetached":
×
1187
                return messagePrefix + "EPG detached from AAEP"
×
1188
        case deleteReason == "CRDeleted":
×
1189
                return messagePrefix + "aaepmonitor CR deleted"
×
1190
        case deleteReason == "AaepRemovedFromCR":
×
1191
                return messagePrefix + "AAEP removed from aaepmonitor CR"
×
1192
        case deleteReason == "AaepEpgAttachedWithVlanUsedInCluster":
×
1193
                return messagePrefix + "EPG with AAEP has overlapping VLAN with existing cluster VLAN"
×
1194
        case deleteReason == "AaepEpgAttachedWithOverlappingVlan":
×
1195
                return messagePrefix + "EPG with AAEP has overlapping VLAN with another EPG"
×
1196
        case deleteReason == "NamespaceDeleted":
×
1197
                return messagePrefix + "Namespace deleted"
×
1198
        case deleteReason == "AaepEpgAttachedWithVlanInUse":
×
1199
                return messagePrefix + "EPG with AAEP has VLAN already used in cluster"
×
1200
        }
1201
        return messagePrefix + "One or many pods are using NAD"
×
1202
}
1203

1204
func (cont *AciController) getNADRevampMessage(createReason string) string {
×
1205
        messagePrefix := "NAD is in sync: "
×
1206
        switch {
×
1207
        case createReason == "NamespaceAnnotationAdded":
×
1208
                return messagePrefix + "Namespace name EPG annotaion added"
×
1209
        case createReason == "AaepEpgAttached":
×
1210
                return messagePrefix + "EPG attached with AAEP"
×
1211
        case createReason == "AaepAddedInCR":
×
1212
                return messagePrefix + "AAEP added back in aaepmonitor CR"
×
1213
        case createReason == "NamespaceCreated":
×
1214
                return messagePrefix + "Namespace created back"
×
1215
        case createReason == "NextEpgNadCreation":
×
1216
                return messagePrefix + "NAD creation for next EPG with same VLAN"
×
1217
        }
1218
        return messagePrefix + "NAD synced with ACI"
×
1219
}
1220

1221
func (cont *AciController) isEpgAttachedWithAaep(epgDn string) bool {
×
1222
        cont.indexMutex.Lock()
×
1223
        defer cont.indexMutex.Unlock()
×
1224
        for aaepName := range cont.sharedAaepMonitor {
×
1225
                encap := cont.getEncapFromAaepEpgAttachObj(aaepName, epgDn)
×
1226
                if encap != "" {
×
1227
                        return true
×
1228
                }
×
1229
        }
1230
        return false
×
1231
}
1232

1233
func (cont *AciController) checkDuplicateAaepEpgAttachRequest(aaepName, epgDn string, vlanID int) bool {
×
1234
        cont.indexMutex.Lock()
×
1235
        defer cont.indexMutex.Unlock()
×
1236
        aaepEpgAttachDataMap, exists := cont.sharedAaepMonitor[aaepName]
×
1237
        if !exists || aaepEpgAttachDataMap == nil {
×
1238
                return false
×
1239
        }
×
1240

1241
        aaepEpgAttachData, exists := aaepEpgAttachDataMap[epgDn]
×
1242
        if !exists || aaepEpgAttachData == nil {
×
1243
                return false
×
1244
        }
×
1245

1246
        if vlanID == aaepEpgAttachData.encapVlan {
×
1247
                return true
×
1248
        }
×
1249
        return false
×
1250
}
1251

1252
func (cont *AciController) checkIfEpgWithOverlappingVlan(aaepName string, vlanId int, epgDn string) bool {
×
1253
        cont.indexMutex.Lock()
×
1254
        defer cont.indexMutex.Unlock()
×
1255
        oldAaepEpgAttachData := cont.getAaepEpgAttachDataLocked(aaepName, epgDn)
×
1256

×
1257
        if oldAaepEpgAttachData != nil {
×
1258
                // If old data exists with NAD created and VLAN is same in old and new data, skip checking for overlapping VLAN for the same EPG
×
1259
                if oldAaepEpgAttachData.encapVlan == vlanId && oldAaepEpgAttachData.nadCreated {
×
1260
                        return false
×
1261
                }
×
1262
        }
1263
        aaepEpgAttachDataMap, exists := cont.sharedAaepMonitor[aaepName]
×
1264
        if !exists || aaepEpgAttachDataMap == nil {
×
1265
                return false
×
1266
        }
×
1267

1268
        for _, aaepEpgData := range aaepEpgAttachDataMap {
×
1269
                if vlanId == aaepEpgData.encapVlan {
×
1270
                        return true
×
1271
                }
×
1272
        }
1273
        return false
×
1274
}
1275

1276
func (cont *AciController) createNadForNextEpg(aaepName string, vlanId int) {
×
1277
        cont.indexMutex.Lock()
×
1278
        defer cont.indexMutex.Unlock()
×
1279

×
1280
        aaepEpgAttachDataMap := cont.sharedAaepMonitor[aaepName]
×
1281
        for epgDn, aaepEpgAttachData := range aaepEpgAttachDataMap {
×
1282
                if aaepEpgAttachData.encapVlan != vlanId || aaepEpgAttachData.nadCreated {
×
1283
                        continue
×
1284
                } else if !cont.namespaceChecks(aaepEpgAttachData.namespaceName, epgDn) {
×
1285
                        cont.log.Debugf("Skipping NAD creation for next EPG %s with AAEP %s and VLAN %d: Namespace checks failed", epgDn, aaepName, vlanId)
×
1286
                        continue
×
1287
                } else {
×
1288
                        cont.log.Infof("Creating NAD for next EPG %s with AAEP %s and VLAN %d, for which previously NAD not created due to overlapping Vlan",
×
1289
                                epgDn, aaepName, vlanId)
×
1290
                        needCacheChange := cont.createNetworkAttachmentDefinition(aaepName, epgDn, aaepEpgAttachData, "NextEpgNadCreation")
×
1291
                        if needCacheChange {
×
1292
                                aaepEpgAttachData.nadCreated = true
×
1293
                                if cont.sharedAaepMonitor[aaepName] == nil {
×
1294
                                        cont.sharedAaepMonitor[aaepName] = make(map[string]*AaepEpgAttachData)
×
1295
                                }
×
1296
                                cont.sharedAaepMonitor[aaepName][epgDn] = aaepEpgAttachData
×
1297
                        }
1298
                        cont.log.Infof("Created NAD for next EPG %s with AAEP %s and VLAN %d", epgDn, aaepName, vlanId)
×
1299
                        break
×
1300
                }
1301
        }
1302
}
1303

1304
func (cont *AciController) checkVlanUsedInCluster(vlanId int) bool {
×
1305
        if vlanId == cont.config.KubeapiVlan {
×
1306
                return true
×
1307
        }
×
1308
        return false
×
1309
}
1310

1311
func (cont *AciController) checkMonitorDataModified(oldData, newData *AaepEpgAttachData) bool {
×
1312
        if oldData == nil {
×
1313
                return true
×
1314
        }
×
1315
        if oldData.namespaceName != newData.namespaceName ||
×
1316
                oldData.nadName != newData.nadName ||
×
1317
                oldData.encapVlan != newData.encapVlan {
×
1318
                return true
×
1319
        }
×
1320
        return false
×
1321
}
1322

1323
func (cont *AciController) handleOldNadDeletion(aaepName, epgDn string,
1324
        oldAaepMonitorData *AaepEpgAttachData, deleteReason string) {
×
1325
        if oldAaepMonitorData == nil {
×
1326
                return
×
1327
        }
×
1328
        cont.indexMutex.Lock()
×
1329
        delete(cont.sharedAaepMonitor[aaepName], epgDn)
×
1330
        if oldAaepMonitorData.nadCreated {
×
1331
                cont.log.Debugf("Deleting NAD for EPG %s with AAEP %s due to %s", epgDn, aaepName, deleteReason)
×
1332
                cont.deleteNetworkAttachmentDefinition(aaepName, epgDn, oldAaepMonitorData, deleteReason)
×
1333
        }
×
1334
        cont.indexMutex.Unlock()
×
1335
        if oldAaepMonitorData.nadCreated {
×
1336
                cont.createNadForNextEpg(aaepName, oldAaepMonitorData.encapVlan)
×
1337
        }
×
1338
}
1339

1340
func (cont *AciController) handleNewNadCreation(aaepName, epgDn string,
1341
        newAaepMonitorData *AaepEpgAttachData, createReason string) string {
×
1342
        cont.indexMutex.Lock()
×
1343
        defer cont.indexMutex.Unlock()
×
1344
        // If there are multiple EPGs associated with same vlan for an aaep,
×
1345
        // then only one NAD will be there.
×
1346
        // After controller restart, if any other EPG notification comes before the already present NAD
×
1347
        // it will result in 2 NADs.
×
1348
        // Below check is to avoid the condition
×
1349
        NADAlreadyInCluster, oldEpgDn := cont.isNADWithSameVlanAlreadyPresent(aaepName, epgDn, newAaepMonitorData)
×
1350
        if NADAlreadyInCluster != nil {
×
1351
                cont.log.Errorf("Cannot create NAD for EPG %s as there is a NAD already present %v with EPG %s attached to AAEP %s", epgDn, NADAlreadyInCluster, oldEpgDn, aaepName)
×
1352
                if cont.sharedAaepMonitor[aaepName] == nil {
×
1353
                        cont.sharedAaepMonitor[aaepName] = make(map[string]*AaepEpgAttachData)
×
1354
                }
×
1355
                cont.sharedAaepMonitor[aaepName][oldEpgDn] = NADAlreadyInCluster
×
1356
                newAaepMonitorData.nadCreated = false
×
1357
                cont.sharedAaepMonitor[aaepName][epgDn] = newAaepMonitorData
×
1358
                return oldEpgDn
×
1359
        }
1360
        needCacheChange := cont.createNetworkAttachmentDefinition(aaepName, epgDn, newAaepMonitorData, createReason)
×
1361
        if needCacheChange {
×
1362
                if cont.sharedAaepMonitor[aaepName] == nil {
×
1363
                        cont.sharedAaepMonitor[aaepName] = make(map[string]*AaepEpgAttachData)
×
1364
                }
×
1365
                cont.sharedAaepMonitor[aaepName][epgDn] = newAaepMonitorData
×
1366
        }
1367
        return ""
×
1368
}
1369

1370
func (cont *AciController) checkAnnotationModified(oldAaepEpgAttachData, newAaepEpgAttachData *AaepEpgAttachData) bool {
×
1371
        if oldAaepEpgAttachData.nadName != newAaepEpgAttachData.nadName || oldAaepEpgAttachData.namespaceName != newAaepEpgAttachData.namespaceName {
×
1372
                return true
×
1373
        }
×
1374
        return false
×
1375
}
1376

1377
func (cont *AciController) isNADWithSameVlanAlreadyPresent(aaepName string, epgDn string, newAaepMonitorData *AaepEpgAttachData) (*AaepEpgAttachData, string) {
×
1378
        allNADs, err := cont.getAllNADs()
×
1379
        if err != nil {
×
1380
                cont.log.Errorf("Failed to get all NADs: %v", err)
×
1381
                return nil, ""
×
1382
        }
×
1383

1384
        for _, nad := range allNADs {
×
1385
                ns := nad.Namespace
×
1386
                annotations := nad.ObjectMeta.Annotations
×
1387
                if annotations == nil {
×
1388
                        continue
×
1389
                }
1390
                // Check for required annotations
1391
                if annotations["managed-by"] != "cisco-network-operator" ||
×
1392
                        annotations["aci-sync-status"] != "in-sync" ||
×
1393
                        annotations["aaep-name"] != aaepName {
×
1394
                        continue
×
1395
                }
1396

1397
                existingEpgDn := annotations["epg-dn"]
×
1398
                // Skip if same EPG DN
×
1399
                if existingEpgDn == epgDn {
×
1400
                        cont.log.Infof("Found existing NAD %s/%s for epg %s ", ns, nad.Name, epgDn)
×
1401
                        return nil, ""
×
1402
                }
×
1403
                // Compare VLAN
1404
                existingVlan, err := strconv.Atoi(annotations["vlan"])
×
1405
                if err != nil {
×
1406
                        cont.log.Warnf("Invalid VLAN in NAD %s/%s: %v", ns, nad.Name, err)
×
1407
                        continue
×
1408
                }
1409

1410
                if existingVlan == newAaepMonitorData.encapVlan {
×
1411
                        cont.log.Infof("Found existing NAD %s/%s with same VLAN %d for aaep %s", ns, nad.Name, existingVlan, aaepName)
×
1412

×
1413
                        return &AaepEpgAttachData{
×
1414
                                nadName:       annotations["cno-name"],
×
1415
                                namespaceName: ns,
×
1416
                                encapVlan:     existingVlan,
×
1417
                                nadCreated:    true,
×
1418
                        }, existingEpgDn
×
1419
                }
×
1420
        }
1421

1422
        return nil, ""
×
1423
}
1424

1425
func (cont *AciController) getAllNADs() ([]nadapi.NetworkAttachmentDefinition, error) {
×
1426
        nadClient := cont.env.(*K8sEnvironment).nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions("")
×
1427

×
1428
        nadList, err := nadClient.List(context.TODO(), metav1.ListOptions{})
×
1429
        if err != nil {
×
1430
                return nil, fmt.Errorf("failed to list all NADs: %v", err)
×
1431
        }
×
1432

1433
        return nadList.Items, nil
×
1434
}
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