@@ -1046,61 +1046,74 @@ func CreateDialContext(ctx context.Context, t *testing.T, ing *v1alpha1.Ingress,
10461046 // TODO(mattmoor): I'm open to tricks that would let us cleanly test multiple
10471047 // public load balancers or LBs with multiple ingresses (below), but want to
10481048 // keep our simple tests simple, thus the [0]s...
1049- // We expect an ingress LB with the form foo.bar.svc.cluster.local (though
1050- // we aren't strictly sensitive to the suffix, this is just illustrative.
10511049 internalDomain := ing .Status .PublicLoadBalancer .Ingress [0 ].DomainInternal
1052- parts := strings .SplitN (internalDomain , "." , 3 )
1053- if len (parts ) < 3 {
1054- t .Fatal ("Too few parts in internal domain:" , internalDomain )
1055- }
1056- name , namespace := parts [0 ], parts [1 ]
1050+ if internalDomain != "" {
1051+ parts := strings .SplitN (internalDomain , "." , 3 )
1052+ if len (parts ) < 3 {
1053+ t .Fatal ("Too few parts in internal domain:" , internalDomain )
1054+ }
1055+ name , namespace := parts [0 ], parts [1 ]
10571056
1058- var svc * corev1.Service
1059- err := reconciler .RetryTestErrors (func (attempts int ) (err error ) {
1060- svc , err = clients .KubeClient .CoreV1 ().Services (namespace ).Get (ctx , name , metav1.GetOptions {})
1061- return err
1062- })
1063- if err != nil {
1064- t .Fatalf ("Unable to retrieve Kubernetes service %s/%s: %v" , namespace , name , err )
1065- }
1057+ var svc * corev1.Service
1058+ err := reconciler .RetryTestErrors (func (attempts int ) (err error ) {
1059+ svc , err = clients .KubeClient .CoreV1 ().Services (namespace ).Get (ctx , name , metav1.GetOptions {})
1060+ return err
1061+ })
1062+ if err != nil {
1063+ t .Fatalf ("Unable to retrieve Kubernetes service %s/%s: %v" , namespace , name , err )
1064+ }
10661065
1067- dial := network .NewBackoffDialer (dialBackoff )
1068- if pkgTest .Flags .IngressEndpoint != "" {
1069- t .Logf ("ingressendpoint: %q" , pkgTest .Flags .IngressEndpoint )
1066+ dial := network .NewBackoffDialer (dialBackoff )
1067+ if pkgTest .Flags .IngressEndpoint != "" {
1068+ t .Logf ("ingressendpoint: %q" , pkgTest .Flags .IngressEndpoint )
10701069
1071- // If we're using a manual --ingressendpoint then don't require
1072- // "type: LoadBalancer", which may not play nice with KinD
1073- return func (ctx context.Context , _ string , address string ) (net.Conn , error ) {
1074- _ , port , err := net .SplitHostPort (address )
1075- if err != nil {
1076- return nil , err
1070+ // If we're using a manual --ingressendpoint then don't require
1071+ // "type: LoadBalancer", which may not play nice with KinD
1072+ return func (ctx context.Context , _ string , address string ) (net.Conn , error ) {
1073+ _ , port , err := net .SplitHostPort (address )
1074+ if err != nil {
1075+ return nil , err
1076+ }
1077+ for _ , sp := range svc .Spec .Ports {
1078+ if fmt .Sprint (sp .Port ) == port {
1079+ return dial (ctx , "tcp" , fmt .Sprintf ("%s:%d" , pkgTest .Flags .IngressEndpoint , sp .NodePort ))
1080+ }
1081+ }
1082+ return nil , fmt .Errorf ("service doesn't contain a matching port: %s" , port )
10771083 }
1078- for _ , sp := range svc .Spec .Ports {
1079- if fmt .Sprint (sp .Port ) == port {
1080- return dial (ctx , "tcp" , fmt .Sprintf ("%s:%d" , pkgTest .Flags .IngressEndpoint , sp .NodePort ))
1084+ } else if len (svc .Status .LoadBalancer .Ingress ) >= 1 {
1085+ ingress := svc .Status .LoadBalancer .Ingress [0 ]
1086+ return func (ctx context.Context , _ string , address string ) (net.Conn , error ) {
1087+ _ , port , err := net .SplitHostPort (address )
1088+ if err != nil {
1089+ return nil , err
1090+ }
1091+ if ingress .IP != "" {
1092+ return dial (ctx , "tcp" , ingress .IP + ":" + port )
10811093 }
1094+ if ingress .Hostname != "" {
1095+ return dial (ctx , "tcp" , ingress .Hostname + ":" + port )
1096+ }
1097+ return nil , errors .New ("service ingress does not contain dialing information" )
10821098 }
1083- return nil , fmt .Errorf ("service doesn't contain a matching port: %s" , port )
10841099 }
1085- } else if len (svc .Status .LoadBalancer .Ingress ) >= 1 {
1086- ingress := svc .Status .LoadBalancer .Ingress [0 ]
1087- return func (ctx context.Context , _ string , address string ) (net.Conn , error ) {
1088- _ , port , err := net .SplitHostPort (address )
1089- if err != nil {
1090- return nil , err
1091- }
1092- if ingress .IP != "" {
1093- return dial (ctx , "tcp" , ingress .IP + ":" + port )
1094- }
1095- if ingress .Hostname != "" {
1096- return dial (ctx , "tcp" , ingress .Hostname + ":" + port )
1097- }
1098- return nil , errors .New ("service ingress does not contain dialing information" )
1100+ t .Fatal ("Service does not have a supported shape (not type LoadBalancer? missing --ingressendpoint?)." )
1101+ } else if ing .Status .PublicLoadBalancer .Ingress [0 ].IP != "" {
1102+ dial := network .NewBackoffDialer (dialBackoff )
1103+ ingressIP := ing .Status .PublicLoadBalancer .Ingress [0 ].IP
1104+
1105+ port := 80
1106+ if ing .Spec .Rules [0 ].Visibility == v1alpha1 .IngressVisibilityExternalIP && ing .Spec .HTTPOption == v1alpha1 .HTTPOptionRedirected {
1107+ port = 443
1108+ }
1109+
1110+ return func (ctx context.Context , _ string , _ string ) (net.Conn , error ) {
1111+ return dial (ctx , "tcp" , fmt .Sprintf ("%s:%d" , ingressIP , port ))
10991112 }
11001113 } else {
1101- t .Fatal ("Service does not have a supported shape (not type LoadBalancer? missing --ingressendpoint?)." )
1102- return nil // Unreachable
1114+ t .Fatal ("No IP or domain found on ingress." )
11031115 }
1116+ return nil // Unreachable
11041117}
11051118
11061119type RequestOption func (* http.Request )
0 commit comments