Skip to content

Update kubernetes-client-api to 7.x #1691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 1, 2025
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>kubernetes-client-api</artifactId>
<version>7.2.0-255.v68c3eee00733</version>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down Expand Up @@ -157,7 +158,7 @@
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-server-mock</artifactId>
<version>6.10.0</version>
<version>7.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@
import hudson.slaves.ComputerLauncher;
import hudson.util.StreamTaskListener;
import io.fabric8.kubernetes.api.model.*;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.Watcher;
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;
import io.fabric8.kubernetes.client.utils.Utils;
import io.fabric8.mockwebserver.http.RecordedRequest;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand All @@ -48,10 +51,10 @@
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import jenkins.model.Jenkins;
import okhttp3.mockwebserver.RecordedRequest;
import org.csanchez.jenkins.plugins.kubernetes.*;
import org.csanchez.jenkins.plugins.kubernetes.PodTemplate;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExternalResource;
Expand All @@ -64,15 +67,25 @@ public class ReaperTest {
@Rule
public JenkinsRule j = new JenkinsRule();

@Rule
public KubernetesServer server = new KubernetesServer();

@Rule
public CapturingReaperListener listener = new CapturingReaperListener();

private KubernetesMockServer server;
private KubernetesClient client;

@Before
public void setUp() {
// TODO: remove when moving to junit 5
server = new KubernetesMockServer();
server.init(InetAddress.getLoopbackAddress(), 0);
client = server.createClient();
}

@After
public void tearDown() {
KubernetesClientProvider.invalidateAll();
server.destroy();
client.close();
}

@Test
Expand Down Expand Up @@ -561,7 +574,7 @@ public void testTerminateAgentOnContainerTerminated() throws IOException, Interr

@Test(timeout = 10_000)
public void testTerminateAgentOnPodFailed() throws IOException, InterruptedException {
System.out.println(server.getKubernetesMockServer().getPort());
System.out.println(server.getPort());
KubernetesCloud cloud = addCloud("k8s", "foo");
KubernetesSlave node = addNode(cloud, "node-123", "node");
Pod node123 = createPod(node);
Expand Down Expand Up @@ -716,7 +729,7 @@ private KubernetesSlave addNode(KubernetesCloud cld, String podName, String node

private KubernetesCloud addCloud(String name, String namespace) {
KubernetesCloud c = new KubernetesCloud(name);
c.setServerUrl(server.getClient().getMasterUrl().toString());
c.setServerUrl(client.getMasterUrl().toString());
c.setNamespace(namespace);
c.setSkipTlsVerify(true);
j.jenkins.clouds.add(c);
Expand All @@ -730,10 +743,10 @@ private KubernetesCloud addCloud(String name, String namespace) {
* @throws InterruptedException interrupted exception
*/
private CapturedRequests kubeClientRequests() throws InterruptedException {
int count = server.getKubernetesMockServer().getRequestCount();
int count = server.getRequestCount();
List<RecordedRequest> requests = new LinkedList<>();
while (count-- > 0) {
RecordedRequest rr = server.getKubernetesMockServer().takeRequest(1, TimeUnit.SECONDS);
RecordedRequest rr = server.takeRequest(1, TimeUnit.SECONDS);
if (rr != null) {
requests.add(rr);
}
Expand All @@ -750,7 +763,7 @@ private CapturedRequests kubeClientRequests() throws InterruptedException {
private CapturedRequests waitForKubeClientRequests(int count) throws InterruptedException {
List<RecordedRequest> requests = new LinkedList<>();
while (count-- > 0) {
requests.add(server.getKubernetesMockServer().takeRequest());
requests.add(server.takeRequest());
}
return new CapturedRequests(requests);
}
Expand All @@ -764,7 +777,7 @@ private CapturedRequests waitForKubeClientRequests(int count) throws Interrupted
private CapturedRequests waitForKubeClientRequests(String path) throws InterruptedException {
List<RecordedRequest> requests = new LinkedList<>();
while (true) {
RecordedRequest rr = server.getKubernetesMockServer().takeRequest();
RecordedRequest rr = server.takeRequest();
requests.add(rr);
if (rr.getPath().equals(path)) {
return new CapturedRequests(requests);
Expand Down Expand Up @@ -878,21 +891,21 @@ public String toString() {
private static WatchEvent outdatedEvent() {
return new WatchEventBuilder()
.withType(Watcher.Action.ERROR.name())
.withNewStatusObject()
.withCode(HttpURLConnection.HTTP_GONE)
.withMessage(
"410: The event in requested index is outdated and cleared (the requested history has been cleared [3/1]) [2]")
.endStatusObject()
.withType(Watcher.Action.ERROR.name())
.withObject(
new StatusBuilder()
.withCode(HttpURLConnection.HTTP_GONE)
.withMessage(
"410: The event in requested index is outdated and cleared (the requested history has been cleared [3/1]) [2]"))
.build();
}

private static WatchEvent errorEvent() {
return new WatchEventBuilder()
.withType(Watcher.Action.ERROR.name())
.withNewStatusObject()
.withCode(HttpURLConnection.HTTP_INTERNAL_ERROR)
.withMessage("500: Internal error")
.endStatusObject()
.withObject(new StatusBuilder()
.withCode(HttpURLConnection.HTTP_INTERNAL_ERROR)
.withMessage("500: Internal error"))
.build();
}

Expand Down