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

noironetworks / aci-containers / 11439

19 Nov 2025 12:39PM UTC coverage: 63.133% (-0.08%) from 63.209%
11439

push

travis-pro

web-flow
Merge pull request #1643 from noironetworks/fixed_namespace_or_vlan_change_during_controller_restart

Fixed issue when namespace and vlan changed during controller restart

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

4 existing lines in 1 file now uncovered.

13376 of 21187 relevant lines covered (63.13%)

0.72 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.indexMutex.Lock()
×
157
        allEpgToSubscribe := make(map[string]bool)
×
158
        cont.log.Infof("Started processing of aaepmonitor CR creation/modification")
×
159
        addedAaeps, removedAaeps := cont.getAaepDiff(aaepMonitorConfig.Spec.Aaeps)
×
160
        for _, aaepName := range addedAaeps {
×
161
                epgsToSubscribe := cont.reconcileNadData(aaepName)
×
162
                for epgDn := range epgsToSubscribe {
×
163
                        allEpgToSubscribe[epgDn] = true
×
164
                }
×
165
        }
166

167
        for _, aaepName := range removedAaeps {
×
168
                cont.cleanAnnotationSubscriptions(aaepName)
×
169

×
170
                aaepEpgAttachDataMap := cont.sharedAaepMonitor[aaepName]
×
171
                delete(cont.sharedAaepMonitor, aaepName)
×
172

×
173
                for epgDn, aaepEpgAttachData := range aaepEpgAttachDataMap {
×
174
                        cont.deleteNetworkAttachmentDefinition(aaepName, epgDn, aaepEpgAttachData, "AaepRemovedFromCR")
×
175
                }
×
176
        }
177
        cont.indexMutex.Unlock()
×
178
        for epgDn := range allEpgToSubscribe {
×
179
                cont.subscribeSafely(epgDn)
×
180
        }
×
181
        cont.log.Infof("Completed processing of aaepmonitor CR creation/modification")
×
182
        return false
×
183
}
184

185
func (cont *AciController) handleAaepMonitorConfigurationDelete(key string) bool {
×
186
        cont.indexMutex.Lock()
×
187
        defer cont.indexMutex.Unlock()
×
188
        cont.log.Infof("Started processing of aaepmonitor CR deletion")
×
189
        for aaepName, aaepEpgAttachDataMap := range cont.sharedAaepMonitor {
×
190
                cont.cleanAnnotationSubscriptions(aaepName)
×
191

×
192
                for epgDn, aaepEpgAttachData := range aaepEpgAttachDataMap {
×
193
                        cont.deleteNetworkAttachmentDefinition(aaepName, epgDn, aaepEpgAttachData, "CRDeleted")
×
194
                }
×
195
        }
196

197
        cont.sharedAaepMonitor = make(map[string]map[string]*AaepEpgAttachData)
×
198
        cont.log.Infof("Completed processing of aaepmonitor CR deletion")
×
199
        return false
×
200
}
201

202
func (cont *AciController) handleAaepEpgAttach(infraRsObj apicapi.ApicObject) {
×
203
        infraRsObjDn := infraRsObj.GetDn()
×
204
        aaepName := cont.matchesAEPFilter(infraRsObjDn)
×
205
        cont.log.Infof("Started processing of EPG: %s attached with AAEP: %s", infraRsObj.GetAttrStr("tDn"), aaepName)
×
206
        if aaepName == "" {
×
207
                cont.log.Debugf("Unable to find AAEP from %s in monitoring list", infraRsObjDn)
×
208
                return
×
209
        }
×
210

211
        state := infraRsObj.GetAttrStr("state")
×
212
        if state != "formed" {
×
213
                cont.indexMutex.Lock()
×
214
                cont.log.Debugf("Skipping NAD creation: %s is with state: %s", infraRsObjDn, state)
×
215
                cont.cleanStaleNADLocked(aaepName, infraRsObj.GetAttrStr("tDn"))
×
216
                cont.indexMutex.Unlock()
×
217
                return
×
218
        }
×
219

220
        epgDn := infraRsObj.GetAttrStr("tDn")
×
221
        encap := infraRsObj.GetAttrStr("encap")
×
222
        vlanID := cont.getVlanId(encap)
×
223

×
NEW
224
        // Clean stale NADs for namespace change
×
NEW
225
        // Handled controller restart and namespace changed at same time
×
NEW
226
        cont.indexMutex.Lock()
×
NEW
227
        cont.cleanStaleNADForNamespaceOrVlanChangeLocked(aaepName, epgDn, vlanID)
×
NEW
228
        cont.indexMutex.Unlock()
×
NEW
229

×
230
        if cont.checkDuplicateAaepEpgAttachRequest(aaepName, epgDn, vlanID) {
×
231
                cont.log.Infof("AAEP %s EPG %s attachment data already exists", aaepName, epgDn)
×
232
                return
×
233
        }
×
234

235
        cont.indexMutex.Lock()
×
236
        oldAaepMonitorData := cont.getAaepEpgAttachDataLocked(aaepName, epgDn)
×
237
        cont.indexMutex.Unlock()
×
238

×
239
        if cont.checkVlanUsedInCluster(vlanID) {
×
240
                // This is needed when user updates vlan to the vlan already used in cluster
×
241
                cont.handleOldNadDeletion(aaepName, epgDn, oldAaepMonitorData, "AaepEpgAttachedWithVlanInUse")
×
242
                cont.log.Errorf("Skipping NAD creation: VLAN %d is already used in cluster", vlanID)
×
243
                return
×
244
        }
×
245

246
        defer cont.subscribeSafely(epgDn)
×
247
        epgVlanMap := &EpgVlanMap{
×
248
                epgDn:     epgDn,
×
249
                encapVlan: vlanID,
×
250
        }
×
251

×
252
        aaepEpgAttachData := cont.collectNadData(epgVlanMap)
×
253
        if aaepEpgAttachData == nil {
×
254
                return
×
255
        }
×
256

257
        if cont.checkIfEpgWithOverlappingVlan(aaepName, vlanID, epgDn, false) {
×
258
                // This is needed when user updates vlan from non-overlapping to overlapping
×
259
                cont.handleOldNadDeletion(aaepName, epgDn, oldAaepMonitorData, "AaepEpgAttachedWithOverlappingVlan")
×
260

×
261
                // Add new entry with nadCreated as false
×
262
                cont.indexMutex.Lock()
×
263
                if cont.sharedAaepMonitor[aaepName] == nil {
×
264
                        cont.sharedAaepMonitor[aaepName] = make(map[string]*AaepEpgAttachData)
×
265
                }
×
266
                aaepEpgAttachData.nadCreated = false
×
267
                cont.sharedAaepMonitor[aaepName][epgDn] = aaepEpgAttachData
×
268
                cont.indexMutex.Unlock()
×
269
                cont.log.Errorf("Skipping NAD creation: EPG %s with AAEP %s has overlapping VLAN %d", epgDn, aaepName, vlanID)
×
270
                return
×
271
        }
272
        cont.syncNADsWithAciState(aaepName, epgDn, oldAaepMonitorData, aaepEpgAttachData, "AaepEpgAttached")
×
273
        cont.log.Infof("Completed processing of EPG: %s attached with AAEP: %s", epgDn, aaepName)
×
274
}
275

276
func (cont *AciController) handleAaepEpgDetach(infraRsObjDn string) {
×
277
        aaepName := cont.matchesAEPFilter(infraRsObjDn)
×
278
        if aaepName == "" {
×
279
                cont.log.Debugf("Unable to find AAEP from %s in monitoring list", infraRsObjDn)
×
280
                return
×
281
        }
×
282

283
        epgDn := cont.getEpgDnFromInfraRsDn(infraRsObjDn)
×
284

×
285
        if epgDn == "" {
×
286
                cont.log.Errorf("Unable to find EPG from %s", infraRsObjDn)
×
287
                return
×
288
        }
×
289

290
        cont.log.Infof("Started processing of EPG: %s detached from AAEP: %s", infraRsObjDn, aaepName)
×
291
        // Need to check if EPG is not attached with any other AAEP
×
292
        if !cont.isEpgAttachedWithAaep("", epgDn, false) {
×
293
                cont.apicConn.UnsubscribeImmediateDnLocked(epgDn, []string{"tagAnnotation"})
×
294
        }
×
295

296
        cont.indexMutex.Lock()
×
297
        aaepEpgAttachData := cont.getAaepEpgAttachDataLocked(aaepName, epgDn)
×
298
        cont.indexMutex.Unlock()
×
299

×
300
        if aaepEpgAttachData == nil {
×
301
                cont.log.Debugf("Monitoring data not available for EPG %s with AAEP %s", epgDn, aaepName)
×
302
                return
×
303
        }
×
304
        if !cont.namespaceChecks(aaepEpgAttachData.namespaceName, epgDn) {
×
305
                cont.log.Debugf("Namespace %s not found", aaepEpgAttachData.namespaceName)
×
306
                return
×
307
        }
×
308

309
        cont.handleOldNadDeletion(aaepName, epgDn, aaepEpgAttachData, "AaepEpgDetached")
×
310
        cont.log.Infof("Completed processing of EPG: %s detached from AAEP: %s", epgDn, aaepName)
×
311
}
312

