diff --git a/cmd/liqonet/route-operator.go b/cmd/liqonet/route-operator.go index 953f1db894..e768a6a607 100644 --- a/cmd/liqonet/route-operator.go +++ b/cmd/liqonet/route-operator.go @@ -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) @@ -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) diff --git a/internal/liqonet/route-operator/overlayOperator.go b/internal/liqonet/route-operator/overlayOperator.go index ca163297ed..7ba9ae7218 100644 --- a/internal/liqonet/route-operator/overlayOperator.go +++ b/internal/liqonet/route-operator/overlayOperator.go @@ -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. @@ -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) @@ -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{ @@ -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, @@ -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. diff --git a/internal/liqonet/route-operator/overlayOperator_test.go b/internal/liqonet/route-operator/overlayOperator_test.go index 25edf8adac..cf6f4d44ef 100644 --- a/internal/liqonet/route-operator/overlayOperator_test.go +++ b/internal/liqonet/route-operator/overlayOperator_test.go @@ -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 @@ -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, @@ -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()) }) diff --git a/pkg/liqonet/utils/utils.go b/pkg/liqonet/utils/utils.go index 9c00cbb513..5ca6633143 100644 --- a/pkg/liqonet/utils/utils.go +++ b/pkg/liqonet/utils/utils.go @@ -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")