Skip to content

Commit

Permalink
Introduce customizable control-plane timeout for ApiServer
Browse files Browse the repository at this point in the history
Fixes #291
  • Loading branch information
dajudge committed May 21, 2024
1 parent cb53240 commit 98401de
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/main/java/com/dajudge/kindcontainer/ApiServerContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.testcontainers.shaded.org.bouncycastle.asn1.x509.GeneralName;
import org.testcontainers.utility.DockerImageName;

import java.time.Duration;
import java.util.*;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -51,6 +52,7 @@ public class ApiServerContainer<T extends ApiServerContainer<T>> extends Kuberne
new GeneralName(GeneralName.iPAddress, "127.0.0.1")
));
private EtcdContainer etcd;
private Duration controlPlaneReadyTimeout = Duration.ofMinutes(5);

/**
* Constructs a new <code>ApiServerContainer</code> with the latest supported Kubernetes version.
Expand Down Expand Up @@ -160,7 +162,7 @@ private void waitForApiServer() {
.pollInterval(ofMillis(100))
.pollDelay(ZERO)
.ignoreExceptions()
.forever()
.timeout(controlPlaneReadyTimeout)
.until(() -> null != TinyK8sClient.fromKubeconfig(getKubeconfig()).v1().nodes().list());
}

Expand Down Expand Up @@ -204,4 +206,15 @@ public void stop() {
super.stop();
etcd.stop();
}

/**
* Sets the timeout applied when waiting for the Kubernetes control plane to become ready.
*
* @param timeout the timeout
* @return <code>this</code>
*/
public T withControlPlaneReadyTimeout(final Duration timeout) {
this.controlPlaneReadyTimeout = timeout;
return self();
}
}
43 changes: 43 additions & 0 deletions src/test/java/com/dajudge/kindcontainer/ApiServerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.dajudge.kindcontainer;

import org.junit.jupiter.api.Test;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

import static com.dajudge.kindcontainer.KubernetesVersionEnum.latest;
import static java.time.Duration.ofSeconds;
import static java.util.concurrent.TimeUnit.MINUTES;
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class ApiServerTest {
@Test
public void configurableTimeout() {
final AtomicBoolean containerCompleted = new AtomicBoolean();
final AtomicReference<Exception> containerFailed = new AtomicReference<>();
final ApiServerContainer<?> apiServer = new ApiServerContainer<>(latest(ApiServerContainerVersion.class).withImage("nginx"))
.withCreateContainerCmdModifier(cmd -> cmd.withEntrypoint().withCmd())
.withCommand("nginx")
.withControlPlaneReadyTimeout(ofSeconds(1));
final Thread containerThread = new Thread(() -> {
try {
apiServer.start();
apiServer.stop();
} catch (final Exception e) {
containerFailed.set(e);
} finally {
containerCompleted.set(true);
}
});
try {
containerThread.start();

await().timeout(5, MINUTES).until(containerCompleted::get);
assertNotNull(containerFailed.get());
} finally {
apiServer.stop();
}
}

}

0 comments on commit 98401de

Please sign in to comment.