Lazily create the KubernetesProfileApplicationListener

This is needed in order to avoid causing problems to environments
that have spring-cloud-kubernetes on the classpath but don't want to
enable it

Fixes: #131
This commit is contained in:
Georgios Andrianakis
2018-05-22 13:10:14 +03:00
committed by Ioannis Canellos
parent d0eeb8ff55
commit 00d96a7cdc
2 changed files with 84 additions and 15 deletions

View File

@@ -17,7 +17,8 @@
package org.springframework.cloud.kubernetes.profile;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import org.springframework.cloud.kubernetes.PodUtils;
import java.util.function.Supplier;
import org.springframework.cloud.kubernetes.LazilyInstantiate;
import org.springframework.cloud.kubernetes.StandardPodUtils;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
@@ -26,30 +27,38 @@ import org.springframework.core.Ordered;
public class KubernetesApplicationContextInitializer implements
ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered {
private final KubernetesProfileApplicationListener listener;
private static final int ORDER = 100;
public KubernetesApplicationContextInitializer() {
//If we are inside Kubernetes this should be perfectly valid.
//If not then we won't add the Kubernetes profile anyway.
this(new StandardPodUtils(new DefaultKubernetesClient()));
}
private final Supplier<KubernetesProfileApplicationListener> listenerSupplier;
public KubernetesApplicationContextInitializer(PodUtils utils) {
this(new KubernetesProfileApplicationListener(utils));
}
public KubernetesApplicationContextInitializer() {
this(LazilyInstantiate.using(() ->
//If we are inside Kubernetes this should be perfectly valid.
//If not then we won't add the Kubernetes profile anyway.
new KubernetesProfileApplicationListener(
new StandardPodUtils(new DefaultKubernetesClient()))
));
}
public KubernetesApplicationContextInitializer(KubernetesProfileApplicationListener listener) {
this.listener = listener;
}
public KubernetesApplicationContextInitializer(
Supplier<KubernetesProfileApplicationListener> listenerSupplier) {
this.listenerSupplier = listenerSupplier;
}
@Override
@Override
public int getOrder() {
return ORDER;
}
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
listener.addKubernetesProfile(applicationContext.getEnvironment());
if(isKubernetesEnabled(applicationContext)){
listenerSupplier.get().addKubernetesProfile(applicationContext.getEnvironment());
}
}
private Boolean isKubernetesEnabled(ConfigurableApplicationContext applicationContext) {
return applicationContext.getEnvironment()
.getProperty("spring.cloud.kubernetes.enabled", Boolean.class, true);
}
}

View File

@@ -0,0 +1,60 @@
package org.springframework.cloud.kubernetes.profile;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.function.Supplier;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
@RunWith(MockitoJUnitRunner.class)
public class KubernetesApplicationContextInitializerTest {
@Mock
private Supplier<KubernetesProfileApplicationListener> listenerSupplier;
@Mock
private ConfigurableApplicationContext applicationContext;
@Mock
private ConfigurableEnvironment environment;
@Mock
private KubernetesProfileApplicationListener listener;
private KubernetesApplicationContextInitializer initializer;
@Before
public void setUp() {
initializer = new KubernetesApplicationContextInitializer(listenerSupplier);
when(applicationContext.getEnvironment()).thenReturn(environment);
when(listenerSupplier.get()).thenReturn(listener);
}
@Test
public void kubernetesDisabled() {
when(environment.getProperty("spring.cloud.kubernetes.enabled", Boolean.class, true))
.thenReturn(false);
initializer.initialize(applicationContext);
verify(listenerSupplier, never()).get();
}
@Test
public void kubernetesEnabled() {
when(environment.getProperty("spring.cloud.kubernetes.enabled", Boolean.class, true))
.thenReturn(true);
initializer.initialize(applicationContext);
verify(listenerSupplier).get();
verify(listener).addKubernetesProfile(environment);
}
}