Skip to content

Commit

Permalink
sync live migration vm port
Browse files Browse the repository at this point in the history
  • Loading branch information
fanriming committed Dec 12, 2021
1 parent 3727385 commit e596c3c
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 6 deletions.
2 changes: 2 additions & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,4 +591,6 @@ func (c *Controller) startWorkers(stopCh <-chan struct{}) {
if c.config.EnableNP {
go wait.Until(c.CheckNodePortGroup, 10*time.Second, stopCh)
}

go wait.Until(c.syncVmLiveMigrationPort, 15*time.Second, stopCh)
}
2 changes: 1 addition & 1 deletion pkg/controller/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func (c *Controller) handleAddNode(key string) error {
}

ipStr := util.GetStringIP(v4IP, v6IP)
if err := c.ovnClient.CreatePort(c.config.NodeSwitch, portName, ipStr, mac, "", "", false, "", ""); err != nil {
if err := c.ovnClient.CreatePort(c.config.NodeSwitch, portName, ipStr, mac, "", "", false, "", "", false); err != nil {
return err
}

Expand Down
49 changes: 48 additions & 1 deletion pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ func (c *Controller) handleAddPod(key string) error {
}
}
portName := ovs.PodNameToPortName(name, namespace, podNet.ProviderName)
if err := c.ovnClient.CreatePort(subnet.Name, portName, ipStr, mac, pod.Name, pod.Namespace, portSecurity, securityGroupAnnotation, vips); err != nil {
if err := c.ovnClient.CreatePort(subnet.Name, portName, ipStr, mac, pod.Name, pod.Namespace, portSecurity, securityGroupAnnotation, vips, podNet.AllowLiveMigration); err != nil {
c.recorder.Eventf(pod, v1.EventTypeWarning, "CreateOVNPortFailed", err.Error())
return err
}
Expand Down Expand Up @@ -1242,3 +1242,50 @@ func appendCheckStatefulSetPodToDel(c *Controller, pod *v1.Pod) (bool, error) {

return false, nil
}

// syncVmLiveMigrationPort set ip address to lsp after live migration
func (c *Controller) syncVmLiveMigrationPort() {
subnets, err := c.subnetsLister.List(labels.Everything())
if err != nil {
klog.Errorf("list get subnet failed, %v", err)
return
}
for _, subnet := range subnets {
// lists pods with the 'liveMigration' flag
ports, err := c.ovnClient.ListLogicalEntity("logical_switch_port",
fmt.Sprintf("external_ids:ls=%s", subnet.Name),
"external_ids:liveMigration=1")
if err != nil {
klog.Errorf("list logical_switch_port failed, %v", err)
return
}

for _, port := range ports {
addr, err := c.ipsLister.Get(port)
if err != nil {
klog.Errorf("get port ip failed, %v", err)
return
}
// lists pods with the same IP address
vmLsps, err := c.ovnClient.ListLogicalEntity("logical_switch_port",
fmt.Sprintf("external_ids:ls=%s", subnet.Name),
fmt.Sprintf("external_ids:ip=%s", strings.ReplaceAll(addr.Spec.IPAddress, ",", "/")))
if err != nil {
klog.Errorf("list logical_switch_port failed, %v", err)
return
}

// reset addresses after live Migration
if len(vmLsps) == 1 {
if err = c.ovnClient.SetPortAddress(port, addr.Spec.MacAddress, addr.Spec.IPAddress); err != nil {
klog.Errorf("set port addresses failed, %v", err)
return
}
if err = c.ovnClient.SetPortExternalIds(port, "liveMigration", "0"); err != nil {
klog.Errorf("set port externalIds failed, %v", err)
return
}
}
}
}
}
54 changes: 50 additions & 4 deletions pkg/ovs/ovn-nbctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,25 @@ func (c Client) DeleteICLogicalRouterPort(az string) error {
return nil
}

func (c Client) SetPortAddress(port, mac, ip string) error {
rets, err := c.ListLogicalEntity("logical_switch_port", fmt.Sprintf("name=%s", port))
if err != nil {
return fmt.Errorf("failed to find port %s: %v", port, err)
}
if len(rets) == 0 {
return nil
}

var addresses []string
addresses = append(addresses, mac)
addresses = append(addresses, strings.Split(ip, ",")...)
if _, err := c.ovnNbCommand("lsp-set-addresses", port, strings.Join(addresses, " ")); err != nil {
klog.Errorf("set port %s addresses failed, %v", port, err)
return err
}
return nil
}

func (c Client) SetPortExternalIds(port, key, value string) error {
rets, err := c.ListLogicalEntity("logical_switch_port", fmt.Sprintf("name=%s", port))
if err != nil {
Expand Down Expand Up @@ -157,13 +176,40 @@ func (c Client) SetPortSecurity(portSecurity bool, port, mac, ipStr, vips string
}

// CreatePort create logical switch port in ovn
func (c Client) CreatePort(ls, port, ip, mac, pod, namespace string, portSecurity bool, securityGroups string, vips string) error {
func (c Client) CreatePort(ls, port, ip, mac, pod, namespace string, portSecurity bool, securityGroups string, vips string, liveMigration bool) error {
var ovnCommand []string
var addresses []string
addresses = append(addresses, mac)
addresses = append(addresses, strings.Split(ip, ",")...)
ovnCommand = []string{MayExist, "lsp-add", ls, port, "--",
"lsp-set-addresses", port, strings.Join(addresses, " ")}
ovnCommand = []string{MayExist, "lsp-add", ls, port}
isAddrConflict := false
if liveMigration {
// add external_id info as the filter of 'live Migration vm port'
ovnCommand = append(ovnCommand,
"--", "set", "logical_switch_port", port, fmt.Sprintf("external_ids:ls=%s", ls),
"--", "set", "logical_switch_port", port, fmt.Sprintf("external_ids:ip=%s", strings.ReplaceAll(ip, ",", "/")))

ports, err := c.ListLogicalEntity("logical_switch_port",
fmt.Sprintf("external_ids:ls=%s", ls),
fmt.Sprintf("external_ids:ip=%s", strings.ReplaceAll(ip, ",", "/")))
if err != nil {
klog.Errorf("list logical entity failed: %v", err)
return err
}
if len(ports) > 0 {
isAddrConflict = true
}
}

if isAddrConflict {
// only set mac, and set flag 'liveMigration'
ovnCommand = append(ovnCommand, "--", "lsp-set-addresses", port, mac, "--",
"set", "logical_switch_port", port, "external_ids:liveMigration=1")
} else {
// set mac and ip
ovnCommand = append(ovnCommand,
"--", "lsp-set-addresses", port, strings.Join(addresses, " "))
}

if portSecurity {
addresses = append(addresses, strings.Split(vips, ",")...)
Expand All @@ -173,7 +219,7 @@ func (c Client) CreatePort(ls, port, ip, mac, pod, namespace string, portSecurit
if securityGroups != "" {
sgList := strings.Split(securityGroups, ",")
ovnCommand = append(ovnCommand,
"--", "set", "logical_switch_port", port, fmt.Sprintf("external_ids:security_groups=%s", securityGroups))
"--", "set", "logical_switch_port", port, fmt.Sprintf("external_ids:security_groups=%s", strings.ReplaceAll(securityGroups, ",", "/")))
for _, sg := range sgList {
ovnCommand = append(ovnCommand,
"--", "set", "logical_switch_port", port, fmt.Sprintf("external_ids:associated_sg_%s=true", sg))
Expand Down

0 comments on commit e596c3c

Please sign in to comment.