CAMEL-9085 Create a Camel-Kubernetes/Openshift v3 Component
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a452d0ac Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a452d0ac Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a452d0ac Branch: refs/heads/master Commit: a452d0ac25c34a4cdf06b1cf3491a807c9072c71 Parents: be3cc2f Author: Andrea Cosentino <anco...@gmail.com> Authored: Mon Sep 14 17:38:54 2015 +0200 Committer: Andrea Cosentino <anco...@gmail.com> Committed: Sun Oct 18 09:33:00 2015 +0200 ---------------------------------------------------------------------- components/camel-kubernetes/pom.xml | 81 +++++ .../kubernetes/KubernetesCategory.java | 44 +++ .../kubernetes/KubernetesComponent.java | 41 +++ .../kubernetes/KubernetesConfiguration.java | 303 +++++++++++++++++++ .../kubernetes/KubernetesConstants.java | 57 ++++ .../kubernetes/KubernetesEndpoint.java | 212 +++++++++++++ .../consumer/KubernetesPodsConsumer.java | 132 ++++++++ ...ubernetesReplicationControllersConsumer.java | 129 ++++++++ .../consumer/KubernetesSecretsConsumer.java | 125 ++++++++ .../consumer/KubernetesServicesConsumer.java | 127 ++++++++ .../kubernetes/consumer/common/PodEvent.java | 32 ++ .../common/ReplicationControllerEvent.java | 33 ++ .../kubernetes/consumer/common/SecretEvent.java | 32 ++ .../consumer/common/ServiceEvent.java | 32 ++ .../KubernetesBuildConfigsProducer.java | 147 +++++++++ .../producer/KubernetesBuildsProducer.java | 143 +++++++++ .../producer/KubernetesNamespacesProducer.java | 159 ++++++++++ .../producer/KubernetesNodesProducer.java | 123 ++++++++ .../producer/KubernetesOperations.java | 96 ++++++ ...bernetesPersistentVolumesClaimsProducer.java | 216 +++++++++++++ .../KubernetesPersistentVolumesProducer.java | 139 +++++++++ .../producer/KubernetesPodsProducer.java | 193 ++++++++++++ ...ubernetesReplicationControllersProducer.java | 223 ++++++++++++++ .../KubernetesResourcesQuotaProducer.java | 210 +++++++++++++ .../producer/KubernetesSecretsProducer.java | 190 ++++++++++++ .../KubernetesServiceAccountsProducer.java | 193 ++++++++++++ .../producer/KubernetesServicesProducer.java | 211 +++++++++++++ .../src/main/resources/META-INF/LICENSE.txt | 203 +++++++++++++ .../src/main/resources/META-INF/NOTICE.txt | 11 + .../org/apache/camel/component/kubernetes | 18 ++ .../kubernetes/KubernetesTestSupport.java | 38 +++ .../consumer/KubernetesPodsConsumerTest.java | 158 ++++++++++ ...netesReplicationControllersConsumerTest.java | 165 ++++++++++ .../consumer/KubernetesSecretsConsumerTest.java | 141 +++++++++ .../KubernetesServicesConsumerTest.java | 145 +++++++++ .../KubernetesBuildConfigsProducerTest.java | 86 ++++++ .../producer/KubernetesBuildsProducerTest.java | 89 ++++++ .../KubernetesNamespacesProducerTest.java | 210 +++++++++++++ .../producer/KubernetesNodesProducerTest.java | 89 ++++++ ...etesPersistentVolumesClaimsProducerTest.java | 191 ++++++++++++ ...KubernetesPersistentVolumesProducerTest.java | 86 ++++++ .../producer/KubernetesPodsProducerTest.java | 205 +++++++++++++ ...netesReplicationControllersProducerTest.java | 215 +++++++++++++ .../KubernetesResourcesQuotaProducerTest.java | 142 +++++++++ .../producer/KubernetesSecretsProducerTest.java | 170 +++++++++++ .../KubernetesServiceAccountsProducerTest.java | 161 ++++++++++ .../KubernetesServicesProducerTest.java | 198 ++++++++++++ .../src/test/resources/log4j.properties | 36 +++ 48 files changed, 6380 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/pom.xml b/components/camel-kubernetes/pom.xml new file mode 100644 index 0000000..cc70793 --- /dev/null +++ b/components/camel-kubernetes/pom.xml @@ -0,0 +1,81 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- 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. --> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.camel</groupId> + <artifactId>components</artifactId> + <version>2.17-SNAPSHOT</version> + </parent> + + <artifactId>camel-kubernetes</artifactId> + <packaging>bundle</packaging> + <name>Camel :: Kubernetes</name> + + <properties> + <camel.osgi.export.pkg>org.apache.camel.component.kubernetes.*</camel.osgi.export.pkg> + <camel.osgi.export.service>org.apache.camel.spi.ComponentResolver;component=kubernetes</camel.osgi.export.service> + </properties> + + <dependencies> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-core</artifactId> + </dependency> + <dependency> + <groupId>io.fabric8</groupId> + <artifactId>kubernetes-client</artifactId> + <version>1.3.52</version> + </dependency> + <dependency> + <groupId>io.fabric8</groupId> + <artifactId>openshift-client</artifactId> + <version>1.3.52</version> + </dependency> + <!-- testing --> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test</artifactId> + <scope>test</scope> + </dependency> + <!-- logging --> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>log4j</groupId> + <artifactId>log4j</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <forkCount>1</forkCount> + <reuseForks>false</reuseForks> + <forkedProcessTimeoutInSeconds>300</forkedProcessTimeoutInSeconds> + </configuration> + </plugin> + </plugins> + </build> +</project> http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesCategory.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesCategory.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesCategory.java new file mode 100644 index 0000000..faf4a89 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesCategory.java @@ -0,0 +1,44 @@ +/** + * 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; + +public interface KubernetesCategory { + + String NAMESPACES = "namespaces"; + + String SERVICES = "services"; + + String REPLICATION_CONTROLLERS = "replicationControllers"; + + String PODS = "pods"; + + String PERSISTENT_VOLUMES = "persistentVolumes"; + + String PERSISTENT_VOLUMES_CLAIMS = "persistentVolumesClaims"; + + String SECRETS = "secrets"; + + String RESOURCES_QUOTA = "resourcesQuota"; + + String SERVICE_ACCOUNTS = "serviceAccounts"; + + String NODES = "nodes"; + + String BUILDS = "builds"; + + String BUILD_CONFIGS = "buildConfigs"; +} http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesComponent.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesComponent.java new file mode 100644 index 0000000..028bfaf --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesComponent.java @@ -0,0 +1,41 @@ +/** + * 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; + +import java.util.Map; + +import org.apache.camel.Endpoint; +import org.apache.camel.impl.DefaultComponent; +import org.apache.camel.util.ObjectHelper; + +/** + * Represents the component that manages {@link GitEndpoint}. + */ +public class KubernetesComponent extends DefaultComponent { + + protected Endpoint createEndpoint(String uri, String remaining, + Map<String, Object> parameters) throws Exception { + KubernetesConfiguration config = new KubernetesConfiguration(); + setProperties(config, parameters); + config.setMasterUrl(remaining); + if (ObjectHelper.isEmpty(config.getMasterUrl())) { + throw new IllegalArgumentException("Master URL must be specified"); + } + KubernetesEndpoint endpoint = new KubernetesEndpoint(uri, this, config); + return endpoint; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesConfiguration.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesConfiguration.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesConfiguration.java new file mode 100644 index 0000000..9c5696f --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesConfiguration.java @@ -0,0 +1,303 @@ +/** + * 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; + +import io.fabric8.kubernetes.client.DefaultKubernetesClient; + +import org.apache.camel.spi.Metadata; +import org.apache.camel.spi.UriParam; +import org.apache.camel.spi.UriParams; +import org.apache.camel.spi.UriPath; + +@UriParams +public class KubernetesConfiguration { + + @UriPath + @Metadata(required = "true") + private String masterUrl; + + @UriParam(enums = "namespaces,services,replicationControllers,pods,persistentVolumes,persistentVolumesClaims,secrets,resourcesQuota,serviceAccounts,nodes,builds,buildConfigs") + @Metadata(required = "true") + private String category; + + @UriParam + private DefaultKubernetesClient kubernetesClient; + + @UriParam + private String username; + + @UriParam + private String password; + + @UriParam(label = "producer", enums = "listNamespaces,listNamespacesByLabels,getNamespace,createNamespace,deleteNamespace,listServices,listServicesByLabels,getService,createService," + + "deleteService,listReplicationControllers,listReplicationControllersByLabels,getReplicationController,createReplicationController,deleteReplicationController,listPods," + + "listPodsByLabels,getPod,createPod,deletePod,listPersistentVolumes,listPersistentVolumesByLabels,getPersistentVolume,listPersistentVolumesClaims,listPersistentVolumesClaimsByLabels," + + "getPersistentVolumeClaim,createPersistentVolumeClaim,deletePersistentVolumeClaim,listSecrets,listSecretsByLabels,getSecret,createSecret,deleteSecret,listResourcesQuota," + + "listResourcesQuotaByLabels,getResourceQuota,createResourceQuota,deleteResourceQuota,listServiceAccounts,listServiceAccountsByLabels,getServiceAccount,createServiceAccount," + + "deleteServiceAccount,listNodes,listNodesByLabels,getNode,listBuilds,listBuildsByLabels,getBuild,listBuildConfigs,listBuildConfigsByLabels,getBuildConfig") + private String operation; + + @UriParam + private String apiVersion; + + @UriParam + private String caCertData; + + @UriParam + private String caCertFile; + + @UriParam + private String clientCertData; + + @UriParam + private String clientCertFile; + + @UriParam + private String clientKeyAlgo; + + @UriParam + private String clientKeyData; + + @UriParam + private String clientKeyFile; + + @UriParam + private String clientKeyPassphrase; + + @UriParam + private String oauthToken; + + @UriParam + private Boolean trustCerts; + + @UriParam(label = "consumer") + private String namespaceName; + + /** + * Kubernetes Master url + */ + public String getMasterUrl() { + return masterUrl; + } + + public void setMasterUrl(String masterUrl) { + this.masterUrl = masterUrl; + } + + /** + * Kubernetes Producer and Consumer category + */ + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } + + /** + * Default KubernetesClient to use if provided + */ + public DefaultKubernetesClient getKubernetesClient() { + return kubernetesClient; + } + + public void setKubernetesClient(DefaultKubernetesClient kubernetesClient) { + this.kubernetesClient = kubernetesClient; + } + + /** + * Username to connect to Kubernetes + */ + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + /** + * Password to connect to Kubernetes + */ + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + /** + * Producer operation to do on Kubernetes + */ + public String getOperation() { + return operation; + } + + public void setOperation(String operation) { + this.operation = operation; + } + + /** + * The Kubernetes API Version to use + */ + public String getApiVersion() { + return apiVersion; + } + + public void setApiVersion(String apiVersion) { + this.apiVersion = apiVersion; + } + + /** + * The CA Cert Data + */ + public String getCaCertData() { + return caCertData; + } + + public void setCaCertData(String caCertData) { + this.caCertData = caCertData; + } + + /** + * The CA Cert File + */ + public String getCaCertFile() { + return caCertFile; + } + + public void setCaCertFile(String caCertFile) { + this.caCertFile = caCertFile; + } + + /** + * The Client Cert Data + */ + public String getClientCertData() { + return clientCertData; + } + + public void setClientCertData(String clientCertData) { + this.clientCertData = clientCertData; + } + + /** + * The Client Cert File + */ + public String getClientCertFile() { + return clientCertFile; + } + + public void setClientCertFile(String clientCertFile) { + this.clientCertFile = clientCertFile; + } + + /** + * The Key Algorithm used by the client + */ + public String getClientKeyAlgo() { + return clientKeyAlgo; + } + + public void setClientKeyAlgo(String clientKeyAlgo) { + this.clientKeyAlgo = clientKeyAlgo; + } + + /** + * The Client Key data + */ + public String getClientKeyData() { + return clientKeyData; + } + + public void setClientKeyData(String clientKeyData) { + this.clientKeyData = clientKeyData; + } + + /** + * The Client Key file + */ + public String getClientKeyFile() { + return clientKeyFile; + } + + public void setClientKeyFile(String clientKeyFile) { + this.clientKeyFile = clientKeyFile; + } + + /** + * The Client Key Passphrase + */ + public String getClientKeyPassphrase() { + return clientKeyPassphrase; + } + + public void setClientKeyPassphrase(String clientKeyPassphrase) { + this.clientKeyPassphrase = clientKeyPassphrase; + } + + /** + * The Auth Token + */ + public String getOauthToken() { + return oauthToken; + } + + public void setOauthToken(String oauthToken) { + this.oauthToken = oauthToken; + } + + /** + * Define if the certs we used are trusted anyway or not + */ + public Boolean getTrustCerts() { + return trustCerts; + } + + public void setTrustCerts(Boolean trustCerts) { + this.trustCerts = trustCerts; + } + + /** + * The namespace name + */ + public String getNamespaceName() { + return namespaceName; + } + + public void setNamespaceName(String namespaceName) { + this.namespaceName = namespaceName; + } + + @Override + public String toString() { + return "KubernetesConfiguration [masterUrl=" + masterUrl + + ", category=" + category + ", kubernetesClient=" + + kubernetesClient + ", username=" + username + ", password=" + + password + ", operation=" + operation + ", apiVersion=" + + apiVersion + ", caCertData=" + caCertData + ", caCertFile=" + + caCertFile + ", clientCertData=" + clientCertData + + ", clientCertFile=" + clientCertFile + ", clientKeyAlgo=" + + clientKeyAlgo + ", clientKeyData=" + clientKeyData + + ", clientKeyFile=" + clientKeyFile + ", clientKeyPassphrase=" + + clientKeyPassphrase + ", oauthToken=" + oauthToken + + ", trustCerts=" + trustCerts + ", namespaceName=" + + namespaceName + "]"; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesConstants.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesConstants.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesConstants.java new file mode 100644 index 0000000..6fcad8e --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesConstants.java @@ -0,0 +1,57 @@ +/** + * 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; + +public interface KubernetesConstants { + // Producer + String KUBERNETES_OPERATION = "CamelKubernetesOperation"; + String KUBERNETES_NAMESPACE_NAME = "CamelKubernetesNamespaceName"; + String KUBERNETES_NAMESPACE_LABELS = "CamelKubernetesNamespaceLabels"; + String KUBERNETES_SERVICE_LABELS = "CamelKubernetesServiceLabels"; + String KUBERNETES_SERVICE_NAME = "CamelKubernetesServiceName"; + String KUBERNETES_SERVICE_SPEC = "CamelKubernetesServiceSpec"; + String KUBERNETES_REPLICATION_CONTROLLERS_LABELS = "CamelKubernetesReplicationControllersLabels"; + String KUBERNETES_REPLICATION_CONTROLLER_NAME = "CamelKubernetesReplicationControllerName"; + String KUBERNETES_REPLICATION_CONTROLLER_SPEC = "CamelKubernetesReplicationControllerSpec"; + String KUBERNETES_PODS_LABELS = "CamelKubernetesPodsLabels"; + String KUBERNETES_POD_NAME = "CamelKubernetesPodName"; + String KUBERNETES_POD_SPEC = "CamelKubernetesPodSpec"; + String KUBERNETES_PERSISTENT_VOLUMES_LABELS = "CamelKubernetesPersistentVolumesLabels"; + String KUBERNETES_PERSISTENT_VOLUME_NAME = "CamelKubernetesPersistentVolumeName"; + String KUBERNETES_PERSISTENT_VOLUMES_CLAIMS_LABELS = "CamelKubernetesPersistentVolumesClaimsLabels"; + String KUBERNETES_PERSISTENT_VOLUME_CLAIM_NAME = "CamelKubernetesPersistentVolumeClaimName"; + String KUBERNETES_PERSISTENT_VOLUME_CLAIM_SPEC = "CamelKubernetesPersistentVolumeClaimSpec"; + String KUBERNETES_SECRETS_LABELS = "CamelKubernetesSecretsLabels"; + String KUBERNETES_SECRET_NAME = "CamelKubernetesSecretName"; + String KUBERNETES_SECRET = "CamelKubernetesSecret"; + String KUBERNETES_RESOURCES_QUOTA_LABELS = "CamelKubernetesResourcesQuotaLabels"; + String KUBERNETES_RESOURCES_QUOTA_NAME = "CamelKubernetesResourcesQuotaName"; + String KUBERNETES_RESOURCE_QUOTA_SPEC = "CamelKubernetesResourceQuotaSpec"; + String KUBERNETES_SERVICE_ACCOUNTS_LABELS = "CamelKubernetesServiceAccountsLabels"; + String KUBERNETES_SERVICE_ACCOUNT_NAME = "CamelKubernetesServiceAccountName"; + String KUBERNETES_SERVICE_ACCOUNT = "CamelKubernetesServiceAccount"; + String KUBERNETES_NODES_LABELS = "CamelKubernetesNodesLabels"; + String KUBERNETES_NODE_NAME = "CamelKubernetesNodeName"; + String KUBERNETES_BUILDS_LABELS = "CamelKubernetesBuildsLabels"; + String KUBERNETES_BUILD_NAME = "CamelKubernetesBuildName"; + String KUBERNETES_BUILD_CONFIGS_LABELS = "CamelKubernetesBuildConfigsLabels"; + String KUBERNETES_BUILD_CONFIG_NAME = "CamelKubernetesBuildConfigName"; + + // Consumer + String KUBERNETES_EVENT_ACTION = "CamelKubernetesEventAction"; + String KUBERNETES_EVENT_TIMESTAMP = "CamelKubernetesEventTimestamp"; +} http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesEndpoint.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesEndpoint.java new file mode 100644 index 0000000..4833a37 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesEndpoint.java @@ -0,0 +1,212 @@ +/** + * 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; + +import io.fabric8.kubernetes.client.Config; +import io.fabric8.kubernetes.client.ConfigBuilder; +import io.fabric8.kubernetes.client.DefaultKubernetesClient; + +import org.apache.camel.Consumer; +import org.apache.camel.Processor; +import org.apache.camel.Producer; +import org.apache.camel.component.kubernetes.consumer.KubernetesPodsConsumer; +import org.apache.camel.component.kubernetes.consumer.KubernetesReplicationControllersConsumer; +import org.apache.camel.component.kubernetes.consumer.KubernetesSecretsConsumer; +import org.apache.camel.component.kubernetes.consumer.KubernetesServicesConsumer; +import org.apache.camel.component.kubernetes.producer.KubernetesNamespacesProducer; +import org.apache.camel.component.kubernetes.producer.KubernetesPersistentVolumesClaimsProducer; +import org.apache.camel.component.kubernetes.producer.KubernetesPersistentVolumesProducer; +import org.apache.camel.component.kubernetes.producer.KubernetesPodsProducer; +import org.apache.camel.component.kubernetes.KubernetesCategory; +import org.apache.camel.component.kubernetes.producer.KubernetesBuildConfigsProducer; +import org.apache.camel.component.kubernetes.producer.KubernetesBuildsProducer; +import org.apache.camel.component.kubernetes.producer.KubernetesNodesProducer; +import org.apache.camel.component.kubernetes.producer.KubernetesReplicationControllersProducer; +import org.apache.camel.component.kubernetes.producer.KubernetesResourcesQuotaProducer; +import org.apache.camel.component.kubernetes.producer.KubernetesSecretsProducer; +import org.apache.camel.component.kubernetes.producer.KubernetesServiceAccountsProducer; +import org.apache.camel.component.kubernetes.producer.KubernetesServicesProducer; +import org.apache.camel.impl.DefaultEndpoint; +import org.apache.camel.spi.UriEndpoint; +import org.apache.camel.spi.UriParam; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@UriEndpoint(scheme = "kubernetes", title = "Kubernetes", syntax = "kubernetes:master", label = "cloud,paas") +public class KubernetesEndpoint extends DefaultEndpoint { + + private static final Logger LOG = LoggerFactory + .getLogger(KubernetesEndpoint.class); + + @UriParam + private KubernetesConfiguration configuration; + + private DefaultKubernetesClient client; + + public KubernetesEndpoint(String uri, KubernetesComponent component, + KubernetesConfiguration config) { + super(uri, component); + this.configuration = config; + } + + @Override + public Producer createProducer() throws Exception { + if (ObjectHelper.isEmpty(configuration.getCategory())) { + throw new IllegalArgumentException( + "A producer category must be specified"); + } else { + String category = configuration.getCategory(); + + switch (category) { + + case KubernetesCategory.NAMESPACES: + return new KubernetesNamespacesProducer(this); + + case KubernetesCategory.SERVICES: + return new KubernetesServicesProducer(this); + + case KubernetesCategory.REPLICATION_CONTROLLERS: + return new KubernetesReplicationControllersProducer(this); + + case KubernetesCategory.PODS: + return new KubernetesPodsProducer(this); + + case KubernetesCategory.PERSISTENT_VOLUMES: + return new KubernetesPersistentVolumesProducer(this); + + case KubernetesCategory.PERSISTENT_VOLUMES_CLAIMS: + return new KubernetesPersistentVolumesClaimsProducer(this); + + case KubernetesCategory.SECRETS: + return new KubernetesSecretsProducer(this); + + case KubernetesCategory.RESOURCES_QUOTA: + return new KubernetesResourcesQuotaProducer(this); + + case KubernetesCategory.SERVICE_ACCOUNTS: + return new KubernetesServiceAccountsProducer(this); + + case KubernetesCategory.NODES: + return new KubernetesNodesProducer(this); + + case KubernetesCategory.BUILDS: + return new KubernetesBuildsProducer(this); + + case KubernetesCategory.BUILD_CONFIGS: + return new KubernetesBuildConfigsProducer(this); + + default: + throw new IllegalArgumentException("The " + category + + " producer category doesn't exist"); + } + } + } + + @Override + public Consumer createConsumer(Processor processor) throws Exception { + if (ObjectHelper.isEmpty(configuration.getCategory())) { + throw new IllegalArgumentException( + "A consumer category must be specified"); + } else { + String category = configuration.getCategory(); + + switch (category) { + + case KubernetesCategory.PODS: + return new KubernetesPodsConsumer(this, processor); + + case KubernetesCategory.SERVICES: + return new KubernetesServicesConsumer(this, processor); + + case KubernetesCategory.REPLICATION_CONTROLLERS: + return new KubernetesReplicationControllersConsumer(this, processor); + + case KubernetesCategory.SECRETS: + return new KubernetesSecretsConsumer(this, processor); + + default: + throw new IllegalArgumentException("The " + category + + " consumer category doesn't exist"); + } + } + } + + @Override + public boolean isSingleton() { + return false; + } + + @Override + protected void doStart() throws Exception { + super.doStart(); + + client = configuration.getKubernetesClient() != null ? configuration + .getKubernetesClient() : createKubernetesClient(); + } + + @Override + protected void doStop() throws Exception { + super.doStop(); + client.close(); + } + + public DefaultKubernetesClient getKubernetesClient() { + return client; + } + + + /** + * The kubernetes Configuration + */ + public KubernetesConfiguration getKubernetesConfiguration() { + return configuration; + } + + private DefaultKubernetesClient createKubernetesClient() { + LOG.debug("Create Kubernetes client with the following Configuration: " + configuration.toString()); + + DefaultKubernetesClient kubeClient = new DefaultKubernetesClient(); + ConfigBuilder builder = new ConfigBuilder(); + builder.withMasterUrl(configuration.getMasterUrl()); + if ((ObjectHelper.isNotEmpty(configuration.getUsername()) && ObjectHelper + .isNotEmpty(configuration.getPassword())) + && ObjectHelper.isEmpty(configuration.getOauthToken())) { + builder.withUsername(configuration.getUsername()); + builder.withPassword(configuration.getPassword()); + } else { + builder.withOauthToken(configuration.getOauthToken()); + } + if (ObjectHelper.isNotEmpty(configuration.getCaCertData())) { + builder.withCaCertData(configuration.getCaCertData()); + } + if (ObjectHelper.isNotEmpty(configuration.getCaCertFile())) { + builder.withCaCertFile(configuration.getCaCertFile()); + } + if (ObjectHelper.isNotEmpty(configuration.getClientCertData())) { + builder.withClientCertData(configuration.getClientCertData()); + } + if (ObjectHelper.isNotEmpty(configuration.getClientCertFile())) { + builder.withClientCertFile(configuration.getClientCertFile()); + } + + Config conf = builder.build(); + + kubeClient = new DefaultKubernetesClient(conf); + return kubeClient; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesPodsConsumer.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesPodsConsumer.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesPodsConsumer.java new file mode 100644 index 0000000..9524f71 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesPodsConsumer.java @@ -0,0 +1,132 @@ +/** + * 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.consumer; + +import io.fabric8.kubernetes.api.model.Pod; +import io.fabric8.kubernetes.client.KubernetesClientException; +import io.fabric8.kubernetes.client.Watcher; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.component.kubernetes.KubernetesConstants; +import org.apache.camel.component.kubernetes.KubernetesEndpoint; +import org.apache.camel.component.kubernetes.consumer.common.PodEvent; +import org.apache.camel.impl.ScheduledPollConsumer; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class KubernetesPodsConsumer extends ScheduledPollConsumer { + + private static final Logger LOG = LoggerFactory + .getLogger(KubernetesPodsConsumer.class); + + private ConcurrentMap<Long, PodEvent> map; + + public KubernetesPodsConsumer(KubernetesEndpoint endpoint, + Processor processor) { + super(endpoint, processor); + } + + @Override + public KubernetesEndpoint getEndpoint() { + return (KubernetesEndpoint) super.getEndpoint(); + } + + @Override + protected void doStart() throws Exception { + super.doStart(); + map = new ConcurrentHashMap<Long, PodEvent>(); + + if (ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration() + .getOauthToken())) { + if (ObjectHelper.isNotEmpty(getEndpoint() + .getKubernetesConfiguration().getNamespaceName())) { + getEndpoint() + .getKubernetesClient() + .pods() + .inNamespace( + getEndpoint().getKubernetesConfiguration() + .getNamespaceName()) + .watch(new Watcher<Pod>() { + + @Override + public void eventReceived( + io.fabric8.kubernetes.client.Watcher.Action action, + Pod resource) { + PodEvent pe = new PodEvent(action, resource); + map.put(System.currentTimeMillis(), pe); + } + + @Override + public void onClose(KubernetesClientException cause) { + if (cause != null) { + LOG.error(cause.getMessage(), cause); + } + + } + }); + } else { + getEndpoint().getKubernetesClient().pods() + .watch(new Watcher<Pod>() { + + @Override + public void eventReceived( + io.fabric8.kubernetes.client.Watcher.Action action, + Pod resource) { + PodEvent pe = new PodEvent(action, resource); + map.put(System.currentTimeMillis(), pe); + } + + @Override + public void onClose(KubernetesClientException cause) { + if (cause != null) { + LOG.error(cause.getMessage(), cause); + } + } + }); + } + } + } + + @Override + protected void doStop() throws Exception { + super.doStop(); + map.clear(); + } + + @Override + protected int poll() throws Exception { + int mapSize = map.size(); + for (ConcurrentMap.Entry<Long, PodEvent> entry : map.entrySet()) { + PodEvent podEvent = (PodEvent) entry.getValue(); + Exchange e = getEndpoint().createExchange(); + e.getIn().setBody(podEvent.getPod()); + e.getIn().setHeader(KubernetesConstants.KUBERNETES_EVENT_ACTION, + podEvent.getAction()); + e.getIn().setHeader(KubernetesConstants.KUBERNETES_EVENT_TIMESTAMP, + entry.getKey()); + getProcessor().process(e); + map.remove(entry.getKey()); + } + return mapSize; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesReplicationControllersConsumer.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesReplicationControllersConsumer.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesReplicationControllersConsumer.java new file mode 100644 index 0000000..574a8f7 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesReplicationControllersConsumer.java @@ -0,0 +1,129 @@ +/** + * 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.consumer; + +import io.fabric8.kubernetes.api.model.ReplicationController; +import io.fabric8.kubernetes.api.model.Service; +import io.fabric8.kubernetes.client.KubernetesClientException; +import io.fabric8.kubernetes.client.Watcher; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.component.kubernetes.KubernetesConstants; +import org.apache.camel.component.kubernetes.KubernetesEndpoint; +import org.apache.camel.component.kubernetes.consumer.common.ReplicationControllerEvent; +import org.apache.camel.component.kubernetes.consumer.common.ServiceEvent; +import org.apache.camel.impl.ScheduledPollConsumer; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class KubernetesReplicationControllersConsumer extends ScheduledPollConsumer { + + private static final Logger LOG = LoggerFactory + .getLogger(KubernetesReplicationControllersConsumer.class); + + private ConcurrentMap<Long, ReplicationControllerEvent> map; + + public KubernetesReplicationControllersConsumer(KubernetesEndpoint endpoint, + Processor processor) { + super(endpoint, processor); + } + + @Override + public KubernetesEndpoint getEndpoint() { + return (KubernetesEndpoint) super.getEndpoint(); + } + + @Override + protected void doStart() throws Exception { + super.doStart(); + map = new ConcurrentHashMap<Long, ReplicationControllerEvent>(); + + if (ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getOauthToken())) { + if (ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getNamespaceName())) { + getEndpoint().getKubernetesClient().replicationControllers().inNamespace(getEndpoint().getKubernetesConfiguration().getNamespaceName()) + .watch(new Watcher<ReplicationController>() { + + @Override + public void eventReceived( + io.fabric8.kubernetes.client.Watcher.Action action, + ReplicationController resource) { + ReplicationControllerEvent rce = new ReplicationControllerEvent(action, resource); + map.put(System.currentTimeMillis(), rce); + + } + + @Override + public void onClose(KubernetesClientException cause) { + if (cause != null) { + LOG.error(cause.getMessage(), cause); + } + } + + + }); + } else { + getEndpoint().getKubernetesClient().replicationControllers() + .watch(new Watcher<ReplicationController>() { + + + @Override + public void eventReceived( + io.fabric8.kubernetes.client.Watcher.Action action, + ReplicationController resource) { + ReplicationControllerEvent se = new ReplicationControllerEvent(action, resource); + map.put(System.currentTimeMillis(), se); + + } + + @Override + public void onClose(KubernetesClientException cause) { + if (cause != null) { + LOG.error(cause.getMessage(), cause); + } + } + }); + } + } + } + + @Override + protected void doStop() throws Exception { + super.doStop(); + map.clear(); + } + + @Override + protected int poll() throws Exception { + int mapSize = map.size(); + for (ConcurrentMap.Entry<Long, ReplicationControllerEvent> entry : map.entrySet()) { + ReplicationControllerEvent serviceEvent = (ReplicationControllerEvent) entry.getValue(); + Exchange e = getEndpoint().createExchange(); + e.getIn().setBody(serviceEvent.getReplicationController()); + e.getIn().setHeader(KubernetesConstants.KUBERNETES_EVENT_ACTION, serviceEvent.getAction()); + e.getIn().setHeader(KubernetesConstants.KUBERNETES_EVENT_TIMESTAMP, entry.getKey()); + getProcessor().process(e); + map.remove(entry.getKey()); + } + return mapSize; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesSecretsConsumer.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesSecretsConsumer.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesSecretsConsumer.java new file mode 100644 index 0000000..4d68a35 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesSecretsConsumer.java @@ -0,0 +1,125 @@ +/** + * 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.consumer; + +import io.fabric8.kubernetes.api.model.Pod; +import io.fabric8.kubernetes.api.model.Secret; +import io.fabric8.kubernetes.client.KubernetesClientException; +import io.fabric8.kubernetes.client.Watcher; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.component.kubernetes.KubernetesConstants; +import org.apache.camel.component.kubernetes.KubernetesEndpoint; +import org.apache.camel.component.kubernetes.consumer.common.PodEvent; +import org.apache.camel.component.kubernetes.consumer.common.SecretEvent; +import org.apache.camel.impl.ScheduledPollConsumer; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class KubernetesSecretsConsumer extends ScheduledPollConsumer { + + private static final Logger LOG = LoggerFactory + .getLogger(KubernetesSecretsConsumer.class); + + private ConcurrentMap<Long, SecretEvent> map; + + public KubernetesSecretsConsumer(KubernetesEndpoint endpoint, + Processor processor) { + super(endpoint, processor); + } + + @Override + public KubernetesEndpoint getEndpoint() { + return (KubernetesEndpoint) super.getEndpoint(); + } + + @Override + protected void doStart() throws Exception { + super.doStart(); + map = new ConcurrentHashMap<Long, SecretEvent>(); + + if (ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getOauthToken())) { + if (ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getNamespaceName())) { + getEndpoint().getKubernetesClient().secrets().inNamespace(getEndpoint().getKubernetesConfiguration().getNamespaceName()) + .watch(new Watcher<Secret>() { + + @Override + public void eventReceived( + io.fabric8.kubernetes.client.Watcher.Action action, + Secret resource) { + SecretEvent se = new SecretEvent(action, resource); + map.put(System.currentTimeMillis(), se); + } + + @Override + public void onClose(KubernetesClientException cause) { + if (cause != null) { + LOG.error(cause.getMessage(), cause); + } + + } + }); + } else { + getEndpoint().getKubernetesClient().secrets() + .watch(new Watcher<Secret>() { + + @Override + public void eventReceived( + io.fabric8.kubernetes.client.Watcher.Action action, + Secret resource) { + SecretEvent se = new SecretEvent(action, resource); + map.put(System.currentTimeMillis(), se); + } + + @Override + public void onClose(KubernetesClientException cause) { + if (cause != null) { + LOG.error(cause.getMessage(), cause); + } + } + }); + } + } + } + + @Override + protected void doStop() throws Exception { + super.doStop(); + map.clear(); + } + + @Override + protected int poll() throws Exception { + int mapSize = map.size(); + for (ConcurrentMap.Entry<Long, SecretEvent> entry : map.entrySet()) { + SecretEvent podEvent = (SecretEvent) entry.getValue(); + Exchange e = getEndpoint().createExchange(); + e.getIn().setBody(podEvent.getSecret()); + e.getIn().setHeader(KubernetesConstants.KUBERNETES_EVENT_ACTION, podEvent.getAction()); + e.getIn().setHeader(KubernetesConstants.KUBERNETES_EVENT_TIMESTAMP, entry.getKey()); + getProcessor().process(e); + map.remove(entry.getKey()); + } + return mapSize; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesServicesConsumer.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesServicesConsumer.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesServicesConsumer.java new file mode 100644 index 0000000..de25388 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesServicesConsumer.java @@ -0,0 +1,127 @@ +/** + * 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.consumer; + +import io.fabric8.kubernetes.api.model.Service; +import io.fabric8.kubernetes.client.KubernetesClientException; +import io.fabric8.kubernetes.client.Watcher; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.component.kubernetes.KubernetesConstants; +import org.apache.camel.component.kubernetes.KubernetesEndpoint; +import org.apache.camel.component.kubernetes.consumer.common.ServiceEvent; +import org.apache.camel.impl.ScheduledPollConsumer; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class KubernetesServicesConsumer extends ScheduledPollConsumer { + + private static final Logger LOG = LoggerFactory + .getLogger(KubernetesServicesConsumer.class); + + private ConcurrentMap<Long, ServiceEvent> map; + + public KubernetesServicesConsumer(KubernetesEndpoint endpoint, + Processor processor) { + super(endpoint, processor); + } + + @Override + public KubernetesEndpoint getEndpoint() { + return (KubernetesEndpoint) super.getEndpoint(); + } + + @Override + protected void doStart() throws Exception { + super.doStart(); + map = new ConcurrentHashMap<Long, ServiceEvent>(); + + if (ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getOauthToken())) { + if (ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getNamespaceName())) { + getEndpoint().getKubernetesClient().services().inNamespace(getEndpoint().getKubernetesConfiguration().getNamespaceName()) + .watch(new Watcher<Service>() { + + @Override + public void eventReceived( + io.fabric8.kubernetes.client.Watcher.Action action, + Service resource) { + ServiceEvent se = new ServiceEvent(action, resource); + map.put(System.currentTimeMillis(), se); + + } + + @Override + public void onClose(KubernetesClientException cause) { + if (cause != null) { + LOG.error(cause.getMessage(), cause); + } + } + + + }); + } else { + getEndpoint().getKubernetesClient().services() + .watch(new Watcher<Service>() { + + + @Override + public void eventReceived( + io.fabric8.kubernetes.client.Watcher.Action action, + Service resource) { + ServiceEvent se = new ServiceEvent(action, resource); + map.put(System.currentTimeMillis(), se); + + } + + @Override + public void onClose(KubernetesClientException cause) { + if (cause != null) { + LOG.error(cause.getMessage(), cause); + } + } + }); + } + } + } + + @Override + protected void doStop() throws Exception { + super.doStop(); + map.clear(); + } + + @Override + protected int poll() throws Exception { + int mapSize = map.size(); + for (ConcurrentMap.Entry<Long, ServiceEvent> entry : map.entrySet()) { + ServiceEvent serviceEvent = (ServiceEvent) entry.getValue(); + Exchange e = getEndpoint().createExchange(); + e.getIn().setBody(serviceEvent.getService()); + e.getIn().setHeader(KubernetesConstants.KUBERNETES_EVENT_ACTION, serviceEvent.getAction()); + e.getIn().setHeader(KubernetesConstants.KUBERNETES_EVENT_TIMESTAMP, entry.getKey()); + getProcessor().process(e); + map.remove(entry.getKey()); + } + return mapSize; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/common/PodEvent.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/common/PodEvent.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/common/PodEvent.java new file mode 100644 index 0000000..7b87b48 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/common/PodEvent.java @@ -0,0 +1,32 @@ +package org.apache.camel.component.kubernetes.consumer.common; + +import io.fabric8.kubernetes.api.model.Pod; +import io.fabric8.kubernetes.client.Watcher.Action; + +public class PodEvent { + private io.fabric8.kubernetes.client.Watcher.Action action; + + private Pod pod; + + public PodEvent(Action action, Pod pod) { + super(); + this.action = action; + this.pod = pod; + } + + public io.fabric8.kubernetes.client.Watcher.Action getAction() { + return action; + } + + public void setAction(io.fabric8.kubernetes.client.Watcher.Action action) { + this.action = action; + } + + public Pod getPod() { + return pod; + } + + public void setPod(Pod pod) { + this.pod = pod; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/common/ReplicationControllerEvent.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/common/ReplicationControllerEvent.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/common/ReplicationControllerEvent.java new file mode 100644 index 0000000..dda946d --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/common/ReplicationControllerEvent.java @@ -0,0 +1,33 @@ +package org.apache.camel.component.kubernetes.consumer.common; + +import io.fabric8.kubernetes.api.model.ReplicationController; +import io.fabric8.kubernetes.api.model.Service; +import io.fabric8.kubernetes.client.Watcher.Action; + +public class ReplicationControllerEvent { + private io.fabric8.kubernetes.client.Watcher.Action action; + + private ReplicationController replicationController; + + public ReplicationControllerEvent(Action action, ReplicationController rc) { + super(); + this.action = action; + this.replicationController = rc; + } + + public io.fabric8.kubernetes.client.Watcher.Action getAction() { + return action; + } + + public void setAction(io.fabric8.kubernetes.client.Watcher.Action action) { + this.action = action; + } + + public ReplicationController getReplicationController() { + return replicationController; + } + + public void setReplicationController(ReplicationController replicationController) { + this.replicationController = replicationController; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/common/SecretEvent.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/common/SecretEvent.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/common/SecretEvent.java new file mode 100644 index 0000000..048a478 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/common/SecretEvent.java @@ -0,0 +1,32 @@ +package org.apache.camel.component.kubernetes.consumer.common; + +import io.fabric8.kubernetes.api.model.Secret; +import io.fabric8.kubernetes.client.Watcher.Action; + +public class SecretEvent { + private io.fabric8.kubernetes.client.Watcher.Action action; + + private Secret secret; + + public SecretEvent(Action action, Secret secret) { + super(); + this.action = action; + this.secret = secret; + } + + public io.fabric8.kubernetes.client.Watcher.Action getAction() { + return action; + } + + public void setAction(io.fabric8.kubernetes.client.Watcher.Action action) { + this.action = action; + } + + public Secret getSecret() { + return secret; + } + + public void setSecret(Secret secret) { + this.secret = secret; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/common/ServiceEvent.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/common/ServiceEvent.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/common/ServiceEvent.java new file mode 100644 index 0000000..199ee44 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/common/ServiceEvent.java @@ -0,0 +1,32 @@ +package org.apache.camel.component.kubernetes.consumer.common; + +import io.fabric8.kubernetes.api.model.Service; +import io.fabric8.kubernetes.client.Watcher.Action; + +public class ServiceEvent { + private io.fabric8.kubernetes.client.Watcher.Action action; + + private Service service; + + public ServiceEvent(Action action, Service service) { + super(); + this.action = action; + this.service = service; + } + + public io.fabric8.kubernetes.client.Watcher.Action getAction() { + return action; + } + + public void setAction(io.fabric8.kubernetes.client.Watcher.Action action) { + this.action = action; + } + + public Service getService() { + return service; + } + + public void setService(Service service) { + this.service = service; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesBuildConfigsProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesBuildConfigsProducer.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesBuildConfigsProducer.java new file mode 100644 index 0000000..9eb23dc --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesBuildConfigsProducer.java @@ -0,0 +1,147 @@ +/** + * 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 io.fabric8.openshift.api.model.Build; +import io.fabric8.openshift.api.model.BuildConfig; +import io.fabric8.openshift.api.model.BuildConfigList; +import io.fabric8.openshift.api.model.BuildList; +import io.fabric8.openshift.api.model.DoneableBuild; +import io.fabric8.openshift.api.model.DoneableBuildConfig; +import io.fabric8.openshift.client.OpenShiftClient; +import io.fabric8.openshift.client.dsl.ClientBuildConfigResource; + +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 KubernetesBuildConfigsProducer extends DefaultProducer { + + private static final Logger LOG = LoggerFactory + .getLogger(KubernetesBuildConfigsProducer.class); + + public KubernetesBuildConfigsProducer(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_BUILD_CONFIGS: + doList(exchange, operation); + break; + + case KubernetesOperations.LIST_BUILD_CONFIGS_BY_LABELS_OPERATION: + doListBuildConfigsByLabels(exchange, operation); + break; + + case KubernetesOperations.GET_BUILD_CONFIG_OPERATION: + doGetBuildConfig(exchange, operation); + break; + + default: + throw new IllegalArgumentException("Unsupported operation " + + operation); + } + } + + protected void doList(Exchange exchange, String operation) throws Exception { + BuildConfigList buildConfigsList = getEndpoint() + .getKubernetesClient().adapt(OpenShiftClient.class).buildConfigs().list(); + exchange.getOut().setBody(buildConfigsList.getItems()); + } + + protected void doListBuildConfigsByLabels(Exchange exchange, + String operation) throws Exception { + BuildConfigList buildConfigsList = null; + Map<String, String> labels = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_BUILD_CONFIGS_LABELS, + Map.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (!ObjectHelper.isEmpty(namespaceName)) { + ClientNonNamespaceOperation<OpenShiftClient, BuildConfig, BuildConfigList, DoneableBuildConfig, ClientBuildConfigResource<BuildConfig, DoneableBuildConfig, Void, Void>> buildConfigs; + buildConfigs = getEndpoint().getKubernetesClient().adapt(OpenShiftClient.class).buildConfigs() + .inNamespace(namespaceName); + for (Map.Entry<String, String> entry : labels.entrySet()) { + buildConfigs.withLabel(entry.getKey(), entry.getValue()); + } + buildConfigsList = buildConfigs.list(); + } else { + ClientOperation<OpenShiftClient, BuildConfig, BuildConfigList, DoneableBuildConfig, ClientBuildConfigResource<BuildConfig, DoneableBuildConfig, Void, Void>> buildConfigs; + buildConfigs = getEndpoint().getKubernetesClient().adapt(OpenShiftClient.class).buildConfigs(); + for (Map.Entry<String, String> entry : labels.entrySet()) { + buildConfigs.withLabel(entry.getKey(), entry.getValue()); + } + buildConfigsList = buildConfigs.list(); + } + exchange.getOut().setBody(buildConfigsList.getItems()); + } + + protected void doGetBuildConfig(Exchange exchange, String operation) + throws Exception { + BuildConfig buildConfig = null; + String buildConfigName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_BUILD_CONFIG_NAME, + String.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (ObjectHelper.isEmpty(buildConfigName)) { + LOG.error("Get a specific Build Config require specify a Build Config name"); + throw new IllegalArgumentException( + "Get a specific Build Config require specify a Build Config name"); + } + if (ObjectHelper.isEmpty(namespaceName)) { + LOG.error("Get a specific Build Config require specify a namespace name"); + throw new IllegalArgumentException( + "Get a specific Build Config require specify a namespace name"); + } + buildConfig = getEndpoint().getKubernetesClient().adapt(OpenShiftClient.class).buildConfigs() + .inNamespace(namespaceName).withName(buildConfigName).get(); + exchange.getOut().setBody(buildConfig); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesBuildsProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesBuildsProducer.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesBuildsProducer.java new file mode 100644 index 0000000..bd54f32 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesBuildsProducer.java @@ -0,0 +1,143 @@ +/** + * 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 io.fabric8.openshift.api.model.Build; +import io.fabric8.openshift.api.model.BuildList; +import io.fabric8.openshift.api.model.DoneableBuild; +import io.fabric8.openshift.client.OpenShiftClient; + +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 KubernetesBuildsProducer extends DefaultProducer { + + private static final Logger LOG = LoggerFactory + .getLogger(KubernetesBuildsProducer.class); + + public KubernetesBuildsProducer(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_BUILD: + doList(exchange, operation); + break; + + case KubernetesOperations.LIST_BUILD_BY_LABELS_OPERATION: + doListBuildByLabels(exchange, operation); + break; + + case KubernetesOperations.GET_BUILD_OPERATION: + doGetBuild(exchange, operation); + break; + + default: + throw new IllegalArgumentException("Unsupported operation " + + operation); + } + } + + protected void doList(Exchange exchange, String operation) throws Exception { + BuildList buildList = getEndpoint() + .getKubernetesClient().adapt(OpenShiftClient.class).builds().list(); + exchange.getOut().setBody(buildList.getItems()); + } + + protected void doListBuildByLabels(Exchange exchange, + String operation) throws Exception { + BuildList buildList = null; + Map<String, String> labels = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_BUILDS_LABELS, + Map.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (!ObjectHelper.isEmpty(namespaceName)) { + ClientNonNamespaceOperation<OpenShiftClient, Build, BuildList, DoneableBuild, ClientResource<Build, DoneableBuild>> builds; + builds = getEndpoint().getKubernetesClient().adapt(OpenShiftClient.class).builds() + .inNamespace(namespaceName); + for (Map.Entry<String, String> entry : labels.entrySet()) { + builds.withLabel(entry.getKey(), entry.getValue()); + } + buildList = builds.list(); + } else { + ClientOperation<OpenShiftClient, Build, BuildList, DoneableBuild, ClientResource<Build, DoneableBuild>> builds; + builds = getEndpoint().getKubernetesClient().adapt(OpenShiftClient.class).builds(); + for (Map.Entry<String, String> entry : labels.entrySet()) { + builds.withLabel(entry.getKey(), entry.getValue()); + } + buildList = builds.list(); + } + exchange.getOut().setBody(buildList.getItems()); + } + + protected void doGetBuild(Exchange exchange, String operation) + throws Exception { + Build build = null; + String buildName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_BUILD_NAME, + String.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (ObjectHelper.isEmpty(buildName)) { + LOG.error("Get a specific Build require specify a Build name"); + throw new IllegalArgumentException( + "Get a specific Build require specify a Build name"); + } + if (ObjectHelper.isEmpty(namespaceName)) { + LOG.error("Get a specific Build require specify a namespace name"); + throw new IllegalArgumentException( + "Get a specific Build require specify a namespace name"); + } + build = getEndpoint().getKubernetesClient().adapt(OpenShiftClient.class).builds() + .inNamespace(namespaceName).withName(buildName).get(); + exchange.getOut().setBody(build); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesNamespacesProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesNamespacesProducer.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesNamespacesProducer.java new file mode 100644 index 0000000..0db6cc5 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesNamespacesProducer.java @@ -0,0 +1,159 @@ +/** + * 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.DoneableNamespace; +import io.fabric8.kubernetes.api.model.EditableNamespace; +import io.fabric8.kubernetes.api.model.Namespace; +import io.fabric8.kubernetes.api.model.NamespaceBuilder; +import io.fabric8.kubernetes.api.model.NamespaceList; +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.dsl.ClientNonNamespaceOperation; +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 KubernetesNamespacesProducer extends DefaultProducer { + + private static final Logger LOG = LoggerFactory + .getLogger(KubernetesNamespacesProducer.class); + + public KubernetesNamespacesProducer(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_NAMESPACE_OPERATION: + doList(exchange, operation); + break; + + case KubernetesOperations.LIST_NAMESPACE_BY_LABELS_OPERATION: + doListNamespaceByLabel(exchange, operation); + break; + + case KubernetesOperations.GET_NAMESPACE_OPERATION: + doGetNamespace(exchange, operation); + break; + + case KubernetesOperations.CREATE_NAMESPACE_OPERATION: + doCreateNamespace(exchange, operation); + break; + + case KubernetesOperations.DELETE_NAMESPACE_OPERATION: + doDeleteNamespace(exchange, operation); + break; + + default: + throw new IllegalArgumentException("Unsupported operation " + + operation); + } + } + + protected void doList(Exchange exchange, String operation) throws Exception { + NamespaceList namespacesList = getEndpoint().getKubernetesClient() + .namespaces().list(); + exchange.getOut().setBody(namespacesList.getItems()); + } + + protected void doListNamespaceByLabel(Exchange exchange, String operation) { + Map<String, String> labels = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_LABELS, Map.class); + if (ObjectHelper.isEmpty(labels)) { + LOG.error("Get a specific namespace by labels require specify a labels set"); + throw new IllegalArgumentException( + "Get a specific namespace by labels require specify a labels set"); + } + ClientNonNamespaceOperation<KubernetesClient, Namespace, NamespaceList, DoneableNamespace, ClientResource<Namespace, DoneableNamespace>> namespaces; + namespaces = getEndpoint().getKubernetesClient().namespaces(); + for (Map.Entry<String, String> entry : labels.entrySet()) { + namespaces.withLabel(entry.getKey(), entry.getValue()); + } + NamespaceList namespace = namespaces.list(); + exchange.getOut().setBody(namespace.getItems()); + } + + protected void doGetNamespace(Exchange exchange, String operation) { + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (ObjectHelper.isEmpty(namespaceName)) { + LOG.error("Get a specific namespace require specify a namespace name"); + throw new IllegalArgumentException( + "Get a specific namespace require specify a namespace name"); + } + Namespace namespace = getEndpoint().getKubernetesClient().namespaces() + .withName(namespaceName).get(); + exchange.getOut().setBody(namespace); + } + + 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); + EditableNamespace ns = new NamespaceBuilder().withNewMetadata() + .withName(namespaceName).withLabels(labels).endMetadata() + .build(); + Namespace namespace = getEndpoint().getKubernetesClient().namespaces() + .create(ns); + exchange.getOut().setBody(namespace); + } + + protected void doDeleteNamespace(Exchange exchange, String operation) { + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (ObjectHelper.isEmpty(namespaceName)) { + LOG.error("Delete a specific namespace require specify a namespace name"); + throw new IllegalArgumentException( + "Delete a specific namespace require specify a namespace name"); + } + Boolean namespace = getEndpoint().getKubernetesClient().namespaces() + .withName(namespaceName).delete(); + exchange.getOut().setBody(namespace); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a452d0ac/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesNodesProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesNodesProducer.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesNodesProducer.java new file mode 100644 index 0000000..cae957b --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesNodesProducer.java @@ -0,0 +1,123 @@ +/** + * 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.DoneableNode; +import io.fabric8.kubernetes.api.model.DoneablePersistentVolume; +import io.fabric8.kubernetes.api.model.Node; +import io.fabric8.kubernetes.api.model.NodeList; +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 KubernetesNodesProducer extends DefaultProducer { + + private static final Logger LOG = LoggerFactory + .getLogger(KubernetesNodesProducer.class); + + public KubernetesNodesProducer(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_NODES: + doList(exchange, operation); + break; + + case KubernetesOperations.LIST_NODES_BY_LABELS_OPERATION: + doListNodesByLabels(exchange, operation); + break; + + case KubernetesOperations.GET_NODE_OPERATION: + doGetNode(exchange, operation); + break; + + default: + throw new IllegalArgumentException("Unsupported operation " + + operation); + } + } + + protected void doList(Exchange exchange, String operation) throws Exception { + NodeList nodeList = getEndpoint() + .getKubernetesClient().nodes().list(); + exchange.getOut().setBody(nodeList.getItems()); + } + + protected void doListNodesByLabels(Exchange exchange, + String operation) throws Exception { + NodeList nodeList = null; + Map<String, String> labels = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NODES_LABELS, + Map.class); + ClientNonNamespaceOperation<KubernetesClient, Node, NodeList, DoneableNode, ClientResource<Node, DoneableNode>> nodes; + nodes = getEndpoint().getKubernetesClient().nodes(); + for (Map.Entry<String, String> entry : labels.entrySet()) { + nodes.withLabel(entry.getKey(), entry.getValue()); + } + nodeList = nodes.list(); + exchange.getOut().setBody(nodeList.getItems()); + } + + protected void doGetNode(Exchange exchange, String operation) + throws Exception { + Node node = null; + String pvName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NODE_NAME, + String.class); + if (ObjectHelper.isEmpty(pvName)) { + LOG.error("Get a specific Node require specify a Node name"); + throw new IllegalArgumentException( + "Get a specific Node require specify a Node name"); + } + node = getEndpoint().getKubernetesClient().nodes().withName(pvName).get(); + + exchange.getOut().setBody(node); + } +}