Skip to content

Commit 03ed0f2

Browse files
geoandRyan Baxter
authored and
Ryan Baxter
committed
Fix profile specific configuration not loaded (#351)
Fixes: #347
1 parent 8e014f2 commit 03ed0f2

File tree

10 files changed

+35
-171
lines changed

10 files changed

+35
-171
lines changed

spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/ConfigMapPropertySource.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ public ConfigMapPropertySource(KubernetesClient client, String name, String name
6262
this(client, name, null, createEnvironmentWithActiveProfiles(profiles));
6363
}
6464

65-
private static Environment createEnvironmentWithActiveProfiles(String[] activeProfiles) {
65+
private static Environment createEnvironmentWithActiveProfiles(
66+
String[] activeProfiles) {
6667
StandardEnvironment environment = new StandardEnvironment();
6768
environment.setActiveProfiles(activeProfiles);
6869
return environment;

spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/ConfigMapPropertySourceLocator.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ private void addPropertySourcesFromPaths(Environment environment,
120120
content, filename, composite);
121121
}
122122
else if (filename.endsWith(".yml") || filename.endsWith(".yaml")) {
123-
addPropertySourceIfNeeded(c -> PROPERTIES_TO_MAP
124-
.apply(yamlParserGenerator(environment)
125-
.apply(c)),
123+
addPropertySourceIfNeeded(
124+
c -> PROPERTIES_TO_MAP
125+
.apply(yamlParserGenerator(environment).apply(c)),
126126
content, filename, composite);
127127
}
128128
}

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

-65
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -16,47 +16,35 @@
1616

1717
package org.springframework.cloud.kubernetes.profile;
1818

19+
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
1920
import org.apache.commons.logging.Log;
2021
import org.apache.commons.logging.LogFactory;
2122

22-
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
23-
import org.springframework.cloud.kubernetes.PodUtils;
24-
import org.springframework.context.ApplicationListener;
23+
import org.springframework.boot.SpringApplication;
24+
import org.springframework.boot.context.config.ConfigFileApplicationListener;
25+
import org.springframework.boot.env.EnvironmentPostProcessor;
26+
import org.springframework.cloud.kubernetes.StandardPodUtils;
2527
import org.springframework.core.Ordered;
2628
import org.springframework.core.env.ConfigurableEnvironment;
2729
import org.springframework.core.env.Environment;
2830

29-
/**
30-
* Adds Kubernetes profiles.
31-
*
32-
* @author Ioannis Canellos
33-
*/
34-
public class KubernetesProfileApplicationListener
35-
implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {
31+
public class KubernetesProfileEnvironmentPostProcessor
32+
implements EnvironmentPostProcessor, Ordered {
3633

3734
private static final Log LOG = LogFactory
38-
.getLog(KubernetesProfileApplicationListener.class);
39-
40-
private static final String KUBERNETES_PROFILE = "kubernetes";
41-
42-
private static final int OFFSET = 1;
35+
.getLog(KubernetesProfileEnvironmentPostProcessor.class);
4336

44-
private static final int ORDER = Ordered.HIGHEST_PRECEDENCE + OFFSET;
37+
// Before ConfigFileApplicationListener so values there can use these ones
38+
private static final int ORDER = ConfigFileApplicationListener.DEFAULT_ORDER - 1;
4539

46-
private final PodUtils utils;
47-
48-
public KubernetesProfileApplicationListener(PodUtils utils) {
49-
this.utils = utils;
50-
}
40+
private static final String KUBERNETES_PROFILE = "kubernetes";
5141

5242
@Override
53-
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
54-
ConfigurableEnvironment environment = event.getEnvironment();
55-
addKubernetesProfile(environment);
56-
}
57-
58-
void addKubernetesProfile(ConfigurableEnvironment environment) {
59-
if (this.utils.isInsideKubernetes()) {
43+
public void postProcessEnvironment(ConfigurableEnvironment environment,
44+
SpringApplication application) {
45+
final StandardPodUtils podUtils = new StandardPodUtils(
46+
new DefaultKubernetesClient());
47+
if (podUtils.isInsideKubernetes()) {
6048
if (hasKubernetesProfile(environment)) {
6149
if (LOG.isDebugEnabled()) {
6250
LOG.debug("'kubernetes' already in list of active profiles");
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
22
org.springframework.cloud.kubernetes.KubernetesAutoConfiguration\
33

4-
org.springframework.context.ApplicationContextInitializer=\
5-
org.springframework.cloud.kubernetes.profile.KubernetesApplicationContextInitializer
4+
5+
org.springframework.boot.env.EnvironmentPostProcessor=\
6+
org.springframework.cloud.kubernetes.profile.KubernetesProfileEnvironmentPostProcessor

spring-cloud-kubernetes-core/src/test/java/org/springframework/cloud/kubernetes/profile/KubernetesProfileApplicationListenerTest.java

-70
This file was deleted.

spring-cloud-kubernetes-integration-tests/simple-core/src/main/java/org/springframework/cloud/kubernetes/it/SimpleCoreApplication.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.cloud.kubernetes.it;
1818

19+
import org.springframework.beans.factory.annotation.Value;
1920
import org.springframework.boot.SpringApplication;
2021
import org.springframework.boot.autoconfigure.SpringBootApplication;
2122
import org.springframework.web.bind.annotation.GetMapping;
@@ -29,9 +30,12 @@ public static void main(String[] args) {
2930
SpringApplication.run(SimpleCoreApplication.class, args);
3031
}
3132

33+
@Value("${greeting.message}")
34+
private String message;
35+
3236
@GetMapping("/greeting")
3337
public Greeting home() {
34-
return new Greeting("Hello Spring Boot");
38+
return new Greeting(message);
3539
}
3640

3741
public static class Greeting {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
greeting:
2+
message: Hello from k8s

spring-cloud-kubernetes-integration-tests/simple-core/src/main/resources/application.yml

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
server:
22
port: 8080
33

4+
greeting:
5+
message: Hello sb
6+
47
# we enable some of the management endpoints to make it possible to restart the application
58
management:
69
endpoint:

spring-cloud-kubernetes-integration-tests/simple-core/src/test/java/org/springframework/cloud/kubernetes/it/GreetingAndHealthIT.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class GreetingAndHealthIT {
3636
@Test
3737
public void testGreetingEndpoint() {
3838
given().baseUri(String.format("http://%s:%d", HOST, PORT)).get("greeting").then()
39-
.statusCode(200).body("message", is("Hello Spring Boot"));
39+
.statusCode(200).body("message", is("Hello from k8s"));
4040
}
4141

4242
@Test

0 commit comments

Comments
 (0)