Skip to content

Commit

Permalink
liqo-route: mac annotation fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cheina97 authored and adamjensenbot committed Sep 13, 2023
1 parent 3ef94da commit 35bf78a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
7 changes: 6 additions & 1 deletion cmd/liqonet/route-operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ func runRouteOperator(commonFlags *liqonetCommonFlags, routeFlags *routeOperator
klog.Errorf("unable to get podIP: %v", err)
os.Exit(1)
}
podName, err := liqonetutils.GetPodName()
if err != nil {
klog.Errorf("unable to get pod name: %v", err)
os.Exit(1)
}
nodeName, err := liqonetutils.GetNodeName()
if err != nil {
klog.Errorf("unable to get node name: %v", err)
Expand Down Expand Up @@ -170,7 +175,7 @@ func runRouteOperator(commonFlags *liqonetCommonFlags, routeFlags *routeOperator
klog.Errorf("unable to start go routine that configures firewall rules for the route controller: %v", err)
os.Exit(1)
}
overlayController, err := routeoperator.NewOverlayController(podIP.String(), vxlanDevice, mutex, nodeMap, overlayMgr.GetClient())
overlayController, err := routeoperator.NewOverlayController(podName, vxlanDevice, mutex, nodeMap, overlayMgr.GetClient())
if err != nil {
klog.Errorf("an error occurred while creating overlay controller: %v", err)
os.Exit(3)
Expand Down
16 changes: 11 additions & 5 deletions internal/liqonet/route-operator/overlayOperator.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var (
type OverlayController struct {
client.Client
vxlanDev *overlay.VxlanDevice
podIP string
podName string
nodesLock *sync.RWMutex
vxlanPeers map[string]*overlay.Neighbor
// For each nodeName contains its IP addr.
Expand Down Expand Up @@ -75,8 +75,14 @@ func (ovc *OverlayController) Reconcile(ctx context.Context, req ctrl.Request) (
}
return ctrl.Result{}, nil
}

// If the pod doesn't have an IP address yet, then we don't need to do anything.
if pod.Status.PodIP == "" {
return ctrl.Result{}, nil
}

// If it is our pod than add the mac address annotation.
if ovc.podIP == pod.Status.PodIP {
if ovc.podName == pod.Name {
if liqonetutils.AddAnnotationToObj(&pod, vxlanMACAddressKey, ovc.vxlanDev.Link.HardwareAddr.String()) {
if err := ovc.Update(ctx, &pod); err != nil {
klog.Errorf("an error occurred while adding mac address annotation to pod {%s}: %v", req.String(), err)
Expand All @@ -102,7 +108,7 @@ func (ovc *OverlayController) Reconcile(ctx context.Context, req ctrl.Request) (
}

// NewOverlayController returns a new controller ready to be setup and started with the controller manager.
func NewOverlayController(podIP string, vxlanDevice *overlay.VxlanDevice, nodesLock *sync.RWMutex,
func NewOverlayController(podName string, vxlanDevice *overlay.VxlanDevice, nodesLock *sync.RWMutex,
vxlanNodes map[string]string, cl client.Client) (*OverlayController, error) {
if vxlanDevice == nil {
return nil, &liqoerrors.WrongParameter{
Expand All @@ -113,7 +119,7 @@ func NewOverlayController(podIP string, vxlanDevice *overlay.VxlanDevice, nodesL
return &OverlayController{
Client: cl,
vxlanDev: vxlanDevice,
podIP: podIP,
podName: podName,
nodesLock: nodesLock,
vxlanPeers: map[string]*overlay.Neighbor{},
vxlanNodes: vxlanNodes,
Expand Down Expand Up @@ -217,7 +223,7 @@ func (ovc *OverlayController) podFilter(obj client.Object) bool {
return false
}
// If it is our pod then process it.
if ovc.podIP == p.Status.PodIP {
if ovc.podName == p.Name {
return true
}
// If it is not our pod then check if the vxlan mac address has been set.
Expand Down
26 changes: 14 additions & 12 deletions internal/liqonet/route-operator/overlayOperator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ import (
)

var (
overlayPodIP = "10.0.0.1"
overlayAnnKey = vxlanMACAddressKey
overlayAnnValue = "45:d0:ae:c9:d6:40"
overlayPeerIP = "10.11.1.1"
overlayPeerMAC = "4e:d0:ae:c9:d6:30"
overlayNamespace = "overlay-namespace"
overlayPodName = "overlay-test-pod"
overlayPodIP = "10.0.0.1"
overlayAnnKey = vxlanMACAddressKey
overlayAnnValue = "45:d0:ae:c9:d6:40"
overlayPeerIP = "10.11.1.1"
overlayPeerMAC = "4e:d0:ae:c9:d6:30"
overlayNamespace = "overlay-namespace"
overlayPodName = "overlay-test-pod"
overlayPodNameWrong = "overlay-test-pod-wrong"

overlayTestPod *corev1.Pod
overlayReq ctrl.Request
Expand Down Expand Up @@ -93,7 +94,7 @@ var _ = Describe("OverlayOperator", func() {
}
// Create dummy overlay operator.
ovc = &OverlayController{
podIP: overlayPodIP,
podName: overlayPodName,
vxlanPeers: make(map[string]*overlay.Neighbor),
vxlanDev: vxlanDevice,
Client: k8sClient,
Expand Down Expand Up @@ -339,20 +340,21 @@ var _ = Describe("OverlayOperator", func() {
})

Context("when object is a pod", func() {
It("and has same ip, should return true", func() {
It("and has same name, should return true", func() {
// Add ip address to the test pod.
overlayTestPod.Status.PodIP = overlayPodIP
ok := ovc.podFilter(overlayTestPod)
Expect(ok).Should(BeTrue())
})

It("has not the same ip and has not been annotated, should return false", func() {
It("has not the same name and has not been annotated, should return false", func() {
overlayTestPod.SetAnnotations(nil)
overlayTestPod.Name = overlayPodNameWrong
ok := ovc.podFilter(overlayTestPod)
Expect(ok).Should(BeFalse())
})

It("has not the same ip and has been annotated, should return true", func() {
It("has not the same name and has been annotated, should return true", func() {
overlayTestPod.Name = overlayPodNameWrong
ok := ovc.podFilter(overlayTestPod)
Expect(ok).Should(BeTrue())
})
Expand Down
9 changes: 9 additions & 0 deletions pkg/liqonet/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ func MapIPToNetwork(newNetwork, oldIP string) (newIP string, err error) {
return
}

// GetPodName returns the pod name.
func GetPodName() (string, error) {
podName, isSet := os.LookupEnv("POD_NAME")
if !isSet || podName == "" {
return "", errors.New("pod name is not yet set")
}
return podName, nil
}

// GetPodIP returns the pod IP address.
func GetPodIP() (net.IP, error) {
ipAddress, isSet := os.LookupEnv("POD_IP")
Expand Down

0 comments on commit 35bf78a

Please sign in to comment.