Skip to content

Commit 66ab297

Browse files
committed
another test run
Signed-off-by: wind57 <eugen.rabii@gmail.com>
1 parent 6048ac5 commit 66ab297

7 files changed

Lines changed: 106 additions & 57 deletions

File tree

spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/Commons.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ public static K3sContainer container() {
7272
return CONTAINER;
7373
}
7474

75-
public static void tagAndPushSpringCloudKubernetesImage(String imageName, K3sContainer container) {
75+
public static void tagAndPushSpringCloudKubernetesImageToLocalDockerRegistry(String imageNameWithoutTag,
76+
K3sContainer container) {
7677
try {
77-
String springCloudImage = "springcloud/" + imageName + ":" + pomVersion();
78-
tagAndPushImage(springCloudImage, container);
78+
String springCloudImageWithTag = "springcloud/" + imageNameWithoutTag + ":" + pomVersion();
79+
tagAndPushImage(springCloudImageWithTag, container);
7980
}
8081
catch (Exception e) {
8182
throw new RuntimeException(e);
@@ -85,25 +86,25 @@ public static void tagAndPushSpringCloudKubernetesImage(String imageName, K3sCon
8586
/**
8687
* tag image and push to local registry.
8788
*/
88-
public static void tagAndPushImage(String imageFromDeploymentWithTag, K3sContainer container) {
89+
public static void tagAndPushImage(String imageNameWithTag, K3sContainer container) {
8990

90-
if (imageAlreadyInK3s(container, imageFromDeploymentWithTag)) {
91+
if (imageAlreadyInK3s(container, imageNameWithTag)) {
9192
return;
9293
}
9394

9495
try {
95-
int lastColon = imageFromDeploymentWithTag.lastIndexOf(':');
96+
int lastColon = imageNameWithTag.lastIndexOf(':');
9697
if (lastColon < 0) {
97-
throw new IllegalArgumentException("image must include tag: " + imageFromDeploymentWithTag);
98+
throw new IllegalArgumentException("image must include tag: " + imageNameWithTag);
9899
}
99100

100-
String imageWithoutTag = imageFromDeploymentWithTag.substring(0, lastColon);
101-
String tag = imageFromDeploymentWithTag.substring(lastColon + 1);
101+
String imageWithoutTag = imageNameWithTag.substring(0, lastColon);
102+
String tag = imageNameWithTag.substring(lastColon + 1);
102103

103104
String targetRepository = "localhost:" + REGISTRY_PORT + "/" + imageWithoutTag;
104105
String targetImageWithTag = targetRepository + ":" + tag;
105106

106-
container.getDockerClient().tagImageCmd(imageFromDeploymentWithTag, targetRepository, tag).exec();
107+
container.getDockerClient().tagImageCmd(imageNameWithTag, targetRepository, tag).exec();
107108

108109
Awaitilities.awaitUntil(120, 1000, () -> {
109110
try {
@@ -217,7 +218,7 @@ private static String fullImageReference(String imageName, String imageVersion)
217218
}
218219

219220
/**
220-
* validates that the provided image does exist in the local docker registry.
221+
* validates that the provided image does exist in the local dcoker cache.
221222
*/
222223
public static void validateImage(String image, K3sContainer container) {
223224
try (ListImagesCmd listImagesCmd = container.getDockerClient().listImagesCmd()) {
@@ -319,14 +320,16 @@ private static boolean imageAlreadyInK3s(K3sContainer container, String imageWit
319320
try {
320321
String stdout = container.execInContainer("ctr", "-n", "k8s.io", "images", "list", "-q").getStdout();
321322

322-
boolean present = Arrays.stream(stdout.split("\\R")).map(String::trim).anyMatch(imageWithTag::equals);
323+
boolean present = Arrays.stream(stdout.split("\\R"))
324+
.map(String::trim)
325+
.anyMatch(line -> line.contains(imageWithTag));
323326

324327
if (present) {
325-
System.out.println("image : " + imageWithTag + " already in k3s, skipping");
328+
LOG.info("image : " + imageWithTag + " already in k3s, skipping");
326329
return true;
327330
}
328331

329-
System.out.println("image : " + imageWithTag + " not in k3s");
332+
LOG.info("image : " + imageWithTag + " not in k3s");
330333
return false;
331334
}
332335
catch (Exception e) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2013-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.integration.tests.commons;
18+
19+
import org.testcontainers.DockerClientFactory;
20+
import org.testcontainers.containers.Network;
21+
22+
/**
23+
* re-use implementation for a Network. It first checks if such a named network already
24+
* exists via docker client API and creates a new one if it does not.
25+
*
26+
* @author wind57
27+
*/
28+
final class FixedIdNetworkProvider {
29+
30+
private FixedIdNetworkProvider() {
31+
32+
}
33+
34+
static Network createReusableNetwork(String name) {
35+
var client = DockerClientFactory.instance().client();
36+
37+
String id = client.listNetworksCmd()
38+
.exec()
39+
.stream()
40+
.filter(network -> name.equals(network.getName()))
41+
.map(com.github.dockerjava.api.model.Network::getId)
42+
.findFirst()
43+
.orElseGet(() -> client.createNetworkCmd().withName(name).withCheckDuplicate(true).exec().getId());
44+
45+
return new org.testcontainers.containers.Network() {
46+
@Override
47+
public String getId() {
48+
return id;
49+
}
50+
51+
@Override
52+
public void close() {
53+
// intentionally no-op
54+
}
55+
};
56+
}
57+
58+
}

spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/FixedPortsK3sContainer.java

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616

1717
package org.springframework.cloud.kubernetes.integration.tests.commons;
1818

19-
import java.io.IOException;
20-
import java.io.UncheckedIOException;
21-
import java.nio.file.Files;
22-
import java.nio.file.Path;
2319
import java.util.Objects;
2420

2521
import com.github.dockerjava.api.model.Bind;
@@ -30,9 +26,8 @@
3026
import org.testcontainers.utility.DockerImageName;
3127
import org.testcontainers.utility.MountableFile;
3228

33-
import org.springframework.test.util.TestSocketUtils;
34-
3529
import static org.springframework.cloud.kubernetes.integration.tests.commons.Constants.TMP_IMAGES;
30+
import static org.springframework.cloud.kubernetes.integration.tests.commons.FixedIdNetworkProvider.createReusableNetwork;
3631

3732
/**
3833
* A K3sContainer, but with fixed port mappings. This is needed because of the nature of
@@ -50,11 +45,23 @@ public final class FixedPortsK3sContainer extends K3sContainer {
5045
/**
5146
* Port for the local running images registry.
5247
*/
53-
public static final int REGISTRY_PORT = TestSocketUtils.findAvailableTcpPort();
54-
55-
private static final Network NETWORK = Network.newNetwork();
48+
public static final int REGISTRY_PORT = 6000;
5649

57-
private static final Path REGISTRIES_YAML = createRegistriesYaml();
50+
/**
51+
* Small doc on how the set-up works. ( 5000 is just an example ) <pre>
52+
* - we start a local registry and expose it on localhost:<5000>
53+
* - from the host, we can push an image with:
54+
* docker push localhost:5000/image:tag
55+
* - the image is now stored in that local registry
56+
* - k3s later sees the image reference: localhost:5000/image:tag
57+
* - inside the k3s container, localhost would point to k3s itself, not to the registry
58+
* - because of the mirror entry in registries.yaml, when k3s/containerd sees
59+
* an image starting with localhost:5000, it actually uses the endpoint 'http://registry:5000'
60+
* - LocalRegistryContainer has withNetworkAliases("registry")
61+
* - this makes the registry reachable from the same Docker network via the hostname "registry"
62+
* </pre>
63+
*/
64+
private static final Network NETWORK = createReusableNetwork("spring-cloud-kubernetes-local-docker-registry");
5865

5966
private static final LocalRegistryContainer REGISTRY = new LocalRegistryContainer().configureFixedPorts()
6067
.withNetwork(NETWORK)
@@ -83,7 +90,8 @@ public final class FixedPortsK3sContainer extends K3sContainer {
8390
.withCommand(RANCHER_COMMAND)
8491
.withReuse(true)
8592
.withNetwork(NETWORK)
86-
.withCopyFileToContainer(MountableFile.forHostPath(REGISTRIES_YAML), "/etc/rancher/k3s/registries.yaml");
93+
.withCopyFileToContainer(MountableFile.forClasspathResource("registries.yaml"),
94+
"/etc/rancher/k3s/registries.yaml");
8795

8896
private FixedPortsK3sContainer(DockerImageName dockerImageName) {
8997
super(dockerImageName);
@@ -106,34 +114,6 @@ private FixedPortsK3sContainer addBinds() {
106114
return this;
107115
}
108116

109-
/**
110-
* Small doc on how the set-up works. ( 5000 is just an example ) <pre>
111-
* - we start a local registry and expose it on localhost:<5000>
112-
* - from the host, we can push an image with:
113-
* docker push localhost:5000/image:tag
114-
* - the image is now stored in that local registry
115-
* - k3s later sees the image reference: localhost:5000/image:tag
116-
* - inside the k3s container, localhost would point to k3s itself, not to the registry
117-
* - because of the mirror entry in registries.yaml, when k3s/containerd sees
118-
* an image starting with localhost:5000, it actually uses the endpoint 'http://registry:5000'
119-
* - LocalRegistryContainer has withNetworkAliases("registry")
120-
* - this makes the registry reachable from the same Docker network via the hostname "registry"
121-
* </pre>
122-
*/
123-
private static Path createRegistriesYaml() {
124-
try {
125-
Path file = Files.createTempFile("registries", ".yaml");
126-
// can't use text blocks here because of checkstyle
127-
String content = "mirrors:\n" + " \"localhost:%s\":\n" + " endpoint:\n"
128-
+ " - \"http://registry:5000\"\n";
129-
Files.writeString(file, content.formatted(REGISTRY_PORT));
130-
return file;
131-
}
132-
catch (IOException e) {
133-
throw new UncheckedIOException(e);
134-
}
135-
}
136-
137117
/**
138118
* official registry image with fixed port 5000.
139119
*/

spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/Images.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@
1919
import java.io.BufferedReader;
2020
import java.io.InputStreamReader;
2121

22+
import org.apache.commons.logging.Log;
23+
import org.apache.commons.logging.LogFactory;
2224
import org.testcontainers.k3s.K3sContainer;
2325

2426
/**
2527
* @author wind57
2628
*/
2729
public final class Images {
2830

31+
private static final Log LOG = LogFactory.getLog(Images.class);
32+
2933
private static final String BUSYBOX = "busybox";
3034

3135
private static final String BUSYBOX_TAR = BUSYBOX + ":" + busyboxVersion();
@@ -116,11 +120,11 @@ private static boolean imageAlreadyInK3s(K3sContainer container, String tarName)
116120
.getStdout()
117121
.contains(tarName);
118122
if (present) {
119-
System.out.println("image : " + tarName + " already in k3s, skipping");
123+
LOG.info("image : " + tarName + " already in k3s, skipping");
120124
return true;
121125
}
122126
else {
123-
System.out.println("image : " + tarName + " not in k3s");
127+
LOG.info("image : " + tarName + " not in k3s");
124128
return false;
125129
}
126130
}

spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/k3s/FabricClientIntegrationTestExtension.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void beforeAll(ExtensionContext context) {
6666
// 2. external image presence
6767
for (String image : scenario.withImages()) {
6868
Commons.validateImage(image, container);
69-
Commons.tagAndPushSpringCloudKubernetesImage(image, container);
69+
Commons.tagAndPushSpringCloudKubernetesImageToLocalDockerRegistry(image, container);
7070
}
7171

7272
// 3. deploy istio

spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/k3s/NativeClientIntegrationTestExtension.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void beforeAll(ExtensionContext context) throws Exception {
6060
// 2. external image presence
6161
for (String image : scenario.withImages()) {
6262
Commons.validateImage(image, container);
63-
Commons.tagAndPushSpringCloudKubernetesImage(image, container);
63+
Commons.tagAndPushSpringCloudKubernetesImageToLocalDockerRegistry(image, container);
6464
}
6565

6666
// 3. set-up RBAC.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
mirrors:
2+
"localhost:6000":
3+
endpoint:
4+
- "http://registry:5000"

0 commit comments

Comments
 (0)