use of io.fabric8.kubernetes.api.model.ServiceSpec in project camel by apache.
the class KubernetesServicesProducerTest method createAndDeleteService.
@Test
public void createAndDeleteService() throws Exception {
if (ObjectHelper.isEmpty(authToken)) {
return;
}
Exchange ex = template.request("direct:createService", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_NAME, "test");
Map<String, String> labels = new HashMap<String, String>();
labels.put("this", "rocks");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_LABELS, labels);
ServiceSpec serviceSpec = new ServiceSpec();
List<ServicePort> lsp = new ArrayList<ServicePort>();
ServicePort sp = new ServicePort();
sp.setPort(8080);
sp.setTargetPort(new IntOrString(8080));
sp.setProtocol("TCP");
lsp.add(sp);
serviceSpec.setPorts(lsp);
Map<String, String> selectorMap = new HashMap<String, String>();
selectorMap.put("containter", "test");
serviceSpec.setSelector(selectorMap);
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_SPEC, serviceSpec);
}
});
Service serv = ex.getOut().getBody(Service.class);
assertEquals(serv.getMetadata().getName(), "test");
ex = template.request("direct:deleteService", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_NAME, "test");
}
});
boolean servDeleted = ex.getOut().getBody(Boolean.class);
assertTrue(servDeleted);
}
use of io.fabric8.kubernetes.api.model.ServiceSpec in project camel by apache.
the class KubernetesServicesProducer method doCreateService.
protected void doCreateService(Exchange exchange, String operation) throws Exception {
Service service = null;
String serviceName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_SERVICE_NAME, String.class);
String namespaceName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class);
ServiceSpec serviceSpec = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_SERVICE_SPEC, ServiceSpec.class);
if (ObjectHelper.isEmpty(serviceName)) {
LOG.error("Create a specific service require specify a service name");
throw new IllegalArgumentException("Create a specific service require specify a service name");
}
if (ObjectHelper.isEmpty(namespaceName)) {
LOG.error("Create a specific service require specify a namespace name");
throw new IllegalArgumentException("Create a specific service require specify a namespace name");
}
if (ObjectHelper.isEmpty(serviceSpec)) {
LOG.error("Create a specific service require specify a service spec bean");
throw new IllegalArgumentException("Create a specific service require specify a service spec bean");
}
Map<String, String> labels = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_SERVICE_LABELS, Map.class);
Service serviceCreating = new ServiceBuilder().withNewMetadata().withName(serviceName).withLabels(labels).endMetadata().withSpec(serviceSpec).build();
service = getEndpoint().getKubernetesClient().services().inNamespace(namespaceName).create(serviceCreating);
exchange.getOut().setBody(service);
}
use of io.fabric8.kubernetes.api.model.ServiceSpec in project camel by apache.
the class KubernetesServicesConsumerTest method createAndDeleteService.
@Test
public void createAndDeleteService() throws Exception {
if (ObjectHelper.isEmpty(authToken)) {
return;
}
mockResultEndpoint.expectedMessageCount(2);
mockResultEndpoint.expectedHeaderValuesReceivedInAnyOrder(KubernetesConstants.KUBERNETES_EVENT_ACTION, "ADDED", "DELETED");
Exchange ex = template.request("direct:createService", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_NAME, "test");
Map<String, String> labels = new HashMap<String, String>();
labels.put("this", "rocks");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_LABELS, labels);
ServiceSpec serviceSpec = new ServiceSpec();
List<ServicePort> lsp = new ArrayList<ServicePort>();
ServicePort sp = new ServicePort();
sp.setPort(8080);
sp.setTargetPort(new IntOrString(8080));
sp.setProtocol("TCP");
lsp.add(sp);
serviceSpec.setPorts(lsp);
Map<String, String> selectorMap = new HashMap<String, String>();
selectorMap.put("containter", "test");
serviceSpec.setSelector(selectorMap);
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_SPEC, serviceSpec);
}
});
Service serv = ex.getOut().getBody(Service.class);
assertEquals(serv.getMetadata().getName(), "test");
ex = template.request("direct:deleteService", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_NAME, "test");
}
});
boolean servDeleted = ex.getOut().getBody(Boolean.class);
assertTrue(servDeleted);
Thread.sleep(3000);
mockResultEndpoint.assertIsSatisfied();
}
use of io.fabric8.kubernetes.api.model.ServiceSpec 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.ServiceSpec in project fabric8-maven-plugin by fabric8io.
the class ApplyMojo method shouldCreateExternalURLForService.
/**
* Should we try to create an external URL for the given service?
* <p/>
* By default lets ignore the kubernetes services and any service which does not expose ports 80 and 443
*
* @return true if we should create an OpenShift Route for this service.
*/
private boolean shouldCreateExternalURLForService(Service service, String id) {
if ("kubernetes".equals(id) || "kubernetes-ro".equals(id)) {
return false;
}
Set<Integer> ports = KubernetesHelper.getPorts(service);
log.debug("Service " + id + " has ports: " + ports);
if (ports.size() == 1) {
String type = null;
ServiceSpec spec = service.getSpec();
if (spec != null) {
type = spec.getType();
if (Objects.equals(type, "LoadBalancer")) {
return true;
}
}
log.info("Not generating route for service " + id + " type is not LoadBalancer: " + type);
return false;
} else {
log.info("Not generating route for service " + id + " as only single port services are supported. Has ports: " + ports);
return false;
}
}
Aggregations