313
func (cont *AciController) handleAnnotationAdded(obj apicapi.ApicObject) bool {
×
314
        annotationDn := obj.GetDn()
×
315
        epgDn := annotationDn[:strings.Index(annotationDn, "/annotationKey-")]
×
316
        aaepMonitorDataMap := cont.getAaepMonitoringDataForEpg(epgDn)
×
317

×
318
        cont.log.Infof("Started processing of EPG: %s annotation %s addition/modification", epgDn, annotationDn)
×
319
        for aaepName, aaepMonitorData := range aaepMonitorDataMap {
×
320
                cont.indexMutex.Lock()
×
321
                oldAaepMonitorData := cont.getAaepEpgAttachDataLocked(aaepName, epgDn)
×
322
                cont.indexMutex.Unlock()
×
323

×
324
                if len(aaepMonitorData) == 0 {
×
325
                        // No new monitoring data available for this EPG with this AAEP but old data exists
×
326
                        // delete old NAD if exists and remove from monitoring map
×
327
                        // create NAD for next EPG with old VLAN
×
328
                        cont.handleOldNadDeletion(aaepName, epgDn, oldAaepMonitorData, "NamespaceAnnotationAdded")
×
329
                        cont.log.Debugf("Insufficient data for NAD creation: Monitoring data not available for EPG %s with AAEP %s", epgDn, aaepName)
×
330
                        continue
×
331
                }
332

333
                aaepEpgAttachData, exists := aaepMonitorData[epgDn]
×
334
                if !exists || aaepEpgAttachData == nil {
×
335
                        cont.handleOldNadDeletion(aaepName, epgDn, oldAaepMonitorData, "NamespaceAnnotationAdded")
×
336
                        cont.log.Debugf("Insufficient data for NAD creation: Monitoring data not available for EPG %s with AAEP %s", epgDn, aaepName)
×
337
                        continue
×
338
                }
339

340
                if !cont.checkMonitorDataModified(oldAaepMonitorData, aaepEpgAttachData) {
×
341
                        cont.log.Debugf("No changes in annotation for EPG %s with AAEP %s", epgDn, aaepName)
×
342
                        continue
×
343
                }
344

345
                if !cont.namespaceChecks(aaepEpgAttachData.namespaceName, epgDn) {
×
346
                        // Namespace not exist, need to delete old NAD if exists and remove from monitoring map
×
347
                        // create NAD for next EPG with old VLAN
×
348
                        cont.handleOldNadDeletion(aaepName, epgDn, oldAaepMonitorData, "NamespaceAnnotationAdded")
×
349
                        cont.log.Debugf("Insufficient data in annotation for NAD creation: Namespace not exist, in case of EPG %s with AAEP %s", epgDn, aaepName)
×
350
                        continue
×
351
                }
352

353
                // This is needed when user updates annotation and VLAN is already overlapping
354
                if cont.checkIfEpgWithOverlappingVlan(aaepName, aaepEpgAttachData.encapVlan, epgDn, false) {
×
355
                        // Add new entry with nadCreated as false
×
356
                        aaepEpgAttachData.nadCreated = false
×
357
                        cont.indexMutex.Lock()
×
358
                        if cont.sharedAaepMonitor[aaepName] == nil {
×
359
                                cont.sharedAaepMonitor[aaepName] = make(map[string]*AaepEpgAttachData)
×
360
                        }
×
361
                        cont.sharedAaepMonitor[aaepName][epgDn] = aaepEpgAttachData
×
362
                        cont.indexMutex.Unlock()
×
363
                        cont.log.Debugf("Skipping EPG annotation add handling: EPG %s with AAEP %s has overlapping VLAN %d", epgDn,
×
364
                                aaepName, aaepEpgAttachData.encapVlan)
×
365
                        continue
×
366
                }
367

368
                cont.syncNADsWithAciState(aaepName, epgDn, oldAaepMonitorData, aaepEpgAttachData, "NamespaceAnnotationAdded")
×
369
        }
370
        cont.log.Infof("Completed processing annotation addition/modification for EPG: %s, AnnotationDn: %s", epgDn, annotationDn)
×
371
        return true
×
372
}
373

374
func (cont *AciController) handleAnnotationDeleted(annotationDn string) {
×
375
        epgDn := annotationDn[:strings.Index(annotationDn, "/annotationKey-")]
×
376
        cont.log.Infof("Started processing of EPG: %s annotation %s deletion", epgDn, annotationDn)
×
377
        aaepMonitorDataMap := cont.getAaepMonitoringDataForEpg(epgDn)
×
378

×
379
        var reason string
×
380
        if !cont.checkIfEPGExists(epgDn) {
×
381
                reason = "EpgDeleted"
×
382
        } else {
×
383
                reason = "NamespaceAnnotationRemoved"
×
384
        }
×
385

386
        for aaepName, aaepMonitorData := range aaepMonitorDataMap {
×
387
                cont.indexMutex.Lock()
×
388
                oldAaepMonitorData := cont.getAaepEpgAttachDataLocked(aaepName, epgDn)
×
389
                cont.indexMutex.Unlock()
×
390

×
391
                if oldAaepMonitorData == nil {
×
392
                        cont.log.Debugf("Monitoring data not available for EPG %s with AAEP %s", epgDn, aaepName)
×
393
                        continue
×
394
                }
395

396
                if oldAaepMonitorData.nadCreated == false {
×
397
                        cont.indexMutex.Lock()
×
398
                        delete(cont.sharedAaepMonitor[aaepName], epgDn)
×
399
                        cont.indexMutex.Unlock()
×
400
                        continue
×
401
                }
402

403
                if aaepMonitorData == nil {
×
404
                        cont.indexMutex.Lock()
×
405
                        delete(cont.sharedAaepMonitor[aaepName], epgDn)
×
406
                        cont.deleteNetworkAttachmentDefinition(aaepName, epgDn, oldAaepMonitorData, reason)
×
407
                        cont.indexMutex.Unlock()
×
408
                        cont.createNadForNextEpg(aaepName, oldAaepMonitorData.encapVlan)
×
409
                        continue
×
410
                }
411

412
                aaepEpgAttachData, exists := aaepMonitorData[epgDn]
×
413
                if !exists || aaepEpgAttachData == nil {
×
414
                        cont.indexMutex.Lock()
×
415
                        delete(cont.sharedAaepMonitor[aaepName], epgDn)
×
416
                        cont.deleteNetworkAttachmentDefinition(aaepName, epgDn, oldAaepMonitorData, reason)
×
417
                        cont.indexMutex.Unlock()
×
418
                        cont.createNadForNextEpg(aaepName, oldAaepMonitorData.encapVlan)
×
419
                        continue
×
420
                }
421

422
                cont.syncNADsWithAciState(aaepName, epgDn, oldAaepMonitorData, aaepEpgAttachData, reason)
×
423
        }
424
        cont.log.Infof("Completed processing annotation deletion for EPG: %s, AnnotationDn: %s", epgDn, annotationDn)
×
425
}
426

427
func (cont *AciController) collectNadData(epgVlanMap *EpgVlanMap) *AaepEpgAttachData {
×
428
        epgDn := epgVlanMap.epgDn
×
429
        epgAnnotations := cont.getEpgAnnotations(epgDn)
×
430
        if epgAnnotations == nil {
×
431
                cont.log.Debugf("No annotations found for EPG %s", epgDn)
×
432
                return nil
×
433
        }
×
434
        namespaceName, nadName := cont.getSpecificEPGAnnotation(epgAnnotations)
×
435

×
436
        if !cont.namespaceChecks(namespaceName, epgDn) {
×
437
                cont.log.Debugf("Namespace not exist, in case of EPG %s", epgDn)
×
438
                return nil
×
439
        }
×
440

441
        aaepMonitoringData := &AaepEpgAttachData{
×
442
                encapVlan:     epgVlanMap.encapVlan,
×
443
                nadName:       nadName,
×
444
                namespaceName: namespaceName,
×
445
                nadCreated:    true,
×
446
        }
×
447

×
448
        return aaepMonitoringData
×
449
}
450

451
func (cont *AciController) getAaepEpgAttachDataLocked(aaepName, epgDn string) *AaepEpgAttachData {
×
452
        aaepEpgAttachDataMap, exists := cont.sharedAaepMonitor[aaepName]
×
453
        if !exists || aaepEpgAttachDataMap == nil {
×
454
                cont.log.Debugf("AAEP %s EPG %s attachment data not found", aaepName, epgDn)
×
455
                return nil
×
456
        }
×
457

458
        aaepEpgAttachData, exists := aaepEpgAttachDataMap[epgDn]
×
459
        if !exists || aaepEpgAttachData == nil {
×
460
                cont.log.Debugf("AAEP %s EPG %s attachment data not found", aaepName, epgDn)
×
461
                return nil
×
462
        }
×
463

464
        cont.log.Infof("Found attachment data: %v for EPG : %s AAEP: %s", *aaepEpgAttachData, epgDn, aaepName)
×
465
        return aaepEpgAttachData
×
466
}
467

468
func (cont *AciController) matchesAEPFilter(infraRsObjDn string) string {
×
469
        cont.indexMutex.Lock()
×
470
        defer cont.indexMutex.Unlock()
×
471
        var aaepName string
×
472
        for aaepName = range cont.sharedAaepMonitor {
×
473
                expectedPrefix := fmt.Sprintf("uni/infra/attentp-%s/", aaepName)
×
474
                if strings.HasPrefix(infraRsObjDn, expectedPrefix) {
×
475
                        return aaepName
×
476
                }
×
477
        }
478
        return ""
×
479
}
480

481
func (cont *AciController) getEpgDnFromInfraRsDn(infraRsObjDn string) string {
×
482
        re := regexp.MustCompile(`\[(.*?)\]`)
×
483
        match := re.FindStringSubmatch(infraRsObjDn)
×
484

×
485
        var epgDn string
×
486
        if len(match) > 1 {
×
487
                epgDn = match[1]
×
488
                return epgDn
×
489
        }
×
490

491
        return epgDn
×
492
}
493

