@@ -270,18 +270,45 @@ func (r *ClTcProgramReconciler) removeLinks(links []bpfmaniov1alpha1.ClTcAttachI
270270
271271// getExpectedLinks expands *AttachInfo into a list of specific attach
272272// points.
273- func (r * ClTcProgramReconciler ) getExpectedLinks (ctx context.Context , attachInfo bpfmaniov1alpha1.ClTcAttachInfo ,
274- ) ([]bpfmaniov1alpha1.ClTcAttachInfoState , error ) {
275- interfaces , err := getInterfaces (& attachInfo .InterfaceSelector , r .ourNode , r .Interfaces )
276- if err != nil {
277- return nil , fmt .Errorf ("failed to get interfaces for TcProgram: %v" , err )
273+ func (r * ClTcProgramReconciler ) getExpectedLinks (ctx context.Context , attachInfo bpfmaniov1alpha1.ClTcAttachInfo ) ([]bpfmaniov1alpha1.ClTcAttachInfoState , error ) {
274+ nodeLinks := []bpfmaniov1alpha1.ClTcAttachInfoState {}
275+ // Helper function to create a ClTcAttachInfoState entry
276+ createLinkEntry := func (interfaceName , netnsPath string ) bpfmaniov1alpha1.ClTcAttachInfoState {
277+ return bpfmaniov1alpha1.ClTcAttachInfoState {
278+ AttachInfoStateCommon : bpfmaniov1alpha1.AttachInfoStateCommon {
279+ ShouldAttach : true ,
280+ UUID : uuid .New ().String (),
281+ LinkId : nil ,
282+ LinkStatus : bpfmaniov1alpha1 .ApAttachNotAttached ,
283+ },
284+ InterfaceName : interfaceName ,
285+ NetnsPath : netnsPath ,
286+ Priority : attachInfo .Priority ,
287+ Direction : attachInfo .Direction ,
288+ ProceedOn : attachInfo .ProceedOn ,
289+ }
278290 }
279291
280- nodeLinks := []bpfmaniov1alpha1.ClTcAttachInfoState {}
292+ // Handle interface discovery
293+ if isInterfacesDiscoveryEnabled (& attachInfo .InterfaceSelector ) {
294+ discoveredInterfaces , err := getDiscoveredInterfaces (& attachInfo .InterfaceSelector , r .Interfaces )
295+ if err != nil {
296+ return nil , fmt .Errorf ("failed to discover interfaces: %w" , err )
297+ }
298+ for _ , intf := range discoveredInterfaces {
299+ nodeLinks = append (nodeLinks , createLinkEntry (intf .interfaceName , intf .netNSPath ))
300+ }
301+ return nodeLinks , nil
302+ }
281303
304+ // Fetch interfaces if discovery is disabled
305+ interfaces , err := getInterfaces (& attachInfo .InterfaceSelector , r .ourNode )
306+ if err != nil {
307+ return nil , fmt .Errorf ("failed to get interfaces for XdpProgram: %w" , err )
308+ }
309+
310+ // Handle network namespaces if provided
282311 if attachInfo .NetworkNamespaces != nil {
283- // There is a network namespace selector, so see if there are any
284- // matching network namespaces on this node.
285312 containerInfo , err := r .Containers .GetContainers (
286313 ctx ,
287314 attachInfo .NetworkNamespaces .Namespace ,
@@ -290,72 +317,27 @@ func (r *ClTcProgramReconciler) getExpectedLinks(ctx context.Context, attachInfo
290317 r .Logger ,
291318 )
292319 if err != nil {
293- return nil , fmt .Errorf ("failed to get container pids: %v " , err )
320+ return nil , fmt .Errorf ("failed to get container pids: %w " , err )
294321 }
295322
296- if containerInfo != nil {
297- // Just use one container per pod to get the pod's network
298- // namespace.
299- containerInfo = GetOneContainerPerPod (containerInfo )
300- for _ , container := range * containerInfo {
301- netnsPath := netnsPathFromPID (container .pid )
302- for _ , iface := range interfaces {
303- link := bpfmaniov1alpha1.ClTcAttachInfoState {
304- AttachInfoStateCommon : bpfmaniov1alpha1.AttachInfoStateCommon {
305- ShouldAttach : true ,
306- UUID : uuid .New ().String (),
307- LinkId : nil ,
308- LinkStatus : bpfmaniov1alpha1 .ApAttachNotAttached ,
309- },
310- InterfaceName : iface ,
311- NetnsPath : netnsPath ,
312- Priority : attachInfo .Priority ,
313- Direction : attachInfo .Direction ,
314- ProceedOn : attachInfo .ProceedOn ,
315- }
316- nodeLinks = append (nodeLinks , link )
317- }
318- }
319- } else {
320- // This is an error -- either namespacePath or pods must be set.
321- r .Logger .Error (fmt .Errorf ("neither namespacePath nor pods is set" ), "internal error" )
323+ if containerInfo == nil {
324+ r .Logger .Info ("NetworkNamespaces is configured but no matching container found" )
325+ return nodeLinks , nil
322326 }
323- } else {
324- for _ , iface := range interfaces {
325- netnsList := getInterfaceNetNsList (& attachInfo .InterfaceSelector , iface , r .Interfaces )
326- if len (netnsList ) == 0 {
327- link := bpfmaniov1alpha1.ClTcAttachInfoState {
328- AttachInfoStateCommon : bpfmaniov1alpha1.AttachInfoStateCommon {
329- ShouldAttach : true ,
330- UUID : uuid .New ().String (),
331- LinkId : nil ,
332- LinkStatus : bpfmaniov1alpha1 .ApAttachNotAttached ,
333- },
334- InterfaceName : iface ,
335- Priority : attachInfo .Priority ,
336- Direction : attachInfo .Direction ,
337- ProceedOn : attachInfo .ProceedOn ,
338- }
339- nodeLinks = append (nodeLinks , link )
340- } else {
341- for _ , netns := range netnsList [iface ] {
342- link := bpfmaniov1alpha1.ClTcAttachInfoState {
343- AttachInfoStateCommon : bpfmaniov1alpha1.AttachInfoStateCommon {
344- ShouldAttach : true ,
345- UUID : uuid .New ().String (),
346- LinkId : nil ,
347- LinkStatus : bpfmaniov1alpha1 .ApAttachNotAttached ,
348- },
349- InterfaceName : iface ,
350- Priority : attachInfo .Priority ,
351- Direction : attachInfo .Direction ,
352- ProceedOn : attachInfo .ProceedOn ,
353- NetnsPath : netns ,
354- }
355- nodeLinks = append (nodeLinks , link )
356- }
327+
328+ containerInfo = GetOneContainerPerPod (containerInfo )
329+ for _ , container := range * containerInfo {
330+ netnsPath := netnsPathFromPID (container .pid )
331+ for _ , iface := range interfaces {
332+ nodeLinks = append (nodeLinks , createLinkEntry (iface , netnsPath ))
357333 }
358334 }
335+ return nodeLinks , nil
336+ }
337+
338+ // Fallback: Assign interfaces without a namespace
339+ for _ , iface := range interfaces {
340+ nodeLinks = append (nodeLinks , createLinkEntry (iface , "" ))
359341 }
360342
361343 return nodeLinks , nil
0 commit comments