Skip to content

Commit 7dbf46f

Browse files
committed
feat: do not output kube version label in selector
Fixes #823 Signed-off-by: Chris Laprun <[email protected]>
1 parent 7ea5831 commit 7dbf46f

File tree

6 files changed

+74
-6
lines changed

6 files changed

+74
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.quarkiverse.operatorsdk.bundle.deployment;
2+
3+
import java.util.Map;
4+
5+
import io.smallrye.config.PropertiesConfigSource;
6+
import io.smallrye.config.SmallRyeConfigBuilder;
7+
import io.smallrye.config.SmallRyeConfigBuilderCustomizer;
8+
9+
/**
10+
* Overrides the default value for the Kubernetes extension-provided {@code add-version-to-label-selectors} when the bundle
11+
* generator is used. See <a href="https://github.com/quarkiverse/quarkus-operator-sdk/issues/823">this issue</a> for more
12+
* details.
13+
*/
14+
public class KubernetesLabelConfigOverrider implements SmallRyeConfigBuilderCustomizer {
15+
@Override
16+
public void configBuilder(SmallRyeConfigBuilder builder) {
17+
// not sure what value for the priority should be used since they seem pretty random
18+
builder.withSources(new PropertiesConfigSource(Map.of("quarkus.kubernetes.add-version-to-label-selectors", "false"),
19+
"KubernetesLabelConfigOverrider", 50));
20+
}
21+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
io.quarkiverse.operatorsdk.bundle.deployment.KubernetesLabelConfigOverrider

bundle-generator/deployment/src/test/java/io/quarkiverse/operatorsdk/bundle/DefaultBundleWhenNoCsvMetadataTest.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package io.quarkiverse.operatorsdk.bundle;
22

33
import static io.quarkiverse.operatorsdk.bundle.Utils.BUNDLE;
4+
import static io.quarkiverse.operatorsdk.bundle.Utils.getCSVFor;
5+
import static org.junit.jupiter.api.Assertions.*;
46

7+
import java.io.IOException;
58
import java.nio.file.Files;
69

7-
import org.junit.jupiter.api.Assertions;
810
import org.junit.jupiter.api.Test;
911
import org.junit.jupiter.api.extension.RegisterExtension;
1012

@@ -26,9 +28,15 @@ public class DefaultBundleWhenNoCsvMetadataTest {
2628
private ProdModeTestResults prodModeTestResults;
2729

2830
@Test
29-
public void shouldWriteBundleEvenWhenCsvMetadataIsNotUsed() {
30-
Assertions.assertTrue(
31-
Files.exists(prodModeTestResults.getBuildDir().resolve(BUNDLE).resolve("reconciler-with-no-csv-metadata")));
31+
public void shouldWriteBundleEvenWhenCsvMetadataIsNotUsed() throws IOException {
32+
final var name = "reconciler-with-no-csv-metadata";
33+
final var bundle = prodModeTestResults.getBuildDir().resolve(BUNDLE);
34+
assertTrue(Files.exists(bundle.resolve(name)));
35+
final var csv = getCSVFor(bundle, name);
36+
final var deployment = csv.getSpec().getInstall().getSpec().getDeployments().get(0);
37+
assertEquals(name, deployment.getName());
38+
// by default, we shouldn't output the version label in the selector match labels as the default controlling this should be overridden by KubernetesLabelConfigOverrider
39+
assertNull(deployment.getSpec().getSelector().getMatchLabels().get("app.kubernetes.io/version"));
3240
}
3341

3442
}

bundle-generator/deployment/src/test/java/io/quarkiverse/operatorsdk/bundle/MultipleOperatorsBundleTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void shouldWriteBundleForTheOperators() throws IOException {
4747
var csv = getCSVFor(bundle, "first-operator");
4848
assertEquals(FirstReconciler.VERSION, csv.getSpec().getVersion());
4949
assertEquals(FirstReconciler.REPLACES, csv.getSpec().getReplaces());
50-
var bundleMeta = getAnnotationFor(bundle, "first-operator");
50+
var bundleMeta = getAnnotationsFor(bundle, "first-operator");
5151
assertEquals(BUNDLE_PACKAGE, bundleMeta.getAnnotations().get("operators.operatorframework.io.bundle.package.v1"));
5252

5353
checkBundleFor(bundle, "second-operator", Second.class);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.quarkiverse.operatorsdk.bundle;
2+
3+
import static io.quarkiverse.operatorsdk.bundle.Utils.BUNDLE;
4+
import static io.quarkiverse.operatorsdk.bundle.Utils.getCSVFor;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertNotNull;
7+
8+
import java.io.IOException;
9+
10+
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.api.extension.RegisterExtension;
12+
13+
import io.quarkiverse.operatorsdk.bundle.sources.First;
14+
import io.quarkiverse.operatorsdk.bundle.sources.ReconcilerWithNoCsvMetadata;
15+
import io.quarkus.test.ProdBuildResults;
16+
import io.quarkus.test.ProdModeTestResults;
17+
import io.quarkus.test.QuarkusProdModeTest;
18+
19+
public class OutputLabelSelectorsIfRequestedTest {
20+
@RegisterExtension
21+
static final QuarkusProdModeTest config = new QuarkusProdModeTest()
22+
.setApplicationName("output-kube-labels")
23+
.overrideConfigKey("quarkus.kubernetes.add-version-to-label-selectors", "true")
24+
.withApplicationRoot((jar) -> jar
25+
.addClasses(First.class, ReconcilerWithNoCsvMetadata.class));
26+
27+
@ProdBuildResults
28+
private ProdModeTestResults prodModeTestResults;
29+
30+
@Test
31+
public void shouldHaveVersionLabelWhenRequested() throws IOException {
32+
final var name = "output-kube-labels";
33+
final var csv = getCSVFor(prodModeTestResults.getBuildDir().resolve(BUNDLE), name);
34+
final var deployment = csv.getSpec().getInstall().getSpec().getDeployments().get(0);
35+
assertEquals(name, deployment.getName());
36+
assertNotNull(deployment.getSpec().getSelector().getMatchLabels().get("app.kubernetes.io/version"));
37+
}
38+
}

bundle-generator/deployment/src/test/java/io/quarkiverse/operatorsdk/bundle/Utils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static ClusterServiceVersion getCSVFor(Path bundle, String operatorName) throws
4141
return Serialization.unmarshal(csvAsString, ClusterServiceVersion.class);
4242
}
4343

44-
static ObjectMeta getAnnotationFor(Path bundle, String operatorName) throws IOException {
44+
static ObjectMeta getAnnotationsFor(Path bundle, String operatorName) throws IOException {
4545
final var annotationPath = bundle.resolve(operatorName).resolve("metadata").resolve("annotations.yaml");
4646
final var annotationsaAsString = Files.readString(annotationPath);
4747
return Serialization.unmarshal(annotationsaAsString, ObjectMeta.class);

0 commit comments

Comments
 (0)