@@ -720,7 +720,9 @@ func TestBindRouteToListeners(t *testing.T) {
720
720
Source : gw ,
721
721
Valid : true ,
722
722
Listeners : []* Listener {
723
- createListener ("listener-80-1" ),
723
+ createModifiedListener ("listener-80-1" , func (l * Listener ) {
724
+ l .Source .Port = 80
725
+ }),
724
726
},
725
727
},
726
728
expectedSectionNameRefs : []ParentRef {
@@ -729,19 +731,24 @@ func TestBindRouteToListeners(t *testing.T) {
729
731
Gateway : & ParentRefGateway {NamespacedName : client .ObjectKeyFromObject (gw )},
730
732
SectionName : hrWithPort .Spec .ParentRefs [0 ].SectionName ,
731
733
Attachment : & ParentRefAttachmentStatus {
732
- Attached : false ,
733
- FailedConditions : []conditions.Condition {
734
- conditions .NewRouteUnsupportedValue (
735
- `spec.parentRefs[0].port: Forbidden: cannot be set` ,
736
- ),
734
+ Attached : true ,
735
+ FailedConditions : nil ,
736
+ AcceptedHostnames : map [string ][]string {
737
+ "test/gateway/listener-80-1" : {"foo.example.com" },
737
738
},
738
- AcceptedHostnames : map [ string ][] string {} ,
739
+ ListenerPort : 80 ,
739
740
},
740
741
Port : hrWithPort .Spec .ParentRefs [0 ].Port ,
741
742
},
742
743
},
743
744
expectedGatewayListeners : []* Listener {
744
- createListener ("listener-80-1" ),
745
+ func () * Listener {
746
+ l := createModifiedListener ("listener-80-1" , func (l * Listener ) {
747
+ l .Source .Port = 80
748
+ })
749
+ l .Routes [CreateRouteKey (hrWithPort )] = routeWithPort
750
+ return l
751
+ }(),
745
752
},
746
753
name : "port is configured" ,
747
754
},
@@ -1904,17 +1911,17 @@ func TestBindL4RouteToListeners(t *testing.T) {
1904
1911
Name : "gateway" ,
1905
1912
},
1906
1913
Listeners : []* Listener {
1907
- createListener ("listener-443" ),
1914
+ createModifiedListener ("listener-443" , func (l * Listener ) {
1915
+ l .Source .Port = 443
1916
+ }),
1908
1917
},
1909
1918
},
1910
1919
expectedSectionNameRefs : []ParentRef {
1911
1920
{
1912
1921
Attachment : & ParentRefAttachmentStatus {
1913
1922
AcceptedHostnames : map [string ][]string {},
1914
1923
FailedConditions : []conditions.Condition {
1915
- conditions .NewRouteUnsupportedValue (
1916
- `spec.parentRefs[0].port: Forbidden: cannot be set` ,
1917
- ),
1924
+ conditions .NewRouteNoMatchingParent (),
1918
1925
},
1919
1926
Attached : false ,
1920
1927
},
@@ -1925,7 +1932,9 @@ func TestBindL4RouteToListeners(t *testing.T) {
1925
1932
},
1926
1933
},
1927
1934
expectedGatewayListeners : []* Listener {
1928
- createListener ("listener-443" ),
1935
+ createModifiedListener ("listener-443" , func (l * Listener ) {
1936
+ l .Source .Port = 443
1937
+ }),
1929
1938
},
1930
1939
name : "port is not nil" ,
1931
1940
},
@@ -3720,3 +3729,141 @@ func TestBindRoutesToListeners(t *testing.T) {
3720
3729
bindRoutesToListeners (nil , nil , nil , nil )
3721
3730
}).ToNot (Panic ())
3722
3731
}
3732
+
3733
+ func TestFindAttachableListenersWithPort (t * testing.T ) {
3734
+ t .Parallel ()
3735
+
3736
+ port80 := gatewayv1 .PortNumber (80 )
3737
+ port443 := gatewayv1 .PortNumber (443 )
3738
+ port8080 := gatewayv1 .PortNumber (8080 )
3739
+
3740
+ httpListener := & Listener {
3741
+ Name : "http-80" ,
3742
+ Attachable : true ,
3743
+ Source : gatewayv1.Listener {
3744
+ Name : "http-80" ,
3745
+ Port : port80 ,
3746
+ },
3747
+ }
3748
+
3749
+ httpsListener := & Listener {
3750
+ Name : "https-443" ,
3751
+ Attachable : true ,
3752
+ Source : gatewayv1.Listener {
3753
+ Name : "https-443" ,
3754
+ Port : port443 ,
3755
+ },
3756
+ }
3757
+
3758
+ nonAttachableListener := & Listener {
3759
+ Name : "http-8080" ,
3760
+ Attachable : false , // not attachable
3761
+ Source : gatewayv1.Listener {
3762
+ Name : "http-8080" ,
3763
+ Port : port8080 ,
3764
+ },
3765
+ }
3766
+
3767
+ tests := []struct {
3768
+ name string
3769
+ parentRef * ParentRef
3770
+ expectedListeners []* Listener
3771
+ expectedListenerExists bool
3772
+ }{
3773
+ {
3774
+ name : "port 80 filter returns only port 80 listener" ,
3775
+ parentRef : & ParentRef {
3776
+ Port : & port80 ,
3777
+ },
3778
+ expectedListeners : []* Listener {httpListener },
3779
+ expectedListenerExists : true ,
3780
+ },
3781
+ {
3782
+ name : "port 443 filter returns only port 443 listener" ,
3783
+ parentRef : & ParentRef {
3784
+ Port : & port443 ,
3785
+ },
3786
+ expectedListeners : []* Listener {httpsListener },
3787
+ expectedListenerExists : true ,
3788
+ },
3789
+ {
3790
+ name : "port 8080 filter returns empty because listener is not attachable" ,
3791
+ parentRef : & ParentRef {
3792
+ Port : & port8080 ,
3793
+ },
3794
+ expectedListeners : []* Listener {},
3795
+ expectedListenerExists : true ,
3796
+ },
3797
+ {
3798
+ name : "port 9999 filter returns empty because no listener has that port" ,
3799
+ parentRef : & ParentRef {
3800
+ Port : helpers .GetPointer (gatewayv1 .PortNumber (9999 )),
3801
+ },
3802
+ expectedListeners : []* Listener {},
3803
+ expectedListenerExists : false ,
3804
+ },
3805
+ {
3806
+ name : "no port specified returns all attachable listeners" ,
3807
+ parentRef : & ParentRef {
3808
+ Port : nil ,
3809
+ },
3810
+ expectedListeners : []* Listener {httpListener , httpsListener },
3811
+ expectedListenerExists : true ,
3812
+ },
3813
+ {
3814
+ name : "sectionName with matching port returns that specific listener" ,
3815
+ parentRef : & ParentRef {
3816
+ SectionName : helpers .GetPointer (gatewayv1 .SectionName ("http-80" )),
3817
+ Port : & port80 ,
3818
+ },
3819
+ expectedListeners : []* Listener {httpListener },
3820
+ expectedListenerExists : true ,
3821
+ },
3822
+ {
3823
+ name : "sectionName with non-matching port returns empty" ,
3824
+ parentRef : & ParentRef {
3825
+ SectionName : helpers .GetPointer (gatewayv1 .SectionName ("http-80" )),
3826
+ Port : & port443 , // wrong port for http-80 listener
3827
+ },
3828
+ expectedListeners : []* Listener {},
3829
+ expectedListenerExists : false ,
3830
+ },
3831
+ {
3832
+ name : "sectionName that doesn't exist returns empty with false" ,
3833
+ parentRef : & ParentRef {
3834
+ SectionName : helpers .GetPointer (gatewayv1 .SectionName ("nonexistent" )),
3835
+ Port : & port80 ,
3836
+ },
3837
+ expectedListeners : []* Listener {},
3838
+ expectedListenerExists : false ,
3839
+ },
3840
+ }
3841
+
3842
+ for _ , tt := range tests {
3843
+ t .Run (tt .name , func (t * testing.T ) {
3844
+ t .Parallel ()
3845
+ g := NewWithT (t )
3846
+
3847
+ attachableListeners , listenerExists := findAttachableListeners (
3848
+ tt .parentRef ,
3849
+ []* Listener {httpListener , httpsListener , nonAttachableListener },
3850
+ )
3851
+
3852
+ g .Expect (listenerExists ).To (Equal (tt .expectedListenerExists ))
3853
+ g .Expect (attachableListeners ).To (HaveLen (len (tt .expectedListeners )))
3854
+
3855
+ // Compare listeners by name since they're the same instances
3856
+ expectedNames := make ([]string , len (tt .expectedListeners ))
3857
+ for i , l := range tt .expectedListeners {
3858
+ expectedNames [i ] = l .Name
3859
+ }
3860
+
3861
+ actualNames := make ([]string , len (attachableListeners ))
3862
+ for i , l := range attachableListeners {
3863
+ actualNames [i ] = l .Name
3864
+ }
3865
+
3866
+ g .Expect (actualNames ).To (ConsistOf (expectedNames ))
3867
+ })
3868
+ }
3869
+ }
0 commit comments