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

Resolve correct version when application version is unset #30591

Merged
merged 1 commit into from
Jan 25, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.quarkus.container.image.deployment;

import static io.quarkus.container.image.deployment.util.EnablementUtil.*;
import static io.quarkus.container.spi.ImageReference.DEFAULT_TAG;
import static io.quarkus.deployment.builditem.ApplicationInfoBuildItem.UNSET_VALUE;

import java.util.Collections;
import java.util.Optional;
Expand Down Expand Up @@ -91,7 +93,11 @@ public void publishImageInfo(ApplicationInfoBuildItem app,
+ effectiveGroup + "' and name '" + effectiveName + "' is invalid");
}

final String effectiveTag = containerImageConfig.tag.orElse(app.getVersion());
String effectiveTag = containerImageConfig.tag.orElse(app.getVersion());
if (effectiveTag.equals(UNSET_VALUE)) {
effectiveTag = DEFAULT_TAG;
}

if (!ImageReference.isValidTag(effectiveTag)) {
throw new IllegalArgumentException("The supplied container-image tag '" + effectiveTag + "' is invalid");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
*/
public class ImageReference {

public static final String DEFAULT_TAG = "latest";
private static final String DOCKER_HUB_REGISTRY = "registry-1.docker.io";
private static final String DEFAULT_TAG = "latest";
private static final String LIBRARY_REPOSITORY_PREFIX = "library/";

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.kubernetes.deployment;

import static io.quarkus.container.spi.ImageReference.DEFAULT_TAG;
import static io.quarkus.deployment.builditem.ApplicationInfoBuildItem.UNSET_VALUE;
import static io.quarkus.kubernetes.deployment.Constants.DEPLOY;
import static io.quarkus.kubernetes.deployment.Constants.DEPLOYMENT_TARGET;
import static io.quarkus.kubernetes.deployment.Constants.DOCKER;
Expand Down Expand Up @@ -142,7 +144,9 @@ public static Map<String, Object> toMap(PlatformConfiguration... platformConfigu
Arrays.stream(platformConfigurations).forEach(p -> {
p.getPartOf().ifPresent(g -> quarkusPrefixed.put(DEKORATE_PREFIX + p.getConfigName() + ".part-of", g));
p.getName().ifPresent(n -> quarkusPrefixed.put(DEKORATE_PREFIX + p.getConfigName() + ".name", n));
p.getVersion().ifPresent(v -> quarkusPrefixed.put(DEKORATE_PREFIX + p.getConfigName() + ".version", v));
p.getVersion()
.map(v -> v.equals(UNSET_VALUE) ? DEFAULT_TAG : v)
.ifPresent(v -> quarkusPrefixed.put(DEKORATE_PREFIX + p.getConfigName() + ".version", v));
});

Map<String, Object> unPrefixed = StreamSupport.stream(config.getPropertyNames().spliterator(), false)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.quarkus.it.kubernetes;

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

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.apps.Deployment;
import io.quarkus.deployment.builditem.ApplicationInfoBuildItem;
import io.quarkus.test.ProdBuildResults;
import io.quarkus.test.ProdModeTestResults;
import io.quarkus.test.QuarkusProdModeTest;

public class KubernetesWithDefaultApplicationVersionTest {

private static final String APP_NAME = "kubernetes-with-default-application-version";
private static final String EXPECTED_VERSION = "latest";

@RegisterExtension
static final QuarkusProdModeTest config = new QuarkusProdModeTest()
.withApplicationRoot((jar) -> jar.addClasses(GreetingResource.class))
.setApplicationName(APP_NAME)
// Simulates when there is no application version
.setApplicationVersion(ApplicationInfoBuildItem.UNSET_VALUE);

@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"))
.satisfies(p -> assertThat(p.toFile().listFiles()).hasSize(2));
List<HasMetadata> kubernetesList = DeserializationUtil
.deserializeAsList(kubernetesDir.resolve("kubernetes.yml"));

assertThat(kubernetesList.get(0)).isInstanceOfSatisfying(Deployment.class, d -> {
assertThat(d.getMetadata()).satisfies(m -> {
assertThat(m.getName()).isEqualTo(APP_NAME);
assertThat(m.getLabels()).contains(entry("app.kubernetes.io/name", APP_NAME),
entry("app.kubernetes.io/version", EXPECTED_VERSION));
});

assertThat(d.getSpec()).satisfies(deploymentSpec -> {
assertThat(deploymentSpec.getTemplate()).satisfies(t -> {
assertThat(t.getSpec()).satisfies(podSpec -> {
assertThat(podSpec.getContainers()).singleElement().satisfies(container -> {
// then, we should use `latest` and not `<< unset >>` which gives an exception.
assertThat(container.getImage()).endsWith(APP_NAME + ":" + EXPECTED_VERSION);
});
});
});
});
});
}
}