forked from fabric8io/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix fabric8io#1139 : Make it easy to get the URL of a service.
+ Implemented getUrl() method for service + Fixed an arquillian dependency error that comes up while running tests in IntelliJ
- Loading branch information
1 parent
0e730c0
commit a7feaf0
Showing
22 changed files
with
849 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
kubernetes-client/src/main/java/io/fabric8/kubernetes/client/ServiceToURLProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.fabric8.kubernetes.client; | ||
|
||
import io.fabric8.kubernetes.api.model.Service; | ||
|
||
public interface ServiceToURLProvider { | ||
enum ServiceToUrlImplPriority { | ||
FIRST(0), SECOND(1), THIRD(2), FOURTH(3); | ||
|
||
private final int value; | ||
|
||
ServiceToUrlImplPriority(final int newVal) { | ||
value = newVal; | ||
} | ||
|
||
public int getValue() { return value; } | ||
} | ||
|
||
int getPriority(); | ||
|
||
String getURL(Service service, String portName, String namespace, KubernetesClient client); | ||
} |
50 changes: 50 additions & 0 deletions
50
kubernetes-client/src/main/java/io/fabric8/kubernetes/client/URLFromEnvVarsImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.fabric8.kubernetes.client; | ||
|
||
import io.fabric8.kubernetes.api.model.Service; | ||
import io.fabric8.kubernetes.client.utils.URLFromServiceUtil; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class URLFromEnvVarsImpl implements ServiceToURLProvider { | ||
public static Logger logger = LoggerFactory.getLogger(URLFromEnvVarsImpl.class); | ||
|
||
public static final String ANNOTATION_EXPOSE_URL = "fabric8.io/exposeUrl"; | ||
|
||
@Override | ||
public String getURL(Service service, String portName, String namespace, KubernetesClient client) { | ||
String serviceHost = URLFromServiceUtil.resolveHostFromEnvVarOrSystemProperty(service.getMetadata().getName()); | ||
String servicePort = URLFromServiceUtil.resolvePortFromEnvVarOrSystemProperty(service.getMetadata().getName(), ""); | ||
String serviceProtocol = URLFromServiceUtil.resolveProtocolFromEnvVarOrSystemProperty(service.getSpec().getPorts().iterator().next().getProtocol(), ""); | ||
|
||
if(!serviceHost.isEmpty() && !servicePort.isEmpty() && !serviceProtocol.isEmpty()) { | ||
return serviceProtocol + "://" + serviceHost + ":" + servicePort; | ||
} else { | ||
String answer = URLFromServiceUtil.getOrCreateAnnotations(service).get(ANNOTATION_EXPOSE_URL); | ||
if(answer != null && !answer.isEmpty()) { | ||
return answer; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public int getPriority() { | ||
return ServiceToUrlImplPriority.THIRD.getValue(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
kubernetes-client/src/main/java/io/fabric8/kubernetes/client/URLFromIngressImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.fabric8.kubernetes.client; | ||
|
||
import io.fabric8.kubernetes.api.model.Service; | ||
import io.fabric8.kubernetes.api.model.ServicePort; | ||
import io.fabric8.kubernetes.api.model.extensions.*; | ||
import io.fabric8.kubernetes.client.utils.URLFromServiceUtil; | ||
|
||
public class URLFromIngressImpl implements ServiceToURLProvider { | ||
|
||
@Override | ||
public String getURL(Service service, String portName, String namespace, KubernetesClient client) { | ||
ServicePort port = URLFromServiceUtil.getServicePortByName(service, portName); | ||
String serviceName = service.getMetadata().getName(); | ||
if(port == null) { | ||
throw new RuntimeException("Couldn't find port: " + portName + " for service " + service.getMetadata().getName()); | ||
} | ||
|
||
IngressList ingresses = client.extensions().ingresses().inNamespace(namespace).list(); | ||
if(ingresses != null && !ingresses.getItems().isEmpty()) { | ||
return URLFromServiceUtil.getURLFromIngressList(ingresses.getItems(), namespace, serviceName, port); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public int getPriority() { | ||
return ServiceToUrlImplPriority.FIRST.getValue(); | ||
} | ||
|
||
} |
90 changes: 90 additions & 0 deletions
90
kubernetes-client/src/main/java/io/fabric8/kubernetes/client/URLFromNodePortImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.fabric8.kubernetes.client; | ||
|
||
import io.fabric8.kubernetes.api.model.*; | ||
import io.fabric8.kubernetes.client.utils.URLFromServiceUtil; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.List; | ||
|
||
public class URLFromNodePortImpl implements ServiceToURLProvider { | ||
public static Logger logger = LoggerFactory.getLogger(URLFromNodePortImpl.class); | ||
|
||
public String getURL(Service service, String portName, String namespace, KubernetesClient client) { | ||
ServicePort port = URLFromServiceUtil.getServicePortByName(service, portName); | ||
String serviceProto = port.getProtocol(); | ||
NodePortUrlComponents urlComponents = null; | ||
Integer nodePort = port.getNodePort(); | ||
if(nodePort != null) { | ||
try { | ||
NodeList nodeList = client.nodes().list(); | ||
if(nodeList != null && nodeList.getItems() != null) { | ||
for(Node item : nodeList.getItems()) { | ||
urlComponents = getUrlComponentsFromNodeList(item.getStatus(), nodePort); | ||
if(urlComponents != null) { | ||
break; | ||
} | ||
} | ||
} | ||
} catch (KubernetesClientException exception) { | ||
logger.warn("Could not find a node! " + exception); | ||
} | ||
} | ||
|
||
return urlComponents != null ? (serviceProto + "://" + urlComponents.getClusterIP() + ":" + urlComponents.getPortNumber()).toLowerCase() : null; | ||
} | ||
|
||
private NodePortUrlComponents getUrlComponentsFromNodeList(NodeStatus nodeStatus, Integer nodePort) { | ||
if(nodeStatus != null) { | ||
List<NodeAddress> addresses = nodeStatus.getAddresses(); | ||
for (NodeAddress address : addresses) { | ||
String ip = address.getAddress(); | ||
if (!ip.isEmpty()) { | ||
return new NodePortUrlComponents(ip, nodePort); | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private class NodePortUrlComponents { | ||
public String getClusterIP() { | ||
return clusterIP; | ||
} | ||
|
||
private String clusterIP; | ||
|
||
public Integer getPortNumber() { | ||
return portNumber; | ||
} | ||
|
||
private Integer portNumber; | ||
|
||
public NodePortUrlComponents(String clusterIP, Integer portNumber) { | ||
this.clusterIP = clusterIP; | ||
this.portNumber = portNumber; | ||
} | ||
} | ||
|
||
@Override | ||
public int getPriority() { | ||
return ServiceToUrlImplPriority.SECOND.getValue(); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/ServiceResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.fabric8.kubernetes.client.dsl; | ||
|
||
public interface ServiceResource<T, D> extends Resource<T, D> { | ||
String getURL(String portName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.