forked from projectcalico/cni-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalico_cni_ipam_test.go
336 lines (304 loc) · 11.8 KB
/
calico_cni_ipam_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package main_test
import (
"context"
"fmt"
"os"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
"github.com/projectcalico/cni-plugin/testutils"
client "github.com/projectcalico/libcalico-go/lib/clientv3"
"github.com/projectcalico/libcalico-go/lib/ipam"
cnet "github.com/projectcalico/libcalico-go/lib/net"
"github.com/projectcalico/libcalico-go/lib/options"
)
var plugin = "calico-ipam"
var defaultIPv4Pool = "192.168.0.0/16"
var _ = Describe("Calico IPAM Tests", func() {
cniVersion := os.Getenv("CNI_SPEC_VERSION")
calicoClient, _ := client.NewFromEnv()
BeforeEach(func() {
testutils.WipeEtcd()
testutils.MustCreateNewIPPool(calicoClient, defaultIPv4Pool, false, false, true)
testutils.MustCreateNewIPPool(calicoClient, "fd80:24e2:f998:72d6::/64", false, false, true)
})
Describe("Run IPAM plugin", func() {
Context("Do it", func() {
DescribeTable("Request different numbers of IP addresses",
func(expectedIPv4, expectedIPv6 bool, netconf string) {
result, _, _ := testutils.RunIPAMPlugin(netconf, "ADD", "", cniVersion)
var ip4Mask, ip6Mask string
for _, ip := range result.IPs {
if ip.Version == "4" {
ip4Mask = ip.Address.Mask.String()
} else if ip.Version == "6" {
ip6Mask = ip.Address.Mask.String()
}
}
if expectedIPv4 {
Expect(ip4Mask).Should(Equal("ffffffff"))
}
if expectedIPv6 {
Expect(ip6Mask).Should(Equal("ffffffffffffffffffffffffffffffff"))
}
_, _, exitCode := testutils.RunIPAMPlugin(netconf, "DEL", "", cniVersion)
Expect(exitCode).Should(Equal(0))
},
Entry("IPAM with no configuration", true, false, fmt.Sprintf(`
{
"cniVersion": "%s",
"name": "net1",
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"log_level": "debug",
"datastore_type": "%s",
"ipam": {
"type": "%s"
}
}`, cniVersion, os.Getenv("ETCD_IP"), os.Getenv("DATASTORE_TYPE"), plugin)),
Entry("IPAM with IPv4 (explicit)", true, false, fmt.Sprintf(`
{
"cniVersion": "%s",
"name": "net1",
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"datastore_type": "%s",
"ipam": {
"type": "%s",
"assign_ipv4": "true"
}
}`, cniVersion, os.Getenv("ETCD_IP"), os.Getenv("DATASTORE_TYPE"), plugin)),
Entry("IPAM with IPv6 only", false, true, fmt.Sprintf(`
{
"cniVersion": "%s",
"name": "net1",
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"datastore_type": "%s",
"ipam": {
"type": "%s",
"assign_ipv4": "false",
"assign_ipv6": "true"
}
}`, cniVersion, os.Getenv("ETCD_IP"), os.Getenv("DATASTORE_TYPE"), plugin)),
Entry("IPAM with IPv4 and IPv6", true, true, fmt.Sprintf(`
{
"cniVersion": "%s",
"name": "net1",
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"datastore_type": "%s",
"log_level": "debug",
"ipam": {
"type": "%s",
"assign_ipv4": "true",
"assign_ipv6": "true"
}
}`, cniVersion, os.Getenv("ETCD_IP"), os.Getenv("DATASTORE_TYPE"), plugin)),
)
})
})
Describe("Run IPAM plugin - Verify IP Pools", func() {
Context("Pass valid pools", func() {
It("Uses the ipv4 pool", func() {
netconf := fmt.Sprintf(`
{
"cniVersion": "%s",
"name": "net1",
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"datastore_type": "%s",
"ipam": {
"type": "%s",
"assign_ipv4": "true",
"ipv4_pools": [ "192.168.0.0/16" ]
}
}`, cniVersion, os.Getenv("ETCD_IP"), os.Getenv("DATASTORE_TYPE"), plugin)
result, _, _ := testutils.RunIPAMPlugin(netconf, "ADD", "", cniVersion)
Expect(result.IPs[0].Address.IP.String()).Should(HavePrefix("192.168."))
})
})
Context("Pass more than one pool", func() {
It("Uses one of the ipv4 pools", func() {
testutils.MustCreateNewIPPool(calicoClient, "192.169.1.0/24", false, false, true)
netconf := fmt.Sprintf(`
{
"cniVersion": "%s",
"name": "net1",
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"datastore_type": "%s",
"ipam": {
"type": "%s",
"assign_ipv4": "true",
"ipv4_pools": [ "192.169.1.0/24", "192.168.0.0/16" ]
}
}`, cniVersion, os.Getenv("ETCD_IP"), os.Getenv("DATASTORE_TYPE"), plugin)
result, _, _ := testutils.RunIPAMPlugin(netconf, "ADD", "", cniVersion)
Expect(result.IPs[0].Address.IP.String()).Should(Or(HavePrefix("192.168."), HavePrefix("192.169.1")))
})
})
Context("Disabled IP pool", func() {
It("Never allocates from the disabled pool", func() {
netconf := fmt.Sprintf(`
{
"cniVersion": "%s",
"name": "net1",
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"datastore_type": "%s",
"ipam": {
"type": "%s",
"assign_ipv4": "true"
}
}`, cniVersion, os.Getenv("ETCD_IP"), os.Getenv("DATASTORE_TYPE"), plugin)
// Get an allocation
result, _, _ := testutils.RunIPAMPlugin(netconf, "ADD", "", cniVersion)
Expect(result.IPs[0].Address.IP.String()).Should(HavePrefix("192.168."))
// Disable the currently enabled pool
pool, err := calicoClient.IPPools().Get(context.Background(), "192-168-0-0-16", options.GetOptions{})
Expect(err).ToNot(HaveOccurred())
pool.Spec.Disabled = true
_, err = calicoClient.IPPools().Update(context.Background(), pool, options.SetOptions{})
Expect(err).ToNot(HaveOccurred())
// Create new (enabled) pool
testutils.MustCreateNewIPPool(calicoClient, "192.169.1.0/24", false, false, true)
// Get an allocation then check that it is not from the disabled pool
result, _, _ = testutils.RunIPAMPlugin(netconf, "ADD", "", cniVersion)
Expect(result.IPs[0].Address.IP.String()).Should(HavePrefix("192.169.1"))
})
})
Context("Pass an invalid pool", func() {
It("fails to get an IP", func() {
// Put the bogus pool last in the array
netconf := fmt.Sprintf(`
{
"cniVersion": "%s",
"name": "net1",
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"datastore_type": "%s",
"ipam": {
"type": "%s",
"assign_ipv4": "true",
"ipv4_pools": [ "192.168.0.0/16", "192.169.1.0/24" ]
}
}`, cniVersion, os.Getenv("ETCD_IP"), os.Getenv("DATASTORE_TYPE"), plugin)
_, err, _ := testutils.RunIPAMPlugin(netconf, "ADD", "", cniVersion)
Expect(err.Msg).Should(ContainSubstring("192.169.1.0/24) does not exist"))
})
It("fails to get an IP", func() {
// Put the bogus pool first in the array
netconf := fmt.Sprintf(`
{
"cniVersion": "%s",
"name": "net1",
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"datastore_type": "%s",
"ipam": {
"type": "%s",
"assign_ipv4": "true",
"ipv4_pools": [ "192.168.0.0/16", "192.169.1.0/24" ]
}
}`, cniVersion, os.Getenv("ETCD_IP"), os.Getenv("DATASTORE_TYPE"), plugin)
_, err, _ := testutils.RunIPAMPlugin(netconf, "ADD", "", cniVersion)
Expect(err.Msg).Should(ContainSubstring("192.169.1.0/24) does not exist"))
})
})
})
Describe("Run IPAM plugin", func() {
netconf := fmt.Sprintf(`
{
"cniVersion": "%s",
"name": "net1",
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"datastore_type": "%s",
"ipam": {
"type": "%s"
}
}`, cniVersion, os.Getenv("ETCD_IP"), os.Getenv("DATASTORE_TYPE"), plugin)
Context("Pass explicit IP address", func() {
It("Return the expected IP", func() {
result, _, _ := testutils.RunIPAMPlugin(netconf, "ADD", "IP=192.168.123.123", cniVersion)
Expect(len(result.IPs)).Should(Equal(1))
Expect(result.IPs[0].Address.String()).Should(Equal("192.168.123.123/32"))
})
It("Return the expected IP twice after deleting in the middle", func() {
result, _, _ := testutils.RunIPAMPlugin(netconf, "ADD", "IP=192.168.123.123", cniVersion)
Expect(len(result.IPs)).Should(Equal(1))
Expect(result.IPs[0].Address.String()).Should(Equal("192.168.123.123/32"))
_, _, _ = testutils.RunIPAMPlugin(netconf, "DEL", "IP=192.168.123.123", cniVersion)
result, _, _ = testutils.RunIPAMPlugin(netconf, "ADD", "IP=192.168.123.123", cniVersion)
Expect(len(result.IPs)).Should(Equal(1))
Expect(result.IPs[0].Address.String()).Should(Equal("192.168.123.123/32"))
})
It("Doesn't allow an explicit IP to be assigned twice", func() {
result, _, _ := testutils.RunIPAMPlugin(netconf, "ADD", "IP=192.168.123.123", cniVersion)
Expect(len(result.IPs)).Should(Equal(1))
Expect(result.IPs[0].Address.String()).Should(Equal("192.168.123.123/32"))
result, _, exitCode := testutils.RunIPAMPlugin(netconf, "ADD", "IP=192.168.123.123", cniVersion)
Expect(exitCode).Should(BeNumerically(">", 0))
})
})
})
Describe("Run IPAM DEL", func() {
netconf := fmt.Sprintf(`
{
"cniVersion": "%s",
"name": "net1",
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"datastore_type": "%s",
"ipam": {
"type": "%s"
}
}`, cniVersion, os.Getenv("ETCD_IP"), os.Getenv("DATASTORE_TYPE"), plugin)
It("should exit successfully even if no address exists", func() {
_, _, exitCode := testutils.RunIPAMPlugin(netconf, "DEL", "IP=192.168.123.123", cniVersion)
Expect(exitCode).Should(Equal(0))
})
Context("when using old IPAM handle", func() {
It("should remove the old handle", func() {
// Create an IP using workload.
workload := "a"
assignArgs := ipam.AssignIPArgs{
IP: cnet.MustParseIP("192.168.123.123"),
HandleID: &workload,
}
ctx := context.Background()
err := calicoClient.IPAM().AssignIP(ctx, assignArgs)
Expect(err).NotTo(HaveOccurred())
// Verify the new IP was set.
ips, err := calicoClient.IPAM().IPsByHandle(ctx, workload)
Expect(err).NotTo(HaveOccurred())
Expect(ips).To(HaveLen(1))
// Remove the IP and handle.
result, _, _ := testutils.RunIPAMPlugin(netconf, "DEL", "IP=192.168.123.123", cniVersion)
Expect(len(result.IPs)).Should(Equal(0))
// Verify that the workload handle is gone.
_, err = calicoClient.IPAM().IPsByHandle(ctx, workload)
Expect(err).To(HaveOccurred())
// Create an IP using the new network name and containerID
handleID := "net1.a"
assignArgs = ipam.AssignIPArgs{
IP: cnet.MustParseIP("192.168.123.123"),
HandleID: &handleID,
}
err = calicoClient.IPAM().AssignIP(ctx, assignArgs)
Expect(err).NotTo(HaveOccurred())
// Verify the new IP was set.
ips, err = calicoClient.IPAM().IPsByHandle(ctx, handleID)
Expect(err).NotTo(HaveOccurred())
Expect(ips).To(HaveLen(1))
// Remove the IP and handle.
result, _, _ = testutils.RunIPAMPlugin(netconf, "DEL", "IP=192.168.123.123", cniVersion)
Expect(len(result.IPs)).Should(Equal(0))
// Verify that the handleID is gone.
_, err = calicoClient.IPAM().IPsByHandle(ctx, handleID)
Expect(err).To(HaveOccurred())
})
})
})
})