From 7ac7dc74e769a0c7b147a12a6e42d7bbe07f9ccd Mon Sep 17 00:00:00 2001 From: wujixin Date: Wed, 12 Jul 2023 10:52:51 +0800 Subject: [PATCH] feat: suport kubevirt nic hotplug Signed-off-by: wujixin --- pkg/controller/pod.go | 15 +++++++++++++++ pkg/daemon/ovs_linux.go | 5 +++++ 2 files changed, 20 insertions(+) diff --git a/pkg/controller/pod.go b/pkg/controller/pod.go index d917052a34e..40d247c4360 100644 --- a/pkg/controller/pod.go +++ b/pkg/controller/pod.go @@ -1058,6 +1058,7 @@ func (c *Controller) syncKubeOvnNet(pod *v1.Pod, podNets []*kubeovnNet) error { key := fmt.Sprintf("%s/%s", pod.Namespace, podName) targetPortNameList := strset.NewWithSize(len(podNets)) portsNeedToDel := []string{} + annotationsNeedToDel := []string{} subnetUsedByPort := make(map[string]string) for _, podNet := range podNets { @@ -1075,6 +1076,12 @@ func (c *Controller) syncKubeOvnNet(pod *v1.Pod, podNets []*kubeovnNet) error { if !targetPortNameList.Has(port.Name) { portsNeedToDel = append(portsNeedToDel, port.Name) subnetUsedByPort[port.Name] = port.ExternalIDs["ls"] + portNameSlice := strings.Split(port.Name, ".") + providerName := strings.Join(portNameSlice[2:], ".") + if providerName == util.OvnProvider { + continue + } + annotationsNeedToDel = append(annotationsNeedToDel, providerName) } } @@ -1100,6 +1107,14 @@ func (c *Controller) syncKubeOvnNet(pod *v1.Pod, podNets []*kubeovnNet) error { } } + for _, providerName := range annotationsNeedToDel { + for annotationKey := range pod.Annotations { + if strings.HasPrefix(key, providerName) { + delete(pod.Annotations, annotationKey) + } + } + } + return nil } diff --git a/pkg/daemon/ovs_linux.go b/pkg/daemon/ovs_linux.go index aec58949d8e..059cc5846c2 100644 --- a/pkg/daemon/ovs_linux.go +++ b/pkg/daemon/ovs_linux.go @@ -191,6 +191,11 @@ func generateNicName(containerID, ifname string) (string, string) { if ifname == "eth0" { return fmt.Sprintf("%s_h", containerID[0:12]), fmt.Sprintf("%s_c", containerID[0:12]) } + // The nic name is 14 length and have prefix pod in the Kubevirt v1.0.0 + if strings.HasPrefix(ifname, "pod") && len(ifname) == 14 { + ifname = ifname[3 : len(ifname)-4] + return fmt.Sprintf("%s_%s_h", containerID[0:12-len(ifname)], ifname), fmt.Sprintf("%s_%s_c", containerID[0:12-len(ifname)], ifname) + } return fmt.Sprintf("%s_%s_h", containerID[0:12-len(ifname)], ifname), fmt.Sprintf("%s_%s_c", containerID[0:12-len(ifname)], ifname) }