org.zodiac.autoconfigure.kubernetes.istio.IstioBootstrapConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.kubernetes.istio;
import java.util.Arrays;
import javax.annotation.PostConstruct;
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.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
@SpringBootConfiguration
@ConditionalOnClass(value = {io.fabric8.kubernetes.client.Config.class, me.snowdrop.istio.client.IstioClient.class})
@ConditionalOnProperty(value = org.zodic.kubernetes.base.constants.KubernetesSystemPropertiesConstants.SPRING_ISTIO_ENABLED, havingValue = "true")
public class IstioBootstrapConfiguration {
private static final Logger LOG = LoggerFactory.getLogger(IstioBootstrapConfiguration.class);
private static final String ISTIO_PROFILE = "istio";
@Bean
@ConditionalOnMissingBean
@ConfigurationProperties(prefix = org.zodic.kubernetes.base.constants.KubernetesSystemPropertiesConstants.SPRING_ISTIO_PREFIX, ignoreInvalidFields = true)
protected IstioClientProperties istioClientProperties() {
return new IstioClientProperties();
}
@Bean
@ConditionalOnMissingBean
protected org.zodic.kubernetes.istio.util.MeshUtils istioMeshUtils(IstioClientProperties istioClientProperties) {
return new org.zodic.kubernetes.istio.util.MeshUtils(istioClientProperties);
}
@SpringBootConfiguration
//@EnableConfigurationProperties(IstioClientProperties.class)
protected static class IstioDetectionConfiguration {
private final org.zodic.kubernetes.istio.util.MeshUtils utils;
private final ConfigurableEnvironment environment;
public IstioDetectionConfiguration(org.zodic.kubernetes.istio.util.MeshUtils utils, ConfigurableEnvironment environment) {
this.utils = utils;
this.environment = environment;
}
@PostConstruct
public void detectIstio() {
addIstioProfile(this.environment);
}
void addIstioProfile(ConfigurableEnvironment environment) {
if (this.utils.isIstioEnabled()) {
if (hasIstioProfile(environment)) {
if (LOG.isDebugEnabled()) {
LOG.debug("'{}' already in list of active profiles.", ISTIO_PROFILE);
}
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Adding '{}' to list of active profiles.", ISTIO_PROFILE);
}
environment.addActiveProfile(ISTIO_PROFILE);
}
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Not running inside kubernetes with istio enabled. Skipping '{}' profile activation.", ISTIO_PROFILE);
}
}
}
private boolean hasIstioProfile(Environment environment) {
return Arrays.stream(environment.getActiveProfiles()).anyMatch(ISTIO_PROFILE::equalsIgnoreCase);
}
}
}