Skip to content

Commit b4b772d

Browse files
committed
feat: deploy Docker registry on Kubernetes
1 parent 3e7451a commit b4b772d

File tree

1 file changed

+53
-12
lines changed

1 file changed

+53
-12
lines changed

it/src/main/java/org/eclipse/jkube/integrationtests/jupiter/api/extension/RegistryExtension.java

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.eclipse.jkube.integrationtests.cli.CliUtils;
2020
import org.eclipse.jkube.integrationtests.jupiter.api.DockerRegistry;
2121
import org.eclipse.jkube.integrationtests.jupiter.api.DockerRegistryHost;
22-
import org.hamcrest.Matchers;
2322
import org.junit.jupiter.api.extension.AfterAllCallback;
2423
import org.junit.jupiter.api.extension.BeforeAllCallback;
2524
import org.junit.jupiter.api.extension.BeforeEachCallback;
@@ -30,35 +29,40 @@
3029
import java.io.File;
3130
import java.io.IOException;
3231
import java.lang.reflect.Field;
32+
import java.util.stream.Stream;
3333

3434
import static org.eclipse.jkube.integrationtests.cli.CliUtils.isWindows;
3535
import static org.hamcrest.MatcherAssert.assertThat;
36+
import static org.hamcrest.Matchers.notNullValue;
3637

3738
public class RegistryExtension implements HasKubernetesClient, BeforeAllCallback, BeforeEachCallback, AfterAllCallback {
3839

3940
private static final Logger log = LoggerFactory.getLogger(RegistryExtension.class);
4041

42+
private static final String K8S_RESOURCE_NAME = "docker-registry";
43+
4144
@Override
4245
public void beforeAll(ExtensionContext context) throws Exception {
4346
final var annotation = context.getRequiredTestClass().getAnnotation(DockerRegistry.class);
4447
CliUtils.runCommand("docker rm -f " + getName(annotation));
4548
log.debug(() -> "Starting Docker Registry Extension");
46-
final CliUtils.CliResult dockerRegistry;
49+
final RegistryInfo dockerRegistry;
4750
if (isWindows()) {
4851
dockerRegistry = startWindowsDockerRegistry(annotation);
4952
} else {
50-
dockerRegistry = startRegularDockerRegistry(annotation);
53+
dockerRegistry = startKubernetesDockerRegistry(context);
5154
}
52-
assertThat(dockerRegistry.getOutput(), dockerRegistry.getExitCode(), Matchers.equalTo(0));
55+
assertThat(dockerRegistry.assertionContext, dockerRegistry.host, notNullValue());
56+
getStore(context).put(RegistryInfo.class, dockerRegistry);
5357
log.debug(() -> "Docker Registry started successfully");
5458
}
5559

5660
@Override
5761
public void beforeEach(ExtensionContext context) throws Exception {
58-
final var annotation = context.getRequiredTestClass().getAnnotation(DockerRegistry.class);
62+
final var registryInfo = getStore(context).get(RegistryInfo.class, RegistryInfo.class);
5963
for (Field f : context.getRequiredTestClass().getDeclaredFields()) {
6064
if (f.isAnnotationPresent(DockerRegistryHost.class) && f.getType() == String.class) {
61-
setFieldValue(f, context.getRequiredTestInstance(), getDockerHost() + ":" + annotation.port());
65+
setFieldValue(f, context.getRequiredTestInstance(), registryInfo.host + ":" + registryInfo.port);
6266
}
6367
}
6468
}
@@ -67,22 +71,47 @@ public void beforeEach(ExtensionContext context) throws Exception {
6771
public void afterAll(ExtensionContext context) throws Exception {
6872
log.debug(() -> "Closing Docker Registry");
6973
CliUtils.runCommand("docker stop " + getName(context.getRequiredTestClass().getAnnotation(DockerRegistry.class)));
74+
if (!isWindows()) {
75+
deleteKubernetesDockerRegistry(context);
76+
}
77+
}
78+
79+
private void deleteKubernetesDockerRegistry(ExtensionContext context) {
80+
final var client = getClient(context);
81+
Stream.of(
82+
client.pods().withName(K8S_RESOURCE_NAME),
83+
client.services().withName(K8S_RESOURCE_NAME)
84+
).forEach(r -> r.withGracePeriod(0L).delete());
7085
}
7186

72-
private static CliUtils.CliResult startRegularDockerRegistry(DockerRegistry dockerRegistry) throws IOException, InterruptedException {
73-
log.debug(() -> "Starting standard Docker Registry");
74-
return CliUtils.runCommand("docker run --rm -d -p " + dockerRegistry.port() +":5000 --name " +
75-
getName(dockerRegistry) + " registry:2");
87+
private RegistryInfo startKubernetesDockerRegistry(ExtensionContext context) throws IOException, InterruptedException {
88+
deleteKubernetesDockerRegistry(context);
89+
final var client = getClient(context);
90+
final var ip = CliUtils.runCommand("minikube ip").getOutput().trim();
91+
final var dockerRegistry = client.run().withName(K8S_RESOURCE_NAME).withImage("registry:2")
92+
.withNewRunConfig()
93+
.addToLabels("app", K8S_RESOURCE_NAME).addToLabels("group", "jkube-it").done();
94+
final var service = client.services().resource(new ServiceBuilder()
95+
.withNewMetadata().withName(K8S_RESOURCE_NAME)
96+
.addToLabels("app", K8S_RESOURCE_NAME).addToLabels("group", "jkube-it").endMetadata()
97+
.withNewSpec().withType("NodePort").withSelector(dockerRegistry.getMetadata().getLabels())
98+
.addNewPort().withName("http").withPort(5000).withTargetPort(new IntOrString(5000)).endPort().endSpec()
99+
.build()).serverSideApply();
100+
return new RegistryInfo(ip, service.getSpec().getPorts().get(0).getNodePort(), null);
76101
}
77102

78-
private static CliUtils.CliResult startWindowsDockerRegistry(DockerRegistry dockerRegistry) throws IOException, InterruptedException {
103+
private static RegistryInfo startWindowsDockerRegistry(DockerRegistry dockerRegistry) throws IOException, InterruptedException {
79104
log.debug(() -> "Starting Windows specific Docker Registry");
80105
final var registry = new File("C:\\registry");
81106
if (!registry.exists() && !registry.mkdirs()) {
82107
throw new IllegalStateException("Directory C:\\registry cannot be created");
83108
}
84-
return CliUtils.runCommand("docker run --rm -d -p " + dockerRegistry.port() +":5000 --name " +
109+
final var result = CliUtils.runCommand("docker run --rm -d -p " + dockerRegistry.port() +":5000 --name " +
85110
getName(dockerRegistry) + " -v C:\\registry:C:\\registry marcnuri/docker-registry-windows:ltsc2022");
111+
if (result.getExitCode() != 0) {
112+
return new RegistryInfo(null, -1, result.getOutput());
113+
}
114+
return new RegistryInfo(getDockerHost(), dockerRegistry.port(), result.getOutput());
86115
}
87116

88117
private static String getName(DockerRegistry dockerRegistry) {
@@ -98,4 +127,16 @@ private static String getDockerHost() {
98127
.replaceAll(":\\d+$", "");
99128
}
100129
}
130+
131+
private static final class RegistryInfo {
132+
private final String host;
133+
private final int port;
134+
private final String assertionContext;
135+
136+
public RegistryInfo(String host, int port, String assertionContext) {
137+
this.host = host;
138+
this.port = port;
139+
this.assertionContext = assertionContext;
140+
}
141+
}
101142
}

0 commit comments

Comments
 (0)