use of io.fabric8.knative.serving.v1.Service 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.knative.serving.v1.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.knative.serving.v1.Service in project camel by apache.
the class KubernetesServiceAccountsProducer method doCreateServiceAccount.
protected void doCreateServiceAccount(Exchange exchange, String operation) throws Exception {
ServiceAccount sa = null;
String namespaceName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class);
ServiceAccount saToCreate = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_SERVICE_ACCOUNT, ServiceAccount.class);
if (ObjectHelper.isEmpty(namespaceName)) {
LOG.error("Create a specific Service Account require specify a namespace name");
throw new IllegalArgumentException("Create a specific Service Account require specify a namespace name");
}
if (ObjectHelper.isEmpty(saToCreate)) {
LOG.error("Create a specific Service Account require specify a Service Account bean");
throw new IllegalArgumentException("Create a specific Service Account require specify a Service Account bean");
}
Map<String, String> labels = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_SERVICE_ACCOUNTS_LABELS, Map.class);
sa = getEndpoint().getKubernetesClient().serviceAccounts().inNamespace(namespaceName).create(saToCreate);
MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
exchange.getOut().setBody(sa);
}
use of io.fabric8.knative.serving.v1.Service in project camel by apache.
the class KubernetesServiceAccountsProducer method doGetServiceAccount.
protected void doGetServiceAccount(Exchange exchange, String operation) throws Exception {
ServiceAccount sa = null;
String saName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_SERVICE_ACCOUNT_NAME, String.class);
String namespaceName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class);
if (ObjectHelper.isEmpty(saName)) {
LOG.error("Get a specific Service Account require specify a Service Account name");
throw new IllegalArgumentException("Get a specific Service Account require specify a Service Account name");
}
if (ObjectHelper.isEmpty(namespaceName)) {
LOG.error("Get a specific Service Account require specify a namespace name");
throw new IllegalArgumentException("Get a specific Service Account require specify a namespace name");
}
sa = getEndpoint().getKubernetesClient().serviceAccounts().inNamespace(namespaceName).withName(saName).get();
MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
exchange.getOut().setBody(sa);
}
use of io.fabric8.knative.serving.v1.Service 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);
}
Aggregations