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+
528675func (r * InterfaceReconciler ) reconcileIPv4 (ctx context.Context , s * scope ) (provider.IPv4 , error ) {
529676 switch {
530677 case len (s .Interface .Spec .IPv4 .Addresses ) > 0 :
0 commit comments