@@ -3,6 +3,7 @@ package state_test
3
3
import (
4
4
. "github.com/onsi/ginkgo/v2"
5
5
. "github.com/onsi/gomega"
6
+ "github.com/onsi/gomega/format"
6
7
apiv1 "k8s.io/api/core/v1"
7
8
discoveryV1 "k8s.io/api/discovery/v1"
8
9
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -18,6 +19,8 @@ import (
18
19
"github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers"
19
20
"github.com/nginxinc/nginx-kubernetes-gateway/internal/manager/index"
20
21
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state"
22
+ "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/conditions"
23
+ "github.com/nginxinc/nginx-kubernetes-gateway/internal/state/graph"
21
24
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/relationship"
22
25
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/relationship/relationshipfakes"
23
26
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/secrets/secretsfakes"
@@ -187,6 +190,7 @@ func createScheme() *runtime.Scheme {
187
190
}
188
191
189
192
var _ = Describe ("ChangeProcessor" , func () {
193
+ format .MaxLength = 0
190
194
Describe ("Normal cases of processing changes" , func () {
191
195
var (
192
196
gc = & v1beta1.GatewayClass {
@@ -220,9 +224,11 @@ var _ = Describe("ChangeProcessor", func() {
220
224
221
225
Describe ("Process gateway resources" , Ordered , func () {
222
226
var (
223
- gcUpdated * v1beta1.GatewayClass
224
- hr1 , hr1Updated , hr2 * v1beta1.HTTPRoute
225
- gw1 , gw1Updated , gw2 * v1beta1.Gateway
227
+ gcUpdated * v1beta1.GatewayClass
228
+ hr1 , hr1Updated , hr2 * v1beta1.HTTPRoute
229
+ gw1 , gw1Updated , gw2 * v1beta1.Gateway
230
+ expGraph * graph.Graph
231
+ expRouteHR1 , expRouteHR2 * graph.Route
226
232
)
227
233
BeforeAll (func () {
228
234
gcUpdated = gc .DeepCopy ()
@@ -242,6 +248,87 @@ var _ = Describe("ChangeProcessor", func() {
242
248
243
249
gw2 = createGatewayWithTLSListener ("gateway-2" )
244
250
})
251
+ BeforeEach (func () {
252
+ expRouteHR1 = & graph.Route {
253
+ Source : hr1 ,
254
+ ParentRefs : []graph.ParentRef {
255
+ {
256
+ Attachment : & graph.ParentRefAttachmentStatus {
257
+ AcceptedHostnames : map [string ][]string {"listener-80-1" : {"foo.example.com" }},
258
+ Attached : true ,
259
+ },
260
+ Gateway : types.NamespacedName {Namespace : "test" , Name : "gateway-1" },
261
+ },
262
+ {
263
+ Attachment : & graph.ParentRefAttachmentStatus {
264
+ AcceptedHostnames : map [string ][]string {"listener-443-1" : {"foo.example.com" }},
265
+ Attached : true ,
266
+ },
267
+ Gateway : types.NamespacedName {Namespace : "test" , Name : "gateway-1" },
268
+ Idx : 1 ,
269
+ },
270
+ },
271
+ Rules : []graph.Rule {{ValidMatches : true , ValidFilters : true }},
272
+ Valid : true ,
273
+ }
274
+
275
+ expRouteHR2 = & graph.Route {
276
+ Source : hr2 ,
277
+ ParentRefs : []graph.ParentRef {
278
+ {
279
+ Attachment : & graph.ParentRefAttachmentStatus {
280
+ AcceptedHostnames : map [string ][]string {"listener-80-1" : {"bar.example.com" }},
281
+ Attached : true ,
282
+ },
283
+ Gateway : types.NamespacedName {Namespace : "test" , Name : "gateway-2" },
284
+ },
285
+ {
286
+ Attachment : & graph.ParentRefAttachmentStatus {
287
+ AcceptedHostnames : map [string ][]string {"listener-443-1" : {"bar.example.com" }},
288
+ Attached : true ,
289
+ },
290
+ Gateway : types.NamespacedName {Namespace : "test" , Name : "gateway-2" },
291
+ Idx : 1 ,
292
+ },
293
+ },
294
+ Rules : []graph.Rule {{ValidMatches : true , ValidFilters : true }},
295
+ Valid : true ,
296
+ }
297
+
298
+ // This is the base case expected graph. Tests will manipulate this to add or remove elements
299
+ // to fit the expected output of the input under test.
300
+ expGraph = & graph.Graph {
301
+ GatewayClass : & graph.GatewayClass {
302
+ Source : gc ,
303
+ Valid : true ,
304
+ },
305
+ Gateway : & graph.Gateway {
306
+ Source : gw1 ,
307
+ Listeners : map [string ]* graph.Listener {
308
+ "listener-80-1" : {
309
+ Source : gw1 .Spec .Listeners [0 ],
310
+ Valid : true ,
311
+ Routes : map [types.NamespacedName ]* graph.Route {
312
+ {Namespace : "test" , Name : "hr-1" }: expRouteHR1 ,
313
+ },
314
+ },
315
+ "listener-443-1" : {
316
+ Source : gw1 .Spec .Listeners [1 ],
317
+ Valid : true ,
318
+ Routes : map [types.NamespacedName ]* graph.Route {
319
+ {Namespace : "test" , Name : "hr-1" }: expRouteHR1 ,
320
+ },
321
+ SecretPath : "path/to/cert" ,
322
+ },
323
+ },
324
+ Valid : true ,
325
+ },
326
+ IgnoredGateways : map [types.NamespacedName ]* v1beta1.Gateway {},
327
+ Routes : map [types.NamespacedName ]* graph.Route {
328
+ {Namespace : "test" , Name : "hr-1" }: expRouteHR1 ,
329
+ },
330
+ }
331
+ })
245
332
246
333
When ("no upsert has occurred" , func () {
247
334
It ("returns empty graph" , func () {
@@ -258,17 +345,32 @@ var _ = Describe("ChangeProcessor", func() {
258
345
259
346
changed , graphCfg := processor .Process ()
260
347
Expect (changed ).To (BeTrue ())
261
- Expect (graphCfg . Routes ).To (BeNil ( ))
348
+ Expect (graphCfg ).To (Equal ( & graph. Graph {} ))
262
349
})
263
350
})
264
351
When ("the first Gateway is upserted" , func () {
265
352
It ("returns populated graph" , func () {
266
353
processor .CaptureUpsertChange (gw1 )
267
354
355
+ expGraph .GatewayClass = nil
356
+
357
+ expGraph .Gateway .Conditions = conditions .NewGatewayInvalid ("GatewayClass doesn't exist" )
358
+ expGraph .Gateway .Valid = false
359
+ expGraph .Gateway .Listeners = nil
360
+
361
+ hrName := types.NamespacedName {Namespace : "test" , Name : "hr-1" }
362
+ expGraph .Routes [hrName ].ParentRefs [0 ].Attachment = & graph.ParentRefAttachmentStatus {
363
+ AcceptedHostnames : map [string ][]string {},
364
+ FailedCondition : conditions .NewRouteInvalidGateway (),
365
+ }
366
+ expGraph .Routes [hrName ].ParentRefs [1 ].Attachment = & graph.ParentRefAttachmentStatus {
367
+ AcceptedHostnames : map [string ][]string {},
368
+ FailedCondition : conditions .NewRouteInvalidGateway (),
369
+ }
370
+
268
371
changed , graphCfg := processor .Process ()
269
372
Expect (changed ).To (BeTrue ())
270
- Expect (graphCfg .Gateway ).ToNot (BeNil ())
271
- Expect (graphCfg .Routes ).To (HaveLen (1 ))
373
+ Expect (helpers .Diff (expGraph , graphCfg )).To (BeEmpty ())
272
374
})
273
375
})
274
376
})
@@ -279,7 +381,7 @@ var _ = Describe("ChangeProcessor", func() {
279
381
280
382
changed , graphCfg := processor .Process ()
281
383
Expect (changed ).To (BeTrue ())
282
- Expect (graphCfg . GatewayClass ). ToNot ( BeNil ())
384
+ Expect (helpers . Diff ( expGraph , graphCfg )). To ( BeEmpty ())
283
385
})
284
386
})
285
387
When ("the first HTTPRoute without a generation changed is processed" , func () {
@@ -297,9 +399,13 @@ var _ = Describe("ChangeProcessor", func() {
297
399
It ("returns populated graph" , func () {
298
400
processor .CaptureUpsertChange (hr1Updated )
299
401
402
+ hrName := types.NamespacedName {Namespace : "test" , Name : "hr-1" }
403
+ expGraph .Gateway .Listeners ["listener-443-1" ].Routes [hrName ].Source .Generation = hr1Updated .Generation
404
+ expGraph .Gateway .Listeners ["listener-80-1" ].Routes [hrName ].Source .Generation = hr1Updated .Generation
405
+
300
406
changed , graphCfg := processor .Process ()
301
407
Expect (changed ).To (BeTrue ())
302
- Expect (graphCfg . Routes ) .To (HaveLen ( 1 ))
408
+ Expect (helpers . Diff ( expGraph , graphCfg )) .To (BeEmpty ( ))
303
409
},
304
410
)
305
411
})
@@ -318,9 +424,11 @@ var _ = Describe("ChangeProcessor", func() {
318
424
It ("returns populated graph" , func () {
319
425
processor .CaptureUpsertChange (gw1Updated )
320
426
427
+ expGraph .Gateway .Source .Generation = gw1Updated .Generation
428
+
321
429
changed , graphCfg := processor .Process ()
322
430
Expect (changed ).To (BeTrue ())
323
- Expect (graphCfg . Gateway ). ToNot ( BeNil ())
431
+ Expect (helpers . Diff ( expGraph , graphCfg )). To ( BeEmpty ())
324
432
})
325
433
})
326
434
When ("the GatewayClass update without generation change is processed" , func () {
@@ -338,9 +446,11 @@ var _ = Describe("ChangeProcessor", func() {
338
446
It ("returns populated graph" , func () {
339
447
processor .CaptureUpsertChange (gcUpdated )
340
448
449
+ expGraph .GatewayClass .Source .Generation = gcUpdated .Generation
450
+
341
451
changed , graphCfg := processor .Process ()
342
452
Expect (changed ).To (BeTrue ())
343
- Expect (graphCfg . GatewayClass ). ToNot ( BeNil ())
453
+ Expect (helpers . Diff ( expGraph , graphCfg )). To ( BeEmpty ())
344
454
})
345
455
})
346
456
When ("no changes are captured" , func () {
@@ -355,18 +465,37 @@ var _ = Describe("ChangeProcessor", func() {
355
465
It ("returns populated graph using first gateway" , func () {
356
466
processor .CaptureUpsertChange (gw2 )
357
467
468
+ expGraph .IgnoredGateways = map [types.NamespacedName ]* v1beta1.Gateway {
469
+ {Namespace : "test" , Name : "gateway-2" }: gw2 ,
470
+ }
471
+
358
472
changed , graphCfg := processor .Process ()
359
473
Expect (changed ).To (BeTrue ())
360
- Expect (graphCfg . Gateway . Source . Name ) .To (Equal ( gw1 . Name ))
474
+ Expect (helpers . Diff ( expGraph , graphCfg )) .To (BeEmpty ( ))
361
475
})
362
476
})
363
477
When ("the second HTTPRoute is upserted" , func () {
364
478
It ("returns populated graph" , func () {
365
479
processor .CaptureUpsertChange (hr2 )
366
480
481
+ hrName := types.NamespacedName {Namespace : "test" , Name : "hr-2" }
482
+
483
+ expGraph .IgnoredGateways = map [types.NamespacedName ]* v1beta1.Gateway {
484
+ {Namespace : "test" , Name : "gateway-2" }: gw2 ,
485
+ }
486
+ expGraph .Routes [hrName ] = expRouteHR2
487
+ expGraph .Routes [hrName ].ParentRefs [0 ].Attachment = & graph.ParentRefAttachmentStatus {
488
+ AcceptedHostnames : map [string ][]string {},
489
+ FailedCondition : conditions .NewTODO ("Gateway is ignored" ),
490
+ }
491
+ expGraph .Routes [hrName ].ParentRefs [1 ].Attachment = & graph.ParentRefAttachmentStatus {
492
+ AcceptedHostnames : map [string ][]string {},
493
+ FailedCondition : conditions .NewTODO ("Gateway is ignored" ),
494
+ }
495
+
367
496
changed , graphCfg := processor .Process ()
368
497
Expect (changed ).To (BeTrue ())
369
- Expect (graphCfg . Routes ) .To (HaveLen ( 2 ))
498
+ Expect (helpers . Diff ( expGraph , graphCfg )) .To (BeEmpty ( ))
370
499
})
371
500
})
372
501
When ("the first Gateway is deleted" , func () {
@@ -376,10 +505,22 @@ var _ = Describe("ChangeProcessor", func() {
376
505
types.NamespacedName {Namespace : "test" , Name : "gateway-1" },
377
506
)
378
507
508
+ // gateway 2 takes over;
509
+ // route 1 has been replaced by route 2
510
+ hr1Name := types.NamespacedName {Namespace : "test" , Name : "hr-1" }
511
+ hr2Name := types.NamespacedName {Namespace : "test" , Name : "hr-2" }
512
+ expGraph .Gateway .Source = gw2
513
+ delete (expGraph .Gateway .Listeners ["listener-80-1" ].Routes , hr1Name )
514
+ delete (expGraph .Gateway .Listeners ["listener-443-1" ].Routes , hr1Name )
515
+ expGraph .Gateway .Listeners ["listener-80-1" ].Routes [hr2Name ] = expRouteHR2
516
+ expGraph .Gateway .Listeners ["listener-443-1" ].Routes [hr2Name ] = expRouteHR2
517
+
518
+ delete (expGraph .Routes , hr1Name )
519
+ expGraph .Routes [hr2Name ] = expRouteHR2
520
+
379
521
changed , graphCfg := processor .Process ()
380
522
Expect (changed ).To (BeTrue ())
381
- Expect (graphCfg .Gateway .Source .Name ).To (Equal (gw2 .Name ))
382
- Expect (graphCfg .Routes ).To (HaveLen (1 ))
523
+ Expect (helpers .Diff (expGraph , graphCfg )).To (BeEmpty ())
383
524
})
384
525
})
385
526
When ("the second HTTPRoute is deleted" , func () {
@@ -389,9 +530,17 @@ var _ = Describe("ChangeProcessor", func() {
389
530
types.NamespacedName {Namespace : "test" , Name : "hr-2" },
390
531
)
391
532
533
+ // gateway 2 still in charge;
534
+ // no routes remain
535
+ hr1Name := types.NamespacedName {Namespace : "test" , Name : "hr-1" }
536
+ expGraph .Gateway .Source = gw2
537
+ delete (expGraph .Gateway .Listeners ["listener-80-1" ].Routes , hr1Name )
538
+ delete (expGraph .Gateway .Listeners ["listener-443-1" ].Routes , hr1Name )
539
+ expGraph .Routes = map [types.NamespacedName ]* graph.Route {}
540
+
392
541
changed , graphCfg := processor .Process ()
393
542
Expect (changed ).To (BeTrue ())
394
- Expect (graphCfg . Routes ) .To (HaveLen ( 0 ))
543
+ Expect (helpers . Diff ( expGraph , graphCfg )) .To (BeEmpty ( ))
395
544
})
396
545
})
397
546
When ("the GatewayClass is deleted" , func () {
@@ -401,9 +550,16 @@ var _ = Describe("ChangeProcessor", func() {
401
550
types.NamespacedName {Name : gcName },
402
551
)
403
552
553
+ expGraph .GatewayClass = nil
554
+ expGraph .Gateway = & graph.Gateway {
555
+ Source : gw2 ,
556
+ Conditions : conditions .NewGatewayInvalid ("GatewayClass doesn't exist" ),
557
+ }
558
+ expGraph .Routes = map [types.NamespacedName ]* graph.Route {}
559
+
404
560
changed , graphCfg := processor .Process ()
405
561
Expect (changed ).To (BeTrue ())
406
- Expect (graphCfg . GatewayClass ) .To (BeNil ())
562
+ Expect (helpers . Diff ( expGraph , graphCfg )) .To (BeEmpty ())
407
563
})
408
564
})
409
565
When ("the second Gateway is deleted" , func () {
@@ -413,9 +569,11 @@ var _ = Describe("ChangeProcessor", func() {
413
569
types.NamespacedName {Namespace : "test" , Name : "gateway-2" },
414
570
)
415
571
572
+ expGraph := & graph.Graph {}
573
+
416
574
changed , graphCfg := processor .Process ()
417
575
Expect (changed ).To (BeTrue ())
418
- Expect (graphCfg . Gateway ) .To (BeNil ())
576
+ Expect (helpers . Diff ( expGraph , graphCfg )) .To (BeEmpty ())
419
577
})
420
578
})
421
579
When ("the first HTTPRoute is deleted" , func () {
@@ -425,9 +583,11 @@ var _ = Describe("ChangeProcessor", func() {
425
583
types.NamespacedName {Namespace : "test" , Name : "hr-1" },
426
584
)
427
585
586
+ expGraph := & graph.Graph {}
587
+
428
588
changed , graphCfg := processor .Process ()
429
589
Expect (changed ).To (BeTrue ())
430
- Expect (graphCfg . Routes ) .To (BeNil ())
590
+ Expect (helpers . Diff ( expGraph , graphCfg )) .To (BeEmpty ())
431
591
})
432
592
})
433
593
})
@@ -766,6 +926,9 @@ var _ = Describe("ChangeProcessor", func() {
766
926
})
767
927
768
928
Describe ("Ensuring non-changing changes don't override previously changing changes" , func () {
929
+ // Note: in these tests, we deliberately don't fully inspect the returned configuration and statuses
930
+ // -- this is done in 'Normal cases of processing changes'
931
+
769
932
var (
770
933
processor * state.ChangeProcessorImpl
771
934
fakeRelationshipCapturer * relationshipfakes.FakeCapturer
0 commit comments