Search in sources :

Example 1 with Namespace

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

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

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

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

the class KubernetesNamespacesConsumerTest method createAndDeletePod.

@Test
public void createAndDeletePod() throws Exception {
    if (ObjectHelper.isEmpty(authToken)) {
        return;
    }
    mockResultEndpoint.expectedMessageCount(5);
    mockResultEndpoint.expectedHeaderValuesReceivedInAnyOrder(KubernetesConstants.KUBERNETES_EVENT_ACTION, "ADDED", "MODIFIED", "MODIFIED", "MODIFIED", "DELETED");
    Exchange ex = template.request("direct:createNamespace", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "test");
            Map<String, String> labels = new HashMap<String, String>();
            labels.put("this", "rocks");
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_LABELS, labels);
        }
    });
    Namespace ns = ex.getOut().getBody(Namespace.class);
    assertEquals(ns.getMetadata().getName(), "test");
    ex = template.request("direct:listByLabels", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            Map<String, String> labels = new HashMap<String, String>();
            labels.put("this", "rocks");
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_LABELS, labels);
        }
    });
    List<Namespace> result = ex.getOut().getBody(List.class);
    boolean testExists = false;
    Iterator<Namespace> it = result.iterator();
    while (it.hasNext()) {
        Namespace namespace = it.next();
        if ("test".equalsIgnoreCase(namespace.getMetadata().getName())) {
            testExists = true;
        }
    }
    assertTrue(testExists);
    ex = template.request("direct:deleteNamespace", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "test");
        }
    });
    boolean nsDeleted = ex.getOut().getBody(Boolean.class);
    assertTrue(nsDeleted);
    Thread.sleep(3000);
    mockResultEndpoint.assertIsSatisfied();
}
Also used : Processor(org.apache.camel.Processor) HashMap(java.util.HashMap) Namespace(io.fabric8.kubernetes.api.model.Namespace) Exchange(org.apache.camel.Exchange) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 5 with Namespace

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

the class KubernetesNamespacesProducer method doCreateNamespace.

protected void doCreateNamespace(Exchange exchange, String operation) {
    String namespaceName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class);
    if (ObjectHelper.isEmpty(namespaceName)) {
        LOG.error("Create a specific namespace require specify a namespace name");
        throw new IllegalArgumentException("Create a specific namespace require specify a namespace name");
    }
    Map<String, String> labels = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_LABELS, Map.class);
    Namespace ns = new NamespaceBuilder().withNewMetadata().withName(namespaceName).withLabels(labels).endMetadata().build();
    Namespace namespace = getEndpoint().getKubernetesClient().namespaces().create(ns);
    MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
    exchange.getOut().setBody(namespace);
}
Also used : DoneableNamespace(io.fabric8.kubernetes.api.model.DoneableNamespace) Namespace(io.fabric8.kubernetes.api.model.Namespace) NamespaceBuilder(io.fabric8.kubernetes.api.model.NamespaceBuilder)

Aggregations

Test (org.junit.Test)70 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)55 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)48 IOException (java.io.IOException)46 HashMap (java.util.HashMap)46 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)41 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)39 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)32 Service (io.fabric8.kubernetes.api.model.Service)31 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)31 ArrayList (java.util.ArrayList)29 Pod (io.fabric8.kubernetes.api.model.Pod)28 FileNotFoundException (java.io.FileNotFoundException)25 OpenShiftNotAvailableException (io.fabric8.openshift.client.OpenShiftNotAvailableException)24 JSONObject (org.json.JSONObject)22 Async (io.vertx.ext.unit.Async)20 Map (java.util.Map)20 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)19 File (java.io.File)19 ConfigMapBuilder (io.fabric8.kubernetes.api.model.ConfigMapBuilder)18