Skip to content

Commit 3752126

Browse files
authored
adapt traffic command to the possibility of missing non-segment routegroup/ingress (#704)
* adapt traffic command to the possibility of missing non-segment routegroup/ingress * make sure test cases consider hostnames within cluster domains correctly * clarify test cases and code with additional comments * simplify the code a bit by using if/else statement
1 parent 3a5d8a5 commit 3752126

File tree

4 files changed

+49
-16
lines changed

4 files changed

+49
-16
lines changed

pkg/core/stack_resources.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,10 @@ func (sc *StackContainer) GenerateService() (*v1.Service, error) {
318318
func (sc *StackContainer) stackHostnames(
319319
spec ingressOrRouteGroupSpec,
320320
segment bool,
321-
) ([]string, error) {
321+
) []string {
322322
// The Ingress segment uses the original hostnames
323323
if segment {
324-
return spec.GetHosts(), nil
324+
return spec.GetHosts()
325325
}
326326
result := sets.NewString()
327327

@@ -339,7 +339,7 @@ func (sc *StackContainer) stackHostnames(
339339
}
340340
}
341341
}
342-
return result.List(), nil
342+
return result.List()
343343
}
344344

345345
func (sc *StackContainer) GenerateIngress() (*networking.Ingress, error) {
@@ -388,10 +388,7 @@ func (sc *StackContainer) generateIngress(segment bool) (
388388
return nil, nil
389389
}
390390

391-
hostnames, err := sc.stackHostnames(sc.ingressSpec, segment)
392-
if err != nil {
393-
return nil, err
394-
}
391+
hostnames := sc.stackHostnames(sc.ingressSpec, segment)
395392
if len(hostnames) == 0 {
396393
return nil, nil
397394
}
@@ -481,10 +478,7 @@ func (sc *StackContainer) generateRouteGroup(segment bool) (
481478
return nil, nil
482479
}
483480

484-
hostnames, err := sc.stackHostnames(sc.routeGroupSpec, segment)
485-
if err != nil {
486-
return nil, err
487-
}
481+
hostnames := sc.stackHostnames(sc.routeGroupSpec, segment)
488482
if len(hostnames) == 0 {
489483
return nil, nil
490484
}

pkg/core/stackset_test.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -860,14 +860,26 @@ func TestStackUpdateFromResources(t *testing.T) {
860860

861861
runTest("ingress isn't considered updated if the generation is different", func(t *testing.T, container *StackContainer) {
862862
container.Stack.Generation = 11
863-
container.ingressSpec = &zv1.StackSetIngressSpec{}
863+
container.ingressSpec = &zv1.StackSetIngressSpec{
864+
Hosts: []string{"foo.example.org"},
865+
}
864866
container.Resources.Deployment = deployment(11, 5, 5)
865867
container.Resources.Service = service(11)
866868
container.Resources.Ingress = ingress(10)
867869
container.Resources.IngressSegment = ingress(11)
868870
container.updateFromResources()
869871
require.EqualValues(t, false, container.resourcesUpdated)
870872
})
873+
runTest("ingress is considered updated if no hostname matches cluster domain", func(t *testing.T, container *StackContainer) {
874+
container.Stack.Generation = 11
875+
container.ingressSpec = &zv1.StackSetIngressSpec{}
876+
container.Resources.Deployment = deployment(11, 5, 5)
877+
container.Resources.Service = service(11)
878+
container.Resources.Ingress = nil
879+
container.Resources.IngressSegment = ingress(11)
880+
container.updateFromResources()
881+
require.EqualValues(t, true, container.resourcesUpdated)
882+
})
871883
runTest("ingress isn't considered updated if it should be gone", func(t *testing.T, container *StackContainer) {
872884
container.Stack.Generation = 11
873885
container.Resources.Deployment = deployment(11, 5, 5)
@@ -896,14 +908,26 @@ func TestStackUpdateFromResources(t *testing.T) {
896908
})
897909
runTest("routegroup isn't considered updated if the generation is different", func(t *testing.T, container *StackContainer) {
898910
container.Stack.Generation = 11
899-
container.routeGroupSpec = &zv1.RouteGroupSpec{}
911+
container.routeGroupSpec = &zv1.RouteGroupSpec{
912+
Hosts: []string{"foo.example.org"},
913+
}
900914
container.Resources.Deployment = deployment(11, 5, 5)
901915
container.Resources.Service = service(11)
902916
container.Resources.RouteGroup = routegroup(10)
903917
container.Resources.RouteGroupSegment = routegroup(11)
904918
container.updateFromResources()
905919
require.EqualValues(t, false, container.resourcesUpdated)
906920
})
921+
runTest("routegroup is considered updated if no hostname matches cluster domain", func(t *testing.T, container *StackContainer) {
922+
container.Stack.Generation = 11
923+
container.routeGroupSpec = &zv1.RouteGroupSpec{}
924+
container.Resources.Deployment = deployment(11, 5, 5)
925+
container.Resources.Service = service(11)
926+
container.Resources.RouteGroup = nil
927+
container.Resources.RouteGroupSegment = routegroup(11)
928+
container.updateFromResources()
929+
require.EqualValues(t, true, container.resourcesUpdated)
930+
})
907931
runTest("routegroup isn't considered updated if it should be gone", func(t *testing.T, container *StackContainer) {
908932
container.Stack.Generation = 11
909933
container.Resources.Deployment = deployment(11, 5, 5)

pkg/core/test_helpers.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ type testStackFactory struct {
2222
func testStack(name string) *testStackFactory {
2323
return &testStackFactory{
2424
container: &StackContainer{
25-
backendPort: &intStrTestPort,
25+
clusterDomains: []string{"example.org"},
26+
backendPort: &intStrTestPort,
2627
Stack: &zv1.Stack{
2728
ObjectMeta: metav1.ObjectMeta{
2829
Name: name,

pkg/core/types.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,14 @@ func (sc *StackContainer) updateFromResources() {
439439

440440
// ingress: ignore if ingress is not set or check if we are up to date
441441
if sc.ingressSpec != nil {
442-
ingressUpdated = sc.Resources.Ingress != nil && IsResourceUpToDate(sc.Stack, sc.Resources.Ingress.ObjectMeta)
442+
// the per-stack ingress must either be present and up-to-date, or not present and not expected.
443+
// the per-stack ingress is not expected if the stack has no hostnames matching the cluster domain.
444+
if sc.Resources.Ingress != nil {
445+
ingressUpdated = IsResourceUpToDate(sc.Stack, sc.Resources.Ingress.ObjectMeta)
446+
} else {
447+
hostnames := sc.stackHostnames(sc.ingressSpec, false)
448+
ingressUpdated = len(hostnames) == 0
449+
}
443450
ingressSegmentUpdated = sc.Resources.IngressSegment != nil &&
444451
IsResourceUpToDate(sc.Stack, sc.Resources.IngressSegment.ObjectMeta)
445452
} else {
@@ -450,7 +457,14 @@ func (sc *StackContainer) updateFromResources() {
450457

451458
// routegroup: ignore if routegroup is not set or check if we are up to date
452459
if sc.routeGroupSpec != nil {
453-
routeGroupUpdated = sc.Resources.RouteGroup != nil && IsResourceUpToDate(sc.Stack, sc.Resources.RouteGroup.ObjectMeta)
460+
// the per-stack route group must either be present and up-to-date, or not present and not expected.
461+
// the per-stack route group is not expected if the stack has no hostnames matching the cluster domain.
462+
if sc.Resources.RouteGroup != nil {
463+
routeGroupUpdated = IsResourceUpToDate(sc.Stack, sc.Resources.RouteGroup.ObjectMeta)
464+
} else {
465+
hostnames := sc.stackHostnames(sc.routeGroupSpec, false)
466+
routeGroupUpdated = len(hostnames) == 0
467+
}
454468
routeGroupSegmentUpdated = sc.Resources.RouteGroupSegment != nil &&
455469
IsResourceUpToDate(
456470
sc.Stack,

0 commit comments

Comments
 (0)