Move cidrmap to subnet package

This will enable reuse outside of gce.
This commit is contained in:
Justin Santa Barbara 2021-05-23 12:03:09 -04:00 committed by justinsb
parent 8b9f4ec41c
commit 1db266f15a
4 changed files with 141 additions and 74 deletions

View File

@ -2,9 +2,13 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = ["subnet.go"],
srcs = [
"cidrmap.go",
"subnet.go",
],
importpath = "k8s.io/kops/pkg/util/subnet",
visibility = ["//visibility:public"],
deps = ["//vendor/k8s.io/klog/v2:go_default_library"],
)
go_test(

126
pkg/util/subnet/cidrmap.go Normal file
View File

@ -0,0 +1,126 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package subnet
import (
"encoding/binary"
"fmt"
"net"
"k8s.io/klog/v2"
)
// CIDRMap is a helper structure to allocate unused CIDRs
type CIDRMap struct {
used []net.IPNet
}
func (c *CIDRMap) MarkInUse(s string) error {
_, cidr, err := net.ParseCIDR(s)
if err != nil {
return fmt.Errorf("error parsing network cidr %q: %v", s, err)
}
c.used = append(c.used, *cidr)
return nil
}
func incrementIP(ip net.IP, mask net.IPMask) error {
maskOnes, maskBits := mask.Size()
if maskBits == 32 {
ip4 := ip.To4()
n := binary.BigEndian.Uint32(ip4)
n += 1 << uint(32-maskOnes)
binary.BigEndian.PutUint32(ip, n)
} else {
ipv6 := ip.To16()
high := binary.BigEndian.Uint64(ipv6[0:8])
low := binary.BigEndian.Uint64(ipv6[8:16])
newHigh := high
newLow := low
shift := 128 - maskOnes
if shift >= 64 {
newHigh += 1 << uint64(shift-64)
} else {
newLow += 1 << uint64(shift)
// Did we overflow?
if newLow < low {
newHigh++
}
}
binary.BigEndian.PutUint64(ip[0:8], newHigh)
binary.BigEndian.PutUint64(ip[8:16], newLow)
}
return nil
}
func duplicateIP(src net.IP) net.IP {
ret := make(net.IP, len(src))
copy(ret, src)
return ret
}
func (c *CIDRMap) Allocate(from string, mask net.IPMask) (*net.IPNet, error) {
_, cidr, err := net.ParseCIDR(from)
if err != nil {
return nil, fmt.Errorf("error parsing CIDR %q: %v", from, err)
}
var candidate net.IPNet
candidate.Mask = mask
candidate.IP = duplicateIP(cidr.IP)
for {
// Note we increment first, so we won't ever use the first range (e.g. 10.0.0.0/n)
if err := incrementIP(candidate.IP, mask); err != nil {
return nil, err
}
// Check we're still in the range we're drawing from
if !cidrsOverlap(cidr, &candidate) {
klog.Infof("candidate CIDR %v is not in CIDR %v", candidate, cidr)
break
}
if !c.isInUse(&candidate) {
if err := c.MarkInUse(candidate.String()); err != nil {
return nil, err
}
return &candidate, nil
}
}
return nil, fmt.Errorf("cannot allocate CIDR of size %v", mask)
}
func (c *CIDRMap) isInUse(n *net.IPNet) bool {
for i := range c.used {
if cidrsOverlap(&c.used[i], n) {
return true
}
}
return false
}
// cidrsOverlap returns true if and only if the two CIDRs are non-disjoint
func cidrsOverlap(l, r *net.IPNet) bool {
return l.Contains(r.IP) || r.Contains(l.IP)
}

View File

@ -23,6 +23,7 @@ go_library(
"//dnsprovider/pkg/dnsprovider/providers/google/clouddns:go_default_library",
"//pkg/apis/kops:go_default_library",
"//pkg/cloudinstances:go_default_library",
"//pkg/util/subnet:go_default_library",
"//protokube/pkg/etcd:go_default_library",
"//upup/pkg/fi:go_default_library",
"//vendor/golang.org/x/oauth2/google:go_default_library",

View File

@ -18,13 +18,13 @@ package gce
import (
"context"
"encoding/binary"
"fmt"
"net"
compute "google.golang.org/api/compute/v1"
"k8s.io/klog/v2"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/util/subnet"
"k8s.io/kops/upup/pkg/fi"
)
@ -90,7 +90,7 @@ func performNetworkAssignmentsIPAliases(ctx context.Context, c *kops.Cluster, cl
subnets = append(subnets, l...)
}
var used cidrMap
var used subnet.CIDRMap
for _, subnet := range subnets {
if !subnetURLs[subnet.SelfLink] {
continue
@ -109,90 +109,26 @@ func performNetworkAssignmentsIPAliases(ctx context.Context, c *kops.Cluster, cl
// CIDRs should be in the RFC1918 range, but otherwise we have no constraints
networkCIDR := "10.0.0.0/8"
podCIDR, err := used.Allocate(networkCIDR, 14)
podCIDR, err := used.Allocate(networkCIDR, net.CIDRMask(14, 32))
if err != nil {
return err
}
serviceCIDR, err := used.Allocate(networkCIDR, 20)
serviceCIDR, err := used.Allocate(networkCIDR, net.CIDRMask(20, 32))
if err != nil {
return err
}
nodeCIDR, err := used.Allocate(networkCIDR, 20)
nodeCIDR, err := used.Allocate(networkCIDR, net.CIDRMask(20, 32))
if err != nil {
return err
}
klog.Infof("Will use %s for Nodes, %s for Pods and %s for Services", nodeCIDR, podCIDR, serviceCIDR)
klog.Infof("Will use %v for Nodes, %v for Pods and %v for Services", nodeCIDR, podCIDR, serviceCIDR)
nodeSubnet.CIDR = nodeCIDR
c.Spec.PodCIDR = podCIDR
c.Spec.ServiceClusterIPRange = serviceCIDR
nodeSubnet.CIDR = nodeCIDR.String()
c.Spec.PodCIDR = podCIDR.String()
c.Spec.ServiceClusterIPRange = serviceCIDR.String()
return nil
}
// cidrMap is a helper structure to allocate unused CIDRs
type cidrMap struct {
used []net.IPNet
}
func (c *cidrMap) MarkInUse(s string) error {
_, cidr, err := net.ParseCIDR(s)
if err != nil {
return fmt.Errorf("error parsing network cidr %q: %v", s, err)
}
c.used = append(c.used, *cidr)
return nil
}
func (c *cidrMap) Allocate(from string, mask int) (string, error) {
_, cidr, err := net.ParseCIDR(from)
if err != nil {
return "", fmt.Errorf("error parsing CIDR %q: %v", from, err)
}
i := *cidr
i.Mask = net.CIDRMask(mask, 32)
for {
ip4 := i.IP.To4()
if ip4 == nil {
return "", fmt.Errorf("expected IPv4 address: %v", from)
}
// Note we increment first, so we won't ever use the first range (e.g. 10.0.0.0/n)
n := binary.BigEndian.Uint32(ip4)
n += 1 << uint(32-mask)
binary.BigEndian.PutUint32(i.IP, n)
if !cidrsOverlap(cidr, &i) {
break
}
if !c.isInUse(&i) {
if err := c.MarkInUse(i.String()); err != nil {
return "", err
}
return i.String(), nil
}
}
return "", fmt.Errorf("cannot allocate CIDR of size %d", mask)
}
func (c *cidrMap) isInUse(n *net.IPNet) bool {
for i := range c.used {
if cidrsOverlap(&c.used[i], n) {
return true
}
}
return false
}
// cidrsOverlap returns true if and only if the two CIDRs are non-disjoint
func cidrsOverlap(l, r *net.IPNet) bool {
return l.Contains(r.IP) || r.Contains(l.IP)
}