Skip to content

Commit

Permalink
no case need skip mac and ip conflict check
Browse files Browse the repository at this point in the history
Signed-off-by: bobz965 <[email protected]>
  • Loading branch information
bobz965 committed Dec 6, 2024
1 parent 6016e30 commit 051170f
Show file tree
Hide file tree
Showing 13 changed files with 209 additions and 235 deletions.
14 changes: 7 additions & 7 deletions pkg/controller/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ func (c *Controller) InitIPAM() error {
mac = ptr.To(lrp.MAC)
}
}
if _, _, _, err = c.ipam.GetStaticAddress(u2oInterconnName, u2oInterconnLrpName, subnet.Status.U2OInterconnectionIP, mac, subnet.Name, true); err != nil {
if _, _, _, err = c.ipam.GetStaticAddress(u2oInterconnName, u2oInterconnLrpName, subnet.Status.U2OInterconnectionIP, mac, subnet.Name); err != nil {
klog.Errorf("failed to init subnet %q u2o interconnection ip to ipam %v", subnet.Name, err)
}
}
Expand Down Expand Up @@ -383,7 +383,7 @@ func (c *Controller) InitIPAM() error {
} else {
ipamKey = util.NodeLspName(ip.Spec.PodName)
}
if _, _, _, err = c.ipam.GetStaticAddress(ipamKey, ip.Name, ip.Spec.IPAddress, &ip.Spec.MacAddress, ip.Spec.Subnet, true); err != nil {
if _, _, _, err = c.ipam.GetStaticAddress(ipamKey, ip.Name, ip.Spec.IPAddress, &ip.Spec.MacAddress, ip.Spec.Subnet); err != nil {
klog.Errorf("failed to init IPAM from IP CR %s: %v", ip.Name, err)
}
}
Expand Down Expand Up @@ -413,7 +413,7 @@ func (c *Controller) InitIPAM() error {
portName := ovs.PodNameToPortName(podName, pod.Namespace, podNet.ProviderName)
ip := pod.Annotations[fmt.Sprintf(util.IPAddressAnnotationTemplate, podNet.ProviderName)]
mac := pod.Annotations[fmt.Sprintf(util.MacAddressAnnotationTemplate, podNet.ProviderName)]
_, _, _, err := c.ipam.GetStaticAddress(key, portName, ip, &mac, podNet.Subnet.Name, true)
_, _, _, err := c.ipam.GetStaticAddress(key, portName, ip, &mac, podNet.Subnet.Name)
if err != nil {
klog.Errorf("failed to init pod %s.%s address %s: %v", podName, pod.Namespace, pod.Annotations[fmt.Sprintf(util.IPAddressAnnotationTemplate, podNet.ProviderName)], err)
} else {
Expand All @@ -440,7 +440,7 @@ func (c *Controller) InitIPAM() error {
continue
}
portName := ovs.PodNameToPortName(vip.Name, vip.Spec.Namespace, provider)
if _, _, _, err = c.ipam.GetStaticAddress(vip.Name, portName, vip.Status.V4ip, &vip.Status.Mac, vip.Spec.Subnet, true); err != nil {
if _, _, _, err = c.ipam.GetStaticAddress(vip.Name, portName, vip.Status.V4ip, &vip.Status.Mac, vip.Spec.Subnet); err != nil {
klog.Errorf("failed to init ipam from vip cr %s: %v", vip.Name, err)
}
}
Expand All @@ -452,7 +452,7 @@ func (c *Controller) InitIPAM() error {
}
for _, eip := range eips {
externalNetwork := util.GetExternalNetwork(eip.Spec.ExternalSubnet)
if _, _, _, err = c.ipam.GetStaticAddress(eip.Name, eip.Name, eip.Status.IP, &eip.Spec.MacAddress, externalNetwork, true); err != nil {
if _, _, _, err = c.ipam.GetStaticAddress(eip.Name, eip.Name, eip.Status.IP, &eip.Spec.MacAddress, externalNetwork); err != nil {
klog.Errorf("failed to init ipam from iptables eip cr %s: %v", eip.Name, err)
}
}
Expand All @@ -463,7 +463,7 @@ func (c *Controller) InitIPAM() error {
return err
}
for _, oeip := range oeips {
if _, _, _, err = c.ipam.GetStaticAddress(oeip.Name, oeip.Name, oeip.Status.V4Ip, &oeip.Status.MacAddress, oeip.Spec.ExternalSubnet, true); err != nil {
if _, _, _, err = c.ipam.GetStaticAddress(oeip.Name, oeip.Name, oeip.Status.V4Ip, &oeip.Status.MacAddress, oeip.Spec.ExternalSubnet); err != nil {
klog.Errorf("failed to init ipam from ovn eip cr %s: %v", oeip.Name, err)
}
}
Expand All @@ -479,7 +479,7 @@ func (c *Controller) InitIPAM() error {
mac := node.Annotations[util.MacAddressAnnotation]
v4IP, v6IP, _, err := c.ipam.GetStaticAddress(portName, portName,
node.Annotations[util.IPAddressAnnotation], &mac,
node.Annotations[util.LogicalSwitchAnnotation], true)
node.Annotations[util.LogicalSwitchAnnotation])
if err != nil {
klog.Errorf("failed to init node %s.%s address %s: %v", node.Name, node.Namespace, node.Annotations[util.IPAddressAnnotation], err)
}
Expand Down
8 changes: 3 additions & 5 deletions pkg/controller/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,9 @@ func (c *Controller) handleDelIPFinalizer(cachedIP *kubeovnv1.IP) error {
func (c *Controller) acquireIPAddress(subnetName, name, nicName string) (string, string, string, error) {
var skippedAddrs []string
var v4ip, v6ip, mac string
checkConflict := true
var err error
for {
v4ip, v6ip, mac, err = c.ipam.GetRandomAddress(name, nicName, nil, subnetName, "", skippedAddrs, checkConflict)
v4ip, v6ip, mac, err = c.ipam.GetRandomAddress(name, nicName, nil, subnetName, "", skippedAddrs)
if err != nil {
klog.Error(err)
return "", "", "", err
Expand All @@ -357,7 +356,6 @@ func (c *Controller) acquireIPAddress(subnetName, name, nicName string) (string,
}

func (c *Controller) acquireStaticIPAddress(subnetName, name, nicName, ip string) (string, string, string, error) {
checkConflict := true
var v4ip, v6ip, mac string
var err error
for _, ipStr := range strings.Split(ip, ",") {
Expand All @@ -366,7 +364,7 @@ func (c *Controller) acquireStaticIPAddress(subnetName, name, nicName, ip string
}
}

if v4ip, v6ip, mac, err = c.ipam.GetStaticAddress(name, nicName, ip, nil, subnetName, checkConflict); err != nil {
if v4ip, v6ip, mac, err = c.ipam.GetStaticAddress(name, nicName, ip, nil, subnetName); err != nil {
klog.Errorf("failed to get static virtual ip '%s', mac '%s', subnet '%s', %v", ip, mac, subnetName, err)
return "", "", "", err
}
Expand Down Expand Up @@ -507,7 +505,7 @@ func (c *Controller) ipAcquireAddress(ip *kubeovnv1.IP, subnet *kubeovnv1.Subnet
err = fmt.Errorf("failed to get random address for ip %s, %w", ip.Name, err)
} else {
// static address
v4IP, v6IP, mac, err = c.acquireStaticAddress(key, portName, ipStr, macPtr, subnet.Name, true)
v4IP, v6IP, mac, err = c.acquireStaticAddress(key, portName, ipStr, macPtr, subnet.Name)
if err == nil {
return v4IP, v6IP, mac, nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ func (c *Controller) handleAddNode(key string) error {
if node.Annotations[util.AllocatedAnnotation] == "true" && node.Annotations[util.IPAddressAnnotation] != "" && node.Annotations[util.MacAddressAnnotation] != "" {
macStr := node.Annotations[util.MacAddressAnnotation]
v4IP, v6IP, mac, err = c.ipam.GetStaticAddress(portName, portName, node.Annotations[util.IPAddressAnnotation],
&macStr, node.Annotations[util.LogicalSwitchAnnotation], true)
&macStr, node.Annotations[util.LogicalSwitchAnnotation])
if err != nil {
klog.Errorf("failed to alloc static ip addrs for node %v: %v", node.Name, err)
return err
}
} else {
v4IP, v6IP, mac, err = c.ipam.GetRandomAddress(portName, portName, nil, c.config.NodeSwitch, "", nil, true)
v4IP, v6IP, mac, err = c.ipam.GetRandomAddress(portName, portName, nil, 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
37 changes: 15 additions & 22 deletions pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -1460,11 +1460,10 @@ const (
)

type kubeovnNet struct {
Type providerType
ProviderName string
Subnet *kubeovnv1.Subnet
IsDefault bool
AllowLiveMigration bool
Type providerType
ProviderName string
Subnet *kubeovnv1.Subnet
IsDefault bool
}

func (c *Controller) getPodAttachmentNet(pod *v1.Pod) ([]*kubeovnNet, error) {
Expand Down Expand Up @@ -1511,14 +1510,9 @@ func (c *Controller) getPodAttachmentNet(pod *v1.Pod) ([]*kubeovnNet, error) {
// allocate kubeovn network
var providerName string
if util.IsOvnNetwork(netCfg) {
allowLiveMigration := false
isDefault := util.IsDefaultNet(pod.Annotations[util.DefaultNetworkAnnotation], attach)

providerName = fmt.Sprintf("%s.%s.%s", attach.Name, attach.Namespace, util.OvnProvider)
if pod.Annotations[util.MigrationJobAnnotation] != "" {
allowLiveMigration = true
}

subnetName := pod.Annotations[fmt.Sprintf(util.LogicalSwitchAnnotationTemplate, providerName)]
if subnetName == "" {
for _, subnet := range subnets {
Expand All @@ -1543,11 +1537,10 @@ func (c *Controller) getPodAttachmentNet(pod *v1.Pod) ([]*kubeovnNet, error) {
}
}
result = append(result, &kubeovnNet{
Type: providerTypeOriginal,
ProviderName: providerName,
Subnet: subnet,
IsDefault: isDefault,
AllowLiveMigration: allowLiveMigration,
Type: providerTypeOriginal,
ProviderName: providerName,
Subnet: subnet,
IsDefault: isDefault,
})
} else {
providerName = fmt.Sprintf("%s.%s", attach.Name, attach.Namespace)
Expand Down Expand Up @@ -1652,7 +1645,7 @@ func (c *Controller) acquireAddress(pod *v1.Pod, podNet *kubeovnNet) (string, st
for {
portName := ovs.PodNameToPortName(podName, pod.Namespace, podNet.ProviderName)

ipv4, ipv6, mac, err := c.ipam.GetRandomAddress(key, portName, macPointer, podNet.Subnet.Name, "", skippedAddrs, !podNet.AllowLiveMigration)
ipv4, ipv6, mac, err := c.ipam.GetRandomAddress(key, portName, macPointer, podNet.Subnet.Name, "", skippedAddrs)
if err != nil {
klog.Error(err)
return "", "", "", podNet.Subnet, err
Expand Down Expand Up @@ -1687,7 +1680,7 @@ func (c *Controller) acquireAddress(pod *v1.Pod, podNet *kubeovnNet) (string, st
ipStr := pod.Annotations[fmt.Sprintf(util.IPAddressAnnotationTemplate, podNet.ProviderName)]

for _, net := range nsNets {
v4IP, v6IP, mac, err = c.acquireStaticAddress(key, portName, ipStr, macPointer, net.Subnet.Name, net.AllowLiveMigration)
v4IP, v6IP, mac, err = c.acquireStaticAddress(key, portName, ipStr, macPointer, net.Subnet.Name)
if err == nil {
return v4IP, v6IP, mac, net.Subnet, nil
}
Expand All @@ -1714,7 +1707,7 @@ func (c *Controller) acquireAddress(pod *v1.Pod, podNet *kubeovnNet) (string, st
var skippedAddrs []string
for {
portName := ovs.PodNameToPortName(podName, pod.Namespace, podNet.ProviderName)
ipv4, ipv6, mac, err := c.ipam.GetRandomAddress(key, portName, macPointer, podNet.Subnet.Name, ipPool[0], skippedAddrs, !podNet.AllowLiveMigration)
ipv4, ipv6, mac, err := c.ipam.GetRandomAddress(key, portName, macPointer, podNet.Subnet.Name, ipPool[0], skippedAddrs)
if err != nil {
klog.Error(err)
return "", "", "", podNet.Subnet, err
Expand Down Expand Up @@ -1753,7 +1746,7 @@ func (c *Controller) acquireAddress(pod *v1.Pod, podNet *kubeovnNet) (string, st
continue
}

v4IP, v6IP, mac, err = c.acquireStaticAddress(key, portName, staticIP, macPointer, net.Subnet.Name, net.AllowLiveMigration)
v4IP, v6IP, mac, err = c.acquireStaticAddress(key, portName, staticIP, macPointer, net.Subnet.Name)
if err == nil {
return v4IP, v6IP, mac, net.Subnet, nil
}
Expand All @@ -1767,7 +1760,7 @@ func (c *Controller) acquireAddress(pod *v1.Pod, podNet *kubeovnNet) (string, st

if index < len(ipPool) {
for _, net := range nsNets {
v4IP, v6IP, mac, err = c.acquireStaticAddress(key, portName, ipPool[index], macPointer, net.Subnet.Name, net.AllowLiveMigration)
v4IP, v6IP, mac, err = c.acquireStaticAddress(key, portName, ipPool[index], macPointer, net.Subnet.Name)
if err == nil {
return v4IP, v6IP, mac, net.Subnet, nil
}
Expand All @@ -1780,7 +1773,7 @@ func (c *Controller) acquireAddress(pod *v1.Pod, podNet *kubeovnNet) (string, st
return "", "", "", podNet.Subnet, ipam.ErrNoAvailable
}

func (c *Controller) acquireStaticAddress(key, nicName, ip string, mac *string, subnet string, liveMigration bool) (string, string, string, error) {
func (c *Controller) acquireStaticAddress(key, nicName, ip string, mac *string, subnet string) (string, string, string, error) {
var v4IP, v6IP, macStr string
var err error
for _, ipStr := range strings.Split(ip, ",") {
Expand All @@ -1789,7 +1782,7 @@ func (c *Controller) acquireStaticAddress(key, nicName, ip string, mac *string,
}
}

if v4IP, v6IP, macStr, err = c.ipam.GetStaticAddress(key, nicName, ip, mac, subnet, !liveMigration); err != nil {
if v4IP, v6IP, macStr, err = c.ipam.GetStaticAddress(key, nicName, ip, mac, subnet); err != nil {
klog.Errorf("failed to get static ip %v, mac %v, subnet %v, err %v", ip, mac, subnet, err)
return "", "", "", err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/vip.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ func (c *Controller) releaseVip(key string) error {
if vip.Status.Mac == "" {
mac = nil
}
if _, _, _, err = c.ipam.GetStaticAddress(key, vip.Name, vip.Status.V4ip, mac, vip.Spec.Subnet, false); err != nil {
if _, _, _, err = c.ipam.GetStaticAddress(key, vip.Name, vip.Status.V4ip, mac, vip.Spec.Subnet); err != nil {
klog.Errorf("failed to recover IPAM from vip CR %s: %v", vip.Name, err)
}
c.updateSubnetStatusQueue.Add(vip.Spec.Subnet)
Expand Down
5 changes: 2 additions & 3 deletions pkg/controller/vpc_nat_gw_eip.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,6 @@ func (c *Controller) delEipQoSInPod(dp, v4ip string, direction kubeovnv1.QoSPoli
}

func (c *Controller) acquireStaticEip(name, _, nicName, ip, externalSubnet string) (string, string, string, error) {
checkConflict := true
var v4ip, v6ip, mac string
var err error
for _, ipStr := range strings.Split(ip, ",") {
Expand All @@ -494,7 +493,7 @@ func (c *Controller) acquireStaticEip(name, _, nicName, ip, externalSubnet strin
}
}

if v4ip, v6ip, mac, err = c.ipam.GetStaticAddress(name, nicName, ip, nil, externalSubnet, checkConflict); err != nil {
if v4ip, v6ip, mac, err = c.ipam.GetStaticAddress(name, nicName, ip, nil, externalSubnet); err != nil {
klog.Errorf("failed to get static ip %v, mac %v, subnet %v, err %v", ip, mac, externalSubnet, err)
return "", "", "", err
}
Expand All @@ -504,7 +503,7 @@ func (c *Controller) acquireStaticEip(name, _, nicName, ip, externalSubnet strin
func (c *Controller) acquireEip(name, _, nicName, externalSubnet string) (string, string, string, error) {
var skippedAddrs []string
for {
ipv4, ipv6, mac, err := c.ipam.GetRandomAddress(name, nicName, nil, externalSubnet, "", skippedAddrs, true)
ipv4, ipv6, mac, err := c.ipam.GetRandomAddress(name, nicName, nil, externalSubnet, "", skippedAddrs)
if err != nil {
klog.Error(err)
return "", "", "", err
Expand Down
Loading

0 comments on commit 051170f

Please sign in to comment.