Skip to content

Commit

Permalink
fix: block subnet deletion when there any ip in use
Browse files Browse the repository at this point in the history
  • Loading branch information
oilbeater committed Dec 1, 2019
1 parent 4f4d2aa commit 5efbea9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
46 changes: 40 additions & 6 deletions pkg/controller/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,23 @@ func (c *Controller) enqueueUpdateSubnet(old, new interface{}) {
oldSubnet := old.(*kubeovnv1.Subnet)
newSubnet := new.(*kubeovnv1.Subnet)

var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(new); err != nil {
utilruntime.HandleError(err)
return
}

if !newSubnet.DeletionTimestamp.IsZero() && newSubnet.Status.UsingIPs == 0 {
c.updateSubnetQueue.Add(key)
return
}

if oldSubnet.Spec.Private != newSubnet.Spec.Private ||
!reflect.DeepEqual(oldSubnet.Spec.AllowSubnets, newSubnet.Spec.AllowSubnets) ||
!reflect.DeepEqual(oldSubnet.Spec.Namespaces, newSubnet.Spec.Namespaces) ||
oldSubnet.Spec.GatewayType != newSubnet.Spec.GatewayType ||
oldSubnet.Spec.GatewayNode != newSubnet.Spec.GatewayNode {
var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(new); err != nil {
utilruntime.HandleError(err)
return
}
klog.V(3).Infof("enqueue update subnet %s", key)
c.updateSubnetQueue.Add(key)
}
Expand Down Expand Up @@ -314,6 +320,24 @@ func (c *Controller) handleAddSubnet(key string) error {
return err
}

if subnet.DeletionTimestamp.IsZero() && !util.ContainsString(subnet.Finalizers, util.ControllerName) {
subnet.Finalizers = append(subnet.Finalizers, util.ControllerName)
if _, err := c.config.KubeOvnClient.KubeovnV1().Subnets().Update(subnet); err != nil {
klog.Errorf("failed to add finalizer to subnet %s, %v", key, err)
return err
}
}

if !subnet.DeletionTimestamp.IsZero() && subnet.Status.UsingIPs == 0 {
subnet.Finalizers = util.RemoveString(subnet.Finalizers, util.ControllerName)
if _, err := c.config.KubeOvnClient.KubeovnV1().Subnets().Update(subnet); err != nil {
klog.Errorf("failed to remove finalizer from subnet %s, %v", key, err)
return err
}
// subnet will be deleted, no need for other reconcile works
return nil
}

if err = formatSubnet(subnet, c); err != nil {
return err
}
Expand Down Expand Up @@ -543,6 +567,16 @@ func (c *Controller) handleUpdateSubnet(key string) error {
return err
}

if !subnet.DeletionTimestamp.IsZero() && subnet.Status.UsingIPs == 0 {
subnet.Finalizers = util.RemoveString(subnet.Finalizers, util.ControllerName)
if _, err := c.config.KubeOvnClient.KubeovnV1().Subnets().Update(subnet); err != nil {
klog.Errorf("failed to remove finalizer from subnet %s, %v", key, err)
return err
}
// subnet will be deleted, no need for other reconcile works
return nil
}

exist, err := c.ovnClient.LogicalSwitchExists(subnet.Name)
if err != nil {
klog.Errorf("failed to list logical switch, %v", err)
Expand Down
1 change: 1 addition & 0 deletions pkg/util/const.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package util

const (
ControllerName = "kube-ovn-controller"
AllocatedAnnotation = "ovn.kubernetes.io/allocated"
MacAddressAnnotation = "ovn.kubernetes.io/mac_address"
IpAddressAnnotation = "ovn.kubernetes.io/ip_address"
Expand Down
20 changes: 20 additions & 0 deletions pkg/util/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,23 @@ func IsStringIn(str string, slice []string) bool {
}
return false
}

// Helper functions to check and remove string from a slice of strings.
func ContainsString(slice []string, s string) bool {
for _, item := range slice {
if item == s {
return true
}
}
return false
}

func RemoveString(slice []string, s string) (result []string) {
for _, item := range slice {
if item == s {
continue
}
result = append(result, item)
}
return
}

0 comments on commit 5efbea9

Please sign in to comment.