Skip to content

Commit 00d96a7

Browse files
geoandiocanel
authored andcommitted
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: spring-cloud#131
1 parent d0eeb8f commit 00d96a7

File tree

2 files changed

+84
-15
lines changed

2 files changed

+84
-15
lines changed

spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/profile/KubernetesApplicationContextInitializer.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
package org.springframework.cloud.kubernetes.profile;
1818

1919
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
20-
import org.springframework.cloud.kubernetes.PodUtils;
20+
import java.util.function.Supplier;
21+
import org.springframework.cloud.kubernetes.LazilyInstantiate;
2122
import org.springframework.cloud.kubernetes.StandardPodUtils;
2223
import org.springframework.context.ApplicationContextInitializer;
2324
import org.springframework.context.ConfigurableApplicationContext;
@@ -26,30 +27,38 @@
2627
public class KubernetesApplicationContextInitializer implements
2728
ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered {
2829

29-
private final KubernetesProfileApplicationListener listener;
3030
private static final int ORDER = 100;
3131

32-
public KubernetesApplicationContextInitializer() {
33-
//If we are inside Kubernetes this should be perfectly valid.
34-
//If not then we won't add the Kubernetes profile anyway.
35-
this(new StandardPodUtils(new DefaultKubernetesClient()));
36-
}
32+
private final Supplier<KubernetesProfileApplicationListener> listenerSupplier;
3733

38-
public KubernetesApplicationContextInitializer(PodUtils utils) {
39-
this(new KubernetesProfileApplicationListener(utils));
40-
}
34+
public KubernetesApplicationContextInitializer() {
35+
this(LazilyInstantiate.using(() ->
36+
//If we are inside Kubernetes this should be perfectly valid.
37+
//If not then we won't add the Kubernetes profile anyway.
38+
new KubernetesProfileApplicationListener(
39+
new StandardPodUtils(new DefaultKubernetesClient()))
40+
));
41+
}
4142

42-
public KubernetesApplicationContextInitializer(KubernetesProfileApplicationListener listener) {
43-
this.listener = listener;
44-
}
43+
public KubernetesApplicationContextInitializer(
44+
Supplier<KubernetesProfileApplicationListener> listenerSupplier) {
45+
this.listenerSupplier = listenerSupplier;
46+
}
4547

46-
@Override
48+
@Override
4749
public int getOrder() {
4850
return ORDER;
4951
}
5052

5153
@Override
5254
public void initialize(ConfigurableApplicationContext applicationContext) {
53-
listener.addKubernetesProfile(applicationContext.getEnvironment());
55+
if(isKubernetesEnabled(applicationContext)){
56+
listenerSupplier.get().addKubernetesProfile(applicationContext.getEnvironment());
57+
}
5458
}
59+
60+
private Boolean isKubernetesEnabled(ConfigurableApplicationContext applicationContext) {
61+
return applicationContext.getEnvironment()
62+
.getProperty("spring.cloud.kubernetes.enabled", Boolean.class, true);
63+
}
5564
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.springframework.cloud.kubernetes.profile;
2+
3+
import static org.mockito.Mockito.never;
4+
import static org.mockito.Mockito.verify;
5+
import static org.mockito.Mockito.when;
6+
7+
import java.util.function.Supplier;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
import org.mockito.Mock;
12+
import org.mockito.junit.MockitoJUnitRunner;
13+
import org.springframework.context.ConfigurableApplicationContext;
14+
import org.springframework.core.env.ConfigurableEnvironment;
15+
16+
@RunWith(MockitoJUnitRunner.class)
17+
public class KubernetesApplicationContextInitializerTest {
18+
19+
@Mock
20+
private Supplier<KubernetesProfileApplicationListener> listenerSupplier;
21+
22+
@Mock
23+
private ConfigurableApplicationContext applicationContext;
24+
25+
@Mock
26+
private ConfigurableEnvironment environment;
27+
28+
@Mock
29+
private KubernetesProfileApplicationListener listener;
30+
31+
private KubernetesApplicationContextInitializer initializer;
32+
33+
@Before
34+
public void setUp() {
35+
initializer = new KubernetesApplicationContextInitializer(listenerSupplier);
36+
when(applicationContext.getEnvironment()).thenReturn(environment);
37+
when(listenerSupplier.get()).thenReturn(listener);
38+
}
39+
40+
@Test
41+
public void kubernetesDisabled() {
42+
when(environment.getProperty("spring.cloud.kubernetes.enabled", Boolean.class, true))
43+
.thenReturn(false);
44+
45+
initializer.initialize(applicationContext);
46+
47+
verify(listenerSupplier, never()).get();
48+
}
49+
50+
@Test
51+
public void kubernetesEnabled() {
52+
when(environment.getProperty("spring.cloud.kubernetes.enabled", Boolean.class, true))
53+
.thenReturn(true);
54+
55+
initializer.initialize(applicationContext);
56+
57+
verify(listenerSupplier).get();
58+
verify(listener).addKubernetesProfile(environment);
59+
}
60+
}

0 commit comments

Comments
 (0)