494
func (cont *AciController) getAaepMonitoringDataForEpg(epgDn string) map[string]map[string]*AaepEpgAttachData {
×
495
        aaepEpgAttachData := make(map[string]map[string]*AaepEpgAttachData)
×
496

×
497
        cont.indexMutex.Lock()
×
498
        defer cont.indexMutex.Unlock()
×
499
        for aaepName := range cont.sharedAaepMonitor {
×
500
                encap := ""
×
501
                aaepEpgAttachObj := cont.getEncapFromAaepEpgAttachObj(aaepName, epgDn)
×
502
                if aaepEpgAttachObj == nil {
×
503
                        cont.log.Debugf("infraRsFuncToEpg not available for EPG %s attached with AAEP %s", epgDn, aaepName)
×
504
                        continue
×
505
                }
506
                if val, ok := aaepEpgAttachObj.Attributes["encap"]; ok {
×
507
                        encap = val.(string)
×
508
                } else {
×
509
                        cont.log.Debugf("Encap missing for infraRsFuncToEpg object of AAEP: %s and EPG: %s", aaepName, epgDn)
×
510
                }
×
511

512
                if encap != "" {
×
513
                        vlanID := cont.getVlanId(encap)
×
514
                        epgVlanMap := EpgVlanMap{
×
515
                                epgDn:     epgDn,
×
516
                                encapVlan: vlanID,
×
517
                        }
×
518
                        if aaepEpgAttachData[aaepName] == nil {
×
519
                                aaepEpgAttachData[aaepName] = make(map[string]*AaepEpgAttachData)
×
520
                        }
×
521
                        nadData := cont.collectNadData(&epgVlanMap)
×
522
                        if nadData != nil {
×
523
                                aaepEpgAttachData[aaepName][epgDn] = nadData
×
524
                        }
×
525
                }
526
        }
527

528
        return aaepEpgAttachData
×
529
}
530

531
func (cont *AciController) cleanAnnotationSubscriptions(aaepName string) {
×
532
        epgVlanMapList := cont.getAaepEpgAttObjDetails(aaepName)
×
533
        if epgVlanMapList == nil {
×
534
                return
×
535
        }
×
536

537
        for _, epgVlanMap := range epgVlanMapList {
×
538
                cont.apicConn.UnsubscribeImmediateDnLocked(epgVlanMap.epgDn, []string{"tagAnnotation"})
×
539
        }
×
540
}
541

542
func (cont *AciController) syncNADsWithAciState(aaepName string, epgDn string, oldAaepEpgAttachData,
543
        aaepEpgAttachData *AaepEpgAttachData, syncReason string) {
×
544
        if oldAaepEpgAttachData != nil {
×
545
                if cont.checkAnnotationModified(oldAaepEpgAttachData, aaepEpgAttachData) {
×
546
                        if oldAaepEpgAttachData.namespaceName != aaepEpgAttachData.namespaceName {
×
547
                                cont.indexMutex.Lock()
×
548
                                if oldAaepEpgAttachData.nadCreated == true {
×
549
                                        cont.deleteNetworkAttachmentDefinition(aaepName, epgDn, oldAaepEpgAttachData, syncReason)
×
550
                                }
×
551
                                delete(cont.sharedAaepMonitor[aaepName], epgDn)
×
552
                                cont.indexMutex.Unlock()
×
553
                        }
554
                } else if oldAaepEpgAttachData.encapVlan != aaepEpgAttachData.encapVlan {
×
555
                        cont.handleOldNadDeletion(aaepName, epgDn, oldAaepEpgAttachData, syncReason)
×
556
                } else {
×
557
                        NADAlreadyInCluster, _ := cont.isNADWithSameVlanAlreadyPresent(aaepName, epgDn, aaepEpgAttachData)
×
558
                        if NADAlreadyInCluster == nil {
×
559
                                cont.handleNewNadCreation(aaepName, epgDn, aaepEpgAttachData, syncReason, false)
×
560
                        }
×
561
                        return
×
562
                }
563
        }
564
        cont.handleNewNadCreation(aaepName, epgDn, aaepEpgAttachData, syncReason, false)
×
565
}
566

567
func (cont *AciController) addDeferredNADs(namespaceName string) {
×
568
        aaepEpgVlanMap := make(map[string][]EpgVlanMap)
×
569

×
570
        // Collect all AAEP EPG attachment details
×
571
        cont.indexMutex.Lock()
×
572
        for aaepName := range cont.sharedAaepMonitor {
×
573
                epgVlanMapList := cont.getAaepEpgAttObjDetails(aaepName)
×
574

×
575
                if epgVlanMapList == nil {
×
576
                        continue
×
577
                }
578

579
                aaepEpgVlanMap[aaepName] = epgVlanMapList
×
580
        }
581
        cont.indexMutex.Unlock()
×
582

×
583
        // Process each AAEP EPG attachment details
×
584
        for aaepName, epgVlanMapList := range aaepEpgVlanMap {
×
585
                for _, epgVlanMap := range epgVlanMapList {
×
586
                        aaepEpgAttachData := cont.collectNadData(&epgVlanMap)
×
587
                        if aaepEpgAttachData == nil || aaepEpgAttachData.namespaceName != namespaceName {
×
588
                                continue
×
589
                        }
590

591
                        if cont.checkIfEpgWithOverlappingVlan(aaepName, aaepEpgAttachData.encapVlan, epgVlanMap.epgDn, false) {
×
592
                                cont.indexMutex.Lock()
×
593
                                if cont.sharedAaepMonitor[aaepName] == nil {
×
594
                                        cont.sharedAaepMonitor[aaepName] = make(map[string]*AaepEpgAttachData)
×
595
                                }
×
596
                                aaepEpgAttachData.nadCreated = false
×
597
                                cont.sharedAaepMonitor[aaepName][epgVlanMap.epgDn] = aaepEpgAttachData
×
598
                                cont.indexMutex.Unlock()
×
599

×
600
                                cont.log.Errorf("Skipping NAD creation: EPG %s with AAEP %s has overlapping VLAN %d", epgVlanMap.epgDn,
×
601
                                        aaepName, aaepEpgAttachData.encapVlan)
×
602
                                continue
×
603
                        } else if cont.checkVlanUsedInCluster(aaepEpgAttachData.encapVlan) {
×
604
                                cont.log.Errorf("Skipping NAD creation: VLAN %d is already used in cluster", aaepEpgAttachData.encapVlan)
×
605
                                continue
×
606
                        } else {
×
607
                                epgDn := epgVlanMap.epgDn
×
608
                                cont.handleNewNadCreation(aaepName, epgDn, aaepEpgAttachData, "NamespaceCreated", false)
×
609
                        }
×
610
                }
611
        }
612
}
613

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

×
617
        resp, err := cont.apicConn.GetApicResponse(uri)
×
618
        if err != nil {
×
619
                cont.log.Errorf("Failed to get response from APIC: %v", err)
×
620
                return nil
×
621
        }
×
622

623
        if len(resp.Imdata) == 0 {
×
624
                cont.log.Debugf("Can't find EPGs attached with AAEP %s", aaepName)
×
625
                return nil
×
626
        }
×
627

628
        epgVlanMapList := make([]EpgVlanMap, 0)
×
629
        for _, respImdata := range resp.Imdata {
×
630
                aaepEpgAttachObj, ok := respImdata["infraRsFuncToEpg"]
×
631
                if !ok {
×
632
                        cont.log.Debugf("Empty AAEP EPG attachment object")
×
633
                        continue
×
634
                }
635

636
                if state, hasState := aaepEpgAttachObj.Attributes["state"].(string); hasState {
×
637
                        if state != "formed" {
×
638
                                aaepEpgAttchDn := aaepEpgAttachObj.Attributes["dn"].(string)
×
639
                                cont.log.Debugf("%s is with state: %s", aaepEpgAttchDn, state)
×
640
                                cont.cleanStaleNADLocked(aaepName, aaepEpgAttachObj.Attributes["tDn"].(string))
×
641
                                continue
×
642
                        }
643
                }
644
                vlanID := 0
×
645
                if encap, hasEncap := aaepEpgAttachObj.Attributes["encap"].(string); hasEncap {
×
646
                        vlanID = cont.getVlanId(encap)
×
647
                }
×
648

649
                epgVlanMap := EpgVlanMap{
×
650
                        epgDn:     aaepEpgAttachObj.Attributes["tDn"].(string),
×
651
                        encapVlan: vlanID,
×
652
                }
×
653
                epgVlanMapList = append(epgVlanMapList, epgVlanMap)
×
654
        }
655

656
        return epgVlanMapList
×
657
}
658

659
func (cont *AciController) getEpgAnnotations(epgDn string) map[string]string {
×
660
        uri := fmt.Sprintf("/api/node/mo/%s.json?query-target=subtree&target-subtree-class=tagAnnotation", epgDn)
×
661
        resp, err := cont.apicConn.GetApicResponse(uri)
×
662
        if err != nil {
×
663
                cont.log.Errorf("Failed to get response from APIC: %v", err)
×
664
                return nil
×
665
        }
×
666

667
        annotationsMap := make(map[string]string)
×
668
        for _, respImdata := range resp.Imdata {
×
669
                annotationObj, ok := respImdata["tagAnnotation"]
×
670
                if !ok {
×
671
                        cont.log.Debugf("Empty tag annotation of EPG %s", epgDn)
×
672
                        continue
×
673
                }
674

675
                key := annotationObj.Attributes["key"].(string)
×
676
                annotationsMap[key] = annotationObj.Attributes["value"].(string)
×
677
        }
678

679
        return annotationsMap
×
680
}
681

682
func (cont *AciController) checkIfEPGExists(epgDn string) bool {
×
683
        uri := fmt.Sprintf("/api/node/mo/%s.json", epgDn)
×
684
        resp, err := cont.apicConn.GetApicResponse(uri)
×
685
        if err != nil {
×
686
                cont.log.Errorf("Failed to get response from APIC: %v", err)
×
687
                return false
×
688
        }
×
689
        if len(resp.Imdata) == 0 {
×
690
                cont.log.Debugf("EPG %s does not exist", epgDn)
×
691
                return false
×
692
        }
×
693
        return true
×
694
}
695

