Skip to content

Commit 568f733

Browse files
author
Antonio Macrì
committed
Sort generated env by name in kubernetes extension
1 parent 551a60d commit 568f733

File tree

4 files changed

+110
-18
lines changed

4 files changed

+110
-18
lines changed

extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/DevClusterHelper.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.nio.charset.StandardCharsets;
1717
import java.security.MessageDigest;
1818
import java.util.ArrayList;
19+
import java.util.Comparator;
1920
import java.util.List;
2021
import java.util.Map;
2122
import java.util.Optional;
@@ -111,16 +112,19 @@ public static List<DecoratorBuildItem> createDecorators(String clusterKind,
111112
image.ifPresent(
112113
i -> result.add(new DecoratorBuildItem(clusterKind, new ApplyContainerImageDecorator(name, i.getImage()))));
113114

114-
Stream.concat(config.convertToBuildItems().stream(), Targetable.filteredByTarget(envs, KUBERNETES))
115-
.forEach(e -> result.add(new DecoratorBuildItem(clusterKind,
116-
new AddEnvVarDecorator(ApplicationContainerDecorator.ANY, name, new EnvBuilder()
117-
.withName(EnvConverter.convertName(e.getName()))
118-
.withValue(e.getValue())
119-
.withSecret(e.getSecret())
120-
.withConfigmap(e.getConfigMap())
121-
.withField(e.getField())
122-
.withPrefix(e.getPrefix())
123-
.build()))));
115+
var stream = Stream.concat(config.convertToBuildItems().stream(), Targetable.filteredByTarget(envs, KUBERNETES));
116+
if (config.idempotent()) {
117+
stream = stream.sorted(Comparator.comparing(e -> EnvConverter.convertName(e.getName())));
118+
}
119+
stream.forEach(e -> result.add(new DecoratorBuildItem(clusterKind,
120+
new AddEnvVarDecorator(ApplicationContainerDecorator.ANY, name, new EnvBuilder()
121+
.withName(EnvConverter.convertName(e.getName()))
122+
.withValue(e.getValue())
123+
.withSecret(e.getSecret())
124+
.withConfigmap(e.getConfigMap())
125+
.withField(e.getField())
126+
.withPrefix(e.getPrefix())
127+
.build()))));
124128

125129
result.add(new DecoratorBuildItem(clusterKind, new ApplyImagePullPolicyDecorator(name, "IfNotPresent")));
126130

extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/VanillaKubernetesProcessor.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import static io.quarkus.kubernetes.spi.KubernetesDeploymentTargetBuildItem.VANILLA_KUBERNETES_PRIORITY;
1313

1414
import java.util.ArrayList;
15+
import java.util.Comparator;
1516
import java.util.List;
1617
import java.util.Map;
1718
import java.util.Optional;
@@ -70,7 +71,6 @@
7071
import io.quarkus.kubernetes.spi.KubernetesServiceAccountBuildItem;
7172
import io.quarkus.kubernetes.spi.Targetable;
7273

73-
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
7474
public class VanillaKubernetesProcessor {
7575

7676
@BuildStep
@@ -246,13 +246,16 @@ public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applic
246246
result.add(new DecoratorBuildItem(KUBERNETES, new ApplyImagePullPolicyDecorator(name, config.imagePullPolicy())));
247247
result.add(new DecoratorBuildItem(KUBERNETES, new AddSelectorToDeploymentDecorator(name)));
248248

249-
Stream.concat(config.convertToBuildItems().stream(), Targetable.filteredByTarget(envs, KUBERNETES))
250-
.forEach(e -> result.add(new DecoratorBuildItem(KUBERNETES,
251-
new AddEnvVarDecorator(ApplicationContainerDecorator.ANY, name,
252-
new EnvBuilder().withName(EnvConverter.convertName(e.getName())).withValue(e.getValue())
253-
.withSecret(e.getSecret()).withConfigmap(e.getConfigMap()).withField(e.getField())
254-
.withPrefix(e.getPrefix())
255-
.build()))));
249+
var stream = Stream.concat(config.convertToBuildItems().stream(), Targetable.filteredByTarget(envs, KUBERNETES));
250+
if (config.idempotent()) {
251+
stream = stream.sorted(Comparator.comparing(e -> EnvConverter.convertName(e.getName())));
252+
}
253+
stream.forEach(e -> result.add(new DecoratorBuildItem(KUBERNETES,
254+
new AddEnvVarDecorator(ApplicationContainerDecorator.ANY, name,
255+
new EnvBuilder().withName(EnvConverter.convertName(e.getName())).withValue(e.getValue())
256+
.withSecret(e.getSecret()).withConfigmap(e.getConfigMap()).withField(e.getField())
257+
.withPrefix(e.getPrefix())
258+
.build()))));
256259

257260
config.containerName().ifPresent(containerName -> result
258261
.add(new DecoratorBuildItem(KUBERNETES, new ChangeContainerNameDecorator(containerName))));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package io.quarkus.it.kubernetes;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.io.IOException;
6+
import java.nio.file.Path;
7+
import java.util.List;
8+
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.extension.RegisterExtension;
11+
12+
import io.fabric8.kubernetes.api.model.EnvVar;
13+
import io.fabric8.kubernetes.api.model.HasMetadata;
14+
import io.fabric8.kubernetes.api.model.apps.Deployment;
15+
import io.quarkus.test.ProdBuildResults;
16+
import io.quarkus.test.ProdModeTestResults;
17+
import io.quarkus.test.QuarkusProdModeTest;
18+
19+
public class KubernetesWithEnvSortedByNameTest {
20+
21+
@RegisterExtension
22+
static final QuarkusProdModeTest config = new QuarkusProdModeTest()
23+
.withApplicationRoot((jar) -> jar.addClasses(GreetingResource.class))
24+
.setApplicationName("env-sorted-by-name")
25+
.setApplicationVersion("0.1-SNAPSHOT")
26+
.withConfigurationResource("kubernetes-with-env-sorted-by-name.properties");
27+
28+
@ProdBuildResults
29+
private ProdModeTestResults prodModeTestResults;
30+
31+
@Test
32+
public void assertGeneratedResources() throws IOException {
33+
Path kubernetesDir = prodModeTestResults.getBuildDir().resolve("kubernetes");
34+
assertThat(kubernetesDir)
35+
.isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.json"))
36+
.isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.yml"));
37+
List<HasMetadata> kubernetesList = DeserializationUtil
38+
.deserializeAsList(kubernetesDir.resolve("kubernetes.yml"));
39+
assertThat(kubernetesList.get(0)).isInstanceOfSatisfying(Deployment.class, d -> {
40+
assertThat(d.getMetadata()).satisfies(m -> {
41+
assertThat(m.getName()).isEqualTo("env-sorted-by-name");
42+
});
43+
44+
assertThat(d.getSpec()).satisfies(deploymentSpec -> {
45+
assertThat(deploymentSpec.getTemplate()).satisfies(t -> {
46+
assertThat(t.getSpec()).satisfies(podSpec -> {
47+
assertThat(podSpec.getContainers()).singleElement().satisfies(container -> {
48+
assertThat(container.getEnv()).extracting(EnvVar::getName).containsExactly(
49+
"KUBERNETES_NAMESPACE",
50+
"AVAR",
51+
"BVAR",
52+
"CVAR",
53+
"DVAR",
54+
"EVAR",
55+
"FVAR",
56+
"GVAR",
57+
"HVAR_MP_REST_URL",
58+
"I_COMPLEX_VALUE",
59+
"LVAR");
60+
});
61+
});
62+
});
63+
});
64+
});
65+
}
66+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
quarkus.kubernetes.idempotent=true
2+
3+
quarkus.kubernetes.env.vars.avar=valuea
4+
quarkus.kubernetes.env.vars.cvar=valuec
5+
quarkus.kubernetes.env.vars.evar=valuee
6+
quarkus.kubernetes.env.vars.gvar=valueg
7+
quarkus.kubernetes.env.vars.lvar=valuel
8+
9+
quarkus.kubernetes.env.mapping.bvar.from-secret=db-secret
10+
quarkus.kubernetes.env.mapping.bvar.with-key=database.password
11+
12+
quarkus.kubernetes.env.mapping.dvar.from-configmap=my-configmap
13+
quarkus.kubernetes.env.mapping.dvar.with-key=keyName
14+
15+
quarkus.kubernetes.env.fields.fvar=metadata.name
16+
17+
quarkus.kubernetes.env.vars."i-complex.value"=true
18+
19+
quarkus.kubernetes.env.mapping.hvar/mp-rest/url.with-key=foo.url

0 commit comments

Comments
 (0)