@@ -34,6 +34,9 @@ import (
34
34
kube #36673).
35
35
3. Ported to use IPs rather than DNS names in tests since our extended
36
36
networking tests don't run with DNS.
37
+ 4. Changed to use pod IPs rather than service IPs in tests since our
38
+ NetworkPolicy implementation doesn't currently support services
39
+ correctly in Namespaces with NetworkPolicies with PodSelectors.
37
40
*/
38
41
39
42
@@ -68,19 +71,21 @@ var _ = Describe("NetworkPolicy", func() {
68
71
}
69
72
}()
70
73
framework .Logf ("Waiting for Server to come up." )
71
- err := framework .WaitForPodRunningInNamespace (f .ClientSet , podServer )
74
+ err := f .WaitForAnEndpoint (service .Name )
75
+ Expect (err ).NotTo (HaveOccurred ())
76
+ podServer , err = f .ClientSet .Core ().Pods (ns .Name ).Get (podServer .Name )
72
77
Expect (err ).NotTo (HaveOccurred ())
73
78
74
79
// Create a pod with name 'client-a', which should be able to communicate with server.
75
80
By ("Creating client which will be able to contact the server since isolation is off." )
76
- testCanConnect (f , ns , "client-can-connect" , service , 80 )
81
+ testCanConnect (f , ns , "client-can-connect" , service , podServer . Status . PodIP , 80 )
77
82
78
83
framework .Logf ("Enabling network isolation." )
79
84
setNamespaceIsolation (f , ns , "DefaultDeny" )
80
85
81
86
// Create a pod with name 'client-b', which will attempt to comunicate with the server,
82
87
// but should not be able to now that isolation is on.
83
- testCannotConnect (f , ns , "client-cannot-connect" , service , 80 )
88
+ testCannotConnect (f , ns , "client-cannot-connect" , service , podServer . Status . PodIP , 80 )
84
89
})
85
90
86
91
It ("should enforce policy based on PodSelector [Feature:NetworkPolicy]" , func () {
@@ -102,7 +107,9 @@ var _ = Describe("NetworkPolicy", func() {
102
107
}
103
108
}()
104
109
framework .Logf ("Waiting for Server to come up." )
105
- err := framework .WaitForPodRunningInNamespace (f .ClientSet , serverPod )
110
+ err := f .WaitForAnEndpoint (service .Name )
111
+ Expect (err ).NotTo (HaveOccurred ())
112
+ serverPod , err = f .ClientSet .Core ().Pods (ns .Name ).Get (serverPod .Name )
106
113
Expect (err ).NotTo (HaveOccurred ())
107
114
108
115
By ("Creating a network policy for the server which allows traffic from the pod 'client-a'." )
@@ -142,8 +149,8 @@ var _ = Describe("NetworkPolicy", func() {
142
149
}()
143
150
144
151
By ("Creating client-a which should be able to contact the server." )
145
- testCanConnect (f , ns , "client-a" , service , 80 )
146
- testCannotConnect (f , ns , "client-b" , service , 80 )
152
+ testCanConnect (f , ns , "client-a" , service , serverPod . Status . PodIP , 80 )
153
+ testCannotConnect (f , ns , "client-b" , service , serverPod . Status . PodIP , 80 )
147
154
})
148
155
149
156
It ("should enforce policy based on Ports [Feature:NetworkPolicy]" , func () {
@@ -165,18 +172,20 @@ var _ = Describe("NetworkPolicy", func() {
165
172
}
166
173
}()
167
174
framework .Logf ("Waiting for Server to come up." )
168
- err := framework .WaitForPodRunningInNamespace (f .ClientSet , serverPod )
175
+ err := f .WaitForAnEndpoint (service .Name )
176
+ Expect (err ).NotTo (HaveOccurred ())
177
+ serverPod , err = f .ClientSet .Core ().Pods (ns .Name ).Get (serverPod .Name )
169
178
Expect (err ).NotTo (HaveOccurred ())
170
179
171
180
By ("Testing pods can connect to both ports when isolation is off." )
172
- testCanConnect (f , ns , "basecase-reachable-80" , service , 80 )
173
- testCanConnect (f , ns , "basecase-reachable-81" , service , 81 )
181
+ testCanConnect (f , ns , "basecase-reachable-80" , service , serverPod . Status . PodIP , 80 )
182
+ testCanConnect (f , ns , "basecase-reachable-81" , service , serverPod . Status . PodIP , 81 )
174
183
175
184
setNamespaceIsolation (f , ns , "DefaultDeny" )
176
185
177
186
By ("Testing pods cannot by default when isolation is turned on." )
178
- testCannotConnect (f , ns , "basecase-unreachable-80" , service , 80 )
179
- testCannotConnect (f , ns , "basecase-unreachable-81" , service , 81 )
187
+ testCannotConnect (f , ns , "basecase-unreachable-80" , service , serverPod . Status . PodIP , 80 )
188
+ testCannotConnect (f , ns , "basecase-unreachable-81" , service , serverPod . Status . PodIP , 81 )
180
189
181
190
By ("Creating a network policy for the Service which allows traffic only to one port." )
182
191
policy := extensions.NetworkPolicy {
@@ -208,8 +217,8 @@ var _ = Describe("NetworkPolicy", func() {
208
217
}
209
218
}()
210
219
211
- testCannotConnect (f , ns , "client-a" , service , 80 )
212
- testCanConnect (f , ns , "client-b" , service , 81 )
220
+ testCannotConnect (f , ns , "client-a" , service , serverPod . Status . PodIP , 80 )
221
+ testCanConnect (f , ns , "client-b" , service , serverPod . Status . PodIP , 81 )
213
222
})
214
223
215
224
It ("shouldn't enforce policy when isolation is off [Feature:NetworkPolicy]" , func () {
@@ -231,12 +240,14 @@ var _ = Describe("NetworkPolicy", func() {
231
240
}
232
241
}()
233
242
framework .Logf ("Waiting for Server to come up." )
234
- err := framework .WaitForPodRunningInNamespace (f .ClientSet , serverPod )
243
+ err := f .WaitForAnEndpoint (service .Name )
244
+ Expect (err ).NotTo (HaveOccurred ())
245
+ serverPod , err = f .ClientSet .Core ().Pods (ns .Name ).Get (serverPod .Name )
235
246
Expect (err ).NotTo (HaveOccurred ())
236
247
237
248
By ("Testing pods can connect to both ports when isolation is off and no policy is defined." )
238
- testCanConnect (f , ns , "basecase-reachable-a" , service , 80 )
239
- testCanConnect (f , ns , "basecase-reachable-b" , service , 81 )
249
+ testCanConnect (f , ns , "basecase-reachable-a" , service , serverPod . Status . PodIP , 80 )
250
+ testCanConnect (f , ns , "basecase-reachable-b" , service , serverPod . Status . PodIP , 81 )
240
251
241
252
By ("Creating a network policy for the Service which allows traffic only to one port." )
242
253
policy := extensions.NetworkPolicy {
@@ -268,8 +279,8 @@ var _ = Describe("NetworkPolicy", func() {
268
279
}
269
280
}()
270
281
271
- testCanConnect (f , ns , "client-a" , service , 80 )
272
- testCanConnect (f , ns , "client-b" , service , 81 )
282
+ testCanConnect (f , ns , "client-a" , service , serverPod . Status . PodIP , 80 )
283
+ testCanConnect (f , ns , "client-b" , service , serverPod . Status . PodIP , 81 )
273
284
})
274
285
275
286
It ("should enforce multiple, stacked policies with overlapping podSelectors [Feature:NetworkPolicy]" , func () {
@@ -291,18 +302,20 @@ var _ = Describe("NetworkPolicy", func() {
291
302
}
292
303
}()
293
304
framework .Logf ("Waiting for Server to come up." )
294
- err := framework .WaitForPodRunningInNamespace (f .ClientSet , serverPod )
305
+ err := f .WaitForAnEndpoint (service .Name )
306
+ Expect (err ).NotTo (HaveOccurred ())
307
+ serverPod , err = f .ClientSet .Core ().Pods (ns .Name ).Get (serverPod .Name )
295
308
Expect (err ).NotTo (HaveOccurred ())
296
309
297
310
By ("Testing pods can connect to both ports when isolation is off." )
298
- testCanConnect (f , ns , "test-a" , service , 80 )
299
- testCanConnect (f , ns , "test-b" , service , 81 )
311
+ testCanConnect (f , ns , "test-a" , service , serverPod . Status . PodIP , 80 )
312
+ testCanConnect (f , ns , "test-b" , service , serverPod . Status . PodIP , 81 )
300
313
301
314
setNamespaceIsolation (f , ns , "DefaultDeny" )
302
315
303
316
By ("Testing pods cannot connect to either port when no policy is defined." )
304
- testCannotConnect (f , ns , "test-a-2" , service , 80 )
305
- testCannotConnect (f , ns , "test-b-2" , service , 81 )
317
+ testCannotConnect (f , ns , "test-a-2" , service , serverPod . Status . PodIP , 80 )
318
+ testCannotConnect (f , ns , "test-b-2" , service , serverPod . Status . PodIP , 81 )
306
319
307
320
By ("Creating a network policy for the Service which allows traffic only to one port." )
308
321
policy := extensions.NetworkPolicy {
@@ -364,8 +377,8 @@ var _ = Describe("NetworkPolicy", func() {
364
377
}
365
378
}()
366
379
367
- testCanConnect (f , ns , "client-a" , service , 80 )
368
- testCanConnect (f , ns , "client-b" , service , 81 )
380
+ testCanConnect (f , ns , "client-a" , service , serverPod . Status . PodIP , 80 )
381
+ testCanConnect (f , ns , "client-b" , service , serverPod . Status . PodIP , 81 )
369
382
})
370
383
371
384
It ("should support allow-all policy [Feature:NetworkPolicy]" , func () {
@@ -387,18 +400,20 @@ var _ = Describe("NetworkPolicy", func() {
387
400
}
388
401
}()
389
402
framework .Logf ("Waiting for Server to come up." )
390
- err := framework .WaitForPodRunningInNamespace (f .ClientSet , serverPod )
403
+ err := f .WaitForAnEndpoint (service .Name )
404
+ Expect (err ).NotTo (HaveOccurred ())
405
+ serverPod , err = f .ClientSet .Core ().Pods (ns .Name ).Get (serverPod .Name )
391
406
Expect (err ).NotTo (HaveOccurred ())
392
407
393
408
By ("Testing pods can connect to both ports when isolation is off." )
394
- testCanConnect (f , ns , "test-a" , service , 80 )
395
- testCanConnect (f , ns , "test-b" , service , 81 )
409
+ testCanConnect (f , ns , "test-a" , service , serverPod . Status . PodIP , 80 )
410
+ testCanConnect (f , ns , "test-b" , service , serverPod . Status . PodIP , 81 )
396
411
397
412
setNamespaceIsolation (f , ns , "DefaultDeny" )
398
413
399
414
By ("Testing pods cannot connect to either port when isolation is on." )
400
- testCannotConnect (f , ns , "test-a" , service , 80 )
401
- testCannotConnect (f , ns , "test-b" , service , 81 )
415
+ testCannotConnect (f , ns , "test-a" , service , serverPod . Status . PodIP , 80 )
416
+ testCannotConnect (f , ns , "test-b" , service , serverPod . Status . PodIP , 81 )
402
417
403
418
By ("Creating a network policy which allows all traffic." )
404
419
policy := extensions.NetworkPolicy {
@@ -423,8 +438,8 @@ var _ = Describe("NetworkPolicy", func() {
423
438
}
424
439
}()
425
440
426
- testCanConnect (f , ns , "client-a" , service , 80 )
427
- testCanConnect (f , ns , "client-b" , service , 81 )
441
+ testCanConnect (f , ns , "client-a" , service , serverPod . Status . PodIP , 80 )
442
+ testCanConnect (f , ns , "client-b" , service , serverPod . Status . PodIP , 81 )
428
443
})
429
444
430
445
It ("should enforce policy based on NamespaceSelector [Feature:NetworkPolicy]" , func () {
@@ -455,7 +470,9 @@ var _ = Describe("NetworkPolicy", func() {
455
470
}
456
471
}()
457
472
framework .Logf ("Waiting for server to come up." )
458
- err = framework .WaitForPodRunningInNamespace (f .ClientSet , serverPod )
473
+ err = f .WaitForAnEndpoint (service .Name )
474
+ Expect (err ).NotTo (HaveOccurred ())
475
+ serverPod , err = f .ClientSet .Core ().Pods (nsA .Name ).Get (serverPod .Name )
459
476
Expect (err ).NotTo (HaveOccurred ())
460
477
461
478
// Create Policy for that service that allows traffic only via namespace B
@@ -493,15 +510,15 @@ var _ = Describe("NetworkPolicy", func() {
493
510
}
494
511
}()
495
512
496
- testCannotConnect (f , nsA , "client-a" , service , 80 )
497
- testCanConnect (f , nsB , "client-b" , service , 80 )
513
+ testCannotConnect (f , nsA , "client-a" , service , serverPod . Status . PodIP , 80 )
514
+ testCanConnect (f , nsB , "client-b" , service , serverPod . Status . PodIP , 80 )
498
515
})
499
516
})
500
517
})
501
518
502
- func testCanConnect (f * framework.Framework , ns * api.Namespace , podName string , service * api.Service , targetPort int ) {
519
+ func testCanConnect (f * framework.Framework , ns * api.Namespace , podName string , service * api.Service , target string , targetPort int ) {
503
520
By (fmt .Sprintf ("Creating client pod %s that should successfully connect to %s." , podName , service .Name ))
504
- podClient := createNetworkClientPod (f , ns , podName , service . Spec . ClusterIP , targetPort )
521
+ podClient := createNetworkClientPod (f , ns , podName , target , targetPort )
505
522
defer func () {
506
523
By (fmt .Sprintf ("Cleaning up the pod %s" , podName ))
507
524
if err := f .ClientSet .Core ().Pods (ns .Name ).Delete (podClient .Name , nil ); err != nil {
@@ -518,9 +535,9 @@ func testCanConnect(f *framework.Framework, ns *api.Namespace, podName string, s
518
535
Expect (err ).NotTo (HaveOccurred (), fmt .Sprintf ("checking %s could communicate with server." , podClient .Name ))
519
536
}
520
537
521
- func testCannotConnect (f * framework.Framework , ns * api.Namespace , podName string , service * api.Service , targetPort int ) {
538
+ func testCannotConnect (f * framework.Framework , ns * api.Namespace , podName string , service * api.Service , target string , targetPort int ) {
522
539
By (fmt .Sprintf ("Creating client pod %s that should not be able to connect to %s." , podName , service .Name ))
523
- podClient := createNetworkClientPod (f , ns , podName , service . Spec . ClusterIP , targetPort )
540
+ podClient := createNetworkClientPod (f , ns , podName , target , targetPort )
524
541
defer func () {
525
542
By (fmt .Sprintf ("Cleaning up the pod %s" , podName ))
526
543
if err := f .ClientSet .Core ().Pods (ns .Name ).Delete (podClient .Name , nil ); err != nil {
0 commit comments