1616
1717package io .fabric8 .kubernetes .client .utils .internal ;
1818
19- import io .fabric8 .kubernetes .api .model .*;
20- import io .fabric8 .kubernetes .api .model .extensions .*;
19+ import io .fabric8 .kubernetes .api .model .IntOrString ;
20+ import io .fabric8 .kubernetes .api .model .Service ;
21+ import io .fabric8 .kubernetes .api .model .ServicePort ;
2122import io .fabric8 .kubernetes .client .utils .URLUtils ;
2223import org .slf4j .Logger ;
2324import org .slf4j .LoggerFactory ;
2425
25- import java .util .LinkedHashMap ;
2626import java .util .List ;
2727import java .util .Locale ;
28- import java .util .Map ;
2928import java .util .Objects ;
3029
30+ import static io .fabric8 .kubernetes .client .utils .KubernetesResourceUtil .getNamespace ;
31+
3132public class URLFromServiceUtil {
3233 public static final Logger logger = LoggerFactory .getLogger (URLFromServiceUtil .class );
3334 public static final String DEFAULT_PROTO = "tcp" ;
@@ -36,7 +37,6 @@ public class URLFromServiceUtil {
3637 private static final String PROTO_SUFFIX = "_TCP_PROTO" ;
3738
3839 private URLFromServiceUtil () {
39- throw new IllegalStateException ("Utility class" );
4040 }
4141
4242 public static String resolveHostFromEnvVarOrSystemProperty (String serviceName ) {
@@ -65,33 +65,13 @@ public static String resolveProtocolFromEnvVarOrSystemProperty(String serviceNam
6565 DEFAULT_PROTO );
6666 }
6767
68- public static Map <String , String > getOrCreateAnnotations (HasMetadata entity ) {
69- ObjectMeta metadata = getOrCreateMetadata (entity );
70- Map <String , String > answer = metadata .getAnnotations ();
71- if (answer == null ) {
72- // use linked so the annotations can be in the FIFO order
73- answer = new LinkedHashMap <>();
74- metadata .setAnnotations (answer );
75- }
76- return answer ;
77- }
78-
79- public static ObjectMeta getOrCreateMetadata (HasMetadata entity ) {
80- ObjectMeta metadata = entity .getMetadata ();
81- if (metadata == null ) {
82- metadata = new ObjectMeta ();
83- entity .setMetadata (metadata );
84- }
85- return metadata ;
86- }
87-
8868 public static String resolvePortFromEnvVarOrSystemProperty (String serviceName , String portName ) {
8969 String envVarName = toServicePortEnvironmentVariable (serviceName , portName );
9070 return getEnvVarOrSystemProperty (envVarName , "" );
9171 }
9272
9373 public static String toServicePortEnvironmentVariable (String serviceName , String portName ) {
94- String name = serviceName + PORT_SUFFIX + (portName .isEmpty () ? "_" + portName : "" );
74+ String name = serviceName + PORT_SUFFIX + (! portName .isEmpty () ? "_" + portName : "" );
9575 return toEnvVariable (name );
9676 }
9777
@@ -103,22 +83,73 @@ public static String toEnvVariable(String serviceName) {
10383 return serviceName .toUpperCase (Locale .ROOT ).replaceAll ("-" , "_" );
10484 }
10585
106- public static String getURLFromIngressList (List <Ingress > ingressList , String namespace , String serviceName ,
86+ public static String getURLFromExtensionsV1beta1IngressList (
87+ List <io .fabric8 .kubernetes .api .model .extensions .Ingress > ingressList , String namespace , String serviceName ,
88+ ServicePort port ) {
89+ for (io .fabric8 .kubernetes .api .model .extensions .Ingress item : ingressList ) {
90+ String ns = getNamespace (item );
91+ if (Objects .equals (ns , namespace ) && item .getSpec () != null ) {
92+ String url = getURLFromIngressSpec (item .getSpec (), serviceName , port );
93+ if (url != null ) {
94+ return url ;
95+ }
96+ }
97+ }
98+ return null ;
99+ }
100+
101+ public static String getURLFromNetworkingV1IngressList (
102+ List <io .fabric8 .kubernetes .api .model .networking .v1 .Ingress > ingressList , String namespace , String serviceName ,
107103 ServicePort port ) {
108- for (Ingress item : ingressList ) {
104+ for (io . fabric8 . kubernetes . api . model . networking . v1 . Ingress item : ingressList ) {
109105 String ns = getNamespace (item );
110106 if (Objects .equals (ns , namespace ) && item .getSpec () != null ) {
111- return getURLFromIngressSpec (item .getSpec (), serviceName , port );
107+ String url = getURLFromNetworkV1IngressSpec (item .getSpec (), serviceName , port );
108+ if (url != null ) {
109+ return url ;
110+ }
112111 }
113112 }
114113 return null ;
115114 }
116115
117- public static String getURLFromIngressSpec (IngressSpec spec , String serviceName , ServicePort port ) {
118- List <IngressRule > ingressRules = spec .getRules ();
116+ public static String getURLFromNetworkV1IngressSpec (io .fabric8 .kubernetes .api .model .networking .v1 .IngressSpec spec ,
117+ String serviceName , ServicePort port ) {
118+ List <io .fabric8 .kubernetes .api .model .networking .v1 .IngressRule > ingressRules = spec .getRules ();
119119 if (ingressRules != null && !ingressRules .isEmpty ()) {
120- for (IngressRule rule : ingressRules ) {
121- HTTPIngressRuleValue http = rule .getHttp ();
120+ for (io .fabric8 .kubernetes .api .model .networking .v1 .IngressRule rule : ingressRules ) {
121+ io .fabric8 .kubernetes .api .model .networking .v1 .HTTPIngressRuleValue http = rule .getHttp ();
122+ if (http != null && http .getPaths () != null ) {
123+ return getURLFromNetworkV1IngressRules (http .getPaths (), spec , serviceName , port , rule );
124+ }
125+ }
126+ }
127+ return null ;
128+ }
129+
130+ public static String getURLFromNetworkV1IngressRules (
131+ List <io .fabric8 .kubernetes .api .model .networking .v1 .HTTPIngressPath > paths ,
132+ io .fabric8 .kubernetes .api .model .networking .v1 .IngressSpec spec , String serviceName ,
133+ ServicePort port , io .fabric8 .kubernetes .api .model .networking .v1 .IngressRule rule ) {
134+ for (io .fabric8 .kubernetes .api .model .networking .v1 .HTTPIngressPath path : paths ) {
135+ io .fabric8 .kubernetes .api .model .networking .v1 .IngressBackend backend = path .getBackend ();
136+ if (backend != null ) {
137+ String backendServiceName = backend .getService ().getName ();
138+ if (serviceName .equals (backendServiceName )
139+ && portsMatch (port , new IntOrString (backend .getService ().getPort ().getNumber ()))) {
140+ return getURLFromIngressBackend (spec .getTls () != null && !spec .getTls ().isEmpty (), path .getPath (), rule .getHost ());
141+ }
142+ }
143+ }
144+ return null ;
145+ }
146+
147+ public static String getURLFromIngressSpec (io .fabric8 .kubernetes .api .model .extensions .IngressSpec spec , String serviceName ,
148+ ServicePort port ) {
149+ List <io .fabric8 .kubernetes .api .model .extensions .IngressRule > ingressRules = spec .getRules ();
150+ if (ingressRules != null && !ingressRules .isEmpty ()) {
151+ for (io .fabric8 .kubernetes .api .model .extensions .IngressRule rule : ingressRules ) {
152+ io .fabric8 .kubernetes .api .model .extensions .HTTPIngressRuleValue http = rule .getHttp ();
122153 if (http != null && http .getPaths () != null ) {
123154 return getURLFromIngressRules (http .getPaths (), spec , serviceName , port , rule );
124155 }
@@ -127,30 +158,33 @@ public static String getURLFromIngressSpec(IngressSpec spec, String serviceName,
127158 return null ;
128159 }
129160
130- public static String getURLFromIngressRules (List <HTTPIngressPath > paths , IngressSpec spec , String serviceName ,
131- ServicePort port , IngressRule rule ) {
132- for (HTTPIngressPath path : paths ) {
133- IngressBackend backend = path .getBackend ();
161+ public static String getURLFromIngressRules (List <io .fabric8 .kubernetes .api .model .extensions .HTTPIngressPath > paths ,
162+ io .fabric8 .kubernetes .api .model .extensions .IngressSpec spec , String serviceName ,
163+ ServicePort port , io .fabric8 .kubernetes .api .model .extensions .IngressRule rule ) {
164+ for (io .fabric8 .kubernetes .api .model .extensions .HTTPIngressPath path : paths ) {
165+ io .fabric8 .kubernetes .api .model .extensions .IngressBackend backend = path .getBackend ();
134166 if (backend != null ) {
135167 String backendServiceName = backend .getServiceName ();
136168 if (serviceName .equals (backendServiceName ) && portsMatch (port , backend .getServicePort ())) {
137- String pathPostFix = path .getPath ();
138- if (spec .getTls () != null ) {
139- return getURLFromTLSHost (rule , pathPostFix );
140- }
141- String answer = rule .getHost ();
142- if (answer != null && !answer .isEmpty ()) {
143- pathPostFix = fixPathPostFixIfEmpty (pathPostFix );
144- return "http://" + URLUtils .pathJoin (answer , pathPostFix );
145- }
169+ return getURLFromIngressBackend (spec .getTls () != null && !spec .getTls ().isEmpty (), path .getPath (), rule .getHost ());
146170 }
147171 }
148172 }
149173 return null ;
150174 }
151175
152- public static String getURLFromTLSHost (IngressRule rule , String pathPostFix ) {
153- String host = rule .getHost ();
176+ private static String getURLFromIngressBackend (boolean tlsProvided , String pathPostFix , String host ) {
177+ if (tlsProvided ) {
178+ return getURLFromTLSHost (host , pathPostFix );
179+ }
180+ if (host != null && !host .isEmpty ()) {
181+ pathPostFix = fixPathPostFixIfEmpty (pathPostFix );
182+ return "http://" + URLUtils .pathJoin (host , pathPostFix );
183+ }
184+ return null ;
185+ }
186+
187+ public static String getURLFromTLSHost (String host , String pathPostFix ) {
154188 if (!host .isEmpty ()) {
155189 pathPostFix = fixPathPostFixIfEmpty (pathPostFix );
156190 return "https://" + URLUtils .pathJoin (host , pathPostFix );
@@ -180,14 +214,6 @@ private static boolean portsMatch(ServicePort servicePort, IntOrString intOrStri
180214 return false ;
181215 }
182216
183- public static String getNamespace (HasMetadata entity ) {
184- if (entity != null ) {
185- return entity .getMetadata () != null ? entity .getMetadata ().getNamespace () : null ;
186- } else {
187- return null ;
188- }
189- }
190-
191217 public static ServicePort getServicePortByName (Service service , String portName ) {
192218 if (portName .isEmpty ()) {
193219 return service .getSpec ().getPorts ().iterator ().next ();
0 commit comments