696
func (cont *AciController) getSpecificEPGAnnotation(annotations map[string]string) (string, string) {
×
697
        namespaceNameAnnotationKey := cont.config.CnoIdentifier + "-namespace"
×
698
        namespaceName, exists := annotations[namespaceNameAnnotationKey]
×
699
        if !exists {
×
700
                cont.log.Debugf("Annotation with key '%s' not found", namespaceNameAnnotationKey)
×
701
        }
×
702

703
        nadNameAnnotationKey := cont.config.CnoIdentifier + "-nad"
×
704
        nadName, exists := annotations[nadNameAnnotationKey]
×
705
        if !exists {
×
706
                cont.log.Debugf("Annotation with key '%s' not found", nadNameAnnotationKey)
×
707
        }
×
708
        return namespaceName, nadName
×
709
}
710

711
func (cont *AciController) namespaceChecks(namespaceName string, epgDn string) bool {
×
712
        if namespaceName == "" {
×
713
                cont.log.Debugf("Defering NAD operation for EPG %s: Namespace name not provided in EPG annotation", epgDn)
×
714
                return false
×
715
        }
×
716

717
        kubeClient := cont.env.(*K8sEnvironment).kubeClient
×
718
        _, err := kubeClient.CoreV1().Namespaces().Get(context.TODO(), namespaceName, metav1.GetOptions{})
×
719
        namespaceExists := err == nil
×
720
        if !namespaceExists {
×
721
                cont.log.Debugf("Defering NAD operation for EPG %s: Namespace %s not exists", epgDn, namespaceName)
×
722
                return false
×
723
        }
×
724

725
        return true
×
726
}
727

728
func (cont *AciController) reconcileNadData(aaepName string) map[string]bool {
×
729
        epgsToSubscribe := make(map[string]bool)
×
730
        epgVlanMapList := cont.getAaepEpgAttObjDetails(aaepName)
×
731

×
732
        for _, epgVlanMap := range epgVlanMapList {
×
733
                if cont.checkVlanUsedInCluster(epgVlanMap.encapVlan) {
×
734
                        cont.log.Errorf("Skipping NAD creation: VLAN %d is already used in cluster", epgVlanMap.encapVlan)
×
735
                        continue
×
736
                }
737
                aaepEpgAttachData := cont.collectNadData(&epgVlanMap)
×
738
                if aaepEpgAttachData == nil {
×
739
                        epgsToSubscribe[epgVlanMap.epgDn] = true
×
740
                        continue
×
741
                }
742

743
                epgDn := epgVlanMap.epgDn
×
744
                if cont.checkIfEpgWithOverlappingVlan(aaepName, aaepEpgAttachData.encapVlan, epgDn, true) {
×
745
                        aaepEpgAttachData.nadCreated = false
×
746
                        if cont.sharedAaepMonitor[aaepName] == nil {
×
747
                                cont.sharedAaepMonitor[aaepName] = make(map[string]*AaepEpgAttachData)
×
748
                        }
×
749
                        cont.sharedAaepMonitor[aaepName][epgDn] = aaepEpgAttachData
×
750
                        epgsToSubscribe[epgDn] = true
×
751
                        cont.log.Errorf("Skipping NAD creation: EPG %s with AAEP %s has overlapping VLAN %d",
×
752
                                epgDn, aaepName, aaepEpgAttachData.encapVlan)
×
753
                        continue
×
754
                }
755

756
                OldepgDn := cont.handleNewNadCreation(aaepName, epgDn, aaepEpgAttachData, "AaepAddedInCR", true)
×
757
                if OldepgDn != "" {
×
758
                        epgsToSubscribe[OldepgDn] = true
×
759
                }
×
760

761
                epgsToSubscribe[epgDn] = true
×
762
        }
763

764
        if _, ok := cont.sharedAaepMonitor[aaepName]; !ok {
×
765
                cont.sharedAaepMonitor[aaepName] = make(map[string]*AaepEpgAttachData)
×
766
        }
×
767

768
        return epgsToSubscribe
×
769
}
770

771
// clean converts a string to lowercase, removes underscores and dots,
772
// and replaces any other invalid character with a hyphen.
773
func cleanApicResourceNames(apicResource string) string {
×
774
        apicResource = strings.ToLower(apicResource)
×
775
        var stringBuilder strings.Builder
×
776
        for _, character := range apicResource {
×
777
                switch {
×
778
                case character == '_' || character == '.':
×
779
                        continue
×
780
                case (character >= 'a' && character <= 'z') || (character >= '0' && character <= '9') || character == '-':
×
781
                        stringBuilder.WriteRune(character)
×
782
                default:
×
783
                        stringBuilder.WriteRune('-')
×
784
                }
785
        }
786
        return strings.Trim(stringBuilder.String(), "-")
×
787
}
788

789
func (cont *AciController) generateDefaultNadName(aaepName, epgDn string) string {
×
790
        parts := strings.Split(epgDn, "/")
×
791

×
792
        tenant := parts[1][3:]
×
793
        appProfile := parts[2][3:]
×
794
        epgName := parts[3][4:]
×
795

×
796
        apicResourceNames := tenant + appProfile + epgName + aaepName
×
797
        hashBytes := sha256.Sum256([]byte(apicResourceNames))
×
798
        hash := hex.EncodeToString(hashBytes[:])[:16]
×
799

×
800
        return fmt.Sprintf("%s-%s-%s-%s",
×
801
                cleanApicResourceNames(tenant), cleanApicResourceNames(appProfile), cleanApicResourceNames(epgName), hash)
×
802
}
×
803

804
func (cont *AciController) isNADUpdateRequired(aaepName string, epgDn string, nadData *AaepEpgAttachData,
805
        existingNAD *nadapi.NetworkAttachmentDefinition) bool {
×
806
        vlanID := nadData.encapVlan
×
807
        namespaceName := nadData.namespaceName
×
808
        customNadName := nadData.nadName
×
809
        defaultNadName := cont.generateDefaultNadName(aaepName, epgDn)
×
810
        existingAnnotaions := existingNAD.ObjectMeta.Annotations
×
811
        if existingAnnotaions != nil {
×
812
                if existingNAD.ObjectMeta.Annotations["aci-sync-status"] == "out-of-sync" || existingNAD.ObjectMeta.Annotations["cno-name"] != customNadName {
×
813
                        return true
×
814
                }
×
815
        } else {
×
816
                // NAD exists, check if VLAN needs to be updated
×
817
                existingConfig := existingNAD.Spec.Config
×
818
                if existingConfig != "" {
×
819
                        var existingCNVConfig map[string]interface{}
×
820
                        if json.Unmarshal([]byte(existingConfig), &existingCNVConfig) == nil {
×
821
                                if existingVLAN, ok := existingCNVConfig["vlan"].(float64); ok {
×
822
                                        if int(existingVLAN) == vlanID {
×
823
                                                // VLAN hasn't changed, no update needed
×
824
                                                cont.log.Infof("NetworkAttachmentDefinition %s already exists with correct VLAN %d in namespace %s",
×
825
                                                        defaultNadName, vlanID, namespaceName)
×
826
                                                return false
×
827
                                        }
×
828
                                } else if vlanID == 0 {
×
829
                                        // Both existing and new have no VLAN, no update needed
×
830
                                        cont.log.Infof("NetworkAttachmentDefinition %s already exists with no VLAN in namespace %s", defaultNadName, namespaceName)
×
831
                                        return false
×
832
                                }
×
833
                        }
834
                }
835
        }
836

837
        return true
×
838
}
839

