diff --git a/plugins/ipam/host-local/backend/allocator/config.go b/plugins/ipam/host-local/backend/allocator/config.go index a5ea3b08..ec8bf639 100644 --- a/plugins/ipam/host-local/backend/allocator/config.go +++ b/plugins/ipam/host-local/backend/allocator/config.go @@ -57,11 +57,11 @@ type IPAMConfig struct { type IPAMEnvArgs struct { types.CommonArgs - IP net.IP `json:"ip,omitempty"` + IP ip.IP `json:"ip,omitempty"` } type IPAMArgs struct { - IPs []net.IP `json:"ips"` + IPs []*ip.IP `json:"ips"` } type RangeSet []Range @@ -84,8 +84,7 @@ func LoadIPAMConfig(bytes []byte, envArgs string) (*IPAMConfig, string, error) { return nil, "", fmt.Errorf("IPAM config missing 'ipam' key") } - // Parse custom IP from env args, the top-level args config and capabilities - // in runtime configuration + // parse custom IP from env args if envArgs != "" { e := IPAMEnvArgs{} err := types.LoadArgs(envArgs, &e) @@ -93,15 +92,19 @@ func LoadIPAMConfig(bytes []byte, envArgs string) (*IPAMConfig, string, error) { return nil, "", err } - if e.IP != nil { - n.IPAM.IPArgs = []net.IP{e.IP} + if e.IP.ToIP() != nil { + n.IPAM.IPArgs = []net.IP{e.IP.ToIP()} } } + // parse custom IPs from CNI args in network config 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 _, i := range n.Args.A.IPs { + n.IPAM.IPArgs = append(n.IPAM.IPArgs, i.ToIP()) + } } + // parse custom IPs from runtime configuration if len(n.RuntimeConfig.IPs) > 0 { for _, i := range n.RuntimeConfig.IPs { n.IPAM.IPArgs = append(n.IPAM.IPArgs, i.ToIP()) diff --git a/plugins/ipam/host-local/backend/allocator/config_test.go b/plugins/ipam/host-local/backend/allocator/config_test.go index b6d7c1db..4c0e0a89 100644 --- a/plugins/ipam/host-local/backend/allocator/config_test.go +++ b/plugins/ipam/host-local/backend/allocator/config_test.go @@ -205,8 +205,9 @@ var _ = Describe("IPAM config", func() { })) }) - It("Should parse CNI_ARGS env", func() { - input := `{ + Context("Should parse CNI_ARGS env", func() { + It("without prefix", func() { + input := `{ "cniVersion": "0.3.1", "name": "mynet", "type": "ipvlan", @@ -224,16 +225,43 @@ var _ = Describe("IPAM config", func() { } }` - envArgs := "IP=10.1.2.10" + envArgs := "IP=10.1.2.10" - conf, _, err := LoadIPAMConfig([]byte(input), envArgs) - Expect(err).NotTo(HaveOccurred()) - Expect(conf.IPArgs).To(Equal([]net.IP{{10, 1, 2, 10}})) + conf, _, err := LoadIPAMConfig([]byte(input), envArgs) + Expect(err).NotTo(HaveOccurred()) + Expect(conf.IPArgs).To(Equal([]net.IP{{10, 1, 2, 10}})) + }) + It("with prefix", func() { + input := `{ + "cniVersion": "0.3.1", + "name": "mynet", + "type": "ipvlan", + "master": "foo0", + "ipam": { + "type": "host-local", + "ranges": [[ + { + "subnet": "10.1.2.0/24", + "rangeStart": "10.1.2.9", + "rangeEnd": "10.1.2.20", + "gateway": "10.1.2.30" + } + ]] + } + }` + + envArgs := "IP=10.1.2.11/24" + + conf, _, err := LoadIPAMConfig([]byte(input), envArgs) + Expect(err).NotTo(HaveOccurred()) + Expect(conf.IPArgs).To(Equal([]net.IP{{10, 1, 2, 11}})) + }) }) - It("Should parse config args", func() { - input := `{ + Context("Should parse config args", func() { + It("without prefix", func() { + input := `{ "cniVersion": "0.3.1", "name": "mynet", "type": "ipvlan", @@ -265,16 +293,62 @@ var _ = Describe("IPAM config", func() { } }` - envArgs := "IP=10.1.2.10" + envArgs := "IP=10.1.2.10" - conf, _, err := LoadIPAMConfig([]byte(input), envArgs) - Expect(err).NotTo(HaveOccurred()) - Expect(conf.IPArgs).To(Equal([]net.IP{ - {10, 1, 2, 10}, - {10, 1, 2, 11}, - {11, 11, 11, 11}, - net.ParseIP("2001:db8:1::11"), - })) + conf, _, err := LoadIPAMConfig([]byte(input), envArgs) + Expect(err).NotTo(HaveOccurred()) + Expect(conf.IPArgs).To(Equal([]net.IP{ + {10, 1, 2, 10}, + {10, 1, 2, 11}, + {11, 11, 11, 11}, + net.ParseIP("2001:db8:1::11"), + })) + }) + + It("with prefix", func() { + input := `{ + "cniVersion": "0.3.1", + "name": "mynet", + "type": "ipvlan", + "master": "foo0", + "args": { + "cni": { + "ips": [ "10.1.2.11/24", "11.11.11.11/24", "2001:db8:1::11/64"] + } + }, + "ipam": { + "type": "host-local", + "ranges": [ + [{ + "subnet": "10.1.2.0/24", + "rangeStart": "10.1.2.9", + "rangeEnd": "10.1.2.20", + "gateway": "10.1.2.30" + }], + [{ + "subnet": "11.1.2.0/24", + "rangeStart": "11.1.2.9", + "rangeEnd": "11.1.2.20", + "gateway": "11.1.2.30" + }], + [{ + "subnet": "2001:db8:1::/64" + }] + ] + } + }` + + envArgs := "IP=10.1.2.10/24" + + conf, _, err := LoadIPAMConfig([]byte(input), envArgs) + Expect(err).NotTo(HaveOccurred()) + Expect(conf.IPArgs).To(Equal([]net.IP{ + {10, 1, 2, 10}, + {10, 1, 2, 11}, + {11, 11, 11, 11}, + net.ParseIP("2001:db8:1::11"), + })) + }) }) It("Should detect overlap between rangesets", func() {