Skip to content

Commit

Permalink
Support only configure static mac_address
Browse files Browse the repository at this point in the history
  • Loading branch information
chestack committed Jan 12, 2022
1 parent bb4e644 commit f95a90e
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 69 deletions.
2 changes: 1 addition & 1 deletion pkg/controller/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,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)
if err != nil {
klog.Errorf("failed to alloc random ip addrs for node %v: %v", node.Name, err)
return err
Expand Down
10 changes: 8 additions & 2 deletions pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -1090,19 +1090,25 @@ func (c *Controller) validatePodIP(podName, subnetName, ipv4, ipv6 string) (bool

func (c *Controller) acquireAddress(pod *v1.Pod, podNet *kubeovnNet) (string, string, string, error) {
key := fmt.Sprintf("%s/%s", pod.Namespace, pod.Name)

macStr := pod.Annotations[fmt.Sprintf(util.MacAddressAnnotationTemplate, podNet.ProviderName)]
if macStr != "" {
if _, err := net.ParseMAC(macStr); err != nil {
return "", "", "", err
}
}

// Random allocate
if pod.Annotations[fmt.Sprintf(util.IpAddressAnnotationTemplate, podNet.ProviderName)] == "" &&
pod.Annotations[fmt.Sprintf(util.IpPoolAnnotationTemplate, podNet.ProviderName)] == "" {
var skippedAddrs []string
for {
nicName := ovs.PodNameToPortName(pod.Name, pod.Namespace, podNet.ProviderName)
ipv4, ipv6, mac, err := c.ipam.GetRandomAddress(key, nicName, podNet.Subnet.Name, skippedAddrs)

ipv4, ipv6, mac, err := c.ipam.GetRandomAddress(key, nicName, macStr, podNet.Subnet.Name, skippedAddrs)
if err != nil {
return "", "", "", err
}

ipv4OK, ipv6OK, err := c.validatePodIP(pod.Name, podNet.Subnet.Name, ipv4, ipv6)
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, subnetName string, skippedAddrs []string) (string, string, string, error) {
func (ipam *IPAM) GetRandomAddress(podName, nicName, mac, subnetName string, skippedAddrs []string) (string, string, string, error) {
ipam.mutex.RLock()
defer ipam.mutex.RUnlock()

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

v4IP, v6IP, mac, err := subnet.GetRandomAddress(podName, nicName, skippedAddrs)
v4IP, v6IP, mac, err := subnet.GetRandomAddress(podName, nicName, mac, skippedAddrs)
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, podName, nicName, subnet)
ips, err = checkAndAppendIpsForDual(ips, mac, podName, nicName, subnet)
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, podName string, nicName string, subnet *Subnet) ([]IP, error) {
func checkAndAppendIpsForDual(ips []IP, mac string, podName string, nicName string, subnet *Subnet) ([]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, podName string, nicName string, subnet *
var err error
if util.CheckProtocol(string(ips[0])) == kubeovnv1.ProtocolIPv4 {
newIps = ips
_, ipAddr, _, err = subnet.getV6RandomAddress(podName, nicName, nil)
_, ipAddr, _, err = subnet.getV6RandomAddress(podName, nicName, mac, nil)
newIps = append(newIps, ipAddr)
} else if util.CheckProtocol(string(ips[0])) == kubeovnv1.ProtocolIPv6 {
ipAddr, _, _, err = subnet.getV4RandomAddress(podName, nicName, nil)
ipAddr, _, _, err = subnet.getV4RandomAddress(podName, nicName, mac, nil)
newIps = append(newIps, ipAddr)
newIps = append(newIps, ips...)
}
Expand Down
57 changes: 39 additions & 18 deletions pkg/ipam/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,44 +159,47 @@ func (subnet *Subnet) popPodNic(podName, nicName string) {
subnet.PodToNicList[podName] = util.RemoveString(subnet.PodToNicList[podName], nicName)
}

func (subnet *Subnet) GetRandomAddress(podName, nicName string, skippedAddrs []string) (IP, IP, string, error) {
func (subnet *Subnet) GetRandomAddress(podName, nicName string, mac string, skippedAddrs []string) (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, skippedAddrs)
return subnet.getDualRandomAddress(podName, nicName, mac, skippedAddrs)
} else if subnet.Protocol == kubeovnv1.ProtocolIPv4 {
return subnet.getV4RandomAddress(podName, nicName, skippedAddrs)
return subnet.getV4RandomAddress(podName, nicName, mac, skippedAddrs)
} else {
return subnet.getV6RandomAddress(podName, nicName, skippedAddrs)
return subnet.getV6RandomAddress(podName, nicName, mac, skippedAddrs)
}
}

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

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

return v4IP, v6IP, mac, nil
}

func (subnet *Subnet) getV4RandomAddress(podName, nicName string, skippedAddrs []string) (IP, IP, string, error) {
if ip, ok := subnet.V4NicToIP[nicName]; ok {
if !util.ContainsString(skippedAddrs, string(ip)) {
return ip, "", subnet.NicToMac[nicName], nil
func (subnet *Subnet) getV4RandomAddress(podName, nicName string, mac string, skippedAddrs []string) (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.
if subnet.V4NicToIP[nicName] != "" && subnet.NicToMac[nicName] != "" {
if !util.ContainsString(skippedAddrs, string(subnet.V4NicToIP[nicName])) {
return subnet.V4NicToIP[nicName], "", subnet.NicToMac[nicName], nil
}
subnet.releaseAddr(podName, nicName)
}
Expand Down Expand Up @@ -240,16 +243,27 @@ func (subnet *Subnet) getV4RandomAddress(podName, nicName string, skippedAddrs [
subnet.V4NicToIP[nicName] = ip
subnet.V4IPToPod[ip] = podName
subnet.pushPodNic(podName, nicName)
return ip, "", subnet.GetRandomMac(podName, nicName), nil
if mac == "" {
return ip, "", subnet.GetRandomMac(podName, nicName), nil
} else {
if err := subnet.GetStaticMac(podName, nicName, mac, true); err != nil {
return "", "", "", err
}
return ip, "", mac, nil
}
}

func (subnet *Subnet) getV6RandomAddress(podName, nicName string, skippedAddrs []string) (IP, IP, string, error) {
if ip, ok := subnet.V6NicToIP[nicName]; ok {
if !util.ContainsString(skippedAddrs, string(ip)) {
return "", ip, subnet.NicToMac[nicName], nil
func (subnet *Subnet) getV6RandomAddress(podName, nicName string, mac string, skippedAddrs []string) (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.
if subnet.V6NicToIP[nicName] != "" && subnet.NicToMac[nicName] != "" {
if !util.ContainsString(skippedAddrs, string(subnet.V6NicToIP[nicName])) {
return "", subnet.V6NicToIP[nicName], subnet.NicToMac[nicName], nil
}
subnet.releaseAddr(podName, nicName)
}

if len(subnet.V6FreeIPList) == 0 {
if len(subnet.V6ReleasedIPList) == 0 {
return "", "", "", ErrNoAvailable
Expand Down Expand Up @@ -290,7 +304,14 @@ func (subnet *Subnet) getV6RandomAddress(podName, nicName string, skippedAddrs [
subnet.V6NicToIP[nicName] = ip
subnet.V6IPToPod[ip] = podName
subnet.pushPodNic(podName, nicName)
return "", ip, subnet.GetRandomMac(podName, nicName), nil
if mac == "" {
return "", ip, subnet.GetRandomMac(podName, nicName), nil
} else {
if err := subnet.GetStaticMac(podName, nicName, mac, true); err != nil {
return "", "", "", err
}
return "", ip, mac, nil
}
}

func (subnet *Subnet) GetStaticAddress(podName, nicName string, ip IP, mac string, force bool, checkConflict bool) (IP, string, error) {
Expand Down

0 comments on commit f95a90e

Please sign in to comment.