Search in sources :

Example 1 with ServiceAccount

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

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

the class KubernetesServiceAccountsProducer method doListServiceAccountsByLabels.

protected void doListServiceAccountsByLabels(Exchange exchange, String operation) throws Exception {
    ServiceAccountList saList = null;
    Map<String, String> labels = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_SERVICE_ACCOUNTS_LABELS, Map.class);
    String namespaceName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class);
    if (!ObjectHelper.isEmpty(namespaceName)) {
        NonNamespaceOperation<ServiceAccount, ServiceAccountList, DoneableServiceAccount, Resource<ServiceAccount, DoneableServiceAccount>> serviceAccounts;
        serviceAccounts = getEndpoint().getKubernetesClient().serviceAccounts().inNamespace(namespaceName);
        for (Map.Entry<String, String> entry : labels.entrySet()) {
            serviceAccounts.withLabel(entry.getKey(), entry.getValue());
        }
        saList = serviceAccounts.list();
    } else {
        MixedOperation<ServiceAccount, ServiceAccountList, DoneableServiceAccount, Resource<ServiceAccount, DoneableServiceAccount>> serviceAccounts;
        serviceAccounts = getEndpoint().getKubernetesClient().serviceAccounts();
        for (Map.Entry<String, String> entry : labels.entrySet()) {
            serviceAccounts.withLabel(entry.getKey(), entry.getValue());
        }
        saList = serviceAccounts.list();
    }
    MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
    exchange.getOut().setBody(saList.getItems());
}
Also used : DoneableServiceAccount(io.fabric8.kubernetes.api.model.DoneableServiceAccount) ServiceAccount(io.fabric8.kubernetes.api.model.ServiceAccount) DoneableServiceAccount(io.fabric8.kubernetes.api.model.DoneableServiceAccount) Resource(io.fabric8.kubernetes.client.dsl.Resource) Map(java.util.Map) ServiceAccountList(io.fabric8.kubernetes.api.model.ServiceAccountList)

Example 3 with ServiceAccount

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

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

the class KubernetesServiceAccountsProducerTest method createAndDeleteServiceAccount.

@Test
public void createAndDeleteServiceAccount() throws Exception {
    if (ObjectHelper.isEmpty(authToken)) {
        return;
    }
    Exchange ex = template.request("direct:create", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_ACCOUNT_NAME, "test");
            Map<String, String> labels = new HashMap<String, String>();
            labels.put("this", "rocks");
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_ACCOUNTS_LABELS, labels);
            ServiceAccount s = new ServiceAccount();
            s.setKind("ServiceAccount");
            Map<String, String> mp = new HashMap<String, String>();
            mp.put("username", Base64.encodeBase64String("pippo".getBytes()));
            mp.put("password", Base64.encodeBase64String("password".getBytes()));
            ObjectMeta meta = new ObjectMeta();
            meta.setName("test");
            s.setMetadata(meta);
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_ACCOUNT, s);
        }
    });
    ServiceAccount sec = ex.getOut().getBody(ServiceAccount.class);
    assertEquals(sec.getMetadata().getName(), "test");
    ex = template.request("direct:delete", new Processor() {

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

Example 5 with ServiceAccount

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

the class KubernetesServiceAccountsProducerTest 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_ACCOUNTS_LABELS, labels);
        }
    });
    List<ServiceAccount> result = ex.getOut().getBody(List.class);
    assertTrue(result.size() == 0);
}
Also used : Exchange(org.apache.camel.Exchange) ServiceAccount(io.fabric8.kubernetes.api.model.ServiceAccount) Processor(org.apache.camel.Processor) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

ServiceAccount (io.fabric8.kubernetes.api.model.ServiceAccount)6 DoneableServiceAccount (io.fabric8.kubernetes.api.model.DoneableServiceAccount)3 Map (java.util.Map)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 Exchange (org.apache.camel.Exchange)2 Processor (org.apache.camel.Processor)2 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)1 ServiceAccountList (io.fabric8.kubernetes.api.model.ServiceAccountList)1 Resource (io.fabric8.kubernetes.client.dsl.Resource)1