Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly set convention and order of KubernetesEnvironmentPropertySource #4865

Merged
merged 1 commit into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ public KubernetesEnvironmentPropertySource(@Nullable List<String> includes, @Nul
super(NAME, getEnv(getEnvNoK8s(), includes, excludes));
}

@Override
public int getOrder() {
return EnvironmentPropertySource.POSITION;
}

@Override
public PropertyConvention getConvention() {
return PropertyConvention.ENVIRONMENT_VARIABLE;
}

static Map<String, String> getEnvNoK8s() {
Map<String, String> props = new HashMap<>(System.getenv());
props.entrySet().removeIf(entry -> VAR_SUFFIXES.stream().anyMatch(s -> entry.getKey().endsWith(s)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import io.micronaut.context.ApplicationContext
import io.micronaut.context.ApplicationContextConfiguration
import io.micronaut.context.exceptions.ConfigurationException
import io.micronaut.core.naming.NameUtils
import spock.lang.Issue
import spock.lang.Specification
import spock.util.environment.RestoreSystemProperties

Expand Down Expand Up @@ -710,6 +711,23 @@ class DefaultEnvironmentSpec extends Specification {
!env.getProperty("v1-service-xpto-service-host", String).isPresent()
}

@Issue("https://github.com/micronaut-projects/micronaut-core/issues/4861")
void "In the Kubernetes environment, environment variables can be read"() {
given:
Environment env = SystemLambda.withEnvironmentVariable("MICRONAUT_SERVER_PORT", "8081")
.execute {
new DefaultEnvironment(new ApplicationContextConfiguration() {
@Override
List<String> getEnvironments() {
return Arrays.asList(Environment.KUBERNETES)
}
}).start()
}

expect:
env.getProperty("micronaut.server.port", Integer).get() == 8081
}

private static Environment startEnv(String files) {
new DefaultEnvironment({["test"]}) {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,14 @@ class KubernetesEnvironmentPropertySourceSpec extends Specification {
suffix << KubernetesEnvironmentPropertySource.VAR_SUFFIXES
}

void "it has the same position and convention than the EnvironmentPropertySource"() {
given:
KubernetesEnvironmentPropertySource keps = new KubernetesEnvironmentPropertySource()
EnvironmentPropertySource eps = new EnvironmentPropertySource()

expect:
keps.order == eps.order
keps.convention == eps.convention
}

}