@@ -2,6 +2,7 @@ package configs
2
2
3
3
import (
4
4
"errors"
5
+ "fmt"
5
6
"reflect"
6
7
"strings"
7
8
"testing"
@@ -794,6 +795,93 @@ func createExpectedConfigForMergeableCafeIngressWithUseClusterIP() version1.Ingr
794
795
return expected
795
796
}
796
797
798
+ func createExpectedConfigForCafeIngressWithUseClusterIPNamedPorts () version1.IngressNginxConfig {
799
+ upstreamZoneSize := "256k"
800
+
801
+ coffeeUpstream := version1.Upstream {
802
+ Name : "default-cafe-ingress-cafe.example.com-coffee-svc-custom-port-name" ,
803
+ LBMethod : "random two least_conn" ,
804
+ UpstreamZoneSize : upstreamZoneSize ,
805
+ UpstreamServers : []version1.UpstreamServer {
806
+ {
807
+ Address : "10.109.204.250:3000" ,
808
+ MaxFails : 1 ,
809
+ MaxConns : 0 ,
810
+ FailTimeout : "10s" ,
811
+ },
812
+ },
813
+ }
814
+
815
+ teaUpstream := version1.Upstream {
816
+ Name : "default-cafe-ingress-cafe.example.com-tea-svc-80" ,
817
+ LBMethod : "random two least_conn" ,
818
+ UpstreamZoneSize : upstreamZoneSize ,
819
+ UpstreamServers : []version1.UpstreamServer {
820
+ {
821
+ Address : "10.109.204.250:80" ,
822
+ MaxFails : 1 ,
823
+ MaxConns : 0 ,
824
+ FailTimeout : "10s" ,
825
+ },
826
+ },
827
+ }
828
+
829
+ expected := version1.IngressNginxConfig {
830
+ Upstreams : []version1.Upstream {
831
+ coffeeUpstream ,
832
+ teaUpstream ,
833
+ },
834
+ Servers : []version1.Server {
835
+ {
836
+ Name : "cafe.example.com" ,
837
+ ServerTokens : "on" ,
838
+ Locations : []version1.Location {
839
+ {
840
+ Path : "/coffee" ,
841
+ ServiceName : "coffee-svc" ,
842
+ Upstream : coffeeUpstream ,
843
+ ProxyConnectTimeout : "60s" ,
844
+ ProxyReadTimeout : "60s" ,
845
+ ProxySendTimeout : "60s" ,
846
+ ClientMaxBodySize : "1m" ,
847
+ ProxyBuffering : true ,
848
+ ProxySSLName : "coffee-svc.default.svc" ,
849
+ },
850
+ {
851
+ Path : "/tea" ,
852
+ ServiceName : "tea-svc" ,
853
+ Upstream : teaUpstream ,
854
+ ProxyConnectTimeout : "60s" ,
855
+ ProxyReadTimeout : "60s" ,
856
+ ProxySendTimeout : "60s" ,
857
+ ClientMaxBodySize : "1m" ,
858
+ ProxyBuffering : true ,
859
+ ProxySSLName : "tea-svc.default.svc" ,
860
+ },
861
+ },
862
+ SSL : true ,
863
+ SSLCertificate : "/etc/nginx/secrets/default-cafe-secret" ,
864
+ SSLCertificateKey : "/etc/nginx/secrets/default-cafe-secret" ,
865
+ StatusZone : "cafe.example.com" ,
866
+ HSTSMaxAge : 2592000 ,
867
+ Ports : []int {80 },
868
+ SSLPorts : []int {443 },
869
+ SSLRedirect : true ,
870
+ HealthChecks : make (map [string ]version1.HealthCheck ),
871
+ },
872
+ },
873
+ Ingress : version1.Ingress {
874
+ Name : "cafe-ingress" ,
875
+ Namespace : "default" ,
876
+ Annotations : map [string ]string {
877
+ "kubernetes.io/ingress.class" : "nginx" ,
878
+ "nginx.org/use-cluster-ip" : "true" ,
879
+ },
880
+ },
881
+ }
882
+ return expected
883
+ }
884
+
797
885
func createExpectedConfigForCafeIngressWithUseClusterIP () version1.IngressNginxConfig {
798
886
upstreamZoneSize := "256k"
799
887
@@ -910,6 +998,50 @@ func TestGenerateNginxCfgWithUseClusterIP(t *testing.T) {
910
998
}
911
999
}
912
1000
1001
+ func TestGenerateNginxCfgWithUseClusterIPWithNamedPorts (t * testing.T ) {
1002
+ t .Parallel ()
1003
+ customPort := 3000
1004
+ customPortName := "custom-port-name"
1005
+ clusterIP := "10.109.204.250"
1006
+ cafeIngressEx := createCafeIngressEx ()
1007
+ cafeIngressEx .Ingress .Annotations ["nginx.org/use-cluster-ip" ] = "true"
1008
+ cafeIngressEx .Endpoints ["coffee-svccustom-port-name" ] = make ([]string , 1 )
1009
+
1010
+ // coffee will use a named port
1011
+ cafeIngressEx .Endpoints ["coffee-svccustom-port-name" ][0 ] = fmt .Sprintf ("%s:%d" , clusterIP , customPort )
1012
+
1013
+ // tea will not use a named port
1014
+ cafeIngressEx .Endpoints ["tea-svc80" ][0 ] = fmt .Sprintf ("%s:%d" , clusterIP , 80 )
1015
+
1016
+ // unset the port number and set the port name for the /coffee path
1017
+ cafeIngressEx .Ingress .Spec .Rules [0 ].HTTP .Paths [0 ].Backend .Service .Port .Number = 0
1018
+ cafeIngressEx .Ingress .Spec .Rules [0 ].HTTP .Paths [0 ].Backend .Service .Port .Name = customPortName
1019
+
1020
+ isPlus := false
1021
+ configParams := NewDefaultConfigParams (isPlus )
1022
+
1023
+ expected := createExpectedConfigForCafeIngressWithUseClusterIPNamedPorts ()
1024
+
1025
+ result , warnings := generateNginxCfg (NginxCfgParams {
1026
+ staticParams : & StaticConfigParams {},
1027
+ ingEx : & cafeIngressEx ,
1028
+ apResources : nil ,
1029
+ dosResource : nil ,
1030
+ isMinion : false ,
1031
+ isPlus : false ,
1032
+ baseCfgParams : configParams ,
1033
+ isResolverConfigured : false ,
1034
+ isWildcardEnabled : false ,
1035
+ })
1036
+
1037
+ if diff := cmp .Diff (expected , result ); diff != "" {
1038
+ t .Errorf ("generateNginxCfg() returned unexpected result (-want +got):\n %s" , diff )
1039
+ }
1040
+ if len (warnings ) != 0 {
1041
+ t .Errorf ("generateNginxCfg() returned warnings: %v" , warnings )
1042
+ }
1043
+ }
1044
+
913
1045
func TestGenerateNginxCfgForLimitReq (t * testing.T ) {
914
1046
t .Parallel ()
915
1047
cafeIngressEx := createCafeIngressEx ()
0 commit comments