blob: 553d59d54311cc38dc736b5a7aecac48448e2b54 [file] [log] [blame]
/*
* 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.concurrent.ExecutorService;
import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ConfigMapList;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.Watcher;
import io.fabric8.kubernetes.client.dsl.FilterWatchListDeletable;
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.ConfigMapEvent;
import org.apache.camel.support.DefaultConsumer;
import org.apache.camel.util.ObjectHelper;
public class KubernetesConfigMapsConsumer extends DefaultConsumer {
private final Processor processor;
private ExecutorService executor;
private ConfigMapsConsumerTask configMapWatcher;
public KubernetesConfigMapsConsumer(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();
configMapWatcher = new ConfigMapsConsumerTask();
executor.submit(configMapWatcher);
}
@Override
protected void doStop() throws Exception {
super.doStop();
log.debug("Stopping Kubernetes ConfigMap Consumer");
if (executor != null) {
if (getEndpoint() != null && getEndpoint().getCamelContext() != null) {
if (configMapWatcher != null) {
configMapWatcher.getWatch().close();
}
getEndpoint().getCamelContext().getExecutorServiceManager().shutdownNow(executor);
} else {
if (configMapWatcher != null) {
configMapWatcher.getWatch().close();
}
executor.shutdownNow();
}
}
executor = null;
}
class ConfigMapsConsumerTask implements Runnable {
private Watch watch;
@Override
public void run() {
FilterWatchListDeletable<ConfigMap, ConfigMapList, Boolean, Watch, Watcher<ConfigMap>> w = null;
if (ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getLabelKey())
&& ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getLabelValue())) {
w = getEndpoint().getKubernetesClient().configMaps().withLabel(getEndpoint().getKubernetesConfiguration().getLabelKey(),
getEndpoint().getKubernetesConfiguration().getLabelValue());
}
if (ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getResourceName())) {
w = (FilterWatchListDeletable<ConfigMap, ConfigMapList, Boolean, Watch, Watcher<ConfigMap>>)getEndpoint().getKubernetesClient().configMaps()
.withName(getEndpoint().getKubernetesConfiguration().getResourceName());
}
watch = w.watch(new Watcher<ConfigMap>() {
@Override
public void eventReceived(io.fabric8.kubernetes.client.Watcher.Action action, ConfigMap resource) {
ConfigMapEvent de = new ConfigMapEvent(action, resource);
Exchange exchange = getEndpoint().createExchange();
exchange.getIn().setBody(de.getConfigMap());
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_EVENT_ACTION, de.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;
}
}
}