Skip to content

Commit

Permalink
fix: validate if node address conflict with subnet cidr
Browse files Browse the repository at this point in the history
  • Loading branch information
oilbeater committed May 6, 2020
1 parent ef99222 commit eb4cb1b
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions pkg/controller/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ func (c *Controller) handleAddSubnet(key string) error {
}
}
}

subnetList, err := c.subnetsLister.List(labels.Everything())
if err != nil {
klog.Errorf("failed to list subnets %v", err)
Expand All @@ -449,6 +450,35 @@ func (c *Controller) handleAddSubnet(key string) error {
return err
}
}

nodes, err := c.nodesLister.List(labels.Everything())
if err != nil {
klog.Errorf("failed to list nodes %v", err)
return err
}
for _, node := range nodes {
for _, addr := range node.Status.Addresses {
if addr.Type == v1.NodeInternalIP && util.CIDRContainIP(subnet.Spec.CIDRBlock, addr.Address) {
err = fmt.Errorf("subnet %s cidr %s conflict with node %s address %s", subnet.Name, subnet.Spec.CIDRBlock, node.Name, addr.Address)
klog.Error(err)
subnet.TypeMeta.Kind = "Subnet"
subnet.TypeMeta.APIVersion = "kubeovn.io/v1"
c.recorder.Eventf(subnet, v1.EventTypeWarning, "ValidateLogicalSwitchFailed", err.Error())
subnet.Status.NotValidated("ValidateLogicalSwitchFailed", err.Error())
bytes, err1 := subnet.Status.Bytes()
if err1 != nil {
klog.Error(err1)
return err1
} else {
if _, err2 := c.config.KubeOvnClient.KubeovnV1().Subnets().Patch(subnet.Name, types.MergePatchType, bytes, "status"); err2 != nil {
klog.Error("patch subnet status failed", err2)
return err2
}
}
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 {
Expand Down

0 comments on commit eb4cb1b

Please sign in to comment.