Skip to content

Commit

Permalink
ipam/host-local: Accept ip ranges as a runtime argument
Browse files Browse the repository at this point in the history
This allows for the runtime to dynamically request IP ranges.

Fixes: #95
  • Loading branch information
Casey Callendrello committed Dec 11, 2017
1 parent 03e316b commit b03d23a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
4 changes: 4 additions & 0 deletions plugins/ipam/host-local/README.md
Expand Up @@ -120,6 +120,10 @@ The following [args conventions](https://github.com/containernetworking/cni/blob

* `ips` (array of strings): A list of custom IPs to attempt to allocate

The following [Capability Args](https://github.com/containernetworking/cni/blob/master/CONVENTIONS.md) are supported:

* `ipRanges`: The exact same as the `ranges` array - a list of address pools

### Custom IP allocation
For every requested custom IP, the `host-local` allocator will request that IP
if it falls within one of the `range` objects. Thus it is possible to specify
Expand Down
19 changes: 14 additions & 5 deletions plugins/ipam/host-local/backend/allocator/config.go
Expand Up @@ -23,12 +23,16 @@ import (
types020 "github.com/containernetworking/cni/pkg/types/020"
)

// The top-level network config, just so we can get the IPAM block
// The top-level network config - IPAM plugins are passed the full configuration
// of the calling plugin, not just the IPAM section.
type Net struct {
Name string `json:"name"`
CNIVersion string `json:"cniVersion"`
IPAM *IPAMConfig `json:"ipam"`
Args *struct {
Name string `json:"name"`
CNIVersion string `json:"cniVersion"`
IPAM *IPAMConfig `json:"ipam"`
RuntimeConfig struct { // The capability arg
IPRanges []RangeSet `json:"ipRanges,omitempty"`
} `json:"runtimeConfig,omitempty"`
Args *struct {
A *IPAMArgs `json:"cni"`
} `json:"args"`
}
Expand Down Expand Up @@ -106,6 +110,11 @@ func LoadIPAMConfig(bytes []byte, envArgs string) (*IPAMConfig, string, error) {
}
n.IPAM.Range = nil

// If a range is supplied as a runtime config, prepend it to the Ranges
if len(n.RuntimeConfig.IPRanges) > 0 {
n.IPAM.Ranges = append(n.RuntimeConfig.IPRanges, n.IPAM.Ranges...)
}

if len(n.IPAM.Ranges) == 0 {
return nil, "", fmt.Errorf("no IP ranges specified")
}
Expand Down
19 changes: 18 additions & 1 deletion plugins/ipam/host-local/backend/allocator/config_test.go
Expand Up @@ -132,12 +132,18 @@ var _ = Describe("IPAM config", func() {
}))
})

It("Should parse a mixed config", func() {
It("Should parse a mixed config with runtime args", func() {
input := `{
"cniVersion": "0.3.1",
"name": "mynet",
"type": "ipvlan",
"master": "foo0",
"runtimeConfig": {
"irrelevant": "a",
"ipRanges": [
[{ "subnet": "12.1.3.0/24" }]
]
},
"ipam": {
"type": "host-local",
"subnet": "10.1.2.0/24",
Expand All @@ -162,6 +168,17 @@ var _ = Describe("IPAM config", func() {
Name: "mynet",
Type: "host-local",
Ranges: []RangeSet{
{ // The RuntimeConfig should always be first
{
RangeStart: net.IP{12, 1, 3, 1},
RangeEnd: net.IP{12, 1, 3, 254},
Gateway: net.IP{12, 1, 3, 1},
Subnet: types.IPNet{
IP: net.IP{12, 1, 3, 0},
Mask: net.CIDRMask(24, 32),
},
},
},
{
{
RangeStart: net.IP{10, 1, 2, 9},
Expand Down

0 comments on commit b03d23a

Please sign in to comment.