|
17 | 17 | package org.springframework.cloud.kubernetes.profile;
|
18 | 18 |
|
19 | 19 | 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; |
21 | 22 | import org.springframework.cloud.kubernetes.StandardPodUtils;
|
22 | 23 | import org.springframework.context.ApplicationContextInitializer;
|
23 | 24 | import org.springframework.context.ConfigurableApplicationContext;
|
|
26 | 27 | public class KubernetesApplicationContextInitializer implements
|
27 | 28 | ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered {
|
28 | 29 |
|
29 |
| - private final KubernetesProfileApplicationListener listener; |
30 | 30 | private static final int ORDER = 100;
|
31 | 31 |
|
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; |
37 | 33 |
|
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 | + } |
41 | 42 |
|
42 |
| - public KubernetesApplicationContextInitializer(KubernetesProfileApplicationListener listener) { |
43 |
| - this.listener = listener; |
44 |
| - } |
| 43 | + public KubernetesApplicationContextInitializer( |
| 44 | + Supplier<KubernetesProfileApplicationListener> listenerSupplier) { |
| 45 | + this.listenerSupplier = listenerSupplier; |
| 46 | + } |
45 | 47 |
|
46 |
| - @Override |
| 48 | + @Override |
47 | 49 | public int getOrder() {
|
48 | 50 | return ORDER;
|
49 | 51 | }
|
50 | 52 |
|
51 | 53 | @Override
|
52 | 54 | public void initialize(ConfigurableApplicationContext applicationContext) {
|
53 |
| - listener.addKubernetesProfile(applicationContext.getEnvironment()); |
| 55 | + if(isKubernetesEnabled(applicationContext)){ |
| 56 | + listenerSupplier.get().addKubernetesProfile(applicationContext.getEnvironment()); |
| 57 | + } |
54 | 58 | }
|
| 59 | + |
| 60 | + private Boolean isKubernetesEnabled(ConfigurableApplicationContext applicationContext) { |
| 61 | + return applicationContext.getEnvironment() |
| 62 | + .getProperty("spring.cloud.kubernetes.enabled", Boolean.class, true); |
| 63 | + } |
55 | 64 | }
|
0 commit comments