http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesOperations.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesOperations.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesOperations.java new file mode 100644 index 0000000..c926cbf --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesOperations.java @@ -0,0 +1,96 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.kubernetes.producer; + +public interface KubernetesOperations { + + // Namespaces + String LIST_NAMESPACE_OPERATION = "listNamespaces"; + String LIST_NAMESPACE_BY_LABELS_OPERATION = "listNamespacesByLabels"; + String GET_NAMESPACE_OPERATION = "getNamespace"; + String CREATE_NAMESPACE_OPERATION = "createNamespace"; + String DELETE_NAMESPACE_OPERATION = "deleteNamespace"; + + // Services + String LIST_SERVICES_OPERATION = "listServices"; + String LIST_SERVICES_BY_LABELS_OPERATION = "listServicesByLabels"; + String GET_SERVICE_OPERATION = "getService"; + String CREATE_SERVICE_OPERATION = "createService"; + String DELETE_SERVICE_OPERATION = "deleteService"; + + // Replication Controllers + String LIST_REPLICATION_CONTROLLERS_OPERATION = "listReplicationControllers"; + String LIST_REPLICATION_CONTROLLERS_BY_LABELS_OPERATION = "listReplicationControllersByLabels"; + String GET_REPLICATION_CONTROLLER_OPERATION = "getReplicationController"; + String CREATE_REPLICATION_CONTROLLER_OPERATION = "createReplicationController"; + String DELETE_REPLICATION_CONTROLLER_OPERATION = "deleteReplicationController"; + + // Pods + String LIST_PODS_OPERATION = "listPods"; + String LIST_PODS_BY_LABELS_OPERATION = "listPodsByLabels"; + String GET_POD_OPERATION = "getPod"; + String CREATE_POD_OPERATION = "createPod"; + String DELETE_POD_OPERATION = "deletePod"; + + // Persistent Volumes + String LIST_PERSISTENT_VOLUMES = "listPersistentVolumes"; + String LIST_PERSISTENT_VOLUMES_BY_LABELS_OPERATION = "listPersistentVolumesByLabels"; + String GET_PERSISTENT_VOLUME_OPERATION = "getPersistentVolume"; + + // Persistent Volumes Claims + String LIST_PERSISTENT_VOLUMES_CLAIMS = "listPersistentVolumesClaims"; + String LIST_PERSISTENT_VOLUMES_CLAIMS_BY_LABELS_OPERATION = "listPersistentVolumesClaimsByLabels"; + String GET_PERSISTENT_VOLUME_CLAIM_OPERATION = "getPersistentVolumeClaim"; + String CREATE_PERSISTENT_VOLUME_CLAIM_OPERATION = "createPersistentVolumeClaim"; + String DELETE_PERSISTENT_VOLUME_CLAIM_OPERATION = "deletePersistentVolumeClaim"; + + // Secrets + String LIST_SECRETS = "listSecrets"; + String LIST_SECRETS_BY_LABELS_OPERATION = "listSecretsByLabels"; + String GET_SECRET_OPERATION = "getSecret"; + String CREATE_SECRET_OPERATION = "createSecret"; + String DELETE_SECRET_OPERATION = "deleteSecret"; + + // Resources quota + String LIST_RESOURCES_QUOTA = "listResourcesQuota"; + String LIST_RESOURCES_QUOTA_BY_LABELS_OPERATION = "listResourcesQuotaByLabels"; + String GET_RESOURCE_QUOTA_OPERATION = "getResourceQuota"; + String CREATE_RESOURCE_QUOTA_OPERATION = "createResourceQuota"; + String DELETE_RESOURCE_QUOTA_OPERATION = "deleteResourceQuota"; + + // Service Accounts + String LIST_SERVICE_ACCOUNTS = "listServiceAccounts"; + String LIST_SERVICE_ACCOUNTS_BY_LABELS_OPERATION = "listServiceAccountsByLabels"; + String GET_SERVICE_ACCOUNT_OPERATION = "getServiceAccount"; + String CREATE_SERVICE_ACCOUNT_OPERATION = "createServiceAccount"; + String DELETE_SERVICE_ACCOUNT_OPERATION = "deleteServiceAccount"; + + // Nodes + String LIST_NODES = "listNodes"; + String LIST_NODES_BY_LABELS_OPERATION = "listNodesByLabels"; + String GET_NODE_OPERATION = "getNode"; + + // Builds + String LIST_BUILD = "listBuilds"; + String LIST_BUILD_BY_LABELS_OPERATION = "listBuildsByLabels"; + String GET_BUILD_OPERATION = "getBuild"; + + // Build Configs + String LIST_BUILD_CONFIGS = "listBuildConfigs"; + String LIST_BUILD_CONFIGS_BY_LABELS_OPERATION = "listBuildConfigsByLabels"; + String GET_BUILD_CONFIG_OPERATION = "getBuildConfig"; +}
http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesPersistentVolumesClaimsProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesPersistentVolumesClaimsProducer.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesPersistentVolumesClaimsProducer.java new file mode 100644 index 0000000..609a00d --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesPersistentVolumesClaimsProducer.java @@ -0,0 +1,216 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.kubernetes.producer; + +import java.util.Map; + +import io.fabric8.kubernetes.api.model.DoneablePersistentVolumeClaim; +import io.fabric8.kubernetes.api.model.EditablePersistentVolumeClaim; +import io.fabric8.kubernetes.api.model.PersistentVolumeClaim; +import io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder; +import io.fabric8.kubernetes.api.model.PersistentVolumeClaimList; +import io.fabric8.kubernetes.api.model.PersistentVolumeClaimSpec; +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.dsl.ClientNonNamespaceOperation; +import io.fabric8.kubernetes.client.dsl.ClientOperation; +import io.fabric8.kubernetes.client.dsl.ClientResource; + +import org.apache.camel.Exchange; +import org.apache.camel.component.kubernetes.KubernetesConstants; +import org.apache.camel.component.kubernetes.KubernetesEndpoint; +import org.apache.camel.impl.DefaultProducer; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class KubernetesPersistentVolumesClaimsProducer extends DefaultProducer { + + private static final Logger LOG = LoggerFactory + .getLogger(KubernetesPersistentVolumesClaimsProducer.class); + + public KubernetesPersistentVolumesClaimsProducer(KubernetesEndpoint endpoint) { + super(endpoint); + } + + @Override + public KubernetesEndpoint getEndpoint() { + return (KubernetesEndpoint) super.getEndpoint(); + } + + @Override + public void process(Exchange exchange) throws Exception { + String operation; + + if (ObjectHelper.isEmpty(getEndpoint().getKubernetesConfiguration() + .getOperation())) { + operation = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_OPERATION, String.class); + } else { + operation = getEndpoint().getKubernetesConfiguration() + .getOperation(); + } + + switch (operation) { + + case KubernetesOperations.LIST_PERSISTENT_VOLUMES_CLAIMS: + doList(exchange, operation); + break; + + case KubernetesOperations.LIST_PERSISTENT_VOLUMES_CLAIMS_BY_LABELS_OPERATION: + doListPersistentVolumesClaimsByLabels(exchange, operation); + break; + + case KubernetesOperations.GET_PERSISTENT_VOLUME_CLAIM_OPERATION: + doGetPersistentVolumeClaim(exchange, operation); + break; + + case KubernetesOperations.CREATE_PERSISTENT_VOLUME_CLAIM_OPERATION: + doCreatePersistentVolumeClaim(exchange, operation); + break; + + case KubernetesOperations.DELETE_PERSISTENT_VOLUME_CLAIM_OPERATION: + doDeletePersistentVolumeClaim(exchange, operation); + break; + + default: + throw new IllegalArgumentException("Unsupported operation " + + operation); + } + } + + protected void doList(Exchange exchange, String operation) throws Exception { + PersistentVolumeClaimList persistentVolumeClaimList = getEndpoint() + .getKubernetesClient().persistentVolumeClaims().list(); + exchange.getOut().setBody(persistentVolumeClaimList.getItems()); + } + + protected void doListPersistentVolumesClaimsByLabels(Exchange exchange, + String operation) throws Exception { + PersistentVolumeClaimList pvcList = null; + Map<String, String> labels = exchange + .getIn() + .getHeader( + KubernetesConstants.KUBERNETES_PERSISTENT_VOLUMES_CLAIMS_LABELS, + Map.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (!ObjectHelper.isEmpty(namespaceName)) { + ClientNonNamespaceOperation<KubernetesClient, PersistentVolumeClaim, PersistentVolumeClaimList, + DoneablePersistentVolumeClaim, ClientResource<PersistentVolumeClaim, DoneablePersistentVolumeClaim>> pvcs; + pvcs = getEndpoint().getKubernetesClient().persistentVolumeClaims() + .inNamespace(namespaceName); + for (Map.Entry<String, String> entry : labels.entrySet()) { + pvcs.withLabel(entry.getKey(), entry.getValue()); + } + pvcList = pvcs.list(); + } else { + ClientOperation<KubernetesClient, PersistentVolumeClaim, PersistentVolumeClaimList, + DoneablePersistentVolumeClaim, ClientResource<PersistentVolumeClaim, DoneablePersistentVolumeClaim>> pvcs; + pvcs = getEndpoint().getKubernetesClient().persistentVolumeClaims(); + for (Map.Entry<String, String> entry : labels.entrySet()) { + pvcs.withLabel(entry.getKey(), entry.getValue()); + } + pvcList = pvcs.list(); + } + exchange.getOut().setBody(pvcList.getItems()); + } + + protected void doGetPersistentVolumeClaim(Exchange exchange, + String operation) throws Exception { + PersistentVolumeClaim pvc = null; + String pvcName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_PERSISTENT_VOLUME_CLAIM_NAME, + String.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (ObjectHelper.isEmpty(pvcName)) { + LOG.error("Get a specific Persistent Volume Claim require specify a Persistent Volume Claim name"); + throw new IllegalArgumentException( + "Get a specific Persistent Volume Claim require specify a Persistent Volume Claim name"); + } + if (ObjectHelper.isEmpty(namespaceName)) { + LOG.error("Get a specific Persistent Volume Claim require specify a namespace name"); + throw new IllegalArgumentException( + "Get a specific Persistent Volume Claim require specify a namespace name"); + } + pvc = getEndpoint().getKubernetesClient().persistentVolumeClaims() + .inNamespace(namespaceName).withName(pvcName).get(); + exchange.getOut().setBody(pvc); + } + + protected void doCreatePersistentVolumeClaim(Exchange exchange, + String operation) throws Exception { + PersistentVolumeClaim pvc = null; + String pvcName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_PERSISTENT_VOLUME_CLAIM_NAME, + String.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + PersistentVolumeClaimSpec pvcSpec = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_PERSISTENT_VOLUME_CLAIM_SPEC, + PersistentVolumeClaimSpec.class); + if (ObjectHelper.isEmpty(pvcName)) { + LOG.error("Create a specific Persistent Volume Claim require specify a Persistent Volume Claim name"); + throw new IllegalArgumentException( + "Create a specific Persistent Volume Claim require specify a Persistent Volume Claim name"); + } + if (ObjectHelper.isEmpty(namespaceName)) { + LOG.error("Create a specific Persistent Volume Claim require specify a namespace name"); + throw new IllegalArgumentException( + "Create a specific Persistent Volume Claim require specify a namespace name"); + } + if (ObjectHelper.isEmpty(pvcSpec)) { + LOG.error("Create a specific Persistent Volume Claim require specify a Persistent Volume Claim spec bean"); + throw new IllegalArgumentException( + "Create a specific Persistent Volume Claim require specify a Persistent Volume Claim spec bean"); + } + Map<String, String> labels = exchange + .getIn() + .getHeader( + KubernetesConstants.KUBERNETES_PERSISTENT_VOLUMES_CLAIMS_LABELS, + Map.class); + EditablePersistentVolumeClaim pvcCreating = new PersistentVolumeClaimBuilder() + .withNewMetadata().withName(pvcName).withLabels(labels) + .endMetadata().withSpec(pvcSpec).build(); + pvc = getEndpoint().getKubernetesClient().persistentVolumeClaims() + .inNamespace(namespaceName).create(pvcCreating); + exchange.getOut().setBody(pvc); + } + + protected void doDeletePersistentVolumeClaim(Exchange exchange, + String operation) throws Exception { + String pvcName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_PERSISTENT_VOLUME_CLAIM_NAME, + String.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (ObjectHelper.isEmpty(pvcName)) { + LOG.error("Delete a specific Persistent Volume Claim require specify a Persistent Volume Claim name"); + throw new IllegalArgumentException( + "Delete a specific Persistent Volume Claim require specify a Persistent Volume Claim name"); + } + if (ObjectHelper.isEmpty(namespaceName)) { + LOG.error("Delete a specific Persistent Volume Claim require specify a namespace name"); + throw new IllegalArgumentException( + "Delete a specific Persistent Volume Claim require specify a namespace name"); + } + boolean pvcDeleted = getEndpoint().getKubernetesClient() + .persistentVolumeClaims().inNamespace(namespaceName) + .withName(pvcName).delete(); + exchange.getOut().setBody(pvcDeleted); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesPersistentVolumesProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesPersistentVolumesProducer.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesPersistentVolumesProducer.java new file mode 100644 index 0000000..48f2beb --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesPersistentVolumesProducer.java @@ -0,0 +1,139 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.kubernetes.producer; + +import java.util.Map; + +import io.fabric8.kubernetes.api.model.DoneablePersistentVolume; +import io.fabric8.kubernetes.api.model.PersistentVolume; +import io.fabric8.kubernetes.api.model.PersistentVolumeList; +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.dsl.ClientNonNamespaceOperation; +import io.fabric8.kubernetes.client.dsl.ClientOperation; +import io.fabric8.kubernetes.client.dsl.ClientResource; + +import org.apache.camel.Exchange; +import org.apache.camel.component.kubernetes.KubernetesConstants; +import org.apache.camel.component.kubernetes.KubernetesEndpoint; +import org.apache.camel.impl.DefaultProducer; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class KubernetesPersistentVolumesProducer extends DefaultProducer { + + private static final Logger LOG = LoggerFactory + .getLogger(KubernetesPersistentVolumesProducer.class); + + public KubernetesPersistentVolumesProducer(KubernetesEndpoint endpoint) { + super(endpoint); + } + + @Override + public KubernetesEndpoint getEndpoint() { + return (KubernetesEndpoint) super.getEndpoint(); + } + + @Override + public void process(Exchange exchange) throws Exception { + String operation; + + if (ObjectHelper.isEmpty(getEndpoint().getKubernetesConfiguration() + .getOperation())) { + operation = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_OPERATION, String.class); + } else { + operation = getEndpoint().getKubernetesConfiguration() + .getOperation(); + } + + switch (operation) { + + case KubernetesOperations.LIST_PERSISTENT_VOLUMES: + doList(exchange, operation); + break; + + case KubernetesOperations.LIST_PERSISTENT_VOLUMES_BY_LABELS_OPERATION: + doListPersistentVolumesByLabels(exchange, operation); + break; + + case KubernetesOperations.GET_PERSISTENT_VOLUME_OPERATION: + doGetPersistentVolume(exchange, operation); + break; + + default: + throw new IllegalArgumentException("Unsupported operation " + + operation); + } + } + + protected void doList(Exchange exchange, String operation) throws Exception { + PersistentVolumeList persistentVolumeList = getEndpoint() + .getKubernetesClient().persistentVolumes().inAnyNamespace().list(); + exchange.getOut().setBody(persistentVolumeList.getItems()); + } + + protected void doListPersistentVolumesByLabels(Exchange exchange, + String operation) throws Exception { + PersistentVolumeList pvList = null; + Map<String, String> labels = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_PERSISTENT_VOLUMES_LABELS, + Map.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (!ObjectHelper.isEmpty(namespaceName)) { + ClientNonNamespaceOperation<KubernetesClient, PersistentVolume, PersistentVolumeList, DoneablePersistentVolume, ClientResource<PersistentVolume, DoneablePersistentVolume>> pvs; + pvs = getEndpoint().getKubernetesClient().persistentVolumes() + .inNamespace(namespaceName); + for (Map.Entry<String, String> entry : labels.entrySet()) { + pvs.withLabel(entry.getKey(), entry.getValue()); + } + pvList = pvs.list(); + } else { + ClientOperation<KubernetesClient, PersistentVolume, PersistentVolumeList, DoneablePersistentVolume, ClientResource<PersistentVolume, DoneablePersistentVolume>> pvs; + pvs = getEndpoint().getKubernetesClient().persistentVolumes(); + for (Map.Entry<String, String> entry : labels.entrySet()) { + pvs.withLabel(entry.getKey(), entry.getValue()); + } + pvList = pvs.list(); + } + exchange.getOut().setBody(pvList.getItems()); + } + + protected void doGetPersistentVolume(Exchange exchange, String operation) + throws Exception { + PersistentVolume pv = null; + String pvName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_PERSISTENT_VOLUME_NAME, + String.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (ObjectHelper.isEmpty(pvName)) { + LOG.error("Get a specific Persistent Volume require specify a Persistent Volume name"); + throw new IllegalArgumentException( + "Get a specific Persistent Volume require specify a Persistent Volume name"); + } + if (ObjectHelper.isEmpty(namespaceName)) { + LOG.error("Get a specific Persistent Volume require specify a namespace name"); + throw new IllegalArgumentException( + "Get a specific Persistent Volume require specify a namespace name"); + } + pv = getEndpoint().getKubernetesClient().persistentVolumes() + .inNamespace(namespaceName).withName(pvName).get(); + exchange.getOut().setBody(pv); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesPodsProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesPodsProducer.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesPodsProducer.java new file mode 100644 index 0000000..1d4e648 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesPodsProducer.java @@ -0,0 +1,193 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.kubernetes.producer; + +import java.util.Map; + +import io.fabric8.kubernetes.api.model.DoneablePod; +import io.fabric8.kubernetes.api.model.EditablePod; +import io.fabric8.kubernetes.api.model.Pod; +import io.fabric8.kubernetes.api.model.PodBuilder; +import io.fabric8.kubernetes.api.model.PodList; +import io.fabric8.kubernetes.api.model.PodSpec; +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.dsl.ClientLoggableResource; +import io.fabric8.kubernetes.client.dsl.ClientOperation; +import io.fabric8.kubernetes.client.dsl.ClientResource; + +import org.apache.camel.Exchange; +import org.apache.camel.component.kubernetes.KubernetesConstants; +import org.apache.camel.component.kubernetes.KubernetesEndpoint; +import org.apache.camel.impl.DefaultProducer; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class KubernetesPodsProducer extends DefaultProducer { + + private static final Logger LOG = LoggerFactory + .getLogger(KubernetesPodsProducer.class); + + public KubernetesPodsProducer(KubernetesEndpoint endpoint) { + super(endpoint); + } + + @Override + public KubernetesEndpoint getEndpoint() { + return (KubernetesEndpoint) super.getEndpoint(); + } + + @Override + public void process(Exchange exchange) throws Exception { + String operation; + + if (ObjectHelper.isEmpty(getEndpoint().getKubernetesConfiguration() + .getOperation())) { + operation = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_OPERATION, String.class); + } else { + operation = getEndpoint().getKubernetesConfiguration() + .getOperation(); + } + + switch (operation) { + + case KubernetesOperations.LIST_PODS_OPERATION: + doList(exchange, operation); + break; + + case KubernetesOperations.LIST_PODS_BY_LABELS_OPERATION: + doListPodsByLabel(exchange, operation); + break; + + case KubernetesOperations.GET_POD_OPERATION: + doGetPod(exchange, operation); + break; + + case KubernetesOperations.CREATE_POD_OPERATION: + doCreatePod(exchange, operation); + break; + + case KubernetesOperations.DELETE_POD_OPERATION: + doDeletePod(exchange, operation); + break; + + default: + throw new IllegalArgumentException("Unsupported operation " + + operation); + } + } + + protected void doList(Exchange exchange, String operation) throws Exception { + PodList podList = getEndpoint().getKubernetesClient().pods().list(); + exchange.getOut().setBody(podList.getItems()); + } + + protected void doListPodsByLabel(Exchange exchange, String operation) { + Map<String, String> labels = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_PODS_LABELS, Map.class); + if (ObjectHelper.isEmpty(labels)) { + LOG.error("Get pods by labels require specify a labels set"); + throw new IllegalArgumentException( + "Get pods by labels require specify a labels set"); + } + ClientOperation<KubernetesClient, Pod, PodList, DoneablePod, ClientLoggableResource<Pod, DoneablePod>> pods; + pods = getEndpoint().getKubernetesClient().pods(); + for (Map.Entry<String, String> entry : labels.entrySet()) { + pods.withLabel(entry.getKey(), entry.getValue()); + } + PodList podList = pods.list(); + exchange.getOut().setBody(podList.getItems()); + } + + protected void doGetPod(Exchange exchange, String operation) + throws Exception { + Pod pod = null; + String podName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_POD_NAME, String.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (ObjectHelper.isEmpty(podName)) { + LOG.error("Get a specific pod require specify a pod name"); + throw new IllegalArgumentException( + "Get a specific pod require specify a pod name"); + } + if (ObjectHelper.isEmpty(namespaceName)) { + LOG.error("Get a specific pod require specify a namespace name"); + throw new IllegalArgumentException( + "Get a specific pod require specify a namespace name"); + } + pod = getEndpoint().getKubernetesClient().pods() + .inNamespace(namespaceName).withName(podName).get(); + exchange.getOut().setBody(pod); + } + + protected void doCreatePod(Exchange exchange, String operation) + throws Exception { + Pod pod = null; + String podName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_POD_NAME, String.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + PodSpec podSpec = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_POD_SPEC, PodSpec.class); + if (ObjectHelper.isEmpty(podName)) { + LOG.error("Create a specific pod require specify a pod name"); + throw new IllegalArgumentException( + "Create a specific pod require specify a pod name"); + } + if (ObjectHelper.isEmpty(namespaceName)) { + LOG.error("Create a specific pod require specify a namespace name"); + throw new IllegalArgumentException( + "Create a specific pod require specify a namespace name"); + } + if (ObjectHelper.isEmpty(podSpec)) { + LOG.error("Create a specific pod require specify a pod spec bean"); + throw new IllegalArgumentException( + "Create a specific pod require specify a pod spec bean"); + } + Map<String, String> labels = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_PODS_LABELS, Map.class); + EditablePod podCreating = new PodBuilder().withNewMetadata() + .withName(podName).withLabels(labels).endMetadata() + .withSpec(podSpec).build(); + pod = getEndpoint().getKubernetesClient().pods() + .inNamespace(namespaceName).create(podCreating); + exchange.getOut().setBody(pod); + } + + protected void doDeletePod(Exchange exchange, String operation) + throws Exception { + String podName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_POD_NAME, String.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (ObjectHelper.isEmpty(podName)) { + LOG.error("Delete a specific pod require specify a pod name"); + throw new IllegalArgumentException( + "Delete a specific pod require specify a pod name"); + } + if (ObjectHelper.isEmpty(namespaceName)) { + LOG.error("Delete a specific pod require specify a namespace name"); + throw new IllegalArgumentException( + "Delete a specific pod require specify a namespace name"); + } + boolean podDeleted = getEndpoint().getKubernetesClient().pods() + .inNamespace(namespaceName).withName(podName).delete(); + exchange.getOut().setBody(podDeleted); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesReplicationControllersProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesReplicationControllersProducer.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesReplicationControllersProducer.java new file mode 100644 index 0000000..5870b09 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesReplicationControllersProducer.java @@ -0,0 +1,223 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.kubernetes.producer; + +import java.util.Map; + +import io.fabric8.kubernetes.api.model.DoneableReplicationController; +import io.fabric8.kubernetes.api.model.EditableReplicationController; +import io.fabric8.kubernetes.api.model.ReplicationController; +import io.fabric8.kubernetes.api.model.ReplicationControllerBuilder; +import io.fabric8.kubernetes.api.model.ReplicationControllerList; +import io.fabric8.kubernetes.api.model.ReplicationControllerSpec; +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.dsl.ClientNonNamespaceOperation; +import io.fabric8.kubernetes.client.dsl.ClientOperation; +import io.fabric8.kubernetes.client.dsl.ClientRollableScallableResource; + +import org.apache.camel.Exchange; +import org.apache.camel.component.kubernetes.KubernetesConstants; +import org.apache.camel.component.kubernetes.KubernetesEndpoint; +import org.apache.camel.impl.DefaultProducer; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class KubernetesReplicationControllersProducer extends DefaultProducer { + + private static final Logger LOG = LoggerFactory + .getLogger(KubernetesReplicationControllersProducer.class); + + public KubernetesReplicationControllersProducer(KubernetesEndpoint endpoint) { + super(endpoint); + } + + @Override + public KubernetesEndpoint getEndpoint() { + return (KubernetesEndpoint) super.getEndpoint(); + } + + @Override + public void process(Exchange exchange) throws Exception { + String operation; + + if (ObjectHelper.isEmpty(getEndpoint().getKubernetesConfiguration() + .getOperation())) { + operation = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_OPERATION, String.class); + } else { + operation = getEndpoint().getKubernetesConfiguration() + .getOperation(); + } + + switch (operation) { + + case KubernetesOperations.LIST_REPLICATION_CONTROLLERS_OPERATION: + doList(exchange, operation); + break; + + case KubernetesOperations.LIST_REPLICATION_CONTROLLERS_BY_LABELS_OPERATION: + doListReplicationControllersByLabels(exchange, operation); + break; + + case KubernetesOperations.GET_REPLICATION_CONTROLLER_OPERATION: + doGetReplicationController(exchange, operation); + break; + + case KubernetesOperations.CREATE_REPLICATION_CONTROLLER_OPERATION: + doCreateReplicationController(exchange, operation); + break; + + case KubernetesOperations.DELETE_REPLICATION_CONTROLLER_OPERATION: + doDeleteReplicationController(exchange, operation); + break; + + default: + throw new IllegalArgumentException("Unsupported operation " + + operation); + } + } + + protected void doList(Exchange exchange, String operation) throws Exception { + ReplicationControllerList rcList = null; + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (!ObjectHelper.isEmpty(namespaceName)) { + rcList = getEndpoint().getKubernetesClient() + .replicationControllers().inNamespace(namespaceName).list(); + } else { + rcList = getEndpoint().getKubernetesClient() + .replicationControllers().list(); + } + exchange.getOut().setBody(rcList.getItems()); + } + + protected void doListReplicationControllersByLabels(Exchange exchange, + String operation) throws Exception { + ReplicationControllerList rcList = null; + Map<String, String> labels = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_REPLICATION_CONTROLLERS_LABELS, + Map.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (!ObjectHelper.isEmpty(namespaceName)) { + ClientNonNamespaceOperation<KubernetesClient, ReplicationController, ReplicationControllerList, DoneableReplicationController, ClientRollableScallableResource<ReplicationController, + DoneableReplicationController>> replicationControllers; + replicationControllers = getEndpoint().getKubernetesClient() + .replicationControllers().inNamespace(namespaceName); + for (Map.Entry<String, String> entry : labels.entrySet()) { + replicationControllers.withLabel(entry.getKey(), + entry.getValue()); + } + rcList = replicationControllers.list(); + } else { + ClientOperation<KubernetesClient, ReplicationController, ReplicationControllerList, DoneableReplicationController, + ClientRollableScallableResource<ReplicationController, DoneableReplicationController>> replicationControllers; + replicationControllers = getEndpoint().getKubernetesClient() + .replicationControllers(); + for (Map.Entry<String, String> entry : labels.entrySet()) { + replicationControllers.withLabel(entry.getKey(), + entry.getValue()); + } + rcList = replicationControllers.list(); + } + exchange.getOut().setBody(rcList.getItems()); + } + + protected void doGetReplicationController(Exchange exchange, + String operation) throws Exception { + ReplicationController rc = null; + String rcName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_REPLICATION_CONTROLLER_NAME, + String.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (ObjectHelper.isEmpty(rcName)) { + LOG.error("Get a specific replication controller require specify a replication controller name"); + throw new IllegalArgumentException( + "Get a specific replication controller require specify a replication controller name"); + } + if (ObjectHelper.isEmpty(namespaceName)) { + LOG.error("Get a specific replication controller require specify a namespace name"); + throw new IllegalArgumentException( + "Get a specific replication controller require specify a namespace name"); + } + rc = getEndpoint().getKubernetesClient().replicationControllers() + .inNamespace(namespaceName).withName(rcName).get(); + exchange.getOut().setBody(rc); + } + + protected void doCreateReplicationController(Exchange exchange, + String operation) throws Exception { + ReplicationController rc = null; + String rcName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_REPLICATION_CONTROLLER_NAME, + String.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + ReplicationControllerSpec rcSpec = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_REPLICATION_CONTROLLER_SPEC, + ReplicationControllerSpec.class); + if (ObjectHelper.isEmpty(rcName)) { + LOG.error("Create a specific replication controller require specify a replication controller name"); + throw new IllegalArgumentException( + "Create a specific replication controller require specify a replication controller name"); + } + if (ObjectHelper.isEmpty(namespaceName)) { + LOG.error("Create a specific replication controller require specify a namespace name"); + throw new IllegalArgumentException( + "Create a specific replication controller require specify a namespace name"); + } + if (ObjectHelper.isEmpty(rcSpec)) { + LOG.error("Create a specific replication controller require specify a replication controller spec bean"); + throw new IllegalArgumentException( + "Create a specific replication controller require specify a replication controller spec bean"); + } + Map<String, String> labels = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_REPLICATION_CONTROLLERS_LABELS, + Map.class); + EditableReplicationController rcCreating = new ReplicationControllerBuilder() + .withNewMetadata().withName(rcName).withLabels(labels) + .endMetadata().withSpec(rcSpec).build(); + rc = getEndpoint().getKubernetesClient().replicationControllers() + .inNamespace(namespaceName).create(rcCreating); + exchange.getOut().setBody(rc); + } + + protected void doDeleteReplicationController(Exchange exchange, + String operation) throws Exception { + String rcName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_REPLICATION_CONTROLLER_NAME, + String.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (ObjectHelper.isEmpty(rcName)) { + LOG.error("Delete a specific replication controller require specify a replication controller name"); + throw new IllegalArgumentException( + "Delete a specific replication controller require specify a replication controller name"); + } + if (ObjectHelper.isEmpty(namespaceName)) { + LOG.error("Delete a specific replication controller require specify a namespace name"); + throw new IllegalArgumentException( + "Delete a specific replication controller require specify a namespace name"); + } + boolean rcDeleted = getEndpoint().getKubernetesClient() + .replicationControllers().inNamespace(namespaceName) + .withName(rcName).delete(); + exchange.getOut().setBody(rcDeleted); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesResourcesQuotaProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesResourcesQuotaProducer.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesResourcesQuotaProducer.java new file mode 100644 index 0000000..d61f418 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesResourcesQuotaProducer.java @@ -0,0 +1,210 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.kubernetes.producer; + +import java.util.Map; + +import io.fabric8.kubernetes.api.model.DoneableResourceQuota; +import io.fabric8.kubernetes.api.model.EditableResourceQuota; +import io.fabric8.kubernetes.api.model.ResourceQuota; +import io.fabric8.kubernetes.api.model.ResourceQuotaBuilder; +import io.fabric8.kubernetes.api.model.ResourceQuotaList; +import io.fabric8.kubernetes.api.model.ResourceQuotaSpec; +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.dsl.ClientNonNamespaceOperation; +import io.fabric8.kubernetes.client.dsl.ClientOperation; +import io.fabric8.kubernetes.client.dsl.ClientResource; + +import org.apache.camel.Exchange; +import org.apache.camel.component.kubernetes.KubernetesConstants; +import org.apache.camel.component.kubernetes.KubernetesEndpoint; +import org.apache.camel.impl.DefaultProducer; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class KubernetesResourcesQuotaProducer extends DefaultProducer { + + private static final Logger LOG = LoggerFactory + .getLogger(KubernetesResourcesQuotaProducer.class); + + public KubernetesResourcesQuotaProducer(KubernetesEndpoint endpoint) { + super(endpoint); + } + + @Override + public KubernetesEndpoint getEndpoint() { + return (KubernetesEndpoint) super.getEndpoint(); + } + + @Override + public void process(Exchange exchange) throws Exception { + String operation; + + if (ObjectHelper.isEmpty(getEndpoint().getKubernetesConfiguration() + .getOperation())) { + operation = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_OPERATION, String.class); + } else { + operation = getEndpoint().getKubernetesConfiguration() + .getOperation(); + } + + switch (operation) { + + case KubernetesOperations.LIST_RESOURCES_QUOTA: + doList(exchange, operation); + break; + + case KubernetesOperations.LIST_SECRETS_BY_LABELS_OPERATION: + doListResourceQuotasByLabels(exchange, operation); + break; + + case KubernetesOperations.GET_RESOURCE_QUOTA_OPERATION: + doGetResourceQuota(exchange, operation); + break; + + case KubernetesOperations.CREATE_RESOURCE_QUOTA_OPERATION: + doCreateResourceQuota(exchange, operation); + break; + + case KubernetesOperations.DELETE_RESOURCE_QUOTA_OPERATION: + doDeleteResourceQuota(exchange, operation); + break; + + default: + throw new IllegalArgumentException("Unsupported operation " + + operation); + } + } + + protected void doList(Exchange exchange, String operation) throws Exception { + ResourceQuotaList resList = getEndpoint().getKubernetesClient() + .resourceQuotas().list(); + exchange.getOut().setBody(resList.getItems()); + } + + protected void doListResourceQuotasByLabels(Exchange exchange, + String operation) throws Exception { + ResourceQuotaList resList = null; + Map<String, String> labels = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_RESOURCES_QUOTA_LABELS, + Map.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (!ObjectHelper.isEmpty(namespaceName)) { + ClientNonNamespaceOperation<KubernetesClient, ResourceQuota, ResourceQuotaList, DoneableResourceQuota, ClientResource<ResourceQuota, DoneableResourceQuota>> resQuota; + resQuota = getEndpoint().getKubernetesClient().resourceQuotas() + .inNamespace(namespaceName); + for (Map.Entry<String, String> entry : labels.entrySet()) { + resQuota.withLabel(entry.getKey(), entry.getValue()); + } + resList = resQuota.list(); + } else { + ClientOperation<KubernetesClient, ResourceQuota, ResourceQuotaList, DoneableResourceQuota, ClientResource<ResourceQuota, DoneableResourceQuota>> resQuota; + resQuota = getEndpoint().getKubernetesClient().resourceQuotas(); + for (Map.Entry<String, String> entry : labels.entrySet()) { + resQuota.withLabel(entry.getKey(), entry.getValue()); + } + resList = resQuota.list(); + } + exchange.getOut().setBody(resList.getItems()); + } + + protected void doGetResourceQuota(Exchange exchange, String operation) + throws Exception { + ResourceQuota rq = null; + String rqName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_RESOURCES_QUOTA_NAME, + String.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (ObjectHelper.isEmpty(rqName)) { + LOG.error("Get a specific Resource Quota require specify a Resource Quota name"); + throw new IllegalArgumentException( + "Get a specific Resource Quota require specify a Resource Quota name"); + } + if (ObjectHelper.isEmpty(namespaceName)) { + LOG.error("Get a specific Resource Quota require specify a namespace name"); + throw new IllegalArgumentException( + "Get a specific Resource Quota require specify a namespace name"); + } + rq = getEndpoint().getKubernetesClient().resourceQuotas() + .inNamespace(namespaceName).withName(rqName).get(); + exchange.getOut().setBody(rq); + } + + protected void doCreateResourceQuota(Exchange exchange, String operation) + throws Exception { + ResourceQuota rq = null; + String rqName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_RESOURCES_QUOTA_NAME, + String.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + ResourceQuotaSpec rqSpec = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_RESOURCE_QUOTA_SPEC, + ResourceQuotaSpec.class); + if (ObjectHelper.isEmpty(rqName)) { + LOG.error("Create a specific resource quota require specify a resource quota name"); + throw new IllegalArgumentException( + "Create a specific resource quota require specify a resource quota name"); + } + if (ObjectHelper.isEmpty(namespaceName)) { + LOG.error("Create a specific resource quota require specify a namespace name"); + throw new IllegalArgumentException( + "Create a specific resource quota require specify a namespace name"); + } + if (ObjectHelper.isEmpty(rqSpec)) { + LOG.error("Create a specific resource quota require specify a resource quota spec bean"); + throw new IllegalArgumentException( + "Create a specific resource quota require specify a resource quota spec bean"); + } + Map<String, String> labels = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_RESOURCES_QUOTA_LABELS, + Map.class); + EditableResourceQuota rqCreating = new ResourceQuotaBuilder() + .withNewMetadata().withName(rqName).withLabels(labels) + .endMetadata().withSpec(rqSpec).build(); + rq = getEndpoint().getKubernetesClient().resourceQuotas() + .inNamespace(namespaceName).create(rqCreating); + exchange.getOut().setBody(rq); + } + + protected void doDeleteResourceQuota(Exchange exchange, String operation) + throws Exception { + String rqName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_RESOURCES_QUOTA_NAME, + String.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (ObjectHelper.isEmpty(rqName)) { + LOG.error("Delete a specific resource quota require specify a resource quota name"); + throw new IllegalArgumentException( + "Delete a specific resource quota require specify a resource quota name"); + } + if (ObjectHelper.isEmpty(namespaceName)) { + LOG.error("Delete a specific resource quota require specify a namespace name"); + throw new IllegalArgumentException( + "Delete a specific resource quota require specify a namespace name"); + } + boolean rqDeleted = getEndpoint().getKubernetesClient() + .resourceQuotas().inNamespace(namespaceName).withName(rqName) + .delete(); + exchange.getOut().setBody(rqDeleted); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesSecretsProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesSecretsProducer.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesSecretsProducer.java new file mode 100644 index 0000000..5c74dda --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesSecretsProducer.java @@ -0,0 +1,190 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.kubernetes.producer; + +import java.util.Map; + +import io.fabric8.kubernetes.api.model.DoneableSecret; +import io.fabric8.kubernetes.api.model.Secret; +import io.fabric8.kubernetes.api.model.SecretList; +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.dsl.ClientNonNamespaceOperation; +import io.fabric8.kubernetes.client.dsl.ClientOperation; +import io.fabric8.kubernetes.client.dsl.ClientResource; + +import org.apache.camel.Exchange; +import org.apache.camel.component.kubernetes.KubernetesConstants; +import org.apache.camel.component.kubernetes.KubernetesEndpoint; +import org.apache.camel.impl.DefaultProducer; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class KubernetesSecretsProducer extends DefaultProducer { + + private static final Logger LOG = LoggerFactory + .getLogger(KubernetesSecretsProducer.class); + + public KubernetesSecretsProducer(KubernetesEndpoint endpoint) { + super(endpoint); + } + + @Override + public KubernetesEndpoint getEndpoint() { + return (KubernetesEndpoint) super.getEndpoint(); + } + + @Override + public void process(Exchange exchange) throws Exception { + String operation; + + if (ObjectHelper.isEmpty(getEndpoint().getKubernetesConfiguration() + .getOperation())) { + operation = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_OPERATION, String.class); + } else { + operation = getEndpoint().getKubernetesConfiguration() + .getOperation(); + } + + switch (operation) { + + case KubernetesOperations.LIST_SECRETS: + doList(exchange, operation); + break; + + case KubernetesOperations.LIST_SECRETS_BY_LABELS_OPERATION: + doListSecretsByLabels(exchange, operation); + break; + + case KubernetesOperations.GET_SECRET_OPERATION: + doGetSecret(exchange, operation); + break; + + case KubernetesOperations.CREATE_SECRET_OPERATION: + doCreateSecret(exchange, operation); + break; + + case KubernetesOperations.DELETE_SECRET_OPERATION: + doDeleteSecret(exchange, operation); + break; + + default: + throw new IllegalArgumentException("Unsupported operation " + + operation); + } + } + + protected void doList(Exchange exchange, String operation) throws Exception { + SecretList secretsList = getEndpoint().getKubernetesClient().secrets() + .list(); + exchange.getOut().setBody(secretsList.getItems()); + } + + protected void doListSecretsByLabels(Exchange exchange, String operation) + throws Exception { + SecretList secretsList = null; + Map<String, String> labels = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_SECRETS_LABELS, Map.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (!ObjectHelper.isEmpty(namespaceName)) { + ClientNonNamespaceOperation<KubernetesClient, Secret, SecretList, DoneableSecret, ClientResource<Secret, DoneableSecret>> secrets; + secrets = getEndpoint().getKubernetesClient().secrets() + .inNamespace(namespaceName); + for (Map.Entry<String, String> entry : labels.entrySet()) { + secrets.withLabel(entry.getKey(), entry.getValue()); + } + secretsList = secrets.list(); + } else { + ClientOperation<KubernetesClient, Secret, SecretList, DoneableSecret, ClientResource<Secret, DoneableSecret>> secrets; + secrets = getEndpoint().getKubernetesClient().secrets(); + for (Map.Entry<String, String> entry : labels.entrySet()) { + secrets.withLabel(entry.getKey(), entry.getValue()); + } + secretsList = secrets.list(); + } + exchange.getOut().setBody(secretsList.getItems()); + } + + protected void doGetSecret(Exchange exchange, String operation) + throws Exception { + Secret secret = null; + String secretName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_SECRET_NAME, String.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (ObjectHelper.isEmpty(secretName)) { + LOG.error("Get a specific Secret require specify a Secret name"); + throw new IllegalArgumentException( + "Get a specific Secret require specify a Secret name"); + } + if (ObjectHelper.isEmpty(namespaceName)) { + LOG.error("Get a specific Secret require specify a namespace name"); + throw new IllegalArgumentException( + "Get a specific Secret require specify a namespace name"); + } + secret = getEndpoint().getKubernetesClient().secrets() + .inNamespace(namespaceName).withName(secretName).get(); + exchange.getOut().setBody(secret); + } + + protected void doCreateSecret(Exchange exchange, String operation) + throws Exception { + Secret secret = null; + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + Secret secretToCreate = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_SECRET, Secret.class); + if (ObjectHelper.isEmpty(namespaceName)) { + LOG.error("Create a specific secret require specify a namespace name"); + throw new IllegalArgumentException( + "Create a specific secret require specify a namespace name"); + } + if (ObjectHelper.isEmpty(secretToCreate)) { + LOG.error("Create a specific secret require specify a secret bean"); + throw new IllegalArgumentException( + "Create a specific secret require specify a secret bean"); + } + Map<String, String> labels = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_SECRETS_LABELS, Map.class); + secret = getEndpoint().getKubernetesClient().secrets() + .inNamespace(namespaceName).create(secretToCreate); + exchange.getOut().setBody(secret); + } + + protected void doDeleteSecret(Exchange exchange, String operation) + throws Exception { + String secretName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_SECRET_NAME, String.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (ObjectHelper.isEmpty(secretName)) { + LOG.error("Delete a specific secret require specify a secret name"); + throw new IllegalArgumentException( + "Delete a specific secret require specify a secret name"); + } + if (ObjectHelper.isEmpty(namespaceName)) { + LOG.error("Delete a specific secret require specify a namespace name"); + throw new IllegalArgumentException( + "Delete a specific secret require specify a namespace name"); + } + boolean secretDeleted = getEndpoint().getKubernetesClient().secrets() + .inNamespace(namespaceName).withName(secretName).delete(); + exchange.getOut().setBody(secretDeleted); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesServiceAccountsProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesServiceAccountsProducer.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesServiceAccountsProducer.java new file mode 100644 index 0000000..9c9a141 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesServiceAccountsProducer.java @@ -0,0 +1,193 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.kubernetes.producer; + +import java.util.Map; + +import io.fabric8.kubernetes.api.model.DoneableSecret; +import io.fabric8.kubernetes.api.model.DoneableServiceAccount; +import io.fabric8.kubernetes.api.model.Secret; +import io.fabric8.kubernetes.api.model.SecretList; +import io.fabric8.kubernetes.api.model.ServiceAccount; +import io.fabric8.kubernetes.api.model.ServiceAccountList; +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.dsl.ClientNonNamespaceOperation; +import io.fabric8.kubernetes.client.dsl.ClientOperation; +import io.fabric8.kubernetes.client.dsl.ClientResource; + +import org.apache.camel.Exchange; +import org.apache.camel.component.kubernetes.KubernetesConstants; +import org.apache.camel.component.kubernetes.KubernetesEndpoint; +import org.apache.camel.impl.DefaultProducer; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class KubernetesServiceAccountsProducer extends DefaultProducer { + + private static final Logger LOG = LoggerFactory + .getLogger(KubernetesServiceAccountsProducer.class); + + public KubernetesServiceAccountsProducer(KubernetesEndpoint endpoint) { + super(endpoint); + } + + @Override + public KubernetesEndpoint getEndpoint() { + return (KubernetesEndpoint) super.getEndpoint(); + } + + @Override + public void process(Exchange exchange) throws Exception { + String operation; + + if (ObjectHelper.isEmpty(getEndpoint().getKubernetesConfiguration() + .getOperation())) { + operation = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_OPERATION, String.class); + } else { + operation = getEndpoint().getKubernetesConfiguration() + .getOperation(); + } + + switch (operation) { + + case KubernetesOperations.LIST_SERVICE_ACCOUNTS: + doList(exchange, operation); + break; + + case KubernetesOperations.LIST_SERVICE_ACCOUNTS_BY_LABELS_OPERATION: + doListServiceAccountsByLabels(exchange, operation); + break; + + case KubernetesOperations.GET_SECRET_OPERATION: + doGetServiceAccount(exchange, operation); + break; + + case KubernetesOperations.CREATE_SERVICE_ACCOUNT_OPERATION: + doCreateServiceAccount(exchange, operation); + break; + + case KubernetesOperations.DELETE_SERVICE_ACCOUNT_OPERATION: + doDeleteServiceAccount(exchange, operation); + break; + + default: + throw new IllegalArgumentException("Unsupported operation " + + operation); + } + } + + protected void doList(Exchange exchange, String operation) throws Exception { + ServiceAccountList saList = getEndpoint().getKubernetesClient().serviceAccounts() + .list(); + exchange.getOut().setBody(saList.getItems()); + } + + 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)) { + ClientNonNamespaceOperation<KubernetesClient, ServiceAccount, ServiceAccountList, DoneableServiceAccount, ClientResource<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 { + ClientOperation<KubernetesClient, ServiceAccount, ServiceAccountList, DoneableServiceAccount, ClientResource<ServiceAccount, DoneableServiceAccount>> serviceAccounts; + serviceAccounts = getEndpoint().getKubernetesClient().serviceAccounts(); + for (Map.Entry<String, String> entry : labels.entrySet()) { + serviceAccounts.withLabel(entry.getKey(), entry.getValue()); + } + saList = serviceAccounts.list(); + } + exchange.getOut().setBody(saList.getItems()); + } + + 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(); + exchange.getOut().setBody(sa); + } + + 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); + exchange.getOut().setBody(sa); + } + + protected void doDeleteServiceAccount(Exchange exchange, String operation) + throws Exception { + 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("Delete a specific Service Account require specify a Service Account name"); + throw new IllegalArgumentException( + "Delete a specific Service Account require specify a Service Account name"); + } + if (ObjectHelper.isEmpty(namespaceName)) { + LOG.error("Delete a specific Service Account require specify a namespace name"); + throw new IllegalArgumentException( + "Delete a specific Service Account require specify a namespace name"); + } + boolean saDeleted = getEndpoint().getKubernetesClient().serviceAccounts() + .inNamespace(namespaceName).withName(saName).delete(); + exchange.getOut().setBody(saDeleted); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesServicesProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesServicesProducer.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesServicesProducer.java new file mode 100644 index 0000000..9c2c427 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesServicesProducer.java @@ -0,0 +1,211 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.kubernetes.producer; + +import java.util.Map; + +import io.fabric8.kubernetes.api.model.DoneableService; +import io.fabric8.kubernetes.api.model.EditableService; +import io.fabric8.kubernetes.api.model.Service; +import io.fabric8.kubernetes.api.model.ServiceBuilder; +import io.fabric8.kubernetes.api.model.ServiceList; +import io.fabric8.kubernetes.api.model.ServiceSpec; +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.dsl.ClientNonNamespaceOperation; +import io.fabric8.kubernetes.client.dsl.ClientOperation; +import io.fabric8.kubernetes.client.dsl.ClientResource; + +import org.apache.camel.Exchange; +import org.apache.camel.component.kubernetes.KubernetesConstants; +import org.apache.camel.component.kubernetes.KubernetesEndpoint; +import org.apache.camel.impl.DefaultProducer; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class KubernetesServicesProducer extends DefaultProducer { + + private static final Logger LOG = LoggerFactory + .getLogger(KubernetesServicesProducer.class); + + public KubernetesServicesProducer(KubernetesEndpoint endpoint) { + super(endpoint); + } + + @Override + public KubernetesEndpoint getEndpoint() { + return (KubernetesEndpoint) super.getEndpoint(); + } + + @Override + public void process(Exchange exchange) throws Exception { + String operation; + + if (ObjectHelper.isEmpty(getEndpoint().getKubernetesConfiguration() + .getOperation())) { + operation = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_OPERATION, String.class); + } else { + operation = getEndpoint().getKubernetesConfiguration() + .getOperation(); + } + + switch (operation) { + + case KubernetesOperations.LIST_SERVICES_OPERATION: + doList(exchange, operation); + break; + + case KubernetesOperations.LIST_SERVICES_BY_LABELS_OPERATION: + doListServiceByLabels(exchange, operation); + break; + + case KubernetesOperations.GET_SERVICE_OPERATION: + doGetService(exchange, operation); + break; + + case KubernetesOperations.CREATE_SERVICE_OPERATION: + doCreateService(exchange, operation); + break; + + case KubernetesOperations.DELETE_SERVICE_OPERATION: + doDeleteService(exchange, operation); + break; + + default: + throw new IllegalArgumentException("Unsupported operation " + + operation); + } + } + + protected void doList(Exchange exchange, String operation) throws Exception { + ServiceList servicesList = null; + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (!ObjectHelper.isEmpty(namespaceName)) { + servicesList = getEndpoint().getKubernetesClient().services() + .inNamespace(namespaceName).list(); + } else { + servicesList = getEndpoint().getKubernetesClient().services() + .list(); + } + exchange.getOut().setBody(servicesList.getItems()); + } + + protected void doListServiceByLabels(Exchange exchange, String operation) + throws Exception { + ServiceList servicesList = null; + Map<String, String> labels = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_SERVICE_LABELS, Map.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (!ObjectHelper.isEmpty(namespaceName)) { + ClientNonNamespaceOperation<KubernetesClient, Service, ServiceList, DoneableService, ClientResource<Service, DoneableService>> services; + services = getEndpoint().getKubernetesClient().services() + .inNamespace(namespaceName); + for (Map.Entry<String, String> entry : labels.entrySet()) { + services.withLabel(entry.getKey(), entry.getValue()); + } + servicesList = services.list(); + } else { + ClientOperation<KubernetesClient, Service, ServiceList, DoneableService, ClientResource<Service, DoneableService>> services; + services = getEndpoint().getKubernetesClient().services(); + for (Map.Entry<String, String> entry : labels.entrySet()) { + services.withLabel(entry.getKey(), entry.getValue()); + } + servicesList = services.list(); + } + exchange.getOut().setBody(servicesList.getItems()); + } + + protected void doGetService(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); + if (ObjectHelper.isEmpty(serviceName)) { + LOG.error("Get a specific service require specify a service name"); + throw new IllegalArgumentException( + "Get a specific service require specify a service name"); + } + if (ObjectHelper.isEmpty(namespaceName)) { + LOG.error("Get a specific service require specify a namespace name"); + throw new IllegalArgumentException( + "Get a specific service require specify a namespace name"); + } + service = getEndpoint().getKubernetesClient().services() + .inNamespace(namespaceName).withName(serviceName).get(); + exchange.getOut().setBody(service); + } + + 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); + EditableService serviceCreating = new ServiceBuilder() + .withNewMetadata().withName(serviceName).withLabels(labels) + .endMetadata().withSpec(serviceSpec).build(); + service = getEndpoint().getKubernetesClient().services() + .inNamespace(namespaceName).create(serviceCreating); + exchange.getOut().setBody(service); + } + + protected void doDeleteService(Exchange exchange, String operation) + throws Exception { + String serviceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_SERVICE_NAME, String.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (ObjectHelper.isEmpty(serviceName)) { + LOG.error("Delete a specific service require specify a service name"); + throw new IllegalArgumentException( + "Delete a specific service require specify a service name"); + } + if (ObjectHelper.isEmpty(namespaceName)) { + LOG.error("Delete a specific service require specify a namespace name"); + throw new IllegalArgumentException( + "Delete a specific service require specify a namespace name"); + } + boolean serviceDeleted = getEndpoint().getKubernetesClient().services() + .inNamespace(namespaceName).withName(serviceName).delete(); + exchange.getOut().setBody(serviceDeleted); + } +}