Search in sources :

Example 1 with Service

use of io.fabric8.kubernetes.api.model.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 2 with Service

use of io.fabric8.kubernetes.api.model.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 3 with Service

use of io.fabric8.kubernetes.api.model.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)

Example 4 with Service

use of io.fabric8.kubernetes.api.model.Service 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();
}
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 5 with Service

use of io.fabric8.kubernetes.api.model.Service in project camel by apache.

the class KubernetesServicesProducerTest method getServiceTest.

@Test
public void getServiceTest() throws Exception {
    if (ObjectHelper.isEmpty(authToken)) {
        return;
    }
    Exchange ex = template.request("direct:getServices", 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, "elasticsearch");
        }
    });
    Service result = ex.getOut().getBody(Service.class);
    assertNull(result);
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Service(io.fabric8.kubernetes.api.model.Service) Test(org.junit.Test)

Aggregations

Service (io.fabric8.kubernetes.api.model.Service)14 HashMap (java.util.HashMap)7 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 KubernetesService (org.eclipse.che.plugin.openshift.client.kubernetes.KubernetesService)6 Pod (io.fabric8.kubernetes.api.model.Pod)5 ServiceList (io.fabric8.kubernetes.api.model.ServiceList)5 List (java.util.List)5 OpenShiftException (org.eclipse.che.plugin.openshift.client.exception.OpenShiftException)5 Test (org.junit.Test)5 PodList (io.fabric8.kubernetes.api.model.PodList)4 ImageStreamTag (io.fabric8.openshift.api.model.ImageStreamTag)4 Exchange (org.apache.camel.Exchange)4 Processor (org.apache.camel.Processor)4 Container (io.fabric8.kubernetes.api.model.Container)3 DoneableService (io.fabric8.kubernetes.api.model.DoneableService)3 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)3 ServiceAccount (io.fabric8.kubernetes.api.model.ServiceAccount)3 ServiceSpec (io.fabric8.kubernetes.api.model.ServiceSpec)3