Skip to content

Commit 6e32713

Browse files
committed
[core] Implement LLDP retrieval and neighbor validation
The interface controller retrieves LLDP adjacency information from the provider and populates the Interface status. The ExpirationTime is computed once from TTL and preserved across reconciliations to avoid continuous status updates caused by clock drift between the device and operator. Neighbor validation is a non-blocking operation during reconciliation - errors are logged but don't prevent device configuration. Validation checks the label first (against another Interface k8s resource), then falls back to the annotation (user-defined string in format "<deviceID>::<portID>").
1 parent 02107a9 commit 6e32713

4 files changed

Lines changed: 299 additions & 3 deletions

File tree

‎internal/controller/core/interface_controller.go‎

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"net/netip"
1111
"slices"
12+
"strings"
1213
"time"
1314

1415
"k8s.io/apimachinery/pkg/api/equality"
@@ -506,6 +507,11 @@ func (r *InterfaceReconciler) reconcile(ctx context.Context, s *scope) (_ ctrl.R
506507
return ctrl.Result{}, fmt.Errorf("failed to get interface status: %w", err)
507508
}
508509

510+
// Neighbor adjacencies is only metadata and should not prevent receonciliation
511+
if err := r.updateNeighborAdjacenciesStatus(ctx, s, &status); err != nil {
512+
ctrl.LoggerFrom(ctx).V(3).Info("failed to update neighbor adjacency status", "error", err, "interface", klog.KObj(s.Interface))
513+
}
514+
509515
cond = metav1.Condition{
510516
Type: v1alpha1.OperationalCondition,
511517
Status: metav1.ConditionTrue,
@@ -525,6 +531,147 @@ func (r *InterfaceReconciler) reconcile(ctx context.Context, s *scope) (_ ctrl.R
525531
return ctrl.Result{RequeueAfter: Jitter(r.RequeueInterval)}, nil
526532
}
527533

534+
// updateNeighborAdjacenciesStatus is a noop when no neighbor info is available or interface is not physical.
535+
func (r *InterfaceReconciler) updateNeighborAdjacenciesStatus(ctx context.Context, s *scope, status *provider.InterfaceStatus) error {
536+
if s.Interface.Spec.Type != v1alpha1.InterfaceTypePhysical || len(status.LLDPAdjacencies) == 0 {
537+
s.Interface.Status.Neighbors = nil
538+
return nil
539+
}
540+
541+
type neighborKey struct{ ChassisID, PortID string }
542+
543+
// Build a map of existing neighbors keyed by ChassisID+PortID to preserve ExpirationTime
544+
existingNeighbors := make(map[neighborKey]v1alpha1.Neighbor)
545+
for _, n := range s.Interface.Status.Neighbors {
546+
existingNeighbors[neighborKey{n.ChassisID, n.PortID}] = n
547+
}
548+
549+
var errs []error
550+
neighbors := make([]v1alpha1.Neighbor, 0, len(status.LLDPAdjacencies))
551+
for _, adj := range status.LLDPAdjacencies {
552+
key := neighborKey{adj.ChassisID, adj.PortID}
553+
554+
adjacency := v1alpha1.Neighbor{
555+
NeighborInfo: v1alpha1.NeighborInfo{
556+
SystemName: adj.SysName,
557+
SystemDescription: adj.SysDescription,
558+
ChassisID: adj.ChassisID,
559+
ChassisIDType: adj.ChassisIDType,
560+
PortID: adj.PortID,
561+
PortIDType: adj.PortIDType,
562+
PortDescription: adj.PortDescription,
563+
},
564+
Validation: v1alpha1.NeighborValidationUndefined,
565+
}
566+
567+
// The TTL reported by the device and the operator clock can drift one from each other for more than one second.
568+
// If we use the TTL to compute ExpirationTime on each reconciliation, then ExpirationTime will keep changing, triggering uncessary updates to the Interface status.
569+
// Thus, we preserve the existing ExpirationTime as long as the neighbor is still valid, at the cost of some inaccuracy.
570+
if existing, found := existingNeighbors[key]; found && existing.ExpirationTime != nil && existing.ExpirationTime.After(time.Now()) {
571+
adjacency.ExpirationTime = existing.ExpirationTime
572+
} else {
573+
t := metav1.NewTime(time.Now().Add(time.Duration(adj.TTL) * time.Second).Truncate(time.Second))
574+
adjacency.ExpirationTime = &t
575+
}
576+
577+
if err := r.validateLLDPAdjacencyThroughLabel(ctx, s.Interface, &adjacency); err != nil {
578+
errs = append(errs, fmt.Errorf("failed to validate LLDP adjacency %q/%q through label: %w", adj.ChassisID, adj.PortID, err))
579+
}
580+
if adjacency.Validation == v1alpha1.NeighborValidationUndefined {
581+
if err := r.validateLLDPAdjacencyThroughAnnotation(ctx, s.Interface, &adjacency); err != nil {
582+
errs = append(errs, fmt.Errorf("failed to validate LLDP adjacency %q/%q through annotation: %w", adj.ChassisID, adj.PortID, err))
583+
}
584+
}
585+
neighbors = append(neighbors, adjacency)
586+
}
587+
s.Interface.Status.Neighbors = neighbors
588+
return kerrors.NewAggregate(errs)
589+
}
590+
591+
// validateLLDPAdjacencyThroughLabel attempts to validate the LLDP adjacency by looking for a neighbor reference label on the interface.
592+
func (r *InterfaceReconciler) validateLLDPAdjacencyThroughLabel(ctx context.Context, intf *v1alpha1.Interface, n *v1alpha1.Neighbor) error {
593+
neighborLabelValue, ok := intf.Labels[v1alpha1.PhysicalInterfaceNeighborLabel]
594+
if !ok {
595+
n.Validation = v1alpha1.NeighborValidationUndefined
596+
return nil
597+
}
598+
599+
key := client.ObjectKey{
600+
Name: neighborLabelValue,
601+
Namespace: intf.Namespace,
602+
}
603+
604+
remoteIntf := new(v1alpha1.Interface)
605+
if err := r.Get(ctx, key, remoteIntf); err != nil {
606+
n.Validation = v1alpha1.NeighborValidationError
607+
if !apierrors.IsNotFound(err) {
608+
return fmt.Errorf("failed to get neighbor interface %q: %w", neighborLabelValue, err)
609+
}
610+
return fmt.Errorf("could not verify neighbor adjacency: neighbor interface %q not found: %w", neighborLabelValue, err)
611+
}
612+
613+
log := ctrl.LoggerFrom(ctx, "LLDP validation", klog.KObj(intf))
614+
615+
remoteDevice, err := deviceutil.GetOwnerDevice(ctx, r, remoteIntf)
616+
if err != nil {
617+
n.Validation = v1alpha1.NeighborValidationError
618+
return fmt.Errorf("could not find the device owning interface %q: %w", remoteIntf.Name, err)
619+
}
620+
621+
if remoteDevice.Status.Hostname == "" {
622+
n.Validation = v1alpha1.NeighborValidationError
623+
return fmt.Errorf("the neighbor device does not have a hostname yet, cannot validate adjacency: neighborInterface=%q", remoteIntf.Name)
624+
}
625+
626+
if remoteDevice.Status.Hostname != n.SystemName {
627+
n.Validation = v1alpha1.NeighborDeviceMismatch
628+
log.V(0).Info("the neighbor device hostname does not match", "expected", n.SystemName, "actual", remoteDevice.Status.Hostname)
629+
return nil
630+
}
631+
632+
if remoteIntf.Spec.Name != n.PortID {
633+
n.Validation = v1alpha1.NeighborInterfaceMismatch
634+
log.V(0).Info("the neighbor interface name does not match", "expected", n.PortID, "actual", remoteIntf.Spec.Name)
635+
return nil
636+
}
637+
638+
// TODO: for NXOS we use the shortened version of the interface name, while LLDP reports the full name.
639+
640+
n.Validation = v1alpha1.NeighborVerified
641+
642+
return nil
643+
}
644+
645+
func (r *InterfaceReconciler) validateLLDPAdjacencyThroughAnnotation(ctx context.Context, intf *v1alpha1.Interface, n *v1alpha1.Neighbor) error {
646+
neighborAnnotationValue, ok := intf.Annotations[v1alpha1.PhysicalInterfaceNeighborRawAnnotation]
647+
if !ok {
648+
n.Validation = v1alpha1.NeighborValidationUndefined
649+
return nil
650+
}
651+
652+
remoteDeviceID, remotePortID, ok := strings.Cut(neighborAnnotationValue, "::")
653+
if !ok || remoteDeviceID == "" || remotePortID == "" {
654+
n.Validation = v1alpha1.NeighborValidationError
655+
return fmt.Errorf("invalid neighbor annotation value %q, expected format is <deviceIdentifier>::<portID>", neighborAnnotationValue)
656+
}
657+
658+
log := ctrl.LoggerFrom(ctx, "LLDP validation", klog.KObj(intf))
659+
if remoteDeviceID != n.ChassisID && remoteDeviceID != n.SystemName {
660+
n.Validation = v1alpha1.NeighborDeviceMismatch
661+
log.V(0).Info("the neighbor device identifier does not match", "annotationValue", remoteDeviceID, "chassisID", n.ChassisID, "systemName", n.SystemName)
662+
return nil
663+
}
664+
665+
if remotePortID != n.PortID {
666+
n.Validation = v1alpha1.NeighborInterfaceMismatch
667+
log.V(0).Info("the neighbor port identifier does not match", "annotationValue", remotePortID, "portID", n.PortID)
668+
return nil
669+
}
670+
671+
n.Validation = v1alpha1.NeighborVerified
672+
return nil
673+
}
674+
528675
func (r *InterfaceReconciler) reconcileIPv4(ctx context.Context, s *scope) (provider.IPv4, error) {
529676
switch {
530677
case len(s.Interface.Spec.IPv4.Addresses) > 0:
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and IronCore contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package core
5+
6+
import (
7+
. "github.com/onsi/ginkgo/v2"
8+
. "github.com/onsi/gomega"
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
"sigs.k8s.io/controller-runtime/pkg/client"
11+
12+
"github.com/ironcore-dev/network-operator/api/core/v1alpha1"
13+
)
14+
15+
var _ = Describe("Interface Controller - LLDP Neighbor", func() {
16+
Context("When reconciling an interface with LLDP neighbor information", func() {
17+
var (
18+
name string
19+
key client.ObjectKey
20+
)
21+
22+
BeforeEach(func() {
23+
By("Creating a Device resource for testing")
24+
device := &v1alpha1.Device{
25+
ObjectMeta: metav1.ObjectMeta{
26+
GenerateName: "test-interface-lldp-",
27+
Namespace: metav1.NamespaceDefault,
28+
},
29+
Spec: v1alpha1.DeviceSpec{
30+
Endpoint: v1alpha1.Endpoint{
31+
Address: "192.168.10.2:9339",
32+
},
33+
},
34+
}
35+
Expect(k8sClient.Create(ctx, device)).To(Succeed())
36+
name = device.Name
37+
key = client.ObjectKey{Name: name, Namespace: metav1.NamespaceDefault}
38+
})
39+
40+
AfterEach(func() {
41+
By("Cleaning up all Interface resources")
42+
Expect(k8sClient.DeleteAllOf(ctx, &v1alpha1.Interface{}, client.InNamespace(metav1.NamespaceDefault))).To(Succeed())
43+
44+
device := &v1alpha1.Device{}
45+
err := k8sClient.Get(ctx, key, device)
46+
Expect(err).NotTo(HaveOccurred())
47+
48+
By("Cleaning up the test Device resource")
49+
Expect(k8sClient.Delete(ctx, device, client.PropagationPolicy(metav1.DeletePropagationForeground))).To(Succeed())
50+
})
51+
52+
It("Should populate LLDP neighbor information in interface status", func() {
53+
By("Configuring the test provider to return LLDP neighbor information")
54+
testProvider.SetLLDPNeighbor("Ethernet1/1", "neighbor-switch", "00:11:22:33:44:55", "Ethernet1/1", 120)
55+
56+
By("Creating a Physical Interface resource")
57+
intf := &v1alpha1.Interface{
58+
ObjectMeta: metav1.ObjectMeta{
59+
Name: name,
60+
Namespace: metav1.NamespaceDefault,
61+
},
62+
Spec: v1alpha1.InterfaceSpec{
63+
DeviceRef: v1alpha1.LocalObjectReference{Name: name},
64+
Name: "Ethernet1/1",
65+
AdminState: v1alpha1.AdminStateUp,
66+
Type: v1alpha1.InterfaceTypePhysical,
67+
},
68+
}
69+
Expect(k8sClient.Create(ctx, intf)).To(Succeed())
70+
71+
By("Verifying the controller populates the LLDP neighbor information in status")
72+
Eventually(func(g Gomega) {
73+
resource := &v1alpha1.Interface{}
74+
g.Expect(k8sClient.Get(ctx, key, resource)).To(Succeed())
75+
g.Expect(resource.Status.Neighbors).NotTo(BeEmpty())
76+
g.Expect(resource.Status.Neighbors[0].SystemName).To(Equal("neighbor-switch"))
77+
g.Expect(resource.Status.Neighbors[0].ChassisID).To(Equal("00:11:22:33:44:55"))
78+
g.Expect(resource.Status.Neighbors[0].PortID).To(Equal("Ethernet1/1"))
79+
g.Expect(resource.Status.Neighbors[0].ExpirationTime).NotTo(BeNil())
80+
}).Should(Succeed())
81+
})
82+
83+
It("Should handle interface without LLDP neighbor", func() {
84+
By("Creating a Physical Interface resource without LLDP neighbor configured")
85+
intf := &v1alpha1.Interface{
86+
ObjectMeta: metav1.ObjectMeta{
87+
Name: name,
88+
Namespace: metav1.NamespaceDefault,
89+
},
90+
Spec: v1alpha1.InterfaceSpec{
91+
DeviceRef: v1alpha1.LocalObjectReference{Name: name},
92+
Name: "Ethernet1/2",
93+
AdminState: v1alpha1.AdminStateUp,
94+
Type: v1alpha1.InterfaceTypePhysical,
95+
},
96+
}
97+
Expect(k8sClient.Create(ctx, intf)).To(Succeed())
98+
99+
By("Verifying the neighbor field is nil when no LLDP neighbor exists")
100+
Eventually(func(g Gomega) {
101+
resource := &v1alpha1.Interface{}
102+
g.Expect(k8sClient.Get(ctx, key, resource)).To(Succeed())
103+
g.Expect(resource.Status.Conditions).NotTo(BeEmpty())
104+
// Neighbors should be empty when no LLDP neighbor exists
105+
if len(resource.Status.Neighbors) > 0 {
106+
g.Expect(resource.Status.Neighbors[0].SystemName).To(BeEmpty())
107+
}
108+
}).Should(Succeed())
109+
})
110+
})
111+
})

‎internal/controller/core/suite_test.go‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ type Provider struct {
440440
RoutingPolicies sets.Set[string]
441441
NVE *v1alpha1.NetworkVirtualizationEdge
442442
LLDP *v1alpha1.LLDP
443+
LLDPNeighbors map[string]*provider.LLDPAdjacency
443444
DHCPRelay *v1alpha1.DHCPRelay
444445
}
445446

@@ -457,6 +458,7 @@ func NewProvider() *Provider {
457458
EVIs: sets.New[int32](),
458459
PrefixSets: sets.New[string](),
459460
RoutingPolicies: sets.New[string](),
461+
LLDPNeighbors: make(map[string]*provider.LLDPAdjacency),
460462
}
461463
}
462464

@@ -519,10 +521,20 @@ func (p *Provider) DeleteInterface(_ context.Context, req *provider.InterfaceReq
519521
return nil
520522
}
521523

522-
func (p *Provider) GetInterfaceStatus(context.Context, *provider.InterfaceRequest) (provider.InterfaceStatus, error) {
523-
return provider.InterfaceStatus{
524+
func (p *Provider) GetInterfaceStatus(_ context.Context, req *provider.InterfaceRequest) (provider.InterfaceStatus, error) {
525+
p.Lock()
526+
defer p.Unlock()
527+
528+
status := provider.InterfaceStatus{
524529
OperStatus: true,
525-
}, nil
530+
}
531+
532+
// Check if there's LLDP neighbor info for this interface
533+
if neighbor, ok := p.LLDPNeighbors[req.Interface.Spec.Name]; ok {
534+
status.LLDPAdjacencies = []provider.LLDPAdjacency{*neighbor}
535+
}
536+
537+
return status, nil
526538
}
527539

528540
func (p *Provider) EnsureBanner(_ context.Context, req *provider.EnsureBannerRequest) error {
@@ -904,3 +916,15 @@ func (p *Provider) GetDHCPRelayStatus(_ context.Context, req *provider.DHCPRelay
904916
}
905917
return status, nil
906918
}
919+
920+
// SetLLDPNeighbor is a test helper to configure LLDP neighbor information for an interface.
921+
func (p *Provider) SetLLDPNeighbor(interfaceName, sysName, chassisID, portID string, ttl uint32) {
922+
p.Lock()
923+
defer p.Unlock()
924+
p.LLDPNeighbors[interfaceName] = &provider.LLDPAdjacency{
925+
SysName: sysName,
926+
ChassisID: chassisID,
927+
PortID: portID,
928+
TTL: ttl,
929+
}
930+
}

‎internal/provider/provider.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@ type InterfaceStatus struct {
136136
OperStatus bool
137137
// OperMessage provides additional information about the operational status of the interface.
138138
OperMessage string
139+
// LLDPAdjacencies provides information about the directly connected neighbors on this interface, if available.
140+
LLDPAdjacencies []LLDPAdjacency
141+
}
142+
143+
// LLDPAdjacencyInfo represents information about a directly connected neighbor on an interface, as discovered through LLDP.
144+
type LLDPAdjacency struct {
145+
SysName string
146+
SysDescription string
147+
ChassisID string
148+
ChassisIDType uint8
149+
PortID string
150+
PortIDType uint8
151+
PortDescription string
152+
TTL uint32
139153
}
140154

141155
// BannerProvider is the interface for the realization of the Banner objects over different providers.

0 commit comments

Comments
 (0)