Skip to content

Commit

Permalink
clean (#1765)
Browse files Browse the repository at this point in the history
  • Loading branch information
wind57 authored Oct 16, 2024
1 parent 8d02d1a commit 7e824ba
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,29 @@
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

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.Bind;
import com.github.dockerjava.api.model.HostConfig;
import com.github.dockerjava.api.model.Image;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.Assertions;
import org.testcontainers.containers.Container;
import org.testcontainers.k3s.K3sContainer;
import org.testcontainers.utility.DockerImageName;

import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;

import static org.awaitility.Awaitility.await;
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;

/**
* A few commons things that can be re-used across clients. This is meant to be used for
Expand All @@ -61,44 +61,10 @@ public final class Commons {

private static final Log LOG = LogFactory.getLog(Commons.class);

/**
* this path is generated by the pipeline of github actions.
*/
private static final String TMP_IMAGES = "/tmp/docker/images";

private Commons() {
throw new AssertionError("No instance provided");
}

private static final String KUBERNETES_VERSION_FILE = "META-INF/springcloudkubernetes-version.txt";

/**
* Rancher version to use for test-containers.
*/
public static final String RANCHER = "rancher/k3s:v1.28.8-k3s1";

/**
* Command to use when starting rancher. Without "server" option, traefik is not
* installed
*/
public static final String RANCHER_COMMAND = "server";

/**
* Test containers exposed ports.
*/
public static final int[] EXPOSED_PORTS = new int[] { 80, 6443, 8080, 8888, 9092 };

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

private static final K3sContainer CONTAINER = new FixedPortsK3sContainer(DockerImageName.parse(Commons.RANCHER))
.configureFixedPorts()
.addBinds()
.withCommand(Commons.RANCHER_COMMAND)
.withReuse(true);

public static K3sContainer container() {
return CONTAINER;
}
Expand Down Expand Up @@ -180,7 +146,7 @@ public static void loadImage(String image, String tag, String tarName, K3sContai
* either get the tar from '/tmp/docker/images', or pull the image.
*/
public static void load(K3sContainer container, String tarName, String imageNameForDownload, String imageVersion) {
File dockerImagesRootDir = Paths.get(Commons.TMP_IMAGES).toFile();
File dockerImagesRootDir = Paths.get(TMP_IMAGES).toFile();
if (dockerImagesRootDir.exists() && dockerImagesRootDir.isDirectory()) {
File[] tars = dockerImagesRootDir.listFiles();
if (tars != null && tars.length > 0) {
Expand All @@ -189,8 +155,7 @@ public static void load(K3sContainer container, String tarName, String imageName
.filter(x -> x.contains(tarName))
.findFirst();
if (found.isPresent()) {
LOG.info("running in github actions, will load from : " + Commons.TMP_IMAGES + " tar : "
+ found.get());
LOG.info("running in github actions, will load from : " + TMP_IMAGES + " tar : " + found.get());
Commons.loadImageFromPath(found.get(), container);
return;
}
Expand Down Expand Up @@ -321,35 +286,4 @@ public static void waitForLogStatement(String message, K3sContainer k3sContainer

}

/**
* A K3sContainer, but with fixed port mappings. This is needed because of the nature
* of some integration tests.
*
* @author wind57
*/
private static final class FixedPortsK3sContainer extends K3sContainer {

private FixedPortsK3sContainer(DockerImageName dockerImageName) {
super(dockerImageName);
}

private FixedPortsK3sContainer configureFixedPorts() {
for (int port : Commons.EXPOSED_PORTS) {
super.addFixedExposedPort(port, port);
}
return this;
}

private FixedPortsK3sContainer addBinds() {
super.withCreateContainerCmdModifier(cmd -> {
HostConfig hostConfig = Objects.requireNonNull(cmd.getHostConfig());
hostConfig.withBinds(Bind.parse(TEMP_FOLDER + ":" + TEMP_FOLDER),
Bind.parse(TMP_IMAGES + ":" + TMP_IMAGES));
});

return this;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2013-2024 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 java.io.File;

/**
* @author wind57
*/
final class Constants {

private Constants() {

}

/**
* this path is generated by the pipeline of github actions.
*/
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.
*/
static final String KUBERNETES_VERSION_FILE = "META-INF/springcloudkubernetes-version.txt";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2013-2024 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 java.util.Objects;

import com.github.dockerjava.api.model.Bind;
import com.github.dockerjava.api.model.HostConfig;
import org.testcontainers.k3s.K3sContainer;
import org.testcontainers.utility.DockerImageName;

import static org.springframework.cloud.kubernetes.integration.tests.commons.Constants.TEMP_FOLDER;
import static org.springframework.cloud.kubernetes.integration.tests.commons.Constants.TMP_IMAGES;

/**
* A K3sContainer, but with fixed port mappings. This is needed because of the nature of
* some integration tests.
*
* @author wind57
*/
final class FixedPortsK3sContainer extends K3sContainer {

/**
* Test containers exposed ports.
*/
private static final int[] EXPOSED_PORTS = new int[] { 80, 6443, 8080, 8888, 9092 };

/**
* Rancher version to use for test-containers.
*/
private static final String RANCHER_VERSION = "rancher/k3s:v1.28.8-k3s1";

/**
* Command to use when starting rancher. Without "server" option, traefik is not
* installed
*/
private static final String RANCHER_COMMAND = "server";

static final K3sContainer CONTAINER = new FixedPortsK3sContainer(DockerImageName.parse(RANCHER_VERSION))
.configureFixedPorts()
.addBinds()
.withCommand(RANCHER_COMMAND)
.withReuse(true);

FixedPortsK3sContainer(DockerImageName dockerImageName) {
super(dockerImageName);
}

FixedPortsK3sContainer configureFixedPorts() {
for (int port : EXPOSED_PORTS) {
super.addFixedExposedPort(port, port);
}
return this;
}

FixedPortsK3sContainer addBinds() {
super.withCreateContainerCmdModifier(cmd -> {
HostConfig hostConfig = Objects.requireNonNull(cmd.getHostConfig());
hostConfig.withBinds(Bind.parse(TEMP_FOLDER + ":" + TEMP_FOLDER),
Bind.parse(TMP_IMAGES + ":" + TMP_IMAGES));
});

return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ public final class Util {

private static final Log LOG = LogFactory.getLog(Util.class);

/** Image we get {@code istioctl} from in order to install Istio. */
public static final String ISTIO_ISTIOCTL = "istio/istioctl";

private final K3sContainer container;

private final KubernetesClient client;
Expand Down

0 comments on commit 7e824ba

Please sign in to comment.