http://git-wip-us.apache.org/repos/asf/camel/blob/89eee3b9/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesOperations.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesOperations.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesOperations.java new file mode 100644 index 0000000..6f5bb38 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesOperations.java @@ -0,0 +1,104 @@ +/** + * 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 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"; + String SCALE_REPLICATION_CONTROLLER_OPERATION = "scaleReplicationController"; + + // 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"; + + // Config Maps + String LIST_CONFIGMAPS = "listConfigMaps"; + String LIST_CONFIGMAPS_BY_LABELS_OPERATION = "listConfigMapsByLabels"; + String GET_CONFIGMAP_OPERATION = "getConfigMap"; + String CREATE_CONFIGMAP_OPERATION = "createConfigMap"; + String DELETE_CONFIGMAP_OPERATION = "deleteConfigMap"; + + // 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/89eee3b9/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/build_configs/KubernetesBuildConfigsComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/build_configs/KubernetesBuildConfigsComponent.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/build_configs/KubernetesBuildConfigsComponent.java new file mode 100644 index 0000000..db20439 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/build_configs/KubernetesBuildConfigsComponent.java @@ -0,0 +1,28 @@ +/** + * 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.build_configs; + +import org.apache.camel.component.kubernetes.AbstractKubernetesComponent; +import org.apache.camel.component.kubernetes.KubernetesConfiguration; + +public class KubernetesBuildConfigsComponent extends AbstractKubernetesComponent { + + protected KubernetesBuildConfigsEndpoint doCreateEndpoint(String uri, String remaining, KubernetesConfiguration config) throws Exception { + KubernetesBuildConfigsEndpoint endpoint = new KubernetesBuildConfigsEndpoint(uri, this, config); + return endpoint; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/89eee3b9/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/build_configs/KubernetesBuildConfigsEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/build_configs/KubernetesBuildConfigsEndpoint.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/build_configs/KubernetesBuildConfigsEndpoint.java new file mode 100644 index 0000000..155d5d7 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/build_configs/KubernetesBuildConfigsEndpoint.java @@ -0,0 +1,51 @@ +/** + * 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.build_configs; + +import org.apache.camel.Consumer; +import org.apache.camel.Processor; +import org.apache.camel.Producer; +import org.apache.camel.component.kubernetes.AbstractKubernetesEndpoint; +import org.apache.camel.component.kubernetes.KubernetesConfiguration; +import org.apache.camel.spi.UriEndpoint; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The Kubernetes Build Config component provides a producer to execute kubernetes build config operations. + */ +@UriEndpoint(firstVersion = "2.17.0", scheme = "kubernetes-build-configs", title = "Kubernetes Build Config", + syntax = "kubernetes-build-configs:masterUrl", producerOnly = true, label = "container,cloud,paas") +public class KubernetesBuildConfigsEndpoint extends AbstractKubernetesEndpoint { + + private static final Logger LOG = LoggerFactory.getLogger(KubernetesBuildConfigsEndpoint.class); + + public KubernetesBuildConfigsEndpoint(String uri, KubernetesBuildConfigsComponent component, KubernetesConfiguration config) { + super(uri, component, config); + } + + @Override + public Producer createProducer() throws Exception { + return new KubernetesBuildConfigsProducer(this); + } + + @Override + public Consumer createConsumer(Processor processor) throws Exception { + throw new IllegalArgumentException("The kubernetes-build-configs doesn't support consumer"); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/89eee3b9/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/build_configs/KubernetesBuildConfigsProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/build_configs/KubernetesBuildConfigsProducer.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/build_configs/KubernetesBuildConfigsProducer.java new file mode 100644 index 0000000..0679224 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/build_configs/KubernetesBuildConfigsProducer.java @@ -0,0 +1,134 @@ +/** + * 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.build_configs; + +import java.util.Map; + +import io.fabric8.kubernetes.client.dsl.MixedOperation; +import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation; +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.DoneableBuildConfig; +import io.fabric8.openshift.client.OpenShiftClient; +import io.fabric8.openshift.client.dsl.BuildConfigResource; + +import org.apache.camel.Exchange; +import org.apache.camel.component.kubernetes.AbstractKubernetesEndpoint; +import org.apache.camel.component.kubernetes.KubernetesConstants; +import org.apache.camel.component.kubernetes.KubernetesOperations; +import org.apache.camel.impl.DefaultProducer; +import org.apache.camel.util.MessageHelper; +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(AbstractKubernetesEndpoint endpoint) { + super(endpoint); + } + + @Override + public AbstractKubernetesEndpoint getEndpoint() { + return (AbstractKubernetesEndpoint) 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)) { + NonNamespaceOperation<BuildConfig, BuildConfigList, DoneableBuildConfig, + BuildConfigResource<BuildConfig, DoneableBuildConfig, Void, Build>> 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 { + MixedOperation<BuildConfig, BuildConfigList, DoneableBuildConfig, + BuildConfigResource<BuildConfig, DoneableBuildConfig, Void, Build>> 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(); + } + MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true); + 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(); + + MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true); + exchange.getOut().setBody(buildConfig); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/89eee3b9/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/builds/KubernetesBuildsComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/builds/KubernetesBuildsComponent.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/builds/KubernetesBuildsComponent.java new file mode 100644 index 0000000..1927bdb --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/builds/KubernetesBuildsComponent.java @@ -0,0 +1,29 @@ +/** + * 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.builds; + +import org.apache.camel.component.kubernetes.AbstractKubernetesComponent; +import org.apache.camel.component.kubernetes.AbstractKubernetesEndpoint; +import org.apache.camel.component.kubernetes.KubernetesConfiguration; + +public class KubernetesBuildsComponent extends AbstractKubernetesComponent { + + protected AbstractKubernetesEndpoint doCreateEndpoint(String uri, String remaining, KubernetesConfiguration config) throws Exception { + KubernetesBuildsEndpoint endpoint = new KubernetesBuildsEndpoint(uri, this, config); + return endpoint; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/89eee3b9/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/builds/KubernetesBuildsEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/builds/KubernetesBuildsEndpoint.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/builds/KubernetesBuildsEndpoint.java new file mode 100644 index 0000000..03a7eb1 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/builds/KubernetesBuildsEndpoint.java @@ -0,0 +1,50 @@ +/** + * 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.builds; + +import org.apache.camel.Consumer; +import org.apache.camel.Processor; +import org.apache.camel.Producer; +import org.apache.camel.component.kubernetes.AbstractKubernetesEndpoint; +import org.apache.camel.component.kubernetes.KubernetesConfiguration; +import org.apache.camel.spi.UriEndpoint; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The Kubernetes Builds component provides a producer to execute kubernetes build operations. + */ +@UriEndpoint(firstVersion = "2.17.0", scheme = "kubernetes-builds", title = "Kubernetes Builds", syntax = "kubernetes-builds:masterUrl", producerOnly = true, label = "container,cloud,paas") +public class KubernetesBuildsEndpoint extends AbstractKubernetesEndpoint { + + private static final Logger LOG = LoggerFactory.getLogger(KubernetesBuildsEndpoint.class); + + public KubernetesBuildsEndpoint(String uri, KubernetesBuildsComponent component, KubernetesConfiguration config) { + super(uri, component, config); + } + + @Override + public Producer createProducer() throws Exception { + return new KubernetesBuildsProducer(this); + } + + @Override + public Consumer createConsumer(Processor processor) throws Exception { + throw new IllegalArgumentException("The kubernetes-builds doesn't support consumer"); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/89eee3b9/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/builds/KubernetesBuildsProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/builds/KubernetesBuildsProducer.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/builds/KubernetesBuildsProducer.java new file mode 100644 index 0000000..e44b6f2 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/builds/KubernetesBuildsProducer.java @@ -0,0 +1,131 @@ +/** + * 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.builds; + +import java.util.Map; + +import io.fabric8.kubernetes.client.dsl.LogWatch; +import io.fabric8.kubernetes.client.dsl.MixedOperation; +import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation; +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 io.fabric8.openshift.client.dsl.BuildResource; + +import org.apache.camel.Exchange; +import org.apache.camel.component.kubernetes.AbstractKubernetesEndpoint; +import org.apache.camel.component.kubernetes.KubernetesConstants; +import org.apache.camel.component.kubernetes.KubernetesEndpoint; +import org.apache.camel.component.kubernetes.KubernetesOperations; +import org.apache.camel.impl.DefaultProducer; +import org.apache.camel.util.MessageHelper; +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(AbstractKubernetesEndpoint endpoint) { + super(endpoint); + } + + @Override + public AbstractKubernetesEndpoint getEndpoint() { + return (AbstractKubernetesEndpoint) 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)) { + NonNamespaceOperation<Build, BuildList, DoneableBuild, BuildResource<Build, DoneableBuild, String, LogWatch>> 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 { + MixedOperation<Build, BuildList, DoneableBuild, BuildResource<Build, DoneableBuild, String, LogWatch>> builds = getEndpoint().getKubernetesClient(). + adapt(OpenShiftClient.class).builds(); + for (Map.Entry<String, String> entry : labels.entrySet()) { + builds.withLabel(entry.getKey(), entry.getValue()); + } + buildList = builds.list(); + } + MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true); + 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(); + + MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true); + exchange.getOut().setBody(build); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/89eee3b9/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/config_maps/KubernetesConfigMapsComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/config_maps/KubernetesConfigMapsComponent.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/config_maps/KubernetesConfigMapsComponent.java new file mode 100644 index 0000000..a543358 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/config_maps/KubernetesConfigMapsComponent.java @@ -0,0 +1,29 @@ +/** + * 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.config_maps; + +import org.apache.camel.component.kubernetes.AbstractKubernetesComponent; +import org.apache.camel.component.kubernetes.AbstractKubernetesEndpoint; +import org.apache.camel.component.kubernetes.KubernetesConfiguration; + +public class KubernetesConfigMapsComponent extends AbstractKubernetesComponent { + + protected KubernetesConfigMapsEndpoint doCreateEndpoint(String uri, String remaining, KubernetesConfiguration config) throws Exception { + KubernetesConfigMapsEndpoint endpoint = new KubernetesConfigMapsEndpoint(uri, this, config); + return endpoint; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/89eee3b9/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/config_maps/KubernetesConfigMapsEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/config_maps/KubernetesConfigMapsEndpoint.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/config_maps/KubernetesConfigMapsEndpoint.java new file mode 100644 index 0000000..88c3a98 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/config_maps/KubernetesConfigMapsEndpoint.java @@ -0,0 +1,69 @@ +/** + * 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.config_maps; + +import org.apache.camel.Consumer; +import org.apache.camel.Processor; +import org.apache.camel.Producer; +import org.apache.camel.component.kubernetes.AbstractKubernetesEndpoint; +import org.apache.camel.component.kubernetes.KubernetesConfiguration; +import org.apache.camel.component.kubernetes.build_configs.KubernetesBuildConfigsProducer; +import org.apache.camel.component.kubernetes.builds.KubernetesBuildsProducer; +import org.apache.camel.component.kubernetes.namespaces.KubernetesNamespacesConsumer; +import org.apache.camel.component.kubernetes.namespaces.KubernetesNamespacesProducer; +import org.apache.camel.component.kubernetes.nodes.KubernetesNodesConsumer; +import org.apache.camel.component.kubernetes.nodes.KubernetesNodesProducer; +import org.apache.camel.component.kubernetes.persistent_volumes.KubernetesPersistentVolumesProducer; +import org.apache.camel.component.kubernetes.persistent_volumes_claims.KubernetesPersistentVolumesClaimsProducer; +import org.apache.camel.component.kubernetes.pods.KubernetesPodsConsumer; +import org.apache.camel.component.kubernetes.pods.KubernetesPodsProducer; +import org.apache.camel.component.kubernetes.replication_controllers.KubernetesReplicationControllersConsumer; +import org.apache.camel.component.kubernetes.replication_controllers.KubernetesReplicationControllersProducer; +import org.apache.camel.component.kubernetes.resources_quota.KubernetesResourcesQuotaProducer; +import org.apache.camel.component.kubernetes.secrets.KubernetesSecretsProducer; +import org.apache.camel.component.kubernetes.service_accounts.KubernetesServiceAccountsProducer; +import org.apache.camel.component.kubernetes.services.KubernetesServicesConsumer; +import org.apache.camel.component.kubernetes.services.KubernetesServicesProducer; +import org.apache.camel.spi.UriEndpoint; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The Kubernetes Configmaps component provides a producer to execute kubernetes configmap operations. + */ +@UriEndpoint(firstVersion = "2.17.0", scheme = "kubernetes-config-maps", title = "Kubernetes ConfigMap", + syntax = "kubernetes-config-maps:masterUrl", producerOnly = true, label = "container,cloud,paas") +public class KubernetesConfigMapsEndpoint extends AbstractKubernetesEndpoint { + + private static final Logger LOG = LoggerFactory.getLogger(KubernetesConfigMapsEndpoint.class); + + public KubernetesConfigMapsEndpoint(String uri, KubernetesConfigMapsComponent component, KubernetesConfiguration config) { + super(uri, component, config); + } + + @Override + public Producer createProducer() throws Exception { + return new KubernetesConfigMapsProducer(this); + } + + @Override + public Consumer createConsumer(Processor processor) throws Exception { + throw new IllegalArgumentException("The kubernetes-configmaps doesn't support consumer"); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/89eee3b9/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/config_maps/KubernetesConfigMapsProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/config_maps/KubernetesConfigMapsProducer.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/config_maps/KubernetesConfigMapsProducer.java new file mode 100644 index 0000000..560d32d --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/config_maps/KubernetesConfigMapsProducer.java @@ -0,0 +1,176 @@ +/** + * 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.config_maps; + +import java.util.HashMap; +import java.util.Map; + +import io.fabric8.kubernetes.api.model.ConfigMap; +import io.fabric8.kubernetes.api.model.ConfigMapBuilder; +import io.fabric8.kubernetes.api.model.ConfigMapList; +import io.fabric8.kubernetes.api.model.DoneableConfigMap; +import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation; +import io.fabric8.kubernetes.client.dsl.Resource; + +import org.apache.camel.Exchange; +import org.apache.camel.component.kubernetes.AbstractKubernetesEndpoint; +import org.apache.camel.component.kubernetes.KubernetesConstants; +import org.apache.camel.component.kubernetes.KubernetesEndpoint; +import org.apache.camel.component.kubernetes.KubernetesOperations; +import org.apache.camel.impl.DefaultProducer; +import org.apache.camel.util.MessageHelper; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class KubernetesConfigMapsProducer extends DefaultProducer { + + private static final Logger LOG = LoggerFactory.getLogger(KubernetesConfigMapsProducer.class); + + public KubernetesConfigMapsProducer(AbstractKubernetesEndpoint endpoint) { + super(endpoint); + } + + @Override + public AbstractKubernetesEndpoint getEndpoint() { + return (AbstractKubernetesEndpoint) 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_CONFIGMAPS: + doList(exchange, operation); + break; + + case KubernetesOperations.LIST_CONFIGMAPS_BY_LABELS_OPERATION: + doListConfigMapsByLabels(exchange, operation); + break; + + case KubernetesOperations.GET_CONFIGMAP_OPERATION: + doGetConfigMap(exchange, operation); + break; + + case KubernetesOperations.CREATE_CONFIGMAP_OPERATION: + doCreateConfigMap(exchange, operation); + break; + + case KubernetesOperations.DELETE_CONFIGMAP_OPERATION: + doDeleteConfigMap(exchange, operation); + break; + + default: + throw new IllegalArgumentException("Unsupported operation " + operation); + } + } + + protected void doList(Exchange exchange, String operation) throws Exception { + ConfigMapList configMapsList = getEndpoint().getKubernetesClient().configMaps().list(); + exchange.getOut().setBody(configMapsList.getItems()); + } + + protected void doListConfigMapsByLabels(Exchange exchange, String operation) throws Exception { + ConfigMapList configMapsList = null; + Map<String, String> labels = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_CONFIGMAPS_LABELS, Map.class); + NonNamespaceOperation<ConfigMap, ConfigMapList, DoneableConfigMap, Resource<ConfigMap, DoneableConfigMap>> configMaps = getEndpoint().getKubernetesClient().configMaps(); + for (Map.Entry<String, String> entry : labels.entrySet()) { + configMaps.withLabel(entry.getKey(), entry.getValue()); + } + configMapsList = configMaps.list(); + + MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true); + exchange.getOut().setBody(configMapsList.getItems()); + } + + protected void doGetConfigMap(Exchange exchange, String operation) throws Exception { + ConfigMap configMap = null; + String cfMapName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_CONFIGMAP_NAME, String.class); + if (ObjectHelper.isEmpty(cfMapName)) { + LOG.error("Get a specific ConfigMap require specify a ConfigMap name"); + throw new IllegalArgumentException("Get a specific ConfigMap require specify a ConfigMap name"); + } + configMap = getEndpoint().getKubernetesClient().configMaps().withName(cfMapName).get(); + + MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true); + exchange.getOut().setBody(configMap); + } + + protected void doCreateConfigMap(Exchange exchange, String operation) throws Exception { + ConfigMap configMap = null; + String cfMapName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_CONFIGMAP_NAME, String.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + HashMap<String, String> configMapData = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_CONFIGMAP_DATA, HashMap.class); + if (ObjectHelper.isEmpty(cfMapName)) { + LOG.error("Create a specific configMap require specify a configMap name"); + throw new IllegalArgumentException( + "Create a specific configMap require specify a configMap name"); + } + if (ObjectHelper.isEmpty(namespaceName)) { + LOG.error("Create a specific configMap require specify a namespace name"); + throw new IllegalArgumentException( + "Create a specific configMap require specify a namespace name"); + } + if (ObjectHelper.isEmpty(configMapData)) { + LOG.error("Create a specific configMap require specify a data map"); + throw new IllegalArgumentException( + "Create a specific configMap require specify a data map"); + } + Map<String, String> labels = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_CONFIGMAPS_LABELS, Map.class); + ConfigMap cfMapCreating = new ConfigMapBuilder().withNewMetadata().withName(cfMapName) + .withLabels(labels).endMetadata().withData(configMapData).build(); + configMap = getEndpoint().getKubernetesClient().configMaps() + .inNamespace(namespaceName).create(cfMapCreating); + + MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true); + exchange.getOut().setBody(configMap); + } + + protected void doDeleteConfigMap(Exchange exchange, String operation) throws Exception { + String configMapName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_CONFIGMAP_NAME, String.class); + String namespaceName = exchange.getIn().getHeader( + KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); + if (ObjectHelper.isEmpty(configMapName)) { + LOG.error("Delete a specific config map require specify a config map name"); + throw new IllegalArgumentException( + "Delete a specific config map require specify a config map name"); + } + if (ObjectHelper.isEmpty(namespaceName)) { + LOG.error("Delete a specific config map require specify a namespace name"); + throw new IllegalArgumentException( + "Delete a specific config map require specify a namespace name"); + } + boolean cfMapDeleted = getEndpoint().getKubernetesClient().configMaps() + .inNamespace(namespaceName).withName(configMapName).delete(); + + MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true); + exchange.getOut().setBody(cfMapDeleted); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/89eee3b9/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesNamespacesConsumer.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesNamespacesConsumer.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesNamespacesConsumer.java deleted file mode 100644 index 901be16..0000000 --- a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesNamespacesConsumer.java +++ /dev/null @@ -1,133 +0,0 @@ -/** - * 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 java.util.concurrent.ExecutorService; - -import io.fabric8.kubernetes.api.model.DoneableNamespace; -import io.fabric8.kubernetes.api.model.Namespace; -import io.fabric8.kubernetes.api.model.NamespaceList; -import io.fabric8.kubernetes.client.KubernetesClientException; -import io.fabric8.kubernetes.client.Watch; -import io.fabric8.kubernetes.client.Watcher; -import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation; -import io.fabric8.kubernetes.client.dsl.Resource; - -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.NamespaceEvent; -import org.apache.camel.impl.DefaultConsumer; -import org.apache.camel.util.ObjectHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class KubernetesNamespacesConsumer extends DefaultConsumer { - - private static final Logger LOG = LoggerFactory.getLogger(KubernetesNamespacesConsumer.class); - - private final Processor processor; - private ExecutorService executor; - private NamespacesConsumerTask nsWatcher; - - public KubernetesNamespacesConsumer(KubernetesEndpoint endpoint, Processor processor) { - super(endpoint, processor); - this.processor = processor; - } - - @Override - public KubernetesEndpoint getEndpoint() { - return (KubernetesEndpoint) super.getEndpoint(); - } - - @Override - protected void doStart() throws Exception { - super.doStart(); - executor = getEndpoint().createExecutor(); - - nsWatcher = new NamespacesConsumerTask(); - executor.submit(nsWatcher); - } - - @Override - protected void doStop() throws Exception { - super.doStop(); - - LOG.debug("Stopping Kubernetes Namespace Consumer"); - if (executor != null) { - if (getEndpoint() != null && getEndpoint().getCamelContext() != null) { - if (nsWatcher != null) { - nsWatcher.getWatch().close(); - } - getEndpoint().getCamelContext().getExecutorServiceManager().shutdownNow(executor); - } else { - if (nsWatcher != null) { - nsWatcher.getWatch().close(); - } - executor.shutdownNow(); - } - } - executor = null; - } - - class NamespacesConsumerTask implements Runnable { - - private Watch watch; - - @Override - public void run() { - NonNamespaceOperation<Namespace, NamespaceList, DoneableNamespace, Resource<Namespace, DoneableNamespace>> w = getEndpoint().getKubernetesClient().namespaces(); - if (ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getNamespace())) { - w.withName(getEndpoint().getKubernetesConfiguration().getNamespace()); - } - watch = w.watch(new Watcher<Namespace>() { - - @Override - public void eventReceived(io.fabric8.kubernetes.client.Watcher.Action action, - Namespace resource) { - NamespaceEvent ne = new NamespaceEvent(action, resource); - Exchange exchange = getEndpoint().createExchange(); - exchange.getIn().setBody(ne.getNamespace()); - exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_EVENT_ACTION, ne.getAction()); - exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_EVENT_TIMESTAMP, System.currentTimeMillis()); - try { - processor.process(exchange); - } catch (Exception e) { - getExceptionHandler().handleException("Error during processing", exchange, e); - } - } - - @Override - public void onClose(KubernetesClientException cause) { - if (cause != null) { - LOG.error(cause.getMessage(), cause); - } - } - }); - } - - public Watch getWatch() { - return watch; - } - - public void setWatch(Watch watch) { - this.watch = watch; - } - } -} - http://git-wip-us.apache.org/repos/asf/camel/blob/89eee3b9/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesNodesConsumer.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesNodesConsumer.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesNodesConsumer.java deleted file mode 100644 index 4aa18c2..0000000 --- a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesNodesConsumer.java +++ /dev/null @@ -1,137 +0,0 @@ -/** - * 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 java.util.concurrent.ExecutorService; - -import io.fabric8.kubernetes.api.model.DoneableNode; -import io.fabric8.kubernetes.api.model.Node; -import io.fabric8.kubernetes.api.model.NodeList; -import io.fabric8.kubernetes.client.KubernetesClientException; -import io.fabric8.kubernetes.client.Watch; -import io.fabric8.kubernetes.client.Watcher; -import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation; -import io.fabric8.kubernetes.client.dsl.Resource; - -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.NodeEvent; -import org.apache.camel.impl.DefaultConsumer; -import org.apache.camel.util.ObjectHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class KubernetesNodesConsumer extends DefaultConsumer { - - private static final Logger LOG = LoggerFactory.getLogger(KubernetesNodesConsumer.class); - - private final Processor processor; - private ExecutorService executor; - private NodesConsumerTask nodesWatcher; - - public KubernetesNodesConsumer(KubernetesEndpoint endpoint, Processor processor) { - super(endpoint, processor); - this.processor = processor; - } - - @Override - public KubernetesEndpoint getEndpoint() { - return (KubernetesEndpoint) super.getEndpoint(); - } - - @Override - protected void doStart() throws Exception { - super.doStart(); - executor = getEndpoint().createExecutor(); - - nodesWatcher = new NodesConsumerTask(); - executor.submit(nodesWatcher); - } - - @Override - protected void doStop() throws Exception { - super.doStop(); - - LOG.debug("Stopping Kubernetes Nodes Consumer"); - if (executor != null) { - if (getEndpoint() != null && getEndpoint().getCamelContext() != null) { - if (nodesWatcher != null) { - nodesWatcher.getWatch().close(); - } - getEndpoint().getCamelContext().getExecutorServiceManager().shutdownNow(executor); - } else { - if (nodesWatcher != null) { - nodesWatcher.getWatch().close(); - } - executor.shutdownNow(); - } - } - executor = null; - } - - class NodesConsumerTask implements Runnable { - - private Watch watch; - - @Override - public void run() { - NonNamespaceOperation<Node, NodeList, DoneableNode, Resource<Node, DoneableNode>> w = getEndpoint().getKubernetesClient().nodes(); - if (ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getLabelKey()) - && ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getLabelValue())) { - w.withLabel(getEndpoint().getKubernetesConfiguration().getLabelKey(), getEndpoint().getKubernetesConfiguration().getLabelValue()); - } - if (ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getResourceName())) { - w.withName(getEndpoint().getKubernetesConfiguration().getResourceName()); - } - watch = w.watch(new Watcher<Node>() { - - @Override - public void eventReceived(io.fabric8.kubernetes.client.Watcher.Action action, - Node resource) { - NodeEvent ne = new NodeEvent(action, resource); - Exchange exchange = getEndpoint().createExchange(); - exchange.getIn().setBody(ne.getNode()); - exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_EVENT_ACTION, ne.getAction()); - exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_EVENT_TIMESTAMP, System.currentTimeMillis()); - try { - processor.process(exchange); - } catch (Exception e) { - getExceptionHandler().handleException("Error during processing", exchange, e); - } - } - - @Override - public void onClose(KubernetesClientException cause) { - if (cause != null) { - LOG.error(cause.getMessage(), cause); - } - - } - }); - } - - public Watch getWatch() { - return watch; - } - - public void setWatch(Watch watch) { - this.watch = watch; - } - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/89eee3b9/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 deleted file mode 100644 index 76222c0..0000000 --- a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesPodsConsumer.java +++ /dev/null @@ -1,140 +0,0 @@ -/** - * 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 java.util.concurrent.ExecutorService; - -import io.fabric8.kubernetes.api.model.DoneablePod; -import io.fabric8.kubernetes.api.model.Pod; -import io.fabric8.kubernetes.api.model.PodList; -import io.fabric8.kubernetes.client.KubernetesClientException; -import io.fabric8.kubernetes.client.Watch; -import io.fabric8.kubernetes.client.Watcher; -import io.fabric8.kubernetes.client.dsl.MixedOperation; -import io.fabric8.kubernetes.client.dsl.PodResource; - -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.DefaultConsumer; -import org.apache.camel.util.ObjectHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class KubernetesPodsConsumer extends DefaultConsumer { - - private static final Logger LOG = LoggerFactory.getLogger(KubernetesPodsConsumer.class); - - private final Processor processor; - private ExecutorService executor; - private PodsConsumerTask podsWatcher; - - public KubernetesPodsConsumer(KubernetesEndpoint endpoint, Processor processor) { - super(endpoint, processor); - this.processor = processor; - } - - @Override - public KubernetesEndpoint getEndpoint() { - return (KubernetesEndpoint) super.getEndpoint(); - } - - @Override - protected void doStart() throws Exception { - super.doStart(); - executor = getEndpoint().createExecutor(); - - podsWatcher = new PodsConsumerTask(); - executor.submit(podsWatcher); - } - - @Override - protected void doStop() throws Exception { - super.doStop(); - - LOG.debug("Stopping Kubernetes Pods Consumer"); - if (executor != null) { - if (getEndpoint() != null && getEndpoint().getCamelContext() != null) { - if (podsWatcher != null) { - podsWatcher.getWatch().close(); - } - getEndpoint().getCamelContext().getExecutorServiceManager().shutdownNow(executor); - } else { - if (podsWatcher != null) { - podsWatcher.getWatch().close(); - } - executor.shutdownNow(); - } - } - executor = null; - } - - class PodsConsumerTask implements Runnable { - - private Watch watch; - - @Override - public void run() { - MixedOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> w = getEndpoint().getKubernetesClient().pods(); - if (ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getNamespace())) { - w.inNamespace(getEndpoint().getKubernetesConfiguration().getNamespace()); - } - if (ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getLabelKey()) - && ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getLabelValue())) { - w.withLabel(getEndpoint().getKubernetesConfiguration().getLabelKey(), getEndpoint().getKubernetesConfiguration().getLabelValue()); - } - if (ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getResourceName())) { - w.withName(getEndpoint().getKubernetesConfiguration().getResourceName()); - } - watch = w.watch(new Watcher<Pod>() { - - @Override - public void eventReceived(io.fabric8.kubernetes.client.Watcher.Action action, - Pod resource) { - PodEvent pe = new PodEvent(action, resource); - Exchange exchange = getEndpoint().createExchange(); - exchange.getIn().setBody(pe.getPod()); - exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_EVENT_ACTION, pe.getAction()); - exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_EVENT_TIMESTAMP, System.currentTimeMillis()); - try { - processor.process(exchange); - } catch (Exception e) { - getExceptionHandler().handleException("Error during processing", exchange, e); - } - } - - @Override - public void onClose(KubernetesClientException cause) { - if (cause != null) { - LOG.error(cause.getMessage(), cause); - } - - } - }); - } - - public Watch getWatch() { - return watch; - } - - public void setWatch(Watch watch) { - this.watch = watch; - } - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/89eee3b9/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 deleted file mode 100644 index cacc8a4..0000000 --- a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesReplicationControllersConsumer.java +++ /dev/null @@ -1,142 +0,0 @@ -/** - * 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 java.util.concurrent.ExecutorService; - -import io.fabric8.kubernetes.api.model.DoneableReplicationController; -import io.fabric8.kubernetes.api.model.ReplicationController; -import io.fabric8.kubernetes.api.model.ReplicationControllerList; -import io.fabric8.kubernetes.client.KubernetesClientException; -import io.fabric8.kubernetes.client.Watch; -import io.fabric8.kubernetes.client.Watcher; -import io.fabric8.kubernetes.client.dsl.MixedOperation; -import io.fabric8.kubernetes.client.dsl.RollableScalableResource; - -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.impl.DefaultConsumer; -import org.apache.camel.util.ObjectHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class KubernetesReplicationControllersConsumer extends DefaultConsumer { - - private static final Logger LOG = LoggerFactory.getLogger(KubernetesReplicationControllersConsumer.class); - - private final Processor processor; - private ExecutorService executor; - private ReplicationControllersConsumerTask rcWatcher; - - public KubernetesReplicationControllersConsumer(KubernetesEndpoint endpoint, Processor processor) { - super(endpoint, processor); - this.processor = processor; - } - - @Override - public KubernetesEndpoint getEndpoint() { - return (KubernetesEndpoint) super.getEndpoint(); - } - - @Override - protected void doStart() throws Exception { - super.doStart(); - - executor = getEndpoint().createExecutor(); - rcWatcher = new ReplicationControllersConsumerTask(); - executor.submit(rcWatcher); - } - - @Override - protected void doStop() throws Exception { - super.doStop(); - - LOG.debug("Stopping Kubernetes Replication Controllers Consumer"); - if (executor != null) { - if (getEndpoint() != null && getEndpoint().getCamelContext() != null) { - if (rcWatcher != null) { - rcWatcher.getWatch().close(); - } - getEndpoint().getCamelContext().getExecutorServiceManager().shutdownNow(executor); - } else { - if (rcWatcher != null) { - rcWatcher.getWatch().close(); - } - executor.shutdownNow(); - } - } - executor = null; - } - - class ReplicationControllersConsumerTask implements Runnable { - - private Watch watch; - - @Override - public void run() { - MixedOperation<ReplicationController, ReplicationControllerList, - DoneableReplicationController, RollableScalableResource<ReplicationController, DoneableReplicationController>> w = getEndpoint().getKubernetesClient().replicationControllers(); - if (ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getNamespace())) { - w.inNamespace(getEndpoint().getKubernetesConfiguration().getNamespace()); - } - if (ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getLabelKey()) - && ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getLabelValue())) { - w.withLabel(getEndpoint().getKubernetesConfiguration().getLabelKey(), getEndpoint().getKubernetesConfiguration().getLabelValue()); - } - if (ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getResourceName())) { - w.withName(getEndpoint().getKubernetesConfiguration().getResourceName()); - } - watch = w.watch(new Watcher<ReplicationController>() { - - @Override - public void eventReceived(io.fabric8.kubernetes.client.Watcher.Action action, - ReplicationController resource) { - ReplicationControllerEvent rce = new ReplicationControllerEvent(action, resource); - Exchange exchange = getEndpoint().createExchange(); - exchange.getIn().setBody(rce.getReplicationController()); - exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_EVENT_ACTION, rce.getAction()); - exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_EVENT_TIMESTAMP, System.currentTimeMillis()); - try { - processor.process(exchange); - } catch (Exception e) { - getExceptionHandler().handleException("Error during processing", exchange, e); - } - - } - - @Override - public void onClose(KubernetesClientException cause) { - if (cause != null) { - LOG.error(cause.getMessage(), cause); - } - } - - }); - } - - public Watch getWatch() { - return watch; - } - - public void setWatch(Watch watch) { - this.watch = watch; - } - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/89eee3b9/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 deleted file mode 100644 index 5353f76..0000000 --- a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/consumer/KubernetesServicesConsumer.java +++ /dev/null @@ -1,141 +0,0 @@ -/** - * 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 java.util.concurrent.ExecutorService; - -import io.fabric8.kubernetes.api.model.DoneableService; -import io.fabric8.kubernetes.api.model.Service; -import io.fabric8.kubernetes.api.model.ServiceList; -import io.fabric8.kubernetes.client.KubernetesClientException; -import io.fabric8.kubernetes.client.Watch; -import io.fabric8.kubernetes.client.Watcher; -import io.fabric8.kubernetes.client.dsl.MixedOperation; -import io.fabric8.kubernetes.client.dsl.Resource; - -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.DefaultConsumer; -import org.apache.camel.util.ObjectHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class KubernetesServicesConsumer extends DefaultConsumer { - - private static final Logger LOG = LoggerFactory.getLogger(KubernetesServicesConsumer.class); - - private final Processor processor; - private ExecutorService executor; - private ServicesConsumerTask servicesWatcher; - - public KubernetesServicesConsumer(KubernetesEndpoint endpoint, Processor processor) { - super(endpoint, processor); - this.processor = processor; - } - - @Override - public KubernetesEndpoint getEndpoint() { - return (KubernetesEndpoint) super.getEndpoint(); - } - - @Override - protected void doStart() throws Exception { - super.doStart(); - executor = getEndpoint().createExecutor(); - - servicesWatcher = new ServicesConsumerTask(); - executor.submit(servicesWatcher); - - } - - @Override - protected void doStop() throws Exception { - super.doStop(); - LOG.debug("Stopping Kubernetes Services Consumer"); - if (executor != null) { - if (getEndpoint() != null && getEndpoint().getCamelContext() != null) { - if (servicesWatcher != null) { - servicesWatcher.getWatch().close(); - } - getEndpoint().getCamelContext().getExecutorServiceManager().shutdownNow(executor); - } else { - if (servicesWatcher != null) { - servicesWatcher.getWatch().close(); - } - executor.shutdownNow(); - } - } - executor = null; - } - - class ServicesConsumerTask implements Runnable { - - private Watch watch; - - @Override - public void run() { - MixedOperation<Service, ServiceList, DoneableService, Resource<Service, DoneableService>> w = getEndpoint().getKubernetesClient().services(); - if (ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getNamespace())) { - w.inNamespace(getEndpoint().getKubernetesConfiguration().getNamespace()); - } - if (ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getLabelKey()) - && ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getLabelValue())) { - w.withLabel(getEndpoint().getKubernetesConfiguration().getLabelKey(), getEndpoint().getKubernetesConfiguration().getLabelValue()); - } - if (ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getResourceName())) { - w.withName(getEndpoint().getKubernetesConfiguration().getResourceName()); - } - watch = w.watch(new Watcher<Service>() { - - @Override - public void eventReceived(io.fabric8.kubernetes.client.Watcher.Action action, - Service resource) { - ServiceEvent se = new ServiceEvent(action, resource); - Exchange exchange = getEndpoint().createExchange(); - exchange.getIn().setBody(se.getService()); - exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_EVENT_ACTION, se.getAction()); - exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_EVENT_TIMESTAMP, System.currentTimeMillis()); - try { - processor.process(exchange); - } catch (Exception e) { - getExceptionHandler().handleException("Error during processing", exchange, e); - } - - } - - @Override - public void onClose(KubernetesClientException cause) { - if (cause != null) { - LOG.error(cause.getMessage(), cause); - } - } - - }); - } - - public Watch getWatch() { - return watch; - } - - public void setWatch(Watch watch) { - this.watch = watch; - } - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/89eee3b9/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/namespaces/KubernetesNamespacesComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/namespaces/KubernetesNamespacesComponent.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/namespaces/KubernetesNamespacesComponent.java new file mode 100644 index 0000000..5dd9904 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/namespaces/KubernetesNamespacesComponent.java @@ -0,0 +1,28 @@ +/** + * 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.namespaces; + +import org.apache.camel.component.kubernetes.AbstractKubernetesComponent; +import org.apache.camel.component.kubernetes.KubernetesConfiguration; + +public class KubernetesNamespacesComponent extends AbstractKubernetesComponent { + + protected KubernetesNamespacesEndpoint doCreateEndpoint(String uri, String remaining, KubernetesConfiguration config) throws Exception { + KubernetesNamespacesEndpoint endpoint = new KubernetesNamespacesEndpoint(uri, this, config); + return endpoint; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/89eee3b9/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/namespaces/KubernetesNamespacesConsumer.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/namespaces/KubernetesNamespacesConsumer.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/namespaces/KubernetesNamespacesConsumer.java new file mode 100644 index 0000000..b9326c8 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/namespaces/KubernetesNamespacesConsumer.java @@ -0,0 +1,133 @@ +/** + * 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.namespaces; + +import java.util.concurrent.ExecutorService; + +import io.fabric8.kubernetes.api.model.DoneableNamespace; +import io.fabric8.kubernetes.api.model.Namespace; +import io.fabric8.kubernetes.api.model.NamespaceList; +import io.fabric8.kubernetes.client.KubernetesClientException; +import io.fabric8.kubernetes.client.Watch; +import io.fabric8.kubernetes.client.Watcher; +import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation; +import io.fabric8.kubernetes.client.dsl.Resource; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.component.kubernetes.AbstractKubernetesEndpoint; +import org.apache.camel.component.kubernetes.KubernetesConstants; +import org.apache.camel.component.kubernetes.consumer.common.NamespaceEvent; +import org.apache.camel.impl.DefaultConsumer; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class KubernetesNamespacesConsumer extends DefaultConsumer { + + private static final Logger LOG = LoggerFactory.getLogger(KubernetesNamespacesConsumer.class); + + private final Processor processor; + private ExecutorService executor; + private NamespacesConsumerTask nsWatcher; + + public KubernetesNamespacesConsumer(AbstractKubernetesEndpoint endpoint, Processor processor) { + super(endpoint, processor); + this.processor = processor; + } + + @Override + public AbstractKubernetesEndpoint getEndpoint() { + return (AbstractKubernetesEndpoint) super.getEndpoint(); + } + + @Override + protected void doStart() throws Exception { + super.doStart(); + executor = getEndpoint().createExecutor(); + + nsWatcher = new NamespacesConsumerTask(); + executor.submit(nsWatcher); + } + + @Override + protected void doStop() throws Exception { + super.doStop(); + + LOG.debug("Stopping Kubernetes Namespace Consumer"); + if (executor != null) { + if (getEndpoint() != null && getEndpoint().getCamelContext() != null) { + if (nsWatcher != null) { + nsWatcher.getWatch().close(); + } + getEndpoint().getCamelContext().getExecutorServiceManager().shutdownNow(executor); + } else { + if (nsWatcher != null) { + nsWatcher.getWatch().close(); + } + executor.shutdownNow(); + } + } + executor = null; + } + + class NamespacesConsumerTask implements Runnable { + + private Watch watch; + + @Override + public void run() { + NonNamespaceOperation<Namespace, NamespaceList, DoneableNamespace, Resource<Namespace, DoneableNamespace>> w = getEndpoint().getKubernetesClient().namespaces(); + if (ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getNamespace())) { + w.withName(getEndpoint().getKubernetesConfiguration().getNamespace()); + } + watch = w.watch(new Watcher<Namespace>() { + + @Override + public void eventReceived(io.fabric8.kubernetes.client.Watcher.Action action, + Namespace resource) { + NamespaceEvent ne = new NamespaceEvent(action, resource); + Exchange exchange = getEndpoint().createExchange(); + exchange.getIn().setBody(ne.getNamespace()); + exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_EVENT_ACTION, ne.getAction()); + exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_EVENT_TIMESTAMP, System.currentTimeMillis()); + try { + processor.process(exchange); + } catch (Exception e) { + getExceptionHandler().handleException("Error during processing", exchange, e); + } + } + + @Override + public void onClose(KubernetesClientException cause) { + if (cause != null) { + LOG.error(cause.getMessage(), cause); + } + } + }); + } + + public Watch getWatch() { + return watch; + } + + public void setWatch(Watch watch) { + this.watch = watch; + } + } +} + http://git-wip-us.apache.org/repos/asf/camel/blob/89eee3b9/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/namespaces/KubernetesNamespacesEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/namespaces/KubernetesNamespacesEndpoint.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/namespaces/KubernetesNamespacesEndpoint.java new file mode 100644 index 0000000..5c74332 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/namespaces/KubernetesNamespacesEndpoint.java @@ -0,0 +1,52 @@ +/** + * 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.namespaces; + +import org.apache.camel.Consumer; +import org.apache.camel.Processor; +import org.apache.camel.Producer; +import org.apache.camel.component.kubernetes.AbstractKubernetesEndpoint; +import org.apache.camel.component.kubernetes.KubernetesConfiguration; +import org.apache.camel.spi.UriEndpoint; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The Kubernetes Namespaces component provides a producer to execute kubernetes namespace operations + * and a consumer to consume namespace events. + */ +@UriEndpoint(firstVersion = "2.17.0", scheme = "kubernetes-namespaces", title = "Kubernetes Namespaces", + syntax = "kubernetes-namespaces:masterUrl", consumerClass = KubernetesNamespacesConsumer.class, label = "container,cloud,paas") +public class KubernetesNamespacesEndpoint extends AbstractKubernetesEndpoint { + + private static final Logger LOG = LoggerFactory.getLogger(KubernetesNamespacesEndpoint.class); + + public KubernetesNamespacesEndpoint(String uri, KubernetesNamespacesComponent component, KubernetesConfiguration config) { + super(uri, component, config); + } + + @Override + public Producer createProducer() throws Exception { + return new KubernetesNamespacesProducer(this); + } + + @Override + public Consumer createConsumer(Processor processor) throws Exception { + return new KubernetesNamespacesConsumer(this, processor); + } + +}