840
func (cont *AciController) createNetworkAttachmentDefinition(aaepName string, epgDn string, nadData *AaepEpgAttachData, createReason string) bool {
×
841
        bridge := cont.config.BridgeName
×
842
        if bridge == "" {
×
843
                cont.log.Errorf("Linux bridge name must be specified when creating NetworkAttachmentDefinitions")
×
844
                return false
×
845
        }
×
846

847
        vlanID := nadData.encapVlan
×
848
        namespaceName := nadData.namespaceName
×
849
        customNadName := nadData.nadName
×
850
        defaultNadName := cont.generateDefaultNadName(aaepName, epgDn)
×
851
        nadClient := cont.env.(*K8sEnvironment).nadClient
×
852
        mtu := 1500
×
853

×
854
        // Check if NAD already exists
×
855
        existingNAD, err := nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Get(context.TODO(), defaultNadName, metav1.GetOptions{})
×
856
        nadExists := err == nil
×
857

×
858
        if nadExists && !cont.isNADUpdateRequired(aaepName, epgDn, nadData, existingNAD) {
×
859
                return true
×
860
        }
×
861

862
        cnvBridgeConfig := map[string]any{
×
863
                "cniVersion": "0.3.1",
×
864
                "name":       defaultNadName,
×
865
                "type":       "bridge",
×
866
                "bridge":     bridge,
×
867
        }
×
868

×
869
        if !nadExists {
×
870
                cnvBridgeConfig["mtu"] = mtu
×
871
                cnvBridgeConfig["disableContainerInterface"] = true
×
872
                // Add optional parameters from controller config if they are set
×
873
                if cont.config.IsGateway != nil {
×
874
                        cnvBridgeConfig["isGateway"] = *cont.config.IsGateway
×
875
                }
×
876
                if cont.config.IsDefaultGateway != nil {
×
877
                        cnvBridgeConfig["isDefaultGateway"] = *cont.config.IsDefaultGateway
×
878
                }
×
879
                if cont.config.ForceAddress != nil {
×
880
                        cnvBridgeConfig["forceAddress"] = *cont.config.ForceAddress
×
881
                }
×
882
                if cont.config.IpMasq != nil {
×
883
                        cnvBridgeConfig["ipMasq"] = *cont.config.IpMasq
×
884
                }
×
885
                if cont.config.IpMasqBackend != "" {
×
886
                        cnvBridgeConfig["ipMasqBackend"] = cont.config.IpMasqBackend
×
887
                }
×
888
                if cont.config.Mtu != nil {
×
889
                        cnvBridgeConfig["mtu"] = *cont.config.Mtu
×
890
                }
×
891
                if cont.config.HairpinMode != nil {
×
892
                        cnvBridgeConfig["hairpinMode"] = *cont.config.HairpinMode
×
893
                }
×
894
                if cont.config.PromiscMode != nil {
×
895
                        cnvBridgeConfig["promiscMode"] = *cont.config.PromiscMode
×
896
                }
×
897
                if cont.config.Enabledad != nil {
×
898
                        cnvBridgeConfig["enabledad"] = *cont.config.Enabledad
×
899
                }
×
900
                if cont.config.Macspoofchk != nil {
×
901
                        cnvBridgeConfig["macspoofchk"] = *cont.config.Macspoofchk
×
902
                }
×
903
                if cont.config.DisableContainerInterface != nil {
×
904
                        cnvBridgeConfig["disableContainerInterface"] = *cont.config.DisableContainerInterface
×
905
                }
×
906
                if cont.config.PortIsolation != nil {
×
907
                        cnvBridgeConfig["portIsolation"] = *cont.config.PortIsolation
×
908
                }
×
909
                if len(cont.config.Ipam) > 0 {
×
910
                        cnvBridgeConfig["ipam"] = cont.config.Ipam
×
911
                }
×
912
        } else {
×
913
                // Preserve existing optional parameters from existing NAD
×
914
                existingConfig := existingNAD.Spec.Config
×
915
                if existingConfig != "" {
×
916
                        var existingCNVConfig map[string]interface{}
×
917
                        if json.Unmarshal([]byte(existingConfig), &existingCNVConfig) == nil {
×
918
                                optionalParams := []string{"isGateway", "isDefaultGateway", "forceAddress", "ipMasq",
×
919
                                        "ipMasqBackend", "mtu", "hairpinMode", "promiscMode", "enabledad", "macspoofchk",
×
920
                                        "disableContainerInterface", "portIsolation", "ipam"}
×
921
                                for _, param := range optionalParams {
×
922
                                        if value, ok := existingCNVConfig[param]; ok {
×
923
                                                cnvBridgeConfig[param] = value
×
924
                                        }
×
925
                                }
926
                        }
927
                }
928
        }
929
        if vlanID > 0 {
×
930
                cnvBridgeConfig["vlan"] = vlanID
×
931
        }
×
932

933
        configJSON, err := json.Marshal(cnvBridgeConfig)
×
934
        if err != nil {
×
935
                cont.log.Errorf("Failed to marshal CNV bridge config: %v", err)
×
936
                return false
×
937
        }
×
938

939
        nad := &nadapi.NetworkAttachmentDefinition{
×
940
                TypeMeta: metav1.TypeMeta{
×
941
                        APIVersion: "k8s.cni.cncf.io/v1",
×
942
                        Kind:       "NetworkAttachmentDefinition",
×
943
                },
×
944
                ObjectMeta: metav1.ObjectMeta{
×
945
                        Name:      defaultNadName,
×
946
                        Namespace: namespaceName,
×
947
                        Labels: map[string]string{
×
948
                                "managed-by": "cisco-network-operator",
×
949
                                "vlan":       strconv.Itoa(vlanID),
×
950
                        },
×
951
                        Annotations: map[string]string{
×
952
                                "managed-by":      "cisco-network-operator",
×
953
                                "vlan":            strconv.Itoa(vlanID),
×
954
                                "cno-name":        customNadName,
×
955
                                "aci-sync-status": "in-sync",
×
956
                                "aaep-name":       aaepName,
×
957
                                "epg-dn":          epgDn,
×
958
                        },
×
959
                },
×
960
                Spec: nadapi.NetworkAttachmentDefinitionSpec{
×
961
                        Config: string(configJSON),
×
962
                },
×
963
        }
×
964

×
965
        if nadExists {
×
966
                nad.ObjectMeta.ResourceVersion = existingNAD.ObjectMeta.ResourceVersion
×
967

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

×
970
                if err != nil {
×
971
                        cont.log.Errorf("Failed to update NetworkAttachmentDefinition %s from namespace %s : %v", customNadName, namespaceName, err)
×
972
                        return false
×
973
                }
×
974

975
                cont.log.Debugf("Existing NAD Annotations: %v, %s", existingNAD.ObjectMeta.Annotations, createReason)
×
976
                if existingNAD.ObjectMeta.Annotations["aci-sync-status"] == "out-of-sync" {
×
977
                        cont.submitEvent(updatedNad, createReason, cont.getNADRevampMessage(createReason))
×
978
                }
×
979
                cont.log.Infof("Updated NetworkAttachmentDefinition %s from namespace %s", defaultNadName, namespaceName)
×
980
        } else {
×
981
                _, err = nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Create(context.TODO(), nad, metav1.CreateOptions{})
×
982
                if err != nil {
×
983
                        cont.log.Errorf("Failed to create NetworkAttachmentDefinition %s in namespace %s : %v", customNadName, namespaceName, err)
×
984
                        return false
×
985
                }
×
986
                cont.log.Infof("Created NetworkAttachmentDefinition %s in namespace %s", defaultNadName, namespaceName)
×
987
        }
988

989
        return true
×
990
}
991

992
func (cont *AciController) deleteNetworkAttachmentDefinition(aaepName string, epgDn string, nadData *AaepEpgAttachData, deleteReason string) {
×
993
        namespaceName := nadData.namespaceName
×
994

×
995
        kubeClient := cont.env.(*K8sEnvironment).kubeClient
×
996
        _, err := kubeClient.CoreV1().Namespaces().Get(context.TODO(), namespaceName, metav1.GetOptions{})
×
997
        namespaceExists := err == nil
×
998
        if !namespaceExists {
×
999
                cont.log.Debugf("Defering NAD deletion for EPG %s: Namespace %s not exists", epgDn, namespaceName)
×
1000
                return
×
1001
        }
×
1002

1003
        nadName := cont.generateDefaultNadName(aaepName, epgDn)
×
1004
        nadClient := cont.env.(*K8sEnvironment).nadClient
×
1005

×
1006
        nadDetails, err := nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Get(context.TODO(), nadName, metav1.GetOptions{})
×
1007
        nadExists := err == nil
×
1008

×
1009
        if nadExists {
×
1010
                if !cont.isVmmLiteNAD(nadDetails) {
×
1011
                        return
×
1012
                }
×
1013

1014
                if cont.isNADinUse(namespaceName, nadName) {
×
1015
                        nadDetails.ObjectMeta.Annotations["aci-sync-status"] = "out-of-sync"
×
1016
                        _, err = nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Update(context.TODO(), nadDetails, metav1.UpdateOptions{})
×
1017
                        if err != nil {
×
1018
                                cont.log.Errorf("Failed to add out-of-sync annotation to the NAD %s from namespace %s : %v", nadName, namespaceName, err)
×
1019
                                return
×
1020
                        }
×
1021
                        cont.submitEvent(nadDetails, deleteReason, cont.getNADDeleteMessage(deleteReason))
×
1022
                        cont.log.Infof("Added annotation out-of-sync for NAD %s from namespace %s", nadName, namespaceName)
×
1023
                        return
×
1024
                }
1025

1026
                delete(nadDetails.ObjectMeta.Annotations, "managed-by")
×
1027
                _, err = nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Update(context.TODO(), nadDetails, metav1.UpdateOptions{})
×
1028
                if err != nil {
×
1029
                        cont.log.Errorf("Failed to remove VMM lite annotation from NetworkAttachmentDefinition %s from namespace %s: %v", nadName, namespaceName, err)
×
1030
                        return
×
1031
                }
×
1032
                err := nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Delete(context.TODO(), nadName, metav1.DeleteOptions{})
×
1033
                if err != nil {
×
1034
                        cont.log.Errorf("Failed to delete NetworkAttachmentDefinition %s from namespace %s : %v", nadName, namespaceName, err)
×
1035
                        nadDetails, err := nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Get(context.TODO(), nadName, metav1.GetOptions{})
×
1036
                        nadExists := err == nil
×
1037
                        if nadExists {
×
1038
                                nadDetails.ObjectMeta.Annotations["managed-by"] = "cisco-network-operator"
×
1039
                                _, updateErr := nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(namespaceName).Update(context.TODO(), nadDetails, metav1.UpdateOptions{})
×
1040
                                if updateErr != nil {
×
1041
                                        cont.log.Errorf("Failed to restore VMM lite annotation to NetworkAttachmentDefinition %s from namespace %s: %v", nadName, namespaceName, updateErr)
×
1042
                                }
×
1043
                        }
1044
                        return
×
1045
                }
1046
                cont.log.Infof("Deleted NAD %s from %s namespace", nadName, namespaceName)
×
1047
        } else {
×
1048
                cont.log.Debugf("NAD %s not there to delete in namespace %s", nadName, namespaceName)
×
1049
        }
×
1050
}
1051

1052
func (cont *AciController) getVlanId(encap string) int {
×
1053
        if after, ok := strings.CutPrefix(encap, "vlan-"); ok {
×
1054
                vlanStr := after
×
1055
                if vlanID, err := strconv.Atoi(vlanStr); err == nil && vlanID > 0 {
×
1056
                        return vlanID
×
1057
                }
×
1058
        } else if after, ok := strings.CutPrefix(encap, "vlan"); ok {
×
1059
                vlanStr := after
×
1060
                if vlanID, err := strconv.Atoi(vlanStr); err == nil && vlanID > 0 {
×
1061
                        return vlanID
×
1062
                }
×
1063
        }
1064

1065
        return 0
×
1066
}
1067

1068
func (cont *AciController) getAaepDiff(crAaeps []string) (addedAaeps, removedAaeps []string) {
×
1069
        crAaepMap := make(map[string]bool)
×
1070
        for _, crAaep := range crAaeps {
×
1071
                crAaepMap[crAaep] = true
×
1072
        }
×
1073

1074
        for _, crAaep := range crAaeps {
×
1075
                if _, ok := cont.sharedAaepMonitor[crAaep]; !ok {
×
1076
                        addedAaeps = append(addedAaeps, crAaep)
×
1077
                }
×
1078
        }
1079

1080
        for cachedAaep := range cont.sharedAaepMonitor {
×
1081
                if !crAaepMap[cachedAaep] {
×
1082
                        removedAaeps = append(removedAaeps, cachedAaep)
×
1083
                }
×
1084
        }
1085

1086
        return
×
1087
}
1088

