host-local: support ip/prefix in env args and CNI args
Signed-off-by: Bruce Ma <brucema19901024@gmail.com>
This commit is contained in:
parent
8de0287741
commit
af26bab500
@ -57,11 +57,11 @@ type IPAMConfig struct {
|
|||||||
|
|
||||||
type IPAMEnvArgs struct {
|
type IPAMEnvArgs struct {
|
||||||
types.CommonArgs
|
types.CommonArgs
|
||||||
IP net.IP `json:"ip,omitempty"`
|
IP ip.IP `json:"ip,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type IPAMArgs struct {
|
type IPAMArgs struct {
|
||||||
IPs []net.IP `json:"ips"`
|
IPs []*ip.IP `json:"ips"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type RangeSet []Range
|
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")
|
return nil, "", fmt.Errorf("IPAM config missing 'ipam' key")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse custom IP from env args, the top-level args config and capabilities
|
// parse custom IP from env args
|
||||||
// in runtime configuration
|
|
||||||
if envArgs != "" {
|
if envArgs != "" {
|
||||||
e := IPAMEnvArgs{}
|
e := IPAMEnvArgs{}
|
||||||
err := types.LoadArgs(envArgs, &e)
|
err := types.LoadArgs(envArgs, &e)
|
||||||
@ -93,15 +92,19 @@ func LoadIPAMConfig(bytes []byte, envArgs string) (*IPAMConfig, string, error) {
|
|||||||
return nil, "", err
|
return nil, "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if e.IP != nil {
|
if e.IP.ToIP() != nil {
|
||||||
n.IPAM.IPArgs = []net.IP{e.IP}
|
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 {
|
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 {
|
if len(n.RuntimeConfig.IPs) > 0 {
|
||||||
for _, i := range n.RuntimeConfig.IPs {
|
for _, i := range n.RuntimeConfig.IPs {
|
||||||
n.IPAM.IPArgs = append(n.IPAM.IPArgs, i.ToIP())
|
n.IPAM.IPArgs = append(n.IPAM.IPArgs, i.ToIP())
|
||||||
|
@ -205,8 +205,9 @@ var _ = Describe("IPAM config", func() {
|
|||||||
}))
|
}))
|
||||||
})
|
})
|
||||||
|
|
||||||
It("Should parse CNI_ARGS env", func() {
|
Context("Should parse CNI_ARGS env", func() {
|
||||||
input := `{
|
It("without prefix", func() {
|
||||||
|
input := `{
|
||||||
"cniVersion": "0.3.1",
|
"cniVersion": "0.3.1",
|
||||||
"name": "mynet",
|
"name": "mynet",
|
||||||
"type": "ipvlan",
|
"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)
|
conf, _, err := LoadIPAMConfig([]byte(input), envArgs)
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
Expect(conf.IPArgs).To(Equal([]net.IP{{10, 1, 2, 10}}))
|
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() {
|
Context("Should parse config args", func() {
|
||||||
input := `{
|
It("without prefix", func() {
|
||||||
|
input := `{
|
||||||
"cniVersion": "0.3.1",
|
"cniVersion": "0.3.1",
|
||||||
"name": "mynet",
|
"name": "mynet",
|
||||||
"type": "ipvlan",
|
"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)
|
conf, _, err := LoadIPAMConfig([]byte(input), envArgs)
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
Expect(conf.IPArgs).To(Equal([]net.IP{
|
Expect(conf.IPArgs).To(Equal([]net.IP{
|
||||||
{10, 1, 2, 10},
|
{10, 1, 2, 10},
|
||||||
{10, 1, 2, 11},
|
{10, 1, 2, 11},
|
||||||
{11, 11, 11, 11},
|
{11, 11, 11, 11},
|
||||||
net.ParseIP("2001:db8:1::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() {
|
It("Should detect overlap between rangesets", func() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user