Skip to content
Open
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 @@ -9,6 +9,7 @@
import com.github.dockerjava.api.command.InspectContainerResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.Network;
import org.testcontainers.shaded.org.awaitility.Awaitility;
import org.testcontainers.shaded.org.bouncycastle.asn1.x509.GeneralName;
import org.testcontainers.utility.DockerImageName;
Expand Down Expand Up @@ -53,6 +54,7 @@ public class ApiServerContainer<T extends ApiServerContainer<T>> extends Kuberne
));
private EtcdContainer etcd;
private Duration controlPlaneReadyTimeout = Duration.ofMinutes(5);
private Network network = Network.newNetwork();

/**
* Constructs a new <code>ApiServerContainer</code> with the latest supported Kubernetes version.
Expand Down Expand Up @@ -93,7 +95,8 @@ public ApiServerContainer(final KubernetesImageSpec<ApiServerContainerVersion> i
.withCopyAsciiToContainer(apiServerCa.getCaKeyStore().getCertificatePem(), API_SERVER_CA)
.withCopyAsciiToContainer(etcdClientKeyPair.getCertificatePem(), ETCD_CLIENT_CERT)
.withCopyAsciiToContainer(etcdClientKeyPair.getPrivateKeyPem(), ETCD_CLIENT_KEY)
.withCopyAsciiToContainer(etcdCa.getCaKeyStore().getCertificatePem(), ETCD_CLIENT_CA);
.withCopyAsciiToContainer(etcdCa.getCaKeyStore().getCertificatePem(), ETCD_CLIENT_CA)
.withNetwork(network);
}

@Override
Expand All @@ -119,7 +122,7 @@ private void createContainerCmdModifier(final CreateContainerCmd cmd) {
put("etcd-cafile", ETCD_CLIENT_CA);
put("etcd-certfile", ETCD_CLIENT_CERT);
put("etcd-keyfile", ETCD_CLIENT_KEY);
put("etcd-servers", "https://localhost:2379");
put("etcd-servers", "https://etcd:2379");
put("service-account-key-file", API_SERVER_PUBKEY);
put("service-account-signing-key-file", API_SERVER_KEY);
put("service-account-issuer", "https://kubernetes.default.svc.cluster.local");
Expand All @@ -143,7 +146,7 @@ private void createContainerCmdModifier(final CreateContainerCmd cmd) {

@Override
protected void containerIsStarting(final InspectContainerResponse containerInfo) {
etcd = new EtcdContainer(etcdImage, etcdCa, containerInfo.getId());
etcd = new EtcdContainer(etcdImage, etcdCa, network);
etcd.start();
waitForApiServer();
waitForDefaultNamespace();
Expand Down Expand Up @@ -205,6 +208,7 @@ public T withEtcdImage(final DockerImageName image) {
public void stop() {
super.stop();
etcd.stop();
network.close();
}

/**
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/dajudge/kindcontainer/EtcdContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.dajudge.kindcontainer.pki.CertAuthority;
import com.dajudge.kindcontainer.pki.KeyStoreWrapper;

import org.testcontainers.containers.Network;
import org.testcontainers.shaded.org.bouncycastle.asn1.x509.GeneralName;
import org.testcontainers.utility.DockerImageName;

Expand All @@ -22,15 +24,15 @@ class EtcdContainer extends BaseGenericContainer<EtcdContainer> {
private static final String STARTUP_SIGNAL_PATH = DOCKER_BASE_PATH + "/startup";
private static final String[] CMD = buildCommand();

EtcdContainer(final DockerImageName image, final CertAuthority etcdCa, final String targetContainerId) {
EtcdContainer(final DockerImageName image, final CertAuthority etcdCa, final Network network) {
super(image);
final KeyStoreWrapper etcdKeypair = etcdCa.newKeyPair(
"CN=etcd",
singletonList(new GeneralName(GeneralName.dNSName, "localhost"))
singletonList(new GeneralName(GeneralName.dNSName, "etcd"))
);
this
.withNetworkAliases("etcd")
.withNetworkMode("container:" + targetContainerId)
.withNetwork(network)
.withEnv("STARTUP_SIGNAL", STARTUP_SIGNAL_PATH)
.withEnv("SERVER_CERT_PATH", SERVER_CERT_PATH)
.withEnv("SERVER_KEY_PATH", SERVER_KEY_PATH)
Expand All @@ -44,7 +46,7 @@ class EtcdContainer extends BaseGenericContainer<EtcdContainer> {

private static String[] buildCommand() {
final Map<String, String> params = new HashMap<String, String>() {{
put("advertise-client-urls", "https://localhost:2379");
put("advertise-client-urls", "https://etcd:2379");
put("cert-file", SERVER_CERT_PATH);
put("key-file", SERVER_KEY_PATH);
put("trusted-ca-file", SERVER_CACERTS_PATH);
Expand All @@ -56,7 +58,7 @@ private static String[] buildCommand() {
put("data-dir", "/var/lib/etcd");
put("initial-advertise-peer-urls", "https://localhost:2380");
put("initial-cluster", "control-plane=https://localhost:2380");
put("listen-client-urls", "https://localhost:2379");
put("listen-client-urls", "https://0.0.0.0:2379");
put("listen-metrics-urls", "http://localhost:2381");
put("listen-peer-urls", "https://localhost:2380");
put("name", "control-plane");
Expand Down