1089
func (cont *AciController) getEncapFromAaepEpgAttachObj(aaepName, epgDn string) *apicapi.ApicObjectBody {
×
1090
        uri := fmt.Sprintf("/api/node/mo/uni/infra/attentp-%s/gen-default/rsfuncToEpg-[%s].json?query-target=self", aaepName, epgDn)
×
1091
        resp, err := cont.apicConn.GetApicResponse(uri)
×
1092
        if err != nil {
×
1093
                cont.log.Errorf("Failed to get response from APIC: AAEP %s and EPG %s ERROR: %v", aaepName, epgDn, err)
×
1094
                return nil
×
1095
        }
×
1096

1097
        if len(resp.Imdata) == 0 {
×
1098
                cont.log.Debugf("Can't find infraRsFuncToEpg object for AAEP %s and EPG %s", aaepName, epgDn)
×
1099
                return nil
×
1100
        }
×
1101
        lresp, ok := resp.Imdata[0]["infraRsFuncToEpg"]
×
1102
        if !ok {
×
1103
                cont.log.Errorf("InfraRsFuncToEpg object not found in response for AAEP %s and EPG %s", aaepName, epgDn)
×
1104
                return nil
×
1105
        }
×
1106

1107
        return lresp
×
1108
}
1109

1110
func (cont *AciController) isVmmLiteNAD(nadDetails *nadapi.NetworkAttachmentDefinition) bool {
×
1111
        return nadDetails.ObjectMeta.Annotations["managed-by"] == "cisco-network-operator"
×
1112
}
×
1113

1114
func (cont *AciController) isNADinUse(namespaceName string, nadName string) bool {
×
1115
        kubeClient := cont.env.(*K8sEnvironment).kubeClient
×
1116
        pods, err := kubeClient.CoreV1().Pods(namespaceName).List(context.TODO(), metav1.ListOptions{})
×
1117
        if err == nil {
×
1118
                var networks []map[string]string
×
1119
                for _, pod := range pods.Items {
×
1120
                        networksAnn, ok := pod.Annotations["k8s.v1.cni.cncf.io/networks"]
×
1121
                        if ok && (networksAnn == nadName) {
×
1122
                                cont.log.Infof("NAD %s is still used by Pod %s/%s", nadName, namespaceName, pod.Name)
×
1123
                                return true
×
1124
                        }
×
1125
                        if err := json.Unmarshal([]byte(networksAnn), &networks); err != nil {
×
1126
                                cont.log.Errorf("Error while getting pod annotations: %v", err)
×
1127
                                return false
×
1128
                        }
×
1129
                        for _, network := range networks {
×
1130
                                if ok && (network["name"] == nadName) {
×
1131
                                        cont.log.Infof("NAD %s is still used by VM %s/%s", nadName, namespaceName, pod.Name)
×
1132
                                        return true
×
1133
                                }
×
1134
                        }
1135
                }
1136
        }
1137
        return false
×
1138
}
1139

1140
func (cont *AciController) getNADDeleteMessage(deleteReason string) string {
×
1141
        messagePrefix := "NAD is in use by pods: "
×
1142
        switch {
×
1143
        case deleteReason == "EpgDeleted":
×
1144
                return messagePrefix + "EPG deleted"
×
1145
        case deleteReason == "NamespaceAnnotationRemoved":
×
1146
                return messagePrefix + "Namespace name EPG annotaion removed"
×
1147
        case deleteReason == "NamespaceAnnotationModified":
×
1148
                return messagePrefix + "Namespace name EPG annotation modified"
×
1149
        case deleteReason == "AaepEpgDetached":
×
1150
                return messagePrefix + "EPG detached from AAEP"
×
1151
        case deleteReason == "CRDeleted":
×
1152
                return messagePrefix + "aaepmonitor CR deleted"
×
1153
        case deleteReason == "AaepRemovedFromCR":
×
1154
                return messagePrefix + "AAEP removed from aaepmonitor CR"
×
1155
        case deleteReason == "AaepEpgAttachedWithVlanUsedInCluster":
×
1156
                return messagePrefix + "EPG with AAEP has overlapping VLAN with existing cluster VLAN"
×
1157
        case deleteReason == "AaepEpgAttachedWithOverlappingVlan":
×
1158
                return messagePrefix + "EPG with AAEP has overlapping VLAN with another EPG"
×
1159
        case deleteReason == "NamespaceDeleted":
×
1160
                return messagePrefix + "Namespace deleted"
×
1161
        case deleteReason == "AaepEpgAttachedWithVlanInUse":
×
1162
                return messagePrefix + "EPG with AAEP has VLAN already used in cluster"
×
1163
        case deleteReason == "VlanUpdatedInAaepEpgAttach":
×
1164
                return messagePrefix + "VLAN updated in AAEP EPG attachment"
×
1165
        case deleteReason == "StaleNadCleanup":
×
1166
                return messagePrefix + "Stale NAD detected"
×
1167
        }
1168
        return messagePrefix + "One or many pods are using NAD"
×
1169
}
1170

1171
func (cont *AciController) getNADRevampMessage(createReason string) string {
×
1172
        messagePrefix := "NAD is in sync: "
×
1173
        switch {
×
1174
        case createReason == "NamespaceAnnotationAdded":
×
1175
                return messagePrefix + "Namespace name EPG annotaion added"
×
1176
        case createReason == "AaepEpgAttached":
×
1177
                return messagePrefix + "EPG attached with AAEP"
×
1178
        case createReason == "AaepAddedInCR":
×
1179
                return messagePrefix + "AAEP added back in aaepmonitor CR"
×
1180
        case createReason == "NamespaceCreated":
×
1181
                return messagePrefix + "Namespace created back"
×
1182
        case createReason == "NextEpgNadCreation":
×
1183
                return messagePrefix + "NAD creation for next EPG with same VLAN"
×
1184
        }
1185
        return messagePrefix + "NAD synced with ACI"
×
1186
}
1187

1188
func (cont *AciController) isEpgAttachedWithAaep(aaepName, epgDn string, locked bool) bool {
×
1189
        if !locked {
×
1190
                cont.indexMutex.Lock()
×
1191
                defer cont.indexMutex.Unlock()
×
1192
        }
×
1193
        if aaepName != "" {
×
1194
                // Used in sync process to check if EPG is still attached with AAEP
×
1195
                // if EPG not attached, then need to delete NAD
×
1196
                aaepEpgAttachObj := cont.getEncapFromAaepEpgAttachObj(aaepName, epgDn)
×
1197
                if aaepEpgAttachObj == nil {
×
1198
                        return false
×
1199
                }
×
1200
                if state, hasState := aaepEpgAttachObj.Attributes["state"].(string); hasState {
×
1201
                        if state == "formed" {
×
1202
                                return true
×
1203
                        }
×
1204
                }
1205
        } else {
×
1206
                for aaepName := range cont.sharedAaepMonitor {
×
1207
                        aaepEpgAttachObj := cont.getEncapFromAaepEpgAttachObj(aaepName, epgDn)
×
1208
                        if aaepEpgAttachObj == nil {
×
1209
                                continue
×
1210
                        }
1211
                        if _, ok := aaepEpgAttachObj.Attributes["encap"]; ok {
×
1212
                                return true
×
1213
                        }
×
1214
                }
1215
        }
1216
        return false
×
1217
}
1218

1219
func (cont *AciController) checkDuplicateAaepEpgAttachRequest(aaepName, epgDn string, vlanID int) bool {
×
1220
        cont.indexMutex.Lock()
×
1221
        defer cont.indexMutex.Unlock()
×
1222
        aaepEpgAttachDataMap, exists := cont.sharedAaepMonitor[aaepName]
×
1223
        if !exists || aaepEpgAttachDataMap == nil {
×
1224
                return false
×
1225
        }
×
1226

1227
        aaepEpgAttachData, exists := aaepEpgAttachDataMap[epgDn]
×
1228
        if !exists || aaepEpgAttachData == nil {
×
1229
                return false
×
1230
        }
×
1231

1232
        if vlanID == aaepEpgAttachData.encapVlan && aaepEpgAttachData.nadCreated {
×
1233
                return true
×
1234
        }
×
1235
        return false
×
1236
}
1237

1238
func (cont *AciController) checkIfEpgWithOverlappingVlan(aaepName string, vlanId int, epgDn string, locked bool) bool {
×
1239
        if !locked {
×
1240
                cont.indexMutex.Lock()
×
1241
                defer cont.indexMutex.Unlock()
×
1242
        }
×
1243
        oldAaepEpgAttachData := cont.getAaepEpgAttachDataLocked(aaepName, epgDn)
×
1244

×
1245
        if oldAaepEpgAttachData != nil {
×
1246
                // 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
×
1247
                if oldAaepEpgAttachData.encapVlan == vlanId && oldAaepEpgAttachData.nadCreated {
×
1248
                        return false
×
1249
                }
×
1250
        }
1251
        aaepEpgAttachDataMap, exists := cont.sharedAaepMonitor[aaepName]
×
1252
        if !exists || aaepEpgAttachDataMap == nil {
×
1253
                return false
×
1254
        }
×
1255

1256
        for _, aaepEpgData := range aaepEpgAttachDataMap {
×
1257
                if vlanId == aaepEpgData.encapVlan && aaepEpgData.nadCreated {
×
1258
                        return true
×
1259
                }
×
1260
        }
1261
        return false
×
1262
}
1263

