Skip to content

Commit

Permalink
fix changging the stopped vm's subnets, the vm cann't start normally (#…
Browse files Browse the repository at this point in the history
…2463)

Co-authored-by: yuanliu <yuanliu@cmss.chinamobile.com>
  • Loading branch information
lynn901 and yuanliu committed Mar 14, 2023
1 parent 7e2e437 commit b26784f
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions pkg/controller/pod.go
Expand Up @@ -548,6 +548,39 @@ func (c *Controller) getPodKubeovnNets(pod *v1.Pod) ([]*kubeovnNet, error) {
return podNets, nil
}

func (c *Controller) changeVMSubnet(vmName, namespace, providerName, subnetName string, pod *v1.Pod) error {
ipName := ovs.PodNameToPortName(vmName, namespace, providerName)
ipCr, err := c.config.KubeOvnClient.KubeovnV1().IPs().Get(context.Background(), ipName, metav1.GetOptions{})
if err != nil {
if !k8serrors.IsNotFound(err) {
errMsg := fmt.Errorf("failed to get ip CR %s: %v", ipName, err)
klog.Error(errMsg)
return errMsg
}
// the returned pointer is not nil if the CR does not exist
ipCr = nil
}
if ipCr != nil {
if ipCr.Spec.Subnet != subnetName {
key := fmt.Sprintf("%s/%s", pod.Namespace, vmName)
ports, err := c.ovnClient.ListNormalLogicalSwitchPorts(true, map[string]string{"pod": key})
if err != nil {
klog.Errorf("failed to list lsps of pod '%s', %v", pod.Name, err)
return err
}
for _, port := range ports {
// when lsp is deleted, the port of pod is deleted from any port-group automatically.
klog.Infof("gc logical switch port %s", port.Name)
if err := c.ovnLegacyClient.DeleteLogicalSwitchPort(port.Name); err != nil {
klog.Errorf("failed to delete lsp %s, %v", port.Name, err)
return err
}
}
}
}
return nil
}

func (c *Controller) handleAddPod(key string) error {
namespace, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
Expand Down Expand Up @@ -604,6 +637,10 @@ func (c *Controller) handleAddPod(key string) error {
}
if isVmPod && c.config.EnableKeepVmIP {
pod.Annotations[fmt.Sprintf(util.VmTemplate, podNet.ProviderName)] = vmName
if err := c.changeVMSubnet(vmName, namespace, podNet.ProviderName, subnet.Name, pod); err != nil {
klog.Errorf("change subnet of pod %s/%s to %s failed: %v", namespace, name, subnet.Name, err)
return err
}
}

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

0 comments on commit b26784f

Please sign in to comment.