Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ static void manifests(Phase phase, Fabric8ClientKubernetesFixture fabric8Kuberne

if (phase.equals(Phase.CREATE)) {
fabric8KubernetesFixture.createAndWait(namespace, configMap, null);
fabric8KubernetesFixture.createAndWait(namespace, null, deployment, service, true);
fabric8KubernetesFixture.createAndWait(namespace, deployment, service, true);
}
else {
fabric8KubernetesFixture.deleteAndWait(namespace, configMap, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,9 @@
package org.springframework.cloud.kubernetes.integration.tests.commons;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
Expand All @@ -33,7 +28,6 @@

import com.github.dockerjava.api.command.ListImagesCmd;
import com.github.dockerjava.api.command.PullImageCmd;
import com.github.dockerjava.api.command.SaveImageCmd;
import com.github.dockerjava.api.model.Image;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -50,9 +44,9 @@
import org.springframework.web.reactive.function.client.WebClient;

import static org.springframework.cloud.kubernetes.integration.tests.commons.Constants.KUBERNETES_VERSION_FILE;
import static org.springframework.cloud.kubernetes.integration.tests.commons.Constants.TEMP_FOLDER;
import static org.springframework.cloud.kubernetes.integration.tests.commons.Constants.TMP_IMAGES;
import static org.springframework.cloud.kubernetes.integration.tests.commons.FixedPortsK3sContainer.CONTAINER;
import static org.springframework.cloud.kubernetes.integration.tests.commons.FixedPortsK3sContainer.REGISTRY_PORT;

/**
* A few commons things that can be re-used across clients. This is meant to be used for
Expand All @@ -78,66 +72,65 @@ public static K3sContainer container() {
return CONTAINER;
}

public static void loadSpringCloudKubernetesImage(String project, K3sContainer container) {
public static void tagAndPushSpringCloudKubernetesImageToLocalDockerRegistry(String imageNameWithoutTag,
K3sContainer container) {
try {
loadImage("springcloud/" + project, pomVersion(), project, container);
String springCloudImageWithTag = "springcloud/" + imageNameWithoutTag + ":" + pomVersion();
tagAndPushImage(springCloudImageWithTag, container);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}

/**
* create a tar, copy it in the running k3s and load this tar as an image.
* tag image and push to local registry.
*/
public static void loadImage(String image, String tag, String tarName, K3sContainer container) {
public static void tagAndPushImage(String imageNameWithTag, K3sContainer container) {

if (imageAlreadyInK3s(container, tarName)) {
if (imageAlreadyInK3s(container, imageNameWithTag)) {
return;
}

// save image
try (SaveImageCmd saveImageCmd = container.getDockerClient().saveImageCmd(image)) {
InputStream imageStream = saveImageCmd.withTag(tag).exec();

Path imagePath = Paths.get(TEMP_FOLDER + "/" + tarName + ".tar");
try {
Files.copy(imageStream, imagePath, StandardCopyOption.REPLACE_EXISTING);
}
catch (IOException e) {
throw new UncheckedIOException(e);
try {
int lastColon = imageNameWithTag.lastIndexOf(':');
if (lastColon < 0) {
throw new IllegalArgumentException("image must include tag: " + imageNameWithTag);
}
// import image with ctr. this works because TEMP_FOLDER is mounted in the
// container

String imageWithoutTag = imageNameWithTag.substring(0, lastColon);
String tag = imageNameWithTag.substring(lastColon + 1);

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

container.getDockerClient().tagImageCmd(imageNameWithTag, targetRepository, tag).exec();

Awaitilities.awaitUntil(120, 1000, () -> {
Container.ExecResult result;
try {
result = container.execInContainer("ctr", "i", "import",
Constants.TEMP_FOLDER + "/" + tarName + ".tar");
container.getDockerClient().pushImageCmd(targetImageWithTag).start().awaitCompletion();
return true;
}
catch (Exception e) {
throw new RuntimeException(e);
LOG.info("failed to push image " + targetImageWithTag + " to local registry", e);
return false;
}
boolean noErrors = result.getStderr() == null || result.getStderr().isEmpty();
if (!noErrors) {
LOG.info("error is : " + result.getStderr());
}
return noErrors;
});
}

catch (Exception e) {
throw new RuntimeException(e);
}
}

/**
* Ensures a common external test image is available inside K3s/containerd.
* It first checks whether the image is already present in K3s.
* If not, it tries to load it as a tar under '/tmp/docker/images'.
* If no matching tar is found, it pulls the image directly inside K3s
* using 'ctr images pull'.
* This is meant for shared test images such as busybox, wiremock, kafka, etc.
* Ensures a common external test image is available inside K3s/containerd. It first
* checks whether the image is already present in K3s. If not, it tries to load it as
* a tar under '/tmp/docker/images'. If no matching tar is found, it pulls the image
* directly inside K3s using 'ctr images pull'. This is meant for shared test images
* such as busybox, wiremock, kafka, etc.
*/
public static void loadOrPullCommonTestImages(K3sContainer container, String tarName,
String imageNameForDownload, String imageVersion) {
public static void loadOrPullCommonTestImages(K3sContainer container, String tarName, String imageNameForDownload,
String imageVersion) {

if (imageAlreadyInK3s(container, tarName)) {
return;
Expand Down Expand Up @@ -225,7 +218,7 @@ private static String fullImageReference(String imageName, String imageVersion)
}

/**
* validates that the provided image does exist in the local docker registry.
* validates that the provided image does exist in the local dcoker cache.
*/
public static void validateImage(String image, K3sContainer container) {
try (ListImagesCmd listImagesCmd = container.getDockerClient().listImagesCmd()) {
Expand All @@ -239,15 +232,14 @@ public static void validateImage(String image, K3sContainer container) {
}
}

public static void pullImage(String image, String tag, String tarName, K3sContainer container)
throws InterruptedException {
public static void pullImage(String imageFromDeployment, K3sContainer container) throws InterruptedException {

if (imageAlreadyInK3s(container, tarName)) {
if (imageAlreadyInK3s(container, imageFromDeployment)) {
return;
}

try (PullImageCmd pullImageCmd = container.getDockerClient().pullImageCmd(image)) {
pullImageCmd.withTag(tag).start().awaitCompletion();
try (PullImageCmd pullImageCmd = container.getDockerClient().pullImageCmd(imageFromDeployment)) {
pullImageCmd.start().awaitCompletion();
}
}

Expand Down Expand Up @@ -324,24 +316,21 @@ private static void loadImageFromPath(String tarName, K3sContainer container) {
});
}

private static boolean imageAlreadyInK3s(K3sContainer container, String tarName) {
private static boolean imageAlreadyInK3s(K3sContainer container, String imageWithTag) {
try {
String stdout = container.execInContainer("ctr", "-n", "k8s.io", "images", "list", "-q").getStdout();

if (tarName == null) {
return false;
}
boolean present = Arrays.stream(stdout.split("\\R"))
.map(String::trim)
.anyMatch(line -> line.contains(imageWithTag));

try {
boolean present = container.execInContainer("sh", "-c", "ctr images list | grep " + tarName)
.getStdout()
.contains(tarName);
if (present) {
System.out.println("image : " + tarName + " already in k3s, skipping");
LOG.info("image : " + imageWithTag + " already in k3s, skipping");
return true;
}
else {
System.out.println("image : " + tarName + " not in k3s");
return false;
}

LOG.info("image : " + imageWithTag + " not in k3s");
return false;
}
catch (Exception e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

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

import java.io.File;

/**
* @author wind57
*/
Expand All @@ -32,11 +30,6 @@ private Constants() {
*/
static final String TMP_IMAGES = "/tmp/docker/images";

/**
* Temporary folder where to load images.
*/
static final String TEMP_FOLDER = new File(System.getProperty("java.io.tmpdir")).getAbsolutePath();

/**
* where is the version situated.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2013-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.Network;

/**
* re-use implementation for a Network. It first checks if such a named network already
* exists via docker client API and creates a new one if it does not.
*
* @author wind57
*/
final class FixedIdNetworkProvider {

private FixedIdNetworkProvider() {

}

static Network createReusableNetwork(String name) {
var client = DockerClientFactory.instance().client();

String id = client.listNetworksCmd()
.exec()
.stream()
.filter(network -> name.equals(network.getName()))
.map(com.github.dockerjava.api.model.Network::getId)
.findFirst()
.orElseGet(() -> client.createNetworkCmd().withName(name).withCheckDuplicate(true).exec().getId());

return new org.testcontainers.containers.Network() {
@Override
public String getId() {
return id;
}

@Override
public void close() {
// intentionally no-op
}
};
}

}
Loading
Loading