Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check network mask when init subnet #4494

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
bobz965 marked this conversation as resolved.
Show resolved Hide resolved
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)
bobz965 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading