Skip to content

Commit

Permalink
Resolve correct container tag when application version is unset
Browse files Browse the repository at this point in the history
  • Loading branch information
Sgitario committed Jan 25, 2023
1 parent bea259f commit 56c604b
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
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);
});
});
});
});
});
}
}

0 comments on commit 56c604b

Please sign in to comment.