use of io.fabric8.kubernetes.api.model.Service in project camel by apache.
the class KubernetesServicesProducerTest method listByLabelsTest.
@Test
public void listByLabelsTest() throws Exception {
if (ObjectHelper.isEmpty(authToken)) {
return;
}
Exchange ex = template.request("direct:listByLabels", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
Map<String, String> labels = new HashMap<String, String>();
labels.put("component", "elasticsearch");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_LABELS, labels);
}
});
List<Service> result = ex.getOut().getBody(List.class);
boolean serviceExists = false;
Iterator<Service> it = result.iterator();
while (it.hasNext()) {
Service service = it.next();
if ("elasticsearch".equalsIgnoreCase(service.getMetadata().getName())) {
serviceExists = true;
}
}
assertFalse(serviceExists);
}
use of io.fabric8.kubernetes.api.model.Service in project carbon-apimgt by wso2.
the class ServiceDiscovererKubernetes method addServicesToEndpointList.
/**
* For each service in {@code serviceList} list, methods are called to add endpoints of different types,
* for each of service's ports
*
* @param serviceList filtered list of services
*/
private void addServicesToEndpointList(List<Service> serviceList, List<Endpoint> endpointList) throws MalformedURLException {
for (Service service : serviceList) {
// Set the parameters that does not change with the service port
String serviceName = service.getMetadata().getName();
String namespace = service.getMetadata().getNamespace();
Map<String, String> labelsMap = service.getMetadata().getLabels();
String labels = (labelsMap != null) ? labelsMap.toString() : "";
ServiceSpec serviceSpec = service.getSpec();
String serviceType = serviceSpec.getType();
if (includeExternalNameTypeServices && EXTERNAL_NAME.equals(serviceType)) {
// Since only a "ExternalName" type service can have an "externalName" (the alias in kube-dns)
addExternalNameEndpoint(serviceName, serviceSpec.getExternalName(), namespace, labels, endpointList);
}
for (ServicePort servicePort : serviceSpec.getPorts()) {
String protocol = servicePort.getName();
if (APIMgtConstants.HTTP.equals(protocol) || APIMgtConstants.HTTPS.equals(protocol)) {
int port = servicePort.getPort();
if (includeClusterIP && !EXTERNAL_NAME.equals(serviceType)) {
// Since almost every service has a cluster IP, except for ExternalName type
addClusterIPEndpoint(serviceName, serviceSpec.getClusterIP(), port, protocol, namespace, labels, endpointList);
}
if (NODE_PORT.equals(serviceType) || LOAD_BALANCER.equals(serviceType)) {
// Because both "NodePort" and "LoadBalancer" types of services have "NodePort" type URLs
addNodePortEndpoint(serviceName, servicePort.getNodePort(), protocol, namespace, labels, endpointList);
}
if (LOAD_BALANCER.equals(serviceType)) {
// Since only "LoadBalancer" type services have "LoadBalancer" type URLs
addLoadBalancerEndpoint(serviceName, service, port, protocol, namespace, labels, endpointList);
}
// A Special case (can be any of the service types above)
addExternalIPEndpoint(serviceName, serviceSpec.getExternalIPs(), port, protocol, namespace, labels, endpointList);
} else if (log.isDebugEnabled()) {
log.debug("Service:{} Namespace:{} Port:{}/{} Application level protocol not defined.", serviceName, namespace, servicePort.getPort(), protocol);
}
}
}
}
use of io.fabric8.kubernetes.api.model.Service in project carbon-apimgt by wso2.
the class ServiceDiscovererKubernetes method listServices.
/**
* {@inheritDoc}
*/
@Override
public List<Endpoint> listServices(String namespace, Map<String, String> criteria) throws ServiceDiscoveryException {
List<Endpoint> endpointList = new ArrayList<>();
if (client != null) {
log.debug("Looking for services, with the specified labels, in namespace {}", namespace);
try {
List<Service> serviceList = client.services().inNamespace(namespace).withLabels(criteria).list().getItems();
addServicesToEndpointList(serviceList, endpointList);
} catch (KubernetesClientException | MalformedURLException e) {
String msg = "Error occurred while trying to list services using Kubernetes client";
throw new ServiceDiscoveryException(msg, e, ExceptionCodes.ERROR_WHILE_TRYING_TO_DISCOVER_SERVICES);
} catch (NoSuchMethodError e) {
String msg = "Filtering criteria in the deployment yaml includes unwanted characters";
throw new ServiceDiscoveryException(msg, e, ExceptionCodes.ERROR_WHILE_TRYING_TO_DISCOVER_SERVICES);
}
}
return endpointList;
}
use of io.fabric8.kubernetes.api.model.Service in project carbon-apimgt by wso2.
the class ServiceDiscovererKubernetes method listServices.
/**
* {@inheritDoc}
*/
@Override
public List<Endpoint> listServices() throws ServiceDiscoveryException {
List<Endpoint> endpointList = new ArrayList<>();
if (client != null) {
log.debug("Looking for services in all namespaces");
try {
List<Service> serviceList = client.services().inNamespace(null).list().getItems();
addServicesToEndpointList(serviceList, endpointList);
} catch (KubernetesClientException | MalformedURLException e) {
String msg = "Error occurred while trying to list services using Kubernetes client";
throw new ServiceDiscoveryException(msg, e, ExceptionCodes.ERROR_WHILE_TRYING_TO_DISCOVER_SERVICES);
}
}
return endpointList;
}
use of io.fabric8.kubernetes.api.model.Service in project carbon-apimgt by wso2.
the class ServiceDiscovererKubernetes method listServices.
/**
* {@inheritDoc}
*/
@Override
public List<Endpoint> listServices(String namespace) throws ServiceDiscoveryException {
List<Endpoint> endpointList = new ArrayList<>();
if (client != null) {
log.debug("Looking for services in namespace {}", namespace);
try {
List<Service> serviceList = client.services().inNamespace(namespace).list().getItems();
addServicesToEndpointList(serviceList, endpointList);
} catch (KubernetesClientException | MalformedURLException e) {
String msg = "Error occurred while trying to list services using Kubernetes client";
throw new ServiceDiscoveryException(msg, e, ExceptionCodes.ERROR_WHILE_TRYING_TO_DISCOVER_SERVICES);
}
}
return endpointList;
}
Aggregations