Skip to content

Commit f22535e

Browse files
袁又袁yuanliu
authored andcommitted
fix changging the stopped vm's subnets, the vm cann't start normally (#2463)
Co-authored-by: yuanliu <yuanliu@cmss.chinamobile.com>
1 parent 5bd71ba commit f22535e

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

pkg/controller/pod.go

+37
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,39 @@ func (c *Controller) getPodKubeovnNets(pod *v1.Pod) ([]*kubeovnNet, error) {
547547
return podNets, nil
548548
}
549549

550+
func (c *Controller) changeVMSubnet(vmName, namespace, providerName, subnetName string, pod *v1.Pod) error {
551+
ipName := ovs.PodNameToPortName(vmName, namespace, providerName)
552+
ipCr, err := c.config.KubeOvnClient.KubeovnV1().IPs().Get(context.Background(), ipName, metav1.GetOptions{})
553+
if err != nil {
554+
if !k8serrors.IsNotFound(err) {
555+
errMsg := fmt.Errorf("failed to get ip CR %s: %v", ipName, err)
556+
klog.Error(errMsg)
557+
return errMsg
558+
}
559+
// the returned pointer is not nil if the CR does not exist
560+
ipCr = nil
561+
}
562+
if ipCr != nil {
563+
if ipCr.Spec.Subnet != subnetName {
564+
key := fmt.Sprintf("%s/%s", pod.Namespace, vmName)
565+
ports, err := c.ovnClient.ListPodLogicalSwitchPorts(key)
566+
if err != nil {
567+
klog.Errorf("failed to list lsps of pod '%s', %v", pod.Name, err)
568+
return err
569+
}
570+
for _, port := range ports {
571+
// when lsp is deleted, the port of pod is deleted from any port-group automatically.
572+
klog.Infof("gc logical switch port %s", port.Name)
573+
if err := c.ovnLegacyClient.DeleteLogicalSwitchPort(port.Name); err != nil {
574+
klog.Errorf("failed to delete lsp %s, %v", port.Name, err)
575+
return err
576+
}
577+
}
578+
}
579+
}
580+
return nil
581+
}
582+
550583
func (c *Controller) handleAddPod(key string) error {
551584
namespace, name, err := cache.SplitMetaNamespaceKey(key)
552585
if err != nil {
@@ -603,6 +636,10 @@ func (c *Controller) handleAddPod(key string) error {
603636
}
604637
if isVmPod && c.config.EnableKeepVmIP {
605638
pod.Annotations[fmt.Sprintf(util.VmTemplate, podNet.ProviderName)] = vmName
639+
if err := c.changeVMSubnet(vmName, namespace, podNet.ProviderName, subnet.Name, pod); err != nil {
640+
klog.Errorf("change subnet of pod %s/%s to %s failed: %v", namespace, name, subnet.Name, err)
641+
return err
642+
}
606643
}
607644

608645
if err := util.ValidatePodCidr(podNet.Subnet.Spec.CIDRBlock, ipStr); err != nil {

0 commit comments

Comments
 (0)