Skip to content

Commit

Permalink
do not calculate subnet.spec.excludeIPs as availableIPs (#3550)
Browse files Browse the repository at this point in the history
Signed-off-by: 马洪贞 <hzma@alauda.io>
  • Loading branch information
hongzhen-ma committed Dec 20, 2023
1 parent 999dc61 commit 32604cf
Showing 1 changed file with 35 additions and 12 deletions.
47 changes: 35 additions & 12 deletions pkg/controller/subnet.go
Expand Up @@ -699,16 +699,6 @@ func (c *Controller) handleAddOrUpdateSubnet(key string) error {
return err
}

if subnet.Spec.Protocol == kubeovnv1.ProtocolDual {
err = calcDualSubnetStatusIP(subnet, c)
} else {
err = calcSubnetStatusIP(subnet, c)
}
if err != nil {
klog.Errorf("calculate subnet %s used ip failed, %v", subnet.Name, err)
return err
}

deleted, err := c.handleSubnetFinalizer(subnet)
if err != nil {
klog.Errorf("handle subnet finalizer failed %v", err)
Expand All @@ -729,6 +719,17 @@ func (c *Controller) handleAddOrUpdateSubnet(key string) error {
return err
}

// availableIPStr valued from ipam, so leave update subnet.status after ipam process
if subnet.Spec.Protocol == kubeovnv1.ProtocolDual {
err = calcDualSubnetStatusIP(subnet, c)
} else {
err = calcSubnetStatusIP(subnet, c)
}
if err != nil {
klog.Errorf("calculate subnet %s used ip failed, %v", subnet.Name, err)
return err
}

if !isOvnSubnet(subnet) {
// subnet provider is not ovn, and vpc is empty, should not reconcile
c.patchSubnetStatus(subnet, "SetNonOvnSubnetSuccess", "")
Expand Down Expand Up @@ -1976,6 +1977,17 @@ func calcDualSubnetStatusIP(subnet *kubeovnv1.Subnet, c *Controller) error {
return err
}

usingIPNums := len(podUsedIPs)
for _, podUsedIP := range podUsedIPs {
for _, excludeIP := range subnet.Spec.ExcludeIps {
if util.ContainsIPs(excludeIP, podUsedIP.Spec.V4IPAddress) || util.ContainsIPs(excludeIP, podUsedIP.Spec.V6IPAddress) {
// This ip cr is allocated from subnet.spec.excludeIPs, do not count it as usingIPNums
usingIPNums--
break
}
}
}

// subnet.Spec.ExcludeIps contains both v4 and v6 addresses
v4ExcludeIPs, v6ExcludeIPs := util.SplitIpsByProtocol(subnet.Spec.ExcludeIps)
// gateway always in excludeIPs
Expand All @@ -1987,7 +1999,7 @@ func calcDualSubnetStatusIP(subnet *kubeovnv1.Subnet, c *Controller) error {
v4availableIPs := util.AddressCount(v4CIDR) - util.CountIPNums(v4toSubIPs)
v6availableIPs := util.AddressCount(v6CIDR) - util.CountIPNums(v6toSubIPs)

usingIPs := float64(len(podUsedIPs))
usingIPs := float64(usingIPNums)

vips, err := c.virtualIpsLister.List(labels.SelectorFromSet(labels.Set{
util.SubnetNameLabel: subnet.Name,
Expand Down Expand Up @@ -2059,10 +2071,21 @@ func calcSubnetStatusIP(subnet *kubeovnv1.Subnet, c *Controller) error {
klog.Error(err)
return err
}
usingIPNums := len(podUsedIPs)
for _, podUsedIP := range podUsedIPs {
for _, excludeIP := range subnet.Spec.ExcludeIps {
if util.ContainsIPs(excludeIP, podUsedIP.Spec.V4IPAddress) || util.ContainsIPs(excludeIP, podUsedIP.Spec.V6IPAddress) {
// This ip cr is allocated from subnet.spec.excludeIPs, do not count it as usingIPNums
usingIPNums--
break
}
}
}

// gateway always in excludeIPs
toSubIPs := util.ExpandExcludeIPs(subnet.Spec.ExcludeIps, subnet.Spec.CIDRBlock)
availableIPs := util.AddressCount(cidr) - util.CountIPNums(toSubIPs)
usingIPs := float64(len(podUsedIPs))
usingIPs := float64(usingIPNums)
vips, err := c.virtualIpsLister.List(labels.SelectorFromSet(labels.Set{
util.SubnetNameLabel: subnet.Name,
util.IPReservedLabel: "",
Expand Down

0 comments on commit 32604cf

Please sign in to comment.