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

Remove some gratuitous CIDR unparsing and reparsing #76

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/network/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ func (pcn *ParsedClusterNetwork) CheckHostNetworks(hostIPNets []*net.IPNet) erro
errList := []error{}
for _, ipNet := range hostIPNets {
for _, clusterNetwork := range pcn.ClusterNetworks {
if CIDRsOverlap(ipNet.String(), clusterNetwork.ClusterCIDR.String()) {
if cidrsOverlap(ipNet, clusterNetwork.ClusterCIDR) {
errList = append(errList, fmt.Errorf("cluster IP: %s conflicts with host network: %s", clusterNetwork.ClusterCIDR.IP.String(), ipNet.String()))
}
}
if CIDRsOverlap(ipNet.String(), pcn.ServiceNetwork.String()) {
if cidrsOverlap(ipNet, pcn.ServiceNetwork) {
errList = append(errList, fmt.Errorf("service IP: %s conflicts with host network: %s", pcn.ServiceNetwork.String(), ipNet.String()))
}
}
Expand Down
18 changes: 5 additions & 13 deletions pkg/network/common/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func ValidateClusterNetwork(clusterNet *networkapi.ClusterNetwork) error {
allErrs = append(allErrs, field.Invalid(field.NewPath("hostsubnetlength"), clusterNet.HostSubnetLength, "subnet length must be at least 2"))
}

if (clusterIPNet != nil) && (serviceIPNet != nil) && CIDRsOverlap(clusterIPNet.String(), serviceIPNet.String()) {
if (clusterIPNet != nil) && (serviceIPNet != nil) && cidrsOverlap(clusterIPNet, serviceIPNet) {
allErrs = append(allErrs, field.Invalid(field.NewPath("serviceNetwork"), clusterNet.ServiceNetwork, "service network overlaps with cluster network"))
}
}
Expand Down Expand Up @@ -97,13 +97,13 @@ func ValidateClusterNetwork(clusterNet *networkapi.ClusterNetwork) error {
}

for _, cidr := range testedCIDRS {
if CIDRsOverlap(clusterIPNet.String(), cidr.String()) {
if cidrsOverlap(clusterIPNet, cidr) {
allErrs = append(allErrs, field.Invalid(field.NewPath("clusterNetworks").Index(i).Child("cidr"), cn.CIDR, fmt.Sprintf("cidr range overlaps with another cidr %q", cidr.String())))
}
}
testedCIDRS = append(testedCIDRS, clusterIPNet)

if (clusterIPNet != nil) && (serviceIPNet != nil) && CIDRsOverlap(clusterIPNet.String(), serviceIPNet.String()) {
if (clusterIPNet != nil) && (serviceIPNet != nil) && cidrsOverlap(clusterIPNet, serviceIPNet) {
allErrs = append(allErrs, field.Invalid(field.NewPath("serviceNetwork"), clusterNet.ServiceNetwork, fmt.Sprintf("service network overlaps with cluster network cidr: %s", clusterIPNet.String())))
}
}
Expand Down Expand Up @@ -163,14 +163,6 @@ func ValidateHostSubnet(hs *networkapi.HostSubnet) error {
}
}

func CIDRsOverlap(cidr1, cidr2 string) bool {
_, ipNet1, err := net.ParseCIDR(cidr1)
if err != nil {
return false
}
_, ipNet2, err := net.ParseCIDR(cidr2)
if err != nil {
return false
}
return ipNet1.Contains(ipNet2.IP) || ipNet2.Contains(ipNet1.IP)
func cidrsOverlap(cidr1, cidr2 *net.IPNet) bool {
return cidr1.Contains(cidr2.IP) || cidr2.Contains(cidr1.IP)
}