Skip to content

Commit

Permalink
add pod static ip validate
Browse files Browse the repository at this point in the history
  • Loading branch information
Icedroid committed Dec 6, 2020
1 parent 27c5718 commit 2aecb3d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,12 @@ func (c *Controller) handleAddPod(key string) error {
pod.Annotations[fmt.Sprintf(util.LogicalSwitchAnnotationTemplate, subnet.Spec.Provider)] = subnet.Name
pod.Annotations[fmt.Sprintf(util.AllocatedAnnotationTemplate, subnet.Spec.Provider)] = "true"

if err := util.ValidatePodCidr(subnet.Spec.CIDRBlock, ip); err != nil {
klog.Errorf("validate pod %s/%s failed, %v", namespace, name, err)
c.recorder.Eventf(pod, v1.EventTypeWarning, "ValidatePodNetworkFailed", err.Error())
return err
}

if isOvnSubnet(subnet) {
if subnet.Spec.Vlan != "" {
vlan, err := c.vlansLister.Get(subnet.Spec.Vlan)
Expand Down
11 changes: 11 additions & 0 deletions pkg/util/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ func ValidatePodNetwork(annotations map[string]string) error {
return nil
}

func ValidatePodCidr(cidrStr, ipStr string) error {
if SubnetBroadCast(cidrStr) == ipStr {
return fmt.Errorf("%s is the broadcast ip in cidr %s", ipStr, cidrStr)
}
if SubnetNumber(cidrStr) == ipStr {
return fmt.Errorf("%s is the network number ip in cidr %s", ipStr, cidrStr)
}

return nil
}

func ValidateVlan(vlan int, vlanRange string) error {
vlans := strings.Split(vlanRange, ",")
if len(vlans) != 2 {
Expand Down

0 comments on commit 2aecb3d

Please sign in to comment.