Search in sources :

Example 6 with Service

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);
}
Also used : ServicePort(io.fabric8.kubernetes.api.model.ServicePort) Processor(org.apache.camel.Processor) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) ServiceSpec(io.fabric8.kubernetes.api.model.ServiceSpec) Service(io.fabric8.kubernetes.api.model.Service) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) Exchange(org.apache.camel.Exchange) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 7 with Service

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);
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Service(io.fabric8.kubernetes.api.model.Service) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 8 with Service

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);
}
Also used : DoneableServiceAccount(io.fabric8.kubernetes.api.model.DoneableServiceAccount) ServiceAccount(io.fabric8.kubernetes.api.model.ServiceAccount)

Example 9 with Service

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);
}
Also used : DoneableServiceAccount(io.fabric8.kubernetes.api.model.DoneableServiceAccount) ServiceAccount(io.fabric8.kubernetes.api.model.ServiceAccount)

Example 10 with Service

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);
}
Also used : ServiceSpec(io.fabric8.kubernetes.api.model.ServiceSpec) DoneableService(io.fabric8.kubernetes.api.model.DoneableService) Service(io.fabric8.kubernetes.api.model.Service) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder)

Aggregations

Service (io.fabric8.kubernetes.api.model.Service)142 Test (org.junit.Test)93 File (java.io.File)60 IOException (java.io.IOException)54 ArrayList (java.util.ArrayList)54 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)48 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)43 HashMap (java.util.HashMap)41 ServiceBuilder (io.fabric8.kubernetes.api.model.ServiceBuilder)38 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)33 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)33 Pod (io.fabric8.kubernetes.api.model.Pod)31 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)30 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)29 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)25 List (java.util.List)25 Map (java.util.Map)25 FileInputStream (java.io.FileInputStream)22 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)20 ServicePortBuilder (io.fabric8.kubernetes.api.model.ServicePortBuilder)20