Skip to content

Commit

Permalink
fix: subnet status usingIp not consistent with availableIPRange and u…
Browse files Browse the repository at this point in the history
…singIPRange (#3608)

* fix: subnet status usingIp not consistent with availableIpRange and usingIpRange

---------

Signed-off-by: bobz965 <[email protected]>
  • Loading branch information
bobz965 authored Jan 9, 2024
1 parent cb28bed commit 9a77310
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 38 deletions.
20 changes: 1 addition & 19 deletions pkg/controller/ovn_eip.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,24 +288,6 @@ func (c *Controller) handleUpdateOvnEip(key string) error {
return err
}
klog.Infof("handle update ovn eip %s", cachedEip.Name)
if !cachedEip.DeletionTimestamp.IsZero() {
subnetName := cachedEip.Spec.ExternalSubnet
if subnetName == "" {
err := fmt.Errorf("failed to create ovn eip '%s', subnet should be set", key)
klog.Error(err)
return err
}
subnet, err := c.subnetsLister.Get(subnetName)
if err != nil {
klog.Errorf("failed to get external subnet, %v", err)
return err
}
if err = c.subnetCountIP(subnet); err != nil {
klog.Errorf("failed to count ovn eip '%s' in subnet, %v", cachedEip.Name, err)
return err
}
return nil
}
if cachedEip.Spec.Type != util.OvnEipTypeLSP {
// node ext gw use lsp eip, has a nic on gw node, so left node to make it ready
if err = c.patchOvnEipStatus(key, true); err != nil {
Expand Down Expand Up @@ -353,13 +335,13 @@ func (c *Controller) handleDelOvnEip(key string) error {
return err
}
}

if eip.Spec.Type == util.OvnEipTypeLRP {
if err := c.OVNNbClient.DeleteLogicalRouterPort(eip.Name); err != nil {
klog.Errorf("failed to delete lrp %s, %v", eip.Name, err)
return err
}
}

if err = c.handleDelOvnEipFinalizer(eip, util.ControllerName); err != nil {
klog.Errorf("failed to handle remove ovn eip finalizer , %v", err)
return err
Expand Down
74 changes: 55 additions & 19 deletions pkg/controller/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2037,10 +2037,11 @@ func (c *Controller) calcDualSubnetStatusIP(subnet *kubeovnv1.Subnet) (*kubeovnv
klog.Error(err)
return nil, err
}

usingIPNums := len(podUsedIPs)
for _, podUsedIP := range podUsedIPs {
for _, excludeIP := range subnet.Spec.ExcludeIps {
var lenIP, lenVip, lenIptablesEip, lenOvnEip int
lenIP = len(podUsedIPs)
usingIPNums := lenIP
for _, excludeIP := range subnet.Spec.ExcludeIps {
for _, podUsedIP := range podUsedIPs {
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--
Expand Down Expand Up @@ -2070,7 +2071,8 @@ func (c *Controller) calcDualSubnetStatusIP(subnet *kubeovnv1.Subnet) (*kubeovnv
klog.Error(err)
return nil, err
}
usingIPs += float64(len(vips))
lenVip = len(vips)
usingIPs += float64(lenVip)

if !isOvnSubnet(subnet) {
eips, err := c.iptablesEipsLister.List(
Expand All @@ -2079,8 +2081,21 @@ func (c *Controller) calcDualSubnetStatusIP(subnet *kubeovnv1.Subnet) (*kubeovnv
klog.Error(err)
return nil, err
}
usingIPs += float64(len(eips))
lenIptablesEip = len(eips)
usingIPs += float64(lenIptablesEip)
}
if subnet.Spec.Vlan != "" {
ovnEips, err := c.ovnEipsLister.List(labels.SelectorFromSet(labels.Set{
util.SubnetNameLabel: subnet.Name,
}))
if err != nil {
klog.Error(err)
return nil, err
}
lenOvnEip := len(ovnEips)
usingIPs += float64(lenOvnEip)
}

v4availableIPs -= usingIPs
if v4availableIPs < 0 {
v4availableIPs = 0
Expand All @@ -2103,6 +2118,13 @@ func (c *Controller) calcDualSubnetStatusIP(subnet *kubeovnv1.Subnet) (*kubeovnv
return subnet, nil
}

if v4UsingIPStr == "" && v6UsingIPStr == "" && usingIPs != 0 {
// in case of subnet deletion, v4 v6 using ip should be 0
err = fmt.Errorf("ipam subnet %s has no ip in using, but some ip cr left: ip %d, vip %d, iptable eip %d, ovn eip %d", subnet.Name, lenIP, lenVip, lenIptablesEip, lenOvnEip)
klog.Error(err)
return nil, err
}

subnet.Status.V4AvailableIPs = v4availableIPs
subnet.Status.V6AvailableIPs = v6availableIPs
subnet.Status.V4UsingIPs = usingIPs
Expand All @@ -2127,14 +2149,16 @@ func (c *Controller) calcSubnetStatusIP(subnet *kubeovnv1.Subnet) (*kubeovnv1.Su
klog.Error(err)
return nil, err
}
var lenIP, lenVip, lenIptablesEip, lenOvnEip int
podUsedIPs, err := c.ipsLister.List(labels.SelectorFromSet(labels.Set{subnet.Name: ""}))
if err != nil {
klog.Error(err)
return nil, err
}
usingIPNums := len(podUsedIPs)
for _, podUsedIP := range podUsedIPs {
for _, excludeIP := range subnet.Spec.ExcludeIps {
lenIP = len(podUsedIPs)
usingIPNums := lenIP
for _, excludeIP := range subnet.Spec.ExcludeIps {
for _, podUsedIP := range podUsedIPs {
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--
Expand All @@ -2155,23 +2179,28 @@ func (c *Controller) calcSubnetStatusIP(subnet *kubeovnv1.Subnet) (*kubeovnv1.Su
klog.Error(err)
return nil, err
}
usingIPs += float64(len(vips))
ovnEips, err := c.ovnEipsLister.List(labels.SelectorFromSet(labels.Set{
util.SubnetNameLabel: subnet.Name,
}))
if err != nil {
klog.Error(err)
return nil, err
}
usingIPs += float64(len(ovnEips))
lenVip = len(vips)
usingIPs += float64(lenVip)
if !isOvnSubnet(subnet) {
eips, err := c.iptablesEipsLister.List(
labels.SelectorFromSet(labels.Set{util.SubnetNameLabel: subnet.Name}))
if err != nil {
klog.Error(err)
return nil, err
}
usingIPs += float64(len(eips))
lenIptablesEip = len(eips)
usingIPs += float64(lenIptablesEip)
}
if subnet.Spec.Vlan != "" {
ovnEips, err := c.ovnEipsLister.List(labels.SelectorFromSet(labels.Set{
util.SubnetNameLabel: subnet.Name,
}))
if err != nil {
klog.Error(err)
return nil, err
}
lenOvnEip = len(ovnEips)
usingIPs += float64(lenOvnEip)
}

availableIPs -= usingIPs
Expand Down Expand Up @@ -2222,6 +2251,13 @@ func (c *Controller) calcSubnetStatusIP(subnet *kubeovnv1.Subnet) (*kubeovnv1.Su
return subnet, nil
}

if v4UsingIPStr == "" && v6UsingIPStr == "" && usingIPs != 0 {
// in case of subnet deletion, v4 v6 using ip should be 0
err = fmt.Errorf("ipam subnet %s has no ip in using, but some ip cr left: ip %d, vip %d, iptable eip %d, ovn eip %d", subnet.Name, lenIP, lenVip, lenIptablesEip, lenOvnEip)
klog.Error(err)
return nil, err
}

bytes, err := subnet.Status.Bytes()
if err != nil {
klog.Error(err)
Expand Down

0 comments on commit 9a77310

Please sign in to comment.