1264
func (cont *AciController) createNadForNextEpg(aaepName string, vlanId int) {
×
1265
        cont.indexMutex.Lock()
×
1266
        defer cont.indexMutex.Unlock()
×
1267

×
1268
        aaepEpgAttachDataMap := cont.sharedAaepMonitor[aaepName]
×
1269
        for epgDn, aaepEpgAttachData := range aaepEpgAttachDataMap {
×
1270
                if aaepEpgAttachData.encapVlan != vlanId || aaepEpgAttachData.nadCreated {
×
1271
                        continue
×
1272
                } else if !cont.namespaceChecks(aaepEpgAttachData.namespaceName, epgDn) {
×
1273
                        cont.log.Debugf("Skipping NAD creation for next EPG %s with AAEP %s and VLAN %d: Namespace checks failed", epgDn, aaepName, vlanId)
×
1274
                        continue
×
1275
                } else {
×
1276
                        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",
×
1277
                                epgDn, aaepName, vlanId)
×
1278
                        needCacheChange := cont.createNetworkAttachmentDefinition(aaepName, epgDn, aaepEpgAttachData, "NextEpgNadCreation")
×
1279
                        if needCacheChange {
×
1280
                                aaepEpgAttachData.nadCreated = true
×
1281
                                if cont.sharedAaepMonitor[aaepName] == nil {
×
1282
                                        cont.sharedAaepMonitor[aaepName] = make(map[string]*AaepEpgAttachData)
×
1283
                                }
×
1284
                                cont.sharedAaepMonitor[aaepName][epgDn] = aaepEpgAttachData
×
1285
                        }
1286
                        cont.log.Infof("Created NAD for next EPG %s with AAEP %s and VLAN %d", epgDn, aaepName, vlanId)
×
1287
                        break
×
1288
                }
1289
        }
1290
}
1291

1292
func (cont *AciController) checkVlanUsedInCluster(vlanId int) bool {
×
1293
        if vlanId == cont.config.KubeapiVlan {
×
1294
                return true
×
1295
        }
×
1296
        return false
×
1297
}
1298

1299
func (cont *AciController) checkMonitorDataModified(oldData, newData *AaepEpgAttachData) bool {
×
1300
        if oldData == nil {
×
1301
                return true
×
1302
        }
×
1303
        if oldData.namespaceName != newData.namespaceName ||
×
1304
                oldData.nadName != newData.nadName ||
×
1305
                oldData.encapVlan != newData.encapVlan {
×
1306
                return true
×
1307
        }
×
1308
        return false
×
1309
}
1310

1311
func (cont *AciController) handleOldNadDeletion(aaepName, epgDn string,
1312
        oldAaepMonitorData *AaepEpgAttachData, deleteReason string) {
×
1313
        if oldAaepMonitorData == nil {
×
1314
                return
×
1315
        }
×
1316
        cont.indexMutex.Lock()
×
1317
        delete(cont.sharedAaepMonitor[aaepName], epgDn)
×
1318
        if oldAaepMonitorData.nadCreated {
×
1319
                cont.log.Debugf("Deleting NAD for EPG %s with AAEP %s due to %s", epgDn, aaepName, deleteReason)
×
1320
                cont.deleteNetworkAttachmentDefinition(aaepName, epgDn, oldAaepMonitorData, deleteReason)
×
1321
        }
×
1322
        cont.indexMutex.Unlock()
×
1323
        if oldAaepMonitorData.nadCreated {
×
1324
                cont.createNadForNextEpg(aaepName, oldAaepMonitorData.encapVlan)
×
1325
        }
×
1326
}
1327

1328
func (cont *AciController) handleNewNadCreation(aaepName, epgDn string,
1329
        newAaepMonitorData *AaepEpgAttachData, createReason string, locked bool) string {
×
1330
        if !locked {
×
1331
                cont.indexMutex.Lock()
×
1332
                defer cont.indexMutex.Unlock()
×
1333
        }
×
1334
        // If there are multiple EPGs associated with same vlan for an aaep,
1335
        // then only one NAD will be there.
1336
        // After controller restart, if any other EPG notification comes before the already present NAD
1337
        // it will result in 2 NADs.
1338
        // Below check is to avoid the condition
1339
        NADAlreadyInCluster, oldEpgDn := cont.isNADWithSameVlanAlreadyPresent(aaepName, epgDn, newAaepMonitorData)
×
1340
        if NADAlreadyInCluster != nil {
×
1341
                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)
×
1342
                if cont.sharedAaepMonitor[aaepName] == nil {
×
1343
                        cont.sharedAaepMonitor[aaepName] = make(map[string]*AaepEpgAttachData)
×
1344
                }
×
1345
                cont.sharedAaepMonitor[aaepName][oldEpgDn] = NADAlreadyInCluster
×
1346
                newAaepMonitorData.nadCreated = false
×
1347
                cont.sharedAaepMonitor[aaepName][epgDn] = newAaepMonitorData
×
1348
                return oldEpgDn
×
1349
        }
1350
        needCacheChange := cont.createNetworkAttachmentDefinition(aaepName, epgDn, newAaepMonitorData, createReason)
×
1351
        if needCacheChange {
×
1352
                if cont.sharedAaepMonitor[aaepName] == nil {
×
1353
                        cont.sharedAaepMonitor[aaepName] = make(map[string]*AaepEpgAttachData)
×
1354
                }
×
1355
                cont.sharedAaepMonitor[aaepName][epgDn] = newAaepMonitorData
×
1356
        }
1357
        return ""
×
1358
}
1359

1360
func (cont *AciController) checkAnnotationModified(oldAaepEpgAttachData, newAaepEpgAttachData *AaepEpgAttachData) bool {
×
1361
        if oldAaepEpgAttachData.nadName != newAaepEpgAttachData.nadName || oldAaepEpgAttachData.namespaceName != newAaepEpgAttachData.namespaceName {
×
1362
                return true
×
1363
        }
×
1364
        return false
×
1365
}
1366

1367
func (cont *AciController) isNADWithSameVlanAlreadyPresent(aaepName string, epgDn string, newAaepMonitorData *AaepEpgAttachData) (*AaepEpgAttachData, string) {
×
1368
        allNADs, err := cont.getAllNADs()
×
1369
        if err != nil {
×
1370
                cont.log.Errorf("Failed to get all NADs: %v", err)
×
1371
                return nil, ""
×
1372
        }
×
1373

1374
        for _, nad := range allNADs {
×
1375
                ns := nad.Namespace
×
1376
                annotations := nad.ObjectMeta.Annotations
×
1377
                if annotations == nil {
×
1378
                        continue
×
1379
                }
1380
                // Check for required annotations
1381
                if annotations["managed-by"] != "cisco-network-operator" ||
×
1382
                        annotations["aci-sync-status"] != "in-sync" ||
×
1383
                        annotations["aaep-name"] != aaepName {
×
1384
                        continue
×
1385
                }
1386

1387
                existingEpgDn := annotations["epg-dn"]
×
1388
                // Skip if same EPG DN
×
1389
                if existingEpgDn == epgDn {
×
1390
                        cont.log.Infof("Found existing NAD %s/%s for epg %s ", ns, nad.Name, epgDn)
×
1391
                        return nil, ""
×
1392
                }
×
1393
                // Compare VLAN
1394
                existingVlan, err := strconv.Atoi(annotations["vlan"])
×
1395
                if err != nil {
×
1396
                        cont.log.Warnf("Invalid VLAN in NAD %s/%s: %v", ns, nad.Name, err)
×
1397
                        continue
×
1398
                }
1399

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

×
1403
                        return &AaepEpgAttachData{
×
1404
                                nadName:       annotations["cno-name"],
×
1405
                                namespaceName: ns,
×
1406
                                encapVlan:     existingVlan,
×
1407
                                nadCreated:    true,
×
1408
                        }, existingEpgDn
×
1409
                }
×
1410
        }
1411

1412
        return nil, ""
×
1413
}
1414

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

×
1418
        nadList, err := nadClient.List(context.TODO(), metav1.ListOptions{})
×
1419
        if err != nil {
×
1420
                return nil, fmt.Errorf("failed to list all NADs: %v", err)
×
1421
        }
×
1422

1423
        return nadList.Items, nil
×
1424
}
1425

