Skip to content

Commit

Permalink
fix: check ls exists before handle it
Browse files Browse the repository at this point in the history
  • Loading branch information
oilbeater committed Aug 2, 2019
1 parent 5e06eae commit 661387e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 26 deletions.
73 changes: 47 additions & 26 deletions pkg/controller/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,39 +175,40 @@ func (c *Controller) handleAddSubnet(key string) error {
}
return err
}
existSubnets, err := c.ovnClient.ListLogicalSwitch()
exist, err := c.ovnClient.LogicalSwitchExists(subnet.Name)
if err != nil {
klog.Errorf("failed to list exist subnet")
klog.Errorf("failed to list logical switch, %v", err)
return err
}
for _, s := range existSubnets {
if s == subnet.Name {
return nil
}
}

if err = util.ValidateSubnet(*subnet); err != nil {
klog.Error(err)
c.recorder.Eventf(subnet, v1.EventTypeWarning, "ValidateLogicalSwitchFailed", err.Error())
return err
}
subnetList, err := c.subnetsLister.List(labels.Everything())
if err != nil {
klog.Errorf("failed to list subnets %v", err)
return err
}
for _, sub := range subnetList {
if sub.Name != subnet.Name && util.CIDRConflict(sub.Spec.CIDRBlock, subnet.Spec.CIDRBlock) {
err = fmt.Errorf("subnet %s cidr %s conflict with subnet %s cidr %s", subnet.Name, subnet.Spec.CIDRBlock, sub.Name, sub.Spec.CIDRBlock)
if !exist {
if err = util.ValidateSubnet(*subnet); err != nil {
klog.Error(err)
subnet.TypeMeta.Kind = "Subnet"
subnet.TypeMeta.APIVersion = "kubeovn.io/v1"
c.recorder.Eventf(subnet, v1.EventTypeWarning, "ValidateLogicalSwitchFailed", err.Error())
return err
}
}
// If multiple namespace use same ls name, only first one will success
err = c.ovnClient.CreateLogicalSwitch(subnet.Name, subnet.Spec.Protocol, subnet.Spec.CIDRBlock, subnet.Spec.Gateway, subnet.Spec.ExcludeIps)
if err != nil {
return err
subnetList, err := c.subnetsLister.List(labels.Everything())
if err != nil {
klog.Errorf("failed to list subnets %v", err)
return err
}
for _, sub := range subnetList {
if sub.Name != subnet.Name && util.CIDRConflict(sub.Spec.CIDRBlock, subnet.Spec.CIDRBlock) {
err = fmt.Errorf("subnet %s cidr %s conflict with subnet %s cidr %s", subnet.Name, subnet.Spec.CIDRBlock, sub.Name, sub.Spec.CIDRBlock)
klog.Error(err)
subnet.TypeMeta.Kind = "Subnet"
subnet.TypeMeta.APIVersion = "kubeovn.io/v1"
c.recorder.Eventf(subnet, v1.EventTypeWarning, "ValidateLogicalSwitchFailed", err.Error())
return err
}
}
// If multiple namespace use same ls name, only first one will success
err = c.ovnClient.CreateLogicalSwitch(subnet.Name, subnet.Spec.Protocol, subnet.Spec.CIDRBlock, subnet.Spec.Gateway, subnet.Spec.ExcludeIps)
if err != nil {
return err
}
}

if subnet.Spec.Private {
Expand All @@ -224,8 +225,19 @@ func (c *Controller) handleUpdateSubnet(key string) error {
}
return err
}
exist, err := c.ovnClient.LogicalSwitchExists(subnet.Name)
if err != nil {
klog.Errorf("failed to list logical switch, %v", err)
return err
}
if !exist {
c.addSubnetQueue.AddRateLimited(key)
return nil
}
if err = util.ValidateSubnet(*subnet); err != nil {
klog.Error(err)
subnet.TypeMeta.Kind = "Subnet"
subnet.TypeMeta.APIVersion = "kubeovn.io/v1"
c.recorder.Eventf(subnet, v1.EventTypeWarning, "ValidateLogicalSwitchFailed", err.Error())
return err
}
Expand All @@ -238,7 +250,16 @@ func (c *Controller) handleUpdateSubnet(key string) error {
}

func (c *Controller) handleDeleteSubnet(key string) error {
err := c.ovnClient.CleanLogicalSwitchAcl(key)
exist, err := c.ovnClient.LogicalSwitchExists(key)
if err != nil {
klog.Errorf("failed to list logical switch, %v", err)
return err
}
if !exist {
return nil
}

err = c.ovnClient.CleanLogicalSwitchAcl(key)
if err != nil {
klog.Errorf("failed to delete acl of logical switch %s %v", key, err)
return err
Expand Down
13 changes: 13 additions & 0 deletions pkg/ovs/ovn-nbctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,19 @@ func (c Client) ListLogicalSwitch() ([]string, error) {
return result, nil
}

func (c Client) LogicalSwitchExists(logicalSwitch string) (bool, error) {
lss, err := c.ListLogicalSwitch()
if err != nil {
return false, err
}
for _, ls := range lss {
if ls == logicalSwitch {
return true, nil
}
}
return false, nil
}

// ListLogicalRouter list logical router names
func (c Client) ListLogicalRouter() ([]string, error) {
output, err := c.ovnNbCommand("lr-list")
Expand Down

0 comments on commit 661387e

Please sign in to comment.