ipam/host-local: support multiple IP ranges
This change allows the host-local allocator to allocate multiple IPs. This is intended to enable dual-stack, but is not limited to only two subnets or separate address families.
This commit is contained in:
@ -20,35 +20,51 @@ import (
|
||||
"net"
|
||||
|
||||
"github.com/containernetworking/cni/pkg/types"
|
||||
types020 "github.com/containernetworking/cni/pkg/types/020"
|
||||
)
|
||||
|
||||
// IPAMConfig represents the IP related network configuration.
|
||||
// This nests Range because we initially only supported a single
|
||||
// range directly, and wish to preserve backwards compatability
|
||||
type IPAMConfig struct {
|
||||
*Range
|
||||
Name string
|
||||
Type string `json:"type"`
|
||||
RangeStart net.IP `json:"rangeStart"`
|
||||
RangeEnd net.IP `json:"rangeEnd"`
|
||||
Subnet types.IPNet `json:"subnet"`
|
||||
Gateway net.IP `json:"gateway"`
|
||||
Routes []types.Route `json:"routes"`
|
||||
DataDir string `json:"dataDir"`
|
||||
ResolvConf string `json:"resolvConf"`
|
||||
Args *IPAMArgs `json:"-"`
|
||||
Type string `json:"type"`
|
||||
Routes []*types.Route `json:"routes"`
|
||||
DataDir string `json:"dataDir"`
|
||||
ResolvConf string `json:"resolvConf"`
|
||||
Ranges []Range `json:"ranges"`
|
||||
IPArgs []net.IP `json:"-"` // Requested IPs from CNI_ARGS and args
|
||||
}
|
||||
|
||||
type IPAMArgs struct {
|
||||
type IPAMEnvArgs struct {
|
||||
types.CommonArgs
|
||||
IP net.IP `json:"ip,omitempty"`
|
||||
}
|
||||
|
||||
type IPAMArgs struct {
|
||||
IPs []net.IP `json:"ips"`
|
||||
}
|
||||
|
||||
// The top-level network config, just so we can get the IPAM block
|
||||
type Net struct {
|
||||
Name string `json:"name"`
|
||||
CNIVersion string `json:"cniVersion"`
|
||||
IPAM *IPAMConfig `json:"ipam"`
|
||||
Args *struct {
|
||||
A *IPAMArgs `json:"cni"`
|
||||
} `json:"args"`
|
||||
}
|
||||
|
||||
type Range struct {
|
||||
RangeStart net.IP `json:"rangeStart,omitempty"` // The first ip, inclusive
|
||||
RangeEnd net.IP `json:"rangeEnd,omitempty"` // The last ip, inclusive
|
||||
Subnet types.IPNet `json:"subnet"`
|
||||
Gateway net.IP `json:"gateway,omitempty"`
|
||||
}
|
||||
|
||||
// NewIPAMConfig creates a NetworkConfig from the given network name.
|
||||
func LoadIPAMConfig(bytes []byte, args string) (*IPAMConfig, string, error) {
|
||||
func LoadIPAMConfig(bytes []byte, envArgs string) (*IPAMConfig, string, error) {
|
||||
n := Net{}
|
||||
if err := json.Unmarshal(bytes, &n); err != nil {
|
||||
return nil, "", err
|
||||
@ -58,12 +74,71 @@ func LoadIPAMConfig(bytes []byte, args string) (*IPAMConfig, string, error) {
|
||||
return nil, "", fmt.Errorf("IPAM config missing 'ipam' key")
|
||||
}
|
||||
|
||||
if args != "" {
|
||||
n.IPAM.Args = &IPAMArgs{}
|
||||
err := types.LoadArgs(args, n.IPAM.Args)
|
||||
// Parse custom IP from both env args *and* the top-level args config
|
||||
if envArgs != "" {
|
||||
e := IPAMEnvArgs{}
|
||||
err := types.LoadArgs(envArgs, &e)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
if e.IP != nil {
|
||||
n.IPAM.IPArgs = []net.IP{e.IP}
|
||||
}
|
||||
}
|
||||
|
||||
if n.Args != nil && n.Args.A != nil && len(n.Args.A.IPs) != 0 {
|
||||
n.IPAM.IPArgs = append(n.IPAM.IPArgs, n.Args.A.IPs...)
|
||||
}
|
||||
|
||||
for idx, _ := range n.IPAM.IPArgs {
|
||||
if err := canonicalizeIP(&n.IPAM.IPArgs[idx]); err != nil {
|
||||
return nil, "", fmt.Errorf("cannot understand ip: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// If a single range (old-style config) is specified, move it to
|
||||
// the Ranges array
|
||||
if n.IPAM.Range != nil && n.IPAM.Range.Subnet.IP != nil {
|
||||
n.IPAM.Ranges = append([]Range{*n.IPAM.Range}, n.IPAM.Ranges...)
|
||||
}
|
||||
n.IPAM.Range = nil
|
||||
|
||||
if len(n.IPAM.Ranges) == 0 {
|
||||
return nil, "", fmt.Errorf("no IP ranges specified")
|
||||
}
|
||||
|
||||
// Validate all ranges
|
||||
numV4 := 0
|
||||
numV6 := 0
|
||||
for i, _ := range n.IPAM.Ranges {
|
||||
if err := n.IPAM.Ranges[i].Canonicalize(); err != nil {
|
||||
return nil, "", fmt.Errorf("Cannot understand range %d: %v", i, err)
|
||||
}
|
||||
if len(n.IPAM.Ranges[i].RangeStart) == 4 {
|
||||
numV4++
|
||||
} else {
|
||||
numV6++
|
||||
}
|
||||
}
|
||||
|
||||
// CNI spec 0.2.0 and below supported only one v4 and v6 address
|
||||
if numV4 > 1 || numV6 > 1 {
|
||||
for _, v := range types020.SupportedVersions {
|
||||
if n.CNIVersion == v {
|
||||
return nil, "", fmt.Errorf("CNI version %v does not support more than 1 range per address family", n.CNIVersion)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check for overlaps
|
||||
l := len(n.IPAM.Ranges)
|
||||
for i, r1 := range n.IPAM.Ranges[:l-1] {
|
||||
for j, r2 := range n.IPAM.Ranges[i+1:] {
|
||||
if r1.Overlaps(&r2) {
|
||||
return nil, "", fmt.Errorf("Range %d overlaps with range %d", i, (i + j + 1))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Copy net name into IPAM so not to drag Net struct around
|
||||
@ -71,14 +146,3 @@ func LoadIPAMConfig(bytes []byte, args string) (*IPAMConfig, string, error) {
|
||||
|
||||
return n.IPAM, n.CNIVersion, nil
|
||||
}
|
||||
|
||||
func convertRoutesToCurrent(routes []types.Route) []*types.Route {
|
||||
var currentRoutes []*types.Route
|
||||
for _, r := range routes {
|
||||
currentRoutes = append(currentRoutes, &types.Route{
|
||||
Dst: r.Dst,
|
||||
GW: r.GW,
|
||||
})
|
||||
}
|
||||
return currentRoutes
|
||||
}
|
||||
|
Reference in New Issue
Block a user