From 1f518d9f2617d18e495697e2d8b0e72d2d8af41f Mon Sep 17 00:00:00 2001 From: changluyi <47097611+changluyi@users.noreply.github.com> Date: Fri, 27 Dec 2024 14:35:45 +0800 Subject: [PATCH] pod should use mac and ips provider by multus firstly (#4800) (#4875) * pod should use mac and ips provider by multus firstly --------- Signed-off-by: clyi --- pkg/controller/pod.go | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/pkg/controller/pod.go b/pkg/controller/pod.go index 8188d82c592..0509ce3a3a5 100644 --- a/pkg/controller/pod.go +++ b/pkg/controller/pod.go @@ -1151,11 +1151,21 @@ func (c *Controller) syncKubeOvnNet(pod *v1.Pod, podNets []*kubeovnNet) (*v1.Pod targetPortNameList := strset.NewWithSize(len(podNets)) portsNeedToDel := []string{} annotationsNeedToDel := []string{} + annotationsNeedToAdd := make(map[string]string) subnetUsedByPort := make(map[string]string) for _, podNet := range podNets { portName := ovs.PodNameToPortName(podName, pod.Namespace, podNet.ProviderName) targetPortNameList.Add(portName) + if podNet.IPRequest != "" { + klog.Infof("pod %s/%s use custom IP %s for provider %s", pod.Namespace, pod.Name, podNet.IPRequest, podNet.ProviderName) + annotationsNeedToAdd[fmt.Sprintf(util.IPAddressAnnotationTemplate, podNet.ProviderName)] = podNet.IPRequest + } + + if podNet.MacRequest != "" { + klog.Infof("pod %s/%s use custom MAC %s for provider %s", pod.Namespace, pod.Name, podNet.MacRequest, podNet.ProviderName) + annotationsNeedToAdd[fmt.Sprintf(util.MacAddressAnnotationTemplate, podNet.ProviderName)] = podNet.MacRequest + } } ports, err := c.OVNNbClient.ListNormalLogicalSwitchPorts(true, map[string]string{"pod": key}) @@ -1177,7 +1187,7 @@ func (c *Controller) syncKubeOvnNet(pod *v1.Pod, podNets []*kubeovnNet) (*v1.Pod } } - if len(portsNeedToDel) == 0 { + if len(portsNeedToDel) == 0 && len(annotationsNeedToAdd) == 0 { return pod, nil } @@ -1206,6 +1216,11 @@ func (c *Controller) syncKubeOvnNet(pod *v1.Pod, podNets []*kubeovnNet) (*v1.Pod } } } + + for key, value := range annotationsNeedToAdd { + patch[key] = value + } + if len(patch) == 0 { return pod, nil } @@ -1461,6 +1476,8 @@ type kubeovnNet struct { Subnet *kubeovnv1.Subnet IsDefault bool AllowLiveMigration bool + IPRequest string + MacRequest string } func (c *Controller) getPodAttachmentNet(pod *v1.Pod) ([]*kubeovnNet, error) { @@ -1538,13 +1555,21 @@ func (c *Controller) getPodAttachmentNet(pod *v1.Pod) ([]*kubeovnNet, error) { return nil, err } } - result = append(result, &kubeovnNet{ + + ret := &kubeovnNet{ Type: providerTypeOriginal, ProviderName: providerName, Subnet: subnet, IsDefault: isDefault, AllowLiveMigration: allowLiveMigration, - }) + } + + if len(attach.IPRequest) != 0 { + ret.IPRequest = strings.Join(attach.IPRequest, ",") + } + + ret.MacRequest = attach.MacRequest + result = append(result, ret) } else { providerName = fmt.Sprintf("%s.%s", attach.Name, attach.Namespace) for _, subnet := range subnets {