Skip to content

Commit

Permalink
don't check conflict for migration pod with only static mac
Browse files Browse the repository at this point in the history
  • Loading branch information
chestack authored and oilbeater committed Mar 3, 2022
1 parent 89aa241 commit 0da84f8
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 62 deletions.
2 changes: 1 addition & 1 deletion pkg/controller/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func (c *Controller) handleAddNode(key string) error {
return err
}
} else {
v4IP, v6IP, mac, err = c.ipam.GetRandomAddress(portName, portName, "", c.config.NodeSwitch, nil)
v4IP, v6IP, mac, err = c.ipam.GetRandomAddress(portName, portName, "", c.config.NodeSwitch, nil, true)
if err != nil {
klog.Errorf("failed to alloc random ip addrs for node %v: %v", node.Name, err)
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -1227,7 +1227,7 @@ func (c *Controller) acquireAddress(pod *v1.Pod, podNet *kubeovnNet) (string, st
for {
nicName := ovs.PodNameToPortName(podName, pod.Namespace, podNet.ProviderName)

ipv4, ipv6, mac, err := c.ipam.GetRandomAddress(key, nicName, macStr, podNet.Subnet.Name, skippedAddrs)
ipv4, ipv6, mac, err := c.ipam.GetRandomAddress(key, nicName, macStr, podNet.Subnet.Name, skippedAddrs, !podNet.AllowLiveMigration)
if err != nil {
return "", "", "", err
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/ipam/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewIPAM() *IPAM {
}
}

func (ipam *IPAM) GetRandomAddress(podName, nicName, mac, subnetName string, skippedAddrs []string) (string, string, string, error) {
func (ipam *IPAM) GetRandomAddress(podName, nicName, mac, subnetName string, skippedAddrs []string, checkConflict bool) (string, string, string, error) {
ipam.mutex.RLock()
defer ipam.mutex.RUnlock()

Expand All @@ -46,7 +46,7 @@ func (ipam *IPAM) GetRandomAddress(podName, nicName, mac, subnetName string, ski
return "", "", "", ErrNoAvailable
}

v4IP, v6IP, mac, err := subnet.GetRandomAddress(podName, nicName, mac, skippedAddrs)
v4IP, v6IP, mac, err := subnet.GetRandomAddress(podName, nicName, mac, skippedAddrs, checkConflict)
klog.Infof("allocate v4 %s v6 %s mac %s for %s", v4IP, v6IP, mac, podName)
return string(v4IP), string(v6IP), mac, err
}
Expand All @@ -67,7 +67,7 @@ func (ipam *IPAM) GetStaticAddress(podName, nicName, ip, mac, subnetName string,
}
ips = append(ips, ipAddr)
}
ips, err = checkAndAppendIpsForDual(ips, mac, podName, nicName, subnet)
ips, err = checkAndAppendIpsForDual(ips, mac, podName, nicName, subnet, checkConflict)
if err != nil {
klog.Errorf("failed to append allocate ip %v mac %s for %s", ips, mac, podName)
return "", "", "", err
Expand All @@ -88,7 +88,7 @@ func (ipam *IPAM) GetStaticAddress(podName, nicName, ip, mac, subnetName string,
return "", "", "", ErrNoAvailable
}

func checkAndAppendIpsForDual(ips []IP, mac string, podName string, nicName string, subnet *Subnet) ([]IP, error) {
func checkAndAppendIpsForDual(ips []IP, mac string, podName string, nicName string, subnet *Subnet, checkConflict bool) ([]IP, error) {
// IP Address for dual-stack should be format of 'IPv4,IPv6'
if subnet.Protocol != kubeovnv1.ProtocolDual || len(ips) == 2 {
return ips, nil
Expand All @@ -99,10 +99,10 @@ func checkAndAppendIpsForDual(ips []IP, mac string, podName string, nicName stri
var err error
if util.CheckProtocol(string(ips[0])) == kubeovnv1.ProtocolIPv4 {
newIps = ips
_, ipAddr, _, err = subnet.getV6RandomAddress(podName, nicName, mac, nil)
_, ipAddr, _, err = subnet.getV6RandomAddress(podName, nicName, mac, nil, checkConflict)
newIps = append(newIps, ipAddr)
} else if util.CheckProtocol(string(ips[0])) == kubeovnv1.ProtocolIPv6 {
ipAddr, _, _, err = subnet.getV4RandomAddress(podName, nicName, mac, nil)
ipAddr, _, _, err = subnet.getV4RandomAddress(podName, nicName, mac, nil, checkConflict)
newIps = append(newIps, ipAddr)
newIps = append(newIps, ips...)
}
Expand Down
24 changes: 12 additions & 12 deletions pkg/ipam/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,41 +159,41 @@ func (subnet *Subnet) popPodNic(podName, nicName string) {
subnet.PodToNicList[podName] = util.RemoveString(subnet.PodToNicList[podName], nicName)
}

func (subnet *Subnet) GetRandomAddress(podName, nicName string, mac string, skippedAddrs []string) (IP, IP, string, error) {
func (subnet *Subnet) GetRandomAddress(podName, nicName string, mac string, skippedAddrs []string, checkConflict bool) (IP, IP, string, error) {
subnet.mutex.Lock()
defer func() {
subnet.pushPodNic(podName, nicName)
subnet.mutex.Unlock()
}()

if subnet.Protocol == kubeovnv1.ProtocolDual {
return subnet.getDualRandomAddress(podName, nicName, mac, skippedAddrs)
return subnet.getDualRandomAddress(podName, nicName, mac, skippedAddrs, checkConflict)
} else if subnet.Protocol == kubeovnv1.ProtocolIPv4 {
return subnet.getV4RandomAddress(podName, nicName, mac, skippedAddrs)
return subnet.getV4RandomAddress(podName, nicName, mac, skippedAddrs, checkConflict)
} else {
return subnet.getV6RandomAddress(podName, nicName, mac, skippedAddrs)
return subnet.getV6RandomAddress(podName, nicName, mac, skippedAddrs, checkConflict)
}
}

func (subnet *Subnet) getDualRandomAddress(podName, nicName string, mac string, skippedAddrs []string) (IP, IP, string, error) {
v4IP, _, _, err := subnet.getV4RandomAddress(podName, nicName, mac, skippedAddrs)
func (subnet *Subnet) getDualRandomAddress(podName, nicName string, mac string, skippedAddrs []string, checkConflict bool) (IP, IP, string, error) {
v4IP, _, _, err := subnet.getV4RandomAddress(podName, nicName, mac, skippedAddrs, checkConflict)
if err != nil {
return "", "", "", err
}
_, v6IP, mac, err := subnet.getV6RandomAddress(podName, nicName, mac, skippedAddrs)
_, v6IP, mac, err := subnet.getV6RandomAddress(podName, nicName, mac, skippedAddrs, checkConflict)
if err != nil {
return "", "", "", err
}

// allocated IPv4 address may be released in getV6RandomAddress()
if subnet.V4NicToIP[nicName] != v4IP {
v4IP, _, _, _ = subnet.getV4RandomAddress(podName, nicName, mac, skippedAddrs)
v4IP, _, _, _ = subnet.getV4RandomAddress(podName, nicName, mac, skippedAddrs, checkConflict)
}

return v4IP, v6IP, mac, nil
}

func (subnet *Subnet) getV4RandomAddress(podName, nicName string, mac string, skippedAddrs []string) (IP, IP, string, error) {
func (subnet *Subnet) getV4RandomAddress(podName, nicName string, mac string, skippedAddrs []string, checkConflict bool) (IP, IP, string, error) {
// After 'macAdd' introduced to support only static mac address, pod restart will run into error mac AddressConflict
// controller will re-enqueue the new pod then wait for old pod deleted and address released.
// here will return only if both ip and mac exist, otherwise only ip without mac returned will trigger CreatePort error.
Expand Down Expand Up @@ -246,14 +246,14 @@ func (subnet *Subnet) getV4RandomAddress(podName, nicName string, mac string, sk
if mac == "" {
return ip, "", subnet.GetRandomMac(podName, nicName), nil
} else {
if err := subnet.GetStaticMac(podName, nicName, mac, true); err != nil {
if err := subnet.GetStaticMac(podName, nicName, mac, checkConflict); err != nil {
return "", "", "", err
}
return ip, "", mac, nil
}
}

func (subnet *Subnet) getV6RandomAddress(podName, nicName string, mac string, skippedAddrs []string) (IP, IP, string, error) {
func (subnet *Subnet) getV6RandomAddress(podName, nicName string, mac string, skippedAddrs []string, checkConflict bool) (IP, IP, string, error) {
// After 'macAdd' introduced to support only static mac address, pod restart will run into error mac AddressConflict
// controller will re-enqueue the new pod then wait for old pod deleted and address released.
// here will return only if both ip and mac exist, otherwise only ip without mac returned will trigger CreatePort error.
Expand Down Expand Up @@ -307,7 +307,7 @@ func (subnet *Subnet) getV6RandomAddress(podName, nicName string, mac string, sk
if mac == "" {
return "", ip, subnet.GetRandomMac(podName, nicName), nil
} else {
if err := subnet.GetStaticMac(podName, nicName, mac, true); err != nil {
if err := subnet.GetStaticMac(podName, nicName, mac, checkConflict); err != nil {
return "", "", "", err
}
return "", ip, mac, nil
Expand Down

0 comments on commit 0da84f8

Please sign in to comment.