From 652a65a5d225890438386553b64db6d1b41a0cfc Mon Sep 17 00:00:00 2001 From: Boris Glimcher Date: Wed, 30 Aug 2023 06:16:54 +0300 Subject: [PATCH] feat: decouple linux devname from object resource id in get and update Signed-off-by: Boris Glimcher --- pkg/evpn/bridge.go | 34 ++++++++++++++++++---------------- pkg/evpn/svi.go | 26 ++++++++++++++++++++------ 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/pkg/evpn/bridge.go b/pkg/evpn/bridge.go index 3e14f996..17571376 100644 --- a/pkg/evpn/bridge.go +++ b/pkg/evpn/bridge.go @@ -11,7 +11,6 @@ import ( "fmt" "log" "net" - "path" "github.com/vishvananda/netlink" @@ -184,18 +183,21 @@ func (s *Server) UpdateLogicalBridge(_ context.Context, in *pb.UpdateLogicalBrid log.Printf("error: %v", err) return nil, err } - resourceID := path.Base(bridge.Name) - iface, err := s.nLink.LinkByName(resourceID) - if err != nil { - err := status.Errorf(codes.NotFound, "unable to find key %s", resourceID) - log.Printf("error: %v", err) - return nil, err - } - // base := iface.Attrs() - // iface.MTU = 1500 // TODO: remove this, just an example - if err := s.nLink.LinkModify(iface); err != nil { - fmt.Printf("Failed to update link: %v", err) - return nil, err + // only if VNI is not empty + if bridge.Spec.Vni != nil { + vxlanName := fmt.Sprintf("vni%d", *bridge.Spec.Vni) + iface, err := s.nLink.LinkByName(vxlanName) + if err != nil { + err := status.Errorf(codes.NotFound, "unable to find key %s", vxlanName) + log.Printf("error: %v", err) + return nil, err + } + // base := iface.Attrs() + // iface.MTU = 1500 // TODO: remove this, just an example + if err := s.nLink.LinkModify(iface); err != nil { + fmt.Printf("Failed to update link: %v", err) + return nil, err + } } response := proto.Clone(in.LogicalBridge).(*pb.LogicalBridge) response.Status = &pb.LogicalBridgeStatus{OperStatus: pb.LBOperStatus_LB_OPER_STATUS_UP} @@ -224,12 +226,12 @@ func (s *Server) GetLogicalBridge(_ context.Context, in *pb.GetLogicalBridgeRequ log.Printf("error: %v", err) return nil, err } - resourceID := path.Base(bridge.Name) // only if VNI is not empty if bridge.Spec.Vni != nil { - _, err := s.nLink.LinkByName(resourceID) + vxlanName := fmt.Sprintf("vni%d", *bridge.Spec.Vni) + _, err := s.nLink.LinkByName(vxlanName) if err != nil { - err := status.Errorf(codes.NotFound, "unable to find key %s", resourceID) + err := status.Errorf(codes.NotFound, "unable to find key %s", vxlanName) log.Printf("error: %v", err) return nil, err } diff --git a/pkg/evpn/svi.go b/pkg/evpn/svi.go index 65dc8ad0..8b97c335 100644 --- a/pkg/evpn/svi.go +++ b/pkg/evpn/svi.go @@ -232,10 +232,17 @@ func (s *Server) UpdateSvi(_ context.Context, in *pb.UpdateSviRequest) (*pb.Svi, log.Printf("error: %v", err) return nil, err } - resourceID := path.Base(svi.Name) - iface, err := s.nLink.LinkByName(resourceID) + // use netlink to find VlanId from LogicalBridge object + bridgeObject, ok := s.Bridges[svi.Spec.LogicalBridge] + if !ok { + err := status.Errorf(codes.NotFound, "unable to find key %s", svi.Spec.LogicalBridge) + log.Printf("error: %v", err) + return nil, err + } + vlanName := fmt.Sprintf("vlan%d", bridgeObject.Spec.VlanId) + iface, err := s.nLink.LinkByName(vlanName) if err != nil { - err := status.Errorf(codes.NotFound, "unable to find key %s", resourceID) + err := status.Errorf(codes.NotFound, "unable to find key %s", vlanName) log.Printf("error: %v", err) return nil, err } @@ -272,10 +279,17 @@ func (s *Server) GetSvi(_ context.Context, in *pb.GetSviRequest) (*pb.Svi, error log.Printf("error: %v", err) return nil, err } - resourceID := path.Base(obj.Name) - _, err := s.nLink.LinkByName(resourceID) + // use netlink to find VlanId from LogicalBridge object + bridgeObject, ok := s.Bridges[obj.Spec.LogicalBridge] + if !ok { + err := status.Errorf(codes.NotFound, "unable to find key %s", obj.Spec.LogicalBridge) + log.Printf("error: %v", err) + return nil, err + } + vlanName := fmt.Sprintf("vlan%d", bridgeObject.Spec.VlanId) + _, err := s.nLink.LinkByName(vlanName) if err != nil { - err := status.Errorf(codes.NotFound, "unable to find key %s", resourceID) + err := status.Errorf(codes.NotFound, "unable to find key %s", vlanName) log.Printf("error: %v", err) return nil, err }