Skip to content

Commit dd9c974

Browse files
authored
Merge pull request #424 from msherif1234/netns-followup
split out interface discovery processing
2 parents 95e4b42 + 8057194 commit dd9c974

File tree

202 files changed

+14702
-3647
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

202 files changed

+14702
-3647
lines changed

config/bpfman-deployment/daemonset.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ spec:
104104
- mountPath: /host/netns
105105
name: host-netns
106106
mountPropagation: HostToContainer
107+
- mountPath: /var/run/netns
108+
name: var-run-netns
109+
mountPropagation: HostToContainer
107110
# The bpfman.toml config file
108111
- name: bpfman-config
109112
mountPath: /etc/bpfman/bpfman.toml
@@ -144,6 +147,9 @@ spec:
144147
name: host-dockerd
145148
- mountPath: /etc/crictl.yaml
146149
name: host-crictl-config
150+
- mountPath: /var/run/netns
151+
name: var-run-netns
152+
mountPropagation: HostToContainer
147153
- name: node-driver-registrar
148154
image: quay.io/bpfman/csi-node-driver-registrar:v2.13.0
149155
imagePullPolicy: IfNotPresent
@@ -218,6 +224,9 @@ spec:
218224
- hostPath:
219225
path: /run/netns
220226
name: host-netns
227+
- hostPath:
228+
path: /var/run/netns
229+
name: var-run-netns
221230
- hostPath:
222231
path: /run/containerd/containerd.sock
223232
name: host-containerd

controllers/bpfman-agent/cl_tc_program.go

Lines changed: 51 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -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

controllers/bpfman-agent/cl_tcx_program.go

