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
79d38b1
commit 36caf46
Showing
22 changed files
with
807 additions
and
21 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; } | ||
} | ||
|
||
ServiceToUrlImplPriority 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.URLFromServiceImplUtil; | ||
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 = URLFromServiceImplUtil.serviceToHostOrBlank(service.getMetadata().getName()); | ||
String servicePort = URLFromServiceImplUtil.serviceToPortOrBlank(service.getMetadata().getName(), ""); | ||
String serviceProtocol = URLFromServiceImplUtil.serviceToProtocol(service.getSpec().getPorts().iterator().next().getProtocol(), ""); | ||
|
||
if(!serviceHost.isEmpty() && !servicePort.isEmpty() && !serviceProtocol.isEmpty()) { | ||
return serviceProtocol + "://" + serviceHost + ":" + servicePort; | ||
} else { | ||
String answer = URLFromServiceImplUtil.getOrCreateAnnotations(service).get(ANNOTATION_EXPOSE_URL); | ||
if(answer != null && !answer.isEmpty()) { | ||
return answer; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public ServiceToUrlImplPriority getPriority() { | ||
return ServiceToUrlImplPriority.THIRD; | ||
} | ||
} |
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.URLFromServiceImplUtil; | ||
|
||
public class URLFromIngressImpl implements ServiceToURLProvider { | ||
|
||
@Override | ||
public String getURL(Service service, String portName, String namespace, KubernetesClient client) { | ||
ServicePort port = URLFromServiceImplUtil.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 URLFromServiceImplUtil.getURLFromIngressList(ingresses.getItems(), namespace, serviceName, port); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public ServiceToUrlImplPriority getPriority() { | ||
return ServiceToUrlImplPriority.FIRST; | ||
} | ||
|
||
} |
68 changes: 68 additions & 0 deletions
68
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,68 @@ | ||
/** | ||
* 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.URLFromServiceImplUtil; | ||
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) { | ||
boolean bFound = false; | ||
ServicePort port = URLFromServiceImplUtil.getServicePortByName(service, portName); | ||
String serviceProto = port.getProtocol(); | ||
String clusterIP = null; | ||
Integer portNumber = 0; | ||
Integer nodePort = port.getNodePort(); | ||
if(nodePort != null) { | ||
try { | ||
NodeList nodeList = client.nodes().list(); | ||
if(nodeList != null && nodeList.getItems() != null) { | ||
for(Node item : nodeList.getItems()) { | ||
NodeStatus status = item.getStatus(); | ||
if(!bFound && status != null) { | ||
List<NodeAddress> addresses = status.getAddresses(); | ||
for(NodeAddress address : addresses) { | ||
String ip = address.getAddress(); | ||
if (!ip.isEmpty()) { | ||
clusterIP = ip; | ||
portNumber = nodePort; | ||
bFound = true; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} catch (KubernetesClientException exception) { | ||
logger.warn("Could not find a node! " + exception); | ||
} | ||
} | ||
return (serviceProto + "://" + clusterIP + ":" + portNumber).toLowerCase(); | ||
} | ||
|
||
@Override | ||
public ServiceToUrlImplPriority getPriority() { | ||
return ServiceToUrlImplPriority.SECOND; | ||
} | ||
|
||
} |
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.