Skip to content

Commit

Permalink
Remove selector field if it's empty from manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
The-Huginn committed Feb 21, 2024
1 parent fbd6abc commit 10a1706
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ private void initCronJobResourceWithDefaults(CronJobBuilder builder) {
// - match labels
if (jobTemplateSpec.buildSelector().getMatchLabels() == null) {
jobTemplateSpec.editSelector().withMatchLabels(new HashMap<>()).endSelector();
} else {
jobTemplateSpec.withSelector(null);
}
// - termination grace period seconds
if (jobTemplateSpec.buildTemplate().getSpec().getTerminationGracePeriodSeconds() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ public void assertGeneratedResources() throws IOException {
});
});
});
assertThat(jobSpec.getSelector()).satisfies(ls -> {
assertThat(ls.getMatchLabels()).containsEntry("app.kubernetes.io/name", APP_NAME);
});
assertThat(jobSpec.getSelector()).isEqualTo(null);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

package io.quarkus.it.kubernetes;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.batch.v1.CronJob;
import io.quarkus.builder.Version;
import io.quarkus.maven.dependency.Dependency;
import io.quarkus.test.ProdBuildResults;
import io.quarkus.test.ProdModeTestResults;
import io.quarkus.test.QuarkusProdModeTest;

public class KubernetesWithCronJobResourceWithoutSelectorTest {

static final String APP_NAME = "kubernetes-with-cronjob-resource-without-selector";

@RegisterExtension
static final QuarkusProdModeTest config = new QuarkusProdModeTest()
.withApplicationRoot((jar) -> jar.addClasses(GreetingResource.class))
.setApplicationName(APP_NAME)
.setApplicationVersion("0.1-SNAPSHOT")
.withConfigurationResource(APP_NAME + ".properties")
.setLogFileName("k8s.log")
.setForcedDependencies(List.of(Dependency.of("io.quarkus", "quarkus-kubernetes", Version.getVersion())));

@ProdBuildResults
private ProdModeTestResults prodModeTestResults;

@Test
public void assertGeneratedResources() throws IOException {
final Path kubernetesDir = prodModeTestResults.getBuildDir().resolve("kubernetes");
assertThat(kubernetesDir)
.isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.json"))
.isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.yml"));
List<HasMetadata> kubernetesList = DeserializationUtil
.deserializeAsList(kubernetesDir.resolve("kubernetes.yml"));

assertThat(kubernetesList).filteredOn(i -> i instanceof CronJob).singleElement().satisfies(i -> {
assertThat(i).isInstanceOfSatisfying(CronJob.class, s -> {
assertThat(s.getMetadata()).satisfies(m -> {
assertThat(m.getName()).isEqualTo(APP_NAME);
});

assertThat(s.getSpec().getJobTemplate().getSpec()).satisfies(jobSpec -> {
assertThat(jobSpec.getSelector()).isEqualTo(null);
});
});
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ public void assertGeneratedResources() throws IOException {
});
});
});
assertThat(jobSpec.getSelector()).satisfies(ls -> {
assertThat(ls.getMatchLabels()).containsEntry("app.kubernetes.io/name", APP_NAME);
});
assertThat(jobSpec.getSelector()).isEqualTo(null);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ public void assertGeneratedResources() throws IOException {
});
});
});
assertThat(jobSpec.getSelector()).satisfies(ls -> {
assertThat(ls.getMatchLabels()).containsEntry("app.kubernetes.io/name", APP_NAME);
});
assertThat(jobSpec.getSelector()).isEqualTo(null);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
quarkus.kubernetes.deployment-kind=CronJob
quarkus.kubernetes.arguments=A,B
quarkus.kubernetes.cron-job.schedule=0 0 0 0 *
quarkus.kubernetes.cron-job.parallelism=10
quarkus.kubernetes.cron-job.restart-policy=Never
quarkus.kubernetes.add-version-to-label-selectors=false
quarkus.kubernetes.add-name-to-label-selectors=false

0 comments on commit 10a1706

Please sign in to comment.