1426
func (cont *AciController) syncAndCleanNadCache() {
×
1427
        var reason string
×
1428
        cont.indexMutex.Lock()
×
1429
        defer cont.indexMutex.Unlock()
×
1430
        cont.log.Infof("Starting sync and clean of NAD cache")
×
1431
        for aaepName, aaepEpgAttachDataMap := range cont.sharedAaepMonitor {
×
1432
                // Get the latest EPG attachment details from APIC for the AAEP
×
1433
                epgVlanMapList := cont.getAaepEpgAttObjDetails(aaepName)
×
1434

×
1435
                // Create a set of EPGs from the APIC state for quick lookup
×
1436
                apicEpgSet := make(map[string]*EpgVlanMap)
×
1437
                for _, epgVlanMap := range epgVlanMapList {
×
1438
                        apicEpgSet[epgVlanMap.epgDn] = &epgVlanMap
×
1439
                }
×
1440

1441
                var namespaceName string
×
1442
                // Iterate through the cache and find entries not in the APIC state
×
1443
                for epgDn, oldAaepEpgAttachData := range aaepEpgAttachDataMap {
×
1444
                        if oldAaepEpgAttachData == nil {
×
1445
                                continue
×
1446
                        }
1447
                        epgAnnotations := cont.getEpgAnnotations(epgDn)
×
1448
                        if epgAnnotations == nil {
×
1449
                                namespaceName = ""
×
1450
                        } else {
×
1451
                                namespaceName, _ = cont.getSpecificEPGAnnotation(epgAnnotations)
×
1452
                        }
×
1453
                        if _, exists := apicEpgSet[epgDn]; !exists {
×
1454
                                cont.log.Infof("Deleting stale cache entry: EPG %s for AAEP %s. Reason: EPG detached or deleted.", epgDn, aaepName)
×
1455
                                delete(cont.sharedAaepMonitor[aaepName], epgDn)
×
1456
                                if oldAaepEpgAttachData.nadCreated {
×
1457
                                        cont.log.Debugf("Deleting NAD for EPG %s with AAEP %s due to AaepEpgDetached", epgDn, aaepName)
×
1458
                                        cont.deleteNetworkAttachmentDefinition(aaepName, epgDn, oldAaepEpgAttachData, "AaepEpgDetached")
×
1459
                                }
×
1460
                                cont.log.Infof("Successfully deleted cache entry: EPG %s for AAEP %s.", epgDn, aaepName)
×
1461
                        } else if namespaceName != aaepEpgAttachDataMap[epgDn].namespaceName {
×
1462
                                cont.log.Infof("Deleting stale cache entry: EPG %s for AAEP %s. Reason: namespace annotation changed from %s to %s",
×
1463
                                        epgDn, aaepName, aaepEpgAttachDataMap[epgDn].namespaceName, namespaceName)
×
1464
                                delete(cont.sharedAaepMonitor[aaepName], epgDn)
×
1465
                                if oldAaepEpgAttachData.nadCreated {
×
1466
                                        if cont.checkIfEPGExists(epgDn) {
×
1467
                                                if namespaceName == "" {
×
1468
                                                        reason = "NamespaceAnnotationRemoved"
×
1469
                                                } else {
×
1470
                                                        reason = "NamespaceAnnotationModified"
×
1471
                                                }
×
1472
                                        } else {
×
1473
                                                reason = "EpgDeleted"
×
1474
                                        }
×
1475
                                        cont.log.Debugf("Deleting NAD for EPG %s with AAEP %s due to AaepEpgDetached", epgDn, aaepName)
×
1476
                                        cont.deleteNetworkAttachmentDefinition(aaepName, epgDn, oldAaepEpgAttachData, reason)
×
1477
                                }
1478
                                cont.log.Infof("Successfully deleted cache entry: EPG %s for AAEP %s.", epgDn, aaepName)
×
1479
                        } else if apicEpgSet[epgDn].encapVlan != aaepEpgAttachDataMap[epgDn].encapVlan {
×
1480
                                cont.log.Infof("Updating NAD for EPG %s with AAEP %s due to VLAN change from %d to %d",
×
1481
                                        epgDn, aaepName, aaepEpgAttachDataMap[epgDn].encapVlan, apicEpgSet[epgDn].encapVlan)
×
1482
                                delete(cont.sharedAaepMonitor[aaepName], epgDn)
×
1483
                                if oldAaepEpgAttachData.nadCreated {
×
1484
                                        cont.log.Debugf("Deleting NAD for EPG %s with AAEP %s due to AaepEpgDetached", epgDn, aaepName)
×
1485
                                        cont.deleteNetworkAttachmentDefinition(aaepName, epgDn, oldAaepEpgAttachData, "VlanUpdatedInAaepEpgAttach")
×
1486
                                }
×
1487
                                cont.log.Infof("Successfully deleted cache entry: EPG %s for AAEP %s.", epgDn, aaepName)
×
1488
                        }
1489
                }
1490
        }
1491
        cont.log.Infof("completed sync and clean of NAD cache")
×
1492
}
1493

1494
func (cont *AciController) syncAndCleanNads() {
×
1495
        cont.indexMutex.Lock()
×
1496
        defer cont.indexMutex.Unlock()
×
1497
        cont.log.Infof("Starting sync and clean of NADs")
×
1498
        allNADs, err := cont.getAllNADs()
×
1499
        if err != nil {
×
1500
                cont.log.Errorf("Failed to get all NADs during sync and clean: %v", err)
×
1501
                return
×
1502
        }
×
1503

1504
        for _, nad := range allNADs {
×
1505
                annotations := nad.ObjectMeta.Annotations
×
1506
                if annotations == nil {
×
1507
                        continue
×
1508
                }
1509
                // Check for required annotations
1510
                if annotations["managed-by"] != "cisco-network-operator" ||
×
1511
                        annotations["aci-sync-status"] != "in-sync" {
×
1512
                        continue
×
1513
                }
1514

1515
                aaepName := annotations["aaep-name"]
×
1516
                epgDn := annotations["epg-dn"]
×
1517
                // Check if EPG is still attached with AAEP
×
1518
                if !cont.isEpgAttachedWithAaep(aaepName, epgDn, true) {
×
1519
                        cont.log.Infof("Deleting stale NAD %s/%s: EPG %s not attached to AAEP %s", nad.Namespace, nad.Name, epgDn, aaepName)
×
1520
                        delete(cont.sharedAaepMonitor[aaepName], epgDn)
×
1521
                        aaepEpgAttachData := &AaepEpgAttachData{
×
1522
                                nadName:       annotations["cno-name"],
×
1523
                                namespaceName: nad.Namespace,
×
1524
                                encapVlan:     cont.getVlanId(annotations["vlan"]),
×
1525
                                nadCreated:    true,
×
1526
                        }
×
1527
                        cont.deleteNetworkAttachmentDefinition(aaepName, epgDn, aaepEpgAttachData, "StaleNadCleanup")
×
1528
                }
×
1529
        }
1530
        cont.log.Infof("Completed sync and clean of NADs")
×
1531
}
1532

1533
func (cont *AciController) subscribeSafely(epgDn string) {
×
1534
        const maxRetries = 5
×
1535
        retries := 0
×
1536

×
1537
        for retries < maxRetries {
×
1538
                defer func() {
×
1539
                        if r := recover(); r != nil && cont.checkIfEPGExists(epgDn) {
×
1540
                                retries++
×
1541
                                cont.log.Infof("Found issue while subscribing for %s: %v. Retry: %d", epgDn, r, retries)
×
1542
                        }
×
1543
                }()
1544

1545
                cont.apicConn.AddImmediateSubscriptionDnLocked(epgDn, []string{"tagAnnotation"}, cont.handleAnnotationAdded,
×
1546
                        cont.handleAnnotationDeleted)
×
1547
                return
×
1548
        }
1549

1550
        cont.log.Errorf("Failed to subscribe to EPG %s after %d retries", epgDn, maxRetries)
×
1551
}
1552

NEW
1553
func (cont *AciController) getAeepEpgAttachDataFromNAD(aaepName, epgDn string) *AaepEpgAttachData {
×
1554
        allNADs, err := cont.getAllNADs()
×
1555
        if err != nil {
×
1556
                cont.log.Errorf("Failed to get all NADs during getting NAD details: %v", err)
×
1557
                return nil
×
1558
        }
×
1559

1560
        for _, nad := range allNADs {
×
1561
                annotations := nad.ObjectMeta.Annotations
×
1562
                if annotations == nil {
×
1563
                        continue
×
1564
                }
1565
                // Check for required annotations
1566
                if annotations["managed-by"] != "cisco-network-operator" ||
×
1567
                        annotations["aci-sync-status"] != "in-sync" {
×
1568
                        continue
×
1569
                }
1570

1571
                nadAaepName := annotations["aaep-name"]
×
1572
                nadEpgDn := annotations["epg-dn"]
×
1573
                // Check if EPG is still attached with AAEP
×
1574
                if aaepName == nadAaepName && epgDn == nadEpgDn {
×
NEW
1575
                        encapVlan, err := strconv.Atoi(annotations["vlan"])
×
NEW
1576
                        if err != nil {
×
NEW
1577
                                cont.log.Errorf("Failed to convert VLAN annotation to int: %v", err)
×
NEW
1578
                                return nil
×
NEW
1579
                        }
×
1580
                        aaepEpgAttachData := &AaepEpgAttachData{
×
1581
                                nadName:       annotations["cno-name"],
×
1582
                                namespaceName: nad.Namespace,
×
NEW
1583
                                encapVlan:     encapVlan,
×
1584
                                nadCreated:    true,
×
1585
                        }
×
1586
                        return aaepEpgAttachData
×
1587
                }
1588
        }
1589
        return nil
×
1590
}
1591

1592
func (cont *AciController) cleanStaleNADLocked(aaepName, epgDn string) {
×
NEW
1593
        aaepEpgAttachData := cont.getAeepEpgAttachDataFromNAD(aaepName, epgDn)
×
1594
        if aaepEpgAttachData != nil {
×
1595
                delete(cont.sharedAaepMonitor[aaepName], epgDn)
×
1596
                cont.deleteNetworkAttachmentDefinition(aaepName, epgDn, aaepEpgAttachData, "StaleNadCleanup")
×
1597
                cont.log.Infof("Deleted stale NAD for EPG: %s detached from AAEP: %s", epgDn, aaepName)
×
1598
        }
×
1599
}
1600

NEW
1601
func (cont *AciController) cleanStaleNADForNamespaceOrVlanChangeLocked(aaepName, epgDn string, vlanID int) {
×
NEW
1602
        aaepEpgAttachDataFromNAD := cont.getAeepEpgAttachDataFromNAD(aaepName, epgDn)
×
NEW
1603

×
NEW
1604
        if aaepEpgAttachDataFromNAD == nil {
×
NEW
1605
                return
×
NEW
1606
        }
×
1607

NEW
1608
        namespaceName := ""
×
NEW
1609
        epgAnnotations := cont.getEpgAnnotations(epgDn)
×
NEW
1610
        if epgAnnotations == nil {
×
NEW
1611
                cont.log.Debugf("No annotations found for EPG %s", epgDn)
×
NEW
1612
        } else {
×
NEW
1613
                namespaceName, _ = cont.getSpecificEPGAnnotation(epgAnnotations)
×
NEW
1614
        }
×
1615

NEW
1616
        if namespaceName != aaepEpgAttachDataFromNAD.namespaceName || vlanID != aaepEpgAttachDataFromNAD.encapVlan {
×
NEW
1617
                cont.deleteNetworkAttachmentDefinition(aaepName, epgDn, aaepEpgAttachDataFromNAD, "StaleNadCleanup")
×
NEW
1618
                cont.log.Infof("Deleted stale NAD for EPG: %s attached with AAEP: %s due to namespace or VLAN change", epgDn, aaepName)
×
NEW
1619
        }
×
1620
}
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