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);
}
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());
}
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);
}
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);
}
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);
}
Aggregations