org.zodiac.autoconfigure.kubernetes.KubernetesAutoConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.kubernetes;
import java.time.Duration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.zodiac.autoconfigure.kubernetes.condition.ConditionalOnKubernetesEnabled;
@SpringBootConfiguration
@ConditionalOnKubernetesEnabled
@ConditionalOnClass(value = {io.fabric8.kubernetes.client.DefaultKubernetesClient.class, io.fabric8.kubernetes.api.KubernetesResourceMappingProvider.class})
public class KubernetesAutoConfiguration {
private static final Logger LOG = LoggerFactory.getLogger(KubernetesAutoConfiguration.class);
@Bean
@ConditionalOnMissingBean
@ConfigurationProperties(prefix = org.zodic.kubernetes.base.constants.KubernetesSystemPropertiesConstants.SPRING_KUBERNETES_PREFIX, ignoreInvalidFields = true)
protected KubernetesClientProperties kubernetesClientProperties() {
return new KubernetesClientProperties();
}
@Bean
@ConditionalOnMissingBean(value = {io.fabric8.kubernetes.client.Config.class})
protected io.fabric8.kubernetes.client.Config kubernetesClientConfig(KubernetesClientProperties kubernetesClientProperties) {
io.fabric8.kubernetes.client.Config base = io.fabric8.kubernetes.client.Config.autoConfigure(null);
io.fabric8.kubernetes.client.Config properties = new io.fabric8.kubernetes.client.ConfigBuilder(base)
// Only set values that have been explicitly specified
.withMasterUrl(or(kubernetesClientProperties.getMasterUrl(), base.getMasterUrl()))
.withApiVersion(or(kubernetesClientProperties.getApiVersion(), base.getApiVersion()))
.withNamespace(or(kubernetesClientProperties.getNamespace(), base.getNamespace()))
.withUsername(or(kubernetesClientProperties.getUsername(), base.getUsername()))
.withPassword(or(kubernetesClientProperties.getPassword(), base.getPassword()))
.withCaCertFile(or(kubernetesClientProperties.getCaCertFile(), base.getCaCertFile()))
.withCaCertData(or(kubernetesClientProperties.getCaCertData(), base.getCaCertData()))
.withClientKeyFile(or(kubernetesClientProperties.getClientKeyFile(), base.getClientKeyFile()))
.withClientKeyData(or(kubernetesClientProperties.getClientKeyData(), base.getClientKeyData()))
.withClientCertFile(or(kubernetesClientProperties.getClientCertFile(), base.getClientCertFile()))
.withClientCertData(or(kubernetesClientProperties.getClientCertData(), base.getClientCertData()))
// No magic is done for the configReloadInfo below so we leave them as is.
.withClientKeyAlgo(or(kubernetesClientProperties.getClientKeyAlgo(), base.getClientKeyAlgo()))
.withClientKeyPassphrase(
or(kubernetesClientProperties.getClientKeyPassphrase(), base.getClientKeyPassphrase()))
.withConnectionTimeout(
orDurationInt(kubernetesClientProperties.getConnectionTimeout(), base.getConnectionTimeout()))
.withRequestTimeout(orDurationInt(kubernetesClientProperties.getRequestTimeout(), base.getRequestTimeout()))
.withRollingTimeout(
orDurationLong(kubernetesClientProperties.getRollingTimeout(), base.getRollingTimeout()))
.withTrustCerts(or(kubernetesClientProperties.isTrustCerts(), base.isTrustCerts()))
.withHttpProxy(or(kubernetesClientProperties.getHttpProxy(), base.getHttpProxy()))
.withHttpsProxy(or(kubernetesClientProperties.getHttpsProxy(), base.getHttpsProxy()))
.withProxyUsername(or(kubernetesClientProperties.getProxyUsername(), base.getProxyUsername()))
.withPassword(or(kubernetesClientProperties.getProxyPassword(), base.getProxyPassword()))
.withNoProxy(or(kubernetesClientProperties.getNoProxy(), base.getNoProxy())).build();
if (properties.getNamespace() == null || properties.getNamespace().isEmpty()) {
LOG.warn("No namespace has been detected. Please specify 'KUBERNETES_NAMESPACE' env var, or use a later kubernetes version (1.3 or later)");
}
return properties;
}
@Bean
@ConditionalOnMissingBean
protected io.fabric8.kubernetes.client.KubernetesClient kubernetesClient(io.fabric8.kubernetes.client.Config config) {
return new io.fabric8.kubernetes.client.DefaultKubernetesClient(config);
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnClass(name = {"io.fabric8.kubernetes.api.model.Pod"})
protected org.zodic.kubernetes.base.support.StandardPodOperations podOperations(io.fabric8.kubernetes.client.KubernetesClient client) {
return new org.zodic.kubernetes.base.support.StandardPodOperations(client);
}
private static D or(D dis, D dat) {
if (dis != null) {
return dis;
} else {
return dat;
}
}
private static Integer orDurationInt(Duration dis, Integer dat) {
if (dis != null) {
return (int)dis.toMillis();
} else {
return dat;
}
}
private static Long orDurationLong(Duration dis, Long dat) {
if (dis != null) {
return dis.toMillis();
} else {
return dat;
}
}
}