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

Use local lookup policy for generated image stream resources #32538

Merged
merged 2 commits into from
Apr 13, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ private ImageUtil() {
* Create an image from the individual parts.
*
* @param registry The registry.
* @param repository The repository.
* @param repository The group.
* @param name The name.
* @param tag The tag.
* @return The image.
*/
public static String getImage(Optional<String> registry, String repository, String name, String tag) {
public static String getImage(Optional<String> registry, String group, String name, String tag) {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("Docker image name cannot be null!");
}
Expand All @@ -29,12 +29,44 @@ public static String getImage(Optional<String> registry, String repository, Stri
}
StringBuilder sb = new StringBuilder();
registry.ifPresent(r -> sb.append(r).append(SLASH));
sb.append(repository).append(SLASH);
sb.append(group).append(SLASH);

sb.append(name).append(COLN).append(tag);
return sb.toString();
}

/**
* Return the image registry.
*
* @param image The docker image.
* @return The image registry.
*/
public static Optional<String> getRegistry(String image) {
String[] parts = image.split(SLASH);
if (parts.length <= 2) {
//name:tag
//group/name:tag
return Optional.empty();
}
return Optional.ofNullable(parts[0]);
}

/**
* Return the image group.
*
* @param image The docker image.
* @return The image group.
*/
public static String getGroup(String image) {
String[] parts = image.split(SLASH);
if (parts.length <= 2) {
//name:tag
//group/name:tag
return parts[0];
}
return parts[1];
}

/**
* Return the docker image repository.
*
Expand Down Expand Up @@ -72,7 +104,7 @@ public static String getName(String image) {
}

if (tagged.contains(COLN)) {
return tagged.substring(0, tagged.indexOf(COLN));
return tagged.substring(0, tagged.lastIndexOf(COLN));
}
return tagged;
}
Expand All @@ -85,7 +117,7 @@ public static String getName(String image) {
*/
public static String getTag(String image) {
if (image.contains(COLN)) {
return image.substring(image.indexOf(COLN) + 1);
return image.substring(image.lastIndexOf(COLN) + 1);
}
return image;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public final class Constants {
static final String S2I = "s2i";
static final String DEFAULT_S2I_IMAGE_NAME = "s2i-java"; //refers to the Dekorate default image.

static final String OPENSHIFT_INTERNAL_REGISTRY = "image-registry.openshift-image-registry.svc:5000";

static final String KNATIVE = "knative";
static final String KNATIVE_SERVICE = "Service";
static final String KNATIVE_SERVICE_GROUP = "serving.knative.dev";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.quarkus.kubernetes.deployment;

import io.dekorate.kubernetes.decorator.NamedResourceDecorator;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.openshift.api.model.ImageStreamSpecFluent;

public class EnableImageStreamLocalLookupPolicyDecorator extends NamedResourceDecorator<ImageStreamSpecFluent<?>> {

public EnableImageStreamLocalLookupPolicyDecorator() {
super("ImageStream", ANY);
}

public EnableImageStreamLocalLookupPolicyDecorator(String name) {
super("ImageStream", name);
}

@Override
public void andThenVisit(ImageStreamSpecFluent<?> spec, ObjectMeta meta) {
spec.withNewLookupPolicy()
.withLocal()
.endLookupPolicy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static io.quarkus.kubernetes.deployment.Constants.KNATIVE;
import static io.quarkus.kubernetes.deployment.Constants.KUBERNETES;
import static io.quarkus.kubernetes.deployment.Constants.MINIKUBE;
import static io.quarkus.kubernetes.deployment.Constants.OPENSHIFT;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -30,7 +29,6 @@
import io.fabric8.kubernetes.api.model.KubernetesList;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.batch.v1.Job;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.dsl.Resource;
Expand Down Expand Up @@ -160,7 +158,6 @@ private DeploymentTargetEntry determineDeploymentTarget(
ContainerImageConfig containerImageConfig) {
final DeploymentTargetEntry selectedTarget;

boolean checkForNamespaceGroupAlignment = false;
List<String> userSpecifiedDeploymentTargets = KubernetesConfigUtil.getExplictilyDeploymentTargets();
if (userSpecifiedDeploymentTargets.isEmpty()) {
selectedTarget = targets.getEntriesSortedByPriority().get(0);
Expand All @@ -184,22 +181,6 @@ private DeploymentTargetEntry determineDeploymentTarget(
}
}

if (OPENSHIFT.equals(selectedTarget.getName())) {
// We should ensure that we have image group and namespace alignment we are not using deployment triggers via DeploymentConfig.
if (!targets.getEntriesSortedByPriority().get(0).getKind().equals("DeploymentConfig")) {
checkForNamespaceGroupAlignment = true;
}
}

//This might also be applicable in other scenarios too (e.g. Knative on Openshift), so we might need to make it slightly more generic.
if (checkForNamespaceGroupAlignment) {
Config config = Config.autoConfigure(null);
if (config.getNamespace() != null && !config.getNamespace().equals(containerImageInfo.getGroup())) {
log.warn("An openshift deployment was requested, but the container image group:" + containerImageInfo.getGroup()
+ " is not aligned with the currently selected project:" + config.getNamespace() + "."
+ "it is strongly advised to align them, or else the image might not be reachable.");
}
}
return selectedTarget;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@
public class OpenshiftProcessor {

private static final int OPENSHIFT_PRIORITY = DEFAULT_PRIORITY;
private static final String OPENSHIFT_INTERNAL_REGISTRY = "image-registry.openshift-image-registry.svc:5000";
private static final String DOCKERIO_REGISTRY = "docker.io";
private static final String OPENSHIFT_V3_APP = "app";
private static final String ANY = null;

@BuildStep
public void checkOpenshift(ApplicationInfoBuildItem applicationInfo, Capabilities capabilities, OpenshiftConfig config,
Expand Down Expand Up @@ -113,10 +113,7 @@ public void populateInternalRegistry(OpenshiftConfig openshiftConfig, ContainerI
DeploymentResourceKind deploymentResourceKind = openshiftConfig.getDeploymentResourceKind(capabilities);
if (deploymentResourceKind != DeploymentResourceKind.DeploymentConfig) {
if (openshiftConfig.isOpenshiftBuildEnabled(containerImageConfig, capabilities)) {
// Images stored in internal openshift registry use the following pattern:
// 'image-registry.openshift-image-registry.svc:5000/{{ project name}}/{{ image name }}: {{image version }}.
// So, we need warn users if group does not match currently selected project.
containerImageRegistry.produce(new FallbackContainerImageRegistryBuildItem(OPENSHIFT_INTERNAL_REGISTRY));
//Don't need fallback namespace, we use local lookup instead.
} else {
containerImageRegistry.produce(new FallbackContainerImageRegistryBuildItem(DOCKERIO_REGISTRY));
}
Expand Down Expand Up @@ -298,6 +295,9 @@ public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applic
.build())));
});

// Enalbe local lookup policy for all image streams
result.add(new DecoratorBuildItem(OPENSHIFT, new EnableImageStreamLocalLookupPolicyDecorator()));

// Handle custom s2i builder images
baseImage.map(BaseImageInfoBuildItem::getImage).ifPresent(builderImage -> {
String builderImageName = ImageUtil.getName(builderImage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ public void assertGeneratedResources() throws IOException {
assertThat(podSpec.getContainers()).singleElement().satisfies(container -> {
assertThat(container.getImage())
.isEqualTo(
"image-registry.openshift-image-registry.svc:5000/testme/" + CUSTOM_NAME
+ ":0.1-SNAPSHOT");
"testme/" + CUSTOM_NAME + ":0.1-SNAPSHOT");
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.openshift.api.model.ImageStream;
import io.quarkus.builder.Version;
import io.quarkus.maven.dependency.Dependency;
import io.quarkus.test.ProdBuildResults;
Expand Down Expand Up @@ -76,13 +77,22 @@ public void assertGeneratedResources() throws IOException {
assertThat(t.getSpec()).satisfies(podSpec -> {
assertThat(podSpec.getContainers()).singleElement().satisfies(container -> {
assertThat(container.getImage())
.isEqualTo(
"image-registry.openshift-image-registry.svc:5000/testme/openshift-with-deployment-resource:0.1-SNAPSHOT");
.isEqualTo("testme/openshift-with-deployment-resource:0.1-SNAPSHOT");
});
});
});
});
});
});

assertThat(kubernetesList).filteredOn(r -> r instanceof ImageStream && r.getMetadata().getName().equals(NAME))
.singleElement().satisfies(r -> {
assertThat(r).isInstanceOfSatisfying(ImageStream.class, i -> {
assertThat(i.getSpec()).satisfies(spec -> {
assertThat(spec.getLookupPolicy().getLocal()).isEqualTo(true);
});
});
});

}
}