Skip to content

Commit

Permalink
feat: use local lookup policy for ImageStreams
Browse files Browse the repository at this point in the history
  • Loading branch information
iocanel committed Apr 12, 2023
1 parent a237517 commit 4f6d9d8
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 28 deletions.
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);
});
});
});

}
}

0 comments on commit 4f6d9d8

Please sign in to comment.