Lines changed: 50 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -234,17 +234,44 @@ func (r *ClTcxProgramReconciler) removeLinks(links []bpfmaniov1alpha1.ClTcxAttac
234234

235235
// getExpectedLinks expands *AttachInfo into a list of specific attach
236236
// points.
237-
func (r *ClTcxProgramReconciler) getExpectedLinks(ctx context.Context, attachInfo bpfmaniov1alpha1.ClTcxAttachInfo,
238-
) ([]bpfmaniov1alpha1.ClTcxAttachInfoState, error) {
239-
interfaces, err := getInterfaces(&attachInfo.InterfaceSelector, r.ourNode, r.Interfaces)
240-
if err != nil {
241-
return nil, fmt.Errorf("failed to get interfaces for TcxProgram: %v", err)
237+
func (r *ClTcxProgramReconciler) getExpectedLinks(ctx context.Context, attachInfo bpfmaniov1alpha1.ClTcxAttachInfo) ([]bpfmaniov1alpha1.ClTcxAttachInfoState, error) {
238+
nodeLinks := []bpfmaniov1alpha1.ClTcxAttachInfoState{}
239+
// Helper function to create a ClTcxAttachInfoState entry
240+
createLinkEntry := func(interfaceName, netnsPath string) bpfmaniov1alpha1.ClTcxAttachInfoState {
241+
return bpfmaniov1alpha1.ClTcxAttachInfoState{
242+
AttachInfoStateCommon: bpfmaniov1alpha1.AttachInfoStateCommon{
243+
ShouldAttach: true,
244+
UUID: uuid.New().String(),
245+
LinkId: nil,
246+
LinkStatus: bpfmaniov1alpha1.ApAttachNotAttached,
247+
},
248+
InterfaceName: interfaceName,
249+
NetnsPath: netnsPath,
250+
Priority: attachInfo.Priority,
251+
Direction: attachInfo.Direction,
252+
}
242253
}
243254

244-
nodeLinks := []bpfmaniov1alpha1.ClTcxAttachInfoState{}
255+
// Handle interface discovery
256+
if isInterfacesDiscoveryEnabled(&attachInfo.InterfaceSelector) {
257+
discoveredInterfaces, err := getDiscoveredInterfaces(&attachInfo.InterfaceSelector, r.Interfaces)
258+
if err != nil {
259+
return nil, fmt.Errorf("failed to discover interfaces: %w", err)
260+
}
261+
for _, intf := range discoveredInterfaces {
262+
nodeLinks = append(nodeLinks, createLinkEntry(intf.interfaceName, intf.netNSPath))
263+
}
264+
return nodeLinks, nil
265+
}
245266

267+
// Fetch interfaces if discovery is disabled
268+
interfaces, err := getInterfaces(&attachInfo.InterfaceSelector, r.ourNode)
269+
if err != nil {
270+
return nil, fmt.Errorf("failed to get interfaces for XdpProgram: %w", err)
271+
}
272+
273+
// Handle network namespaces if provided
246274
if attachInfo.NetworkNamespaces != nil {
247-
// If the pod selector is set, we use the pod selector.
248275
containerInfo, err := r.Containers.GetContainers(
249276
ctx,
250277
attachInfo.NetworkNamespaces.Namespace,
@@ -253,69 +280,27 @@ func (r *ClTcxProgramReconciler) getExpectedLinks(ctx context.Context, attachInf
253280
r.Logger,
254281
)
255282
if err != nil {
256-
return nil, fmt.Errorf("failed to get container pids: %v", err)
283+
return nil, fmt.Errorf("failed to get container pids: %w", err)
257284
}
258285

259-
if containerInfo != nil {
260-
// Just use one container per pod to get the pod's network
261-
// namespace.
262-
containerInfo = GetOneContainerPerPod(containerInfo)
263-
for _, container := range *containerInfo {
264-
netnsPath := netnsPathFromPID(container.pid)
265-
for _, iface := range interfaces {
266-
link := bpfmaniov1alpha1.ClTcxAttachInfoState{
267-
AttachInfoStateCommon: bpfmaniov1alpha1.AttachInfoStateCommon{
268-
ShouldAttach: true,
269-
UUID: uuid.New().String(),
270-
LinkId: nil,
271-
LinkStatus: bpfmaniov1alpha1.ApAttachNotAttached,
272-
},
273-
InterfaceName: iface,
274-
NetnsPath: netnsPath,
275-
Priority: attachInfo.Priority,
276-
Direction: attachInfo.Direction,
277-
}
278-
nodeLinks = append(nodeLinks, link)
279-
}
280-
}
281-
} else {
282-
// This is an error -- either namespacePath or pods must be set.
283-
r.Logger.Error(fmt.Errorf("neither namespacePath nor pods is set"), "internal error")
286+
if containerInfo == nil {
287+
r.Logger.Info("NetworkNamespaces is configured but no matching container found")
288+
return nodeLinks, nil
284289
}
285-
} else {
286-
for _, iface := range interfaces {
287-
netnsList := getInterfaceNetNsList(&attachInfo.InterfaceSelector, iface, r.Interfaces)
288-
if len(netnsList) == 0 {
289-
link := bpfmaniov1alpha1.ClTcxAttachInfoState{
290-
AttachInfoStateCommon: bpfmaniov1alpha1.AttachInfoStateCommon{
291-
ShouldAttach: true,
292-
UUID: uuid.New().String(),
293-
LinkId: nil,
294-
LinkStatus: bpfmaniov1alpha1.ApAttachNotAttached,
295-
},
296-
InterfaceName: iface,
297-
Priority: attachInfo.Priority,
298-
Direction: attachInfo.Direction,
299-
}
300-
nodeLinks = append(nodeLinks, link)
301-
} else {
302-
for _, netns := range netnsList[iface] {
303-
link := bpfmaniov1alpha1.ClTcxAttachInfoState{
304-
AttachInfoStateCommon: bpfmaniov1alpha1.AttachInfoStateCommon{
305-
ShouldAttach: true,
306-
UUID: uuid.New().String(),
307-
LinkId: nil,
308-
LinkStatus: bpfmaniov1alpha1.ApAttachNotAttached,
309-
},
310-
InterfaceName: iface,
311-
Priority: attachInfo.Priority,
312-
Direction: attachInfo.Direction,
313-
NetnsPath: netns,
314-
}
315-
nodeLinks = append(nodeLinks, link)
316-
}
290+
291+
containerInfo = GetOneContainerPerPod(containerInfo)
292+
for _, container := range *containerInfo {
293+
netnsPath := netnsPathFromPID(container.pid)
294+
for _, iface := range interfaces {
295+
nodeLinks = append(nodeLinks, createLinkEntry(iface, netnsPath))
317296
}
318297
}
298+
return nodeLinks, nil
299+
}
300+
301+
// Fallback: Assign interfaces without a namespace
302+
for _, iface := range interfaces {
303+
nodeLinks = append(nodeLinks, createLinkEntry(iface, ""))
319304
}
320305

321306
return nodeLinks, nil

0 commit comments

Comments
 (0)