All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodic.kubernetes.confcenter.reload.PollingConfigurationChangeDetector Maven / Gradle / Ivy

package org.zodic.kubernetes.confcenter.reload;

import java.util.List;

import javax.annotation.PostConstruct;

import io.fabric8.kubernetes.client.KubernetesClient;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.scheduling.annotation.Scheduled;
import org.zodic.kubernetes.confcenter.ConfigMapPropertySource;
import org.zodic.kubernetes.confcenter.ConfigMapPropertySourceLocator;
import org.zodic.kubernetes.confcenter.SecretsPropertySource;
import org.zodic.kubernetes.confcenter.SecretsPropertySourceLocator;

/**
 * A change detector that periodically retrieves secrets and configmaps and fire a reload when something changes.
 *
 */
public class PollingConfigurationChangeDetector extends ConfigurationChangeDetector {

    protected Log log = LogFactory.getLog(getClass());

    private ConfigMapPropertySourceLocator configMapPropertySourceLocator;

    private SecretsPropertySourceLocator secretsPropertySourceLocator;

    public PollingConfigurationChangeDetector(AbstractEnvironment environment, ConfigReloadInfo configReloadInfo,
        KubernetesClient kubernetesClient, ConfigurationUpdateStrategy strategy,
        ConfigMapPropertySourceLocator configMapPropertySourceLocator,
        SecretsPropertySourceLocator secretsPropertySourceLocator) {
        super(environment, configReloadInfo, kubernetesClient, strategy);

        this.configMapPropertySourceLocator = configMapPropertySourceLocator;
        this.secretsPropertySourceLocator = secretsPropertySourceLocator;
    }

    @PostConstruct
    public void init() {
        this.log.info("Kubernetes polling configuration change detector activated");
    }

    @Scheduled(initialDelayString = "${spring.cloud.kubernetes.reload.period:15000}",
        fixedDelayString = "${spring.cloud.kubernetes.reload.period:15000}")
    public void executeCycle() {

        boolean changedConfigMap = false;
        if (this.configReloadInfo.isMonitoringConfigMaps()) {
            List currentConfigMapSources =
                findPropertySources(ConfigMapPropertySource.class);

            if (!currentConfigMapSources.isEmpty()) {
                changedConfigMap =
                    changed(locateMapPropertySources(this.configMapPropertySourceLocator, this.environment),
                        currentConfigMapSources);
            }
        }

        boolean changedSecrets = false;
        if (this.configReloadInfo.isMonitoringSecrets()) {
            MapPropertySource currentSecretSource = findPropertySource(SecretsPropertySource.class);
            if (currentSecretSource != null) {
                MapPropertySource newSecretSource = this.secretsPropertySourceLocator.locatePropertySource(this.environment);
                changedSecrets = changed(currentSecretSource, newSecretSource);
            }
        }

        if (changedConfigMap || changedSecrets) {
            reloadProperties();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy