Skip to content

Commit

Permalink
Check network mask when init subnet
Browse files Browse the repository at this point in the history
Signed-off-by: zhuanlan <[email protected]>
  • Loading branch information
Longchuanzheng authored and bobz965 committed Sep 29, 2024
1 parent e90a58a commit 2426209
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pkg/controller/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ func (c *Controller) InitIPAM() error {
subnetProviderMaps := make(map[string]string, len(subnets))
for _, subnet := range subnets {
subnetProviderMaps[subnet.Name] = subnet.Spec.Provider

klog.Infof("Init subnet %s", subnet.Name)

if err := c.ipam.AddOrUpdateSubnet(subnet.Name, subnet.Spec.CIDRBlock, subnet.Spec.Gateway, subnet.Spec.ExcludeIps); err != nil {
klog.Errorf("failed to init subnet %s: %v", subnet.Name, err)
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/util/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,3 +715,12 @@ func InvalidSpecialCIDR(s string) error {
}
return nil
}

func InvalidNetworkMask(network *net.IPNet) error {
if ones, bits := network.Mask.Size(); ones == bits {
err := fmt.Errorf("invalid network mask %d", ones)
klog.Error(err)
return err
}
return nil
}
13 changes: 11 additions & 2 deletions pkg/util/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,17 @@ func ValidateSubnet(subnet kubeovnv1.Subnet) error {
klog.Errorf("invalid subnet %s cidr %s, %s", subnet.Name, cidr, err)
return err
}
if _, _, err := net.ParseCIDR(cidr); err != nil {
return fmt.Errorf("subnet %s cidr %s is invalid", subnet.Name, cidr)
_, network, err := net.ParseCIDR(cidr)
if err != nil {
err = fmt.Errorf("subnet %s cidr %s is invalid, due to %w", subnet.Name, cidr, err)
klog.Error(err)
return err
}
// check network mask is 32 in ipv4 or 128 in ipv6
if err = InvalidNetworkMask(network); err != nil {
err = fmt.Errorf("subnet %s cidr %s mask is invalid, due to %w", subnet.Name, cidr, err)
klog.Error(err)
return err
}
}

Expand Down

0 comments on commit 2426209

Please sign in to comment.