Skip to content

Commit 792141b

Browse files
committed
The kube resolver should set the endpoints when the subsets json structure is not present.
Motivation: The kube resolver sets the list of endpoints (pods) from the subsets member of the endpoints json object. When the service does not have any pods, the subsets member is absent and causes an NPE downstream in the resolver assuming the endpoints list is never null. Changes: Always set the list of endpoints to consistently populate the list.
1 parent bbdfd4b commit 792141b

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/main/java/io/vertx/serviceresolver/kube/impl/KubeServiceState.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ void handleEndpoints(JsonObject item) {
9898
String name = metadata.getString("name");
9999
if (this.name.equals(name)) {
100100
JsonArray subsets = item.getJsonArray("subsets");
101+
EndpointBuilder<B, SocketAddress> builder = endpointsBuilder;
101102
if (subsets != null) {
102-
EndpointBuilder<B, SocketAddress> builder = endpointsBuilder;
103103
for (int j = 0;j < subsets.size();j++) {
104104
List<String> podIps = new ArrayList<>();
105105
JsonObject subset = subsets.getJsonObject(j);
@@ -119,8 +119,8 @@ void handleEndpoints(JsonObject item) {
119119
}
120120
}
121121
}
122-
this.endpoints.set(builder.build());
123122
}
123+
this.endpoints.set(builder.build());
124124
}
125125
}
126126
}

src/test/java/io/vertx/tests/kube/KubeServiceResolverMockTest.java

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
import java.net.InetAddress;
1717
import java.util.*;
1818

19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.fail;
21+
1922
public class KubeServiceResolverMockTest extends KubeServiceResolverTestBase {
2023

2124
@Rule

src/test/java/io/vertx/tests/kube/KubeServiceResolverTestBase.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99
import org.junit.Ignore;
1010
import org.junit.Test;
1111

12+
import java.util.Collections;
1213
import java.util.List;
1314

15+
import static org.junit.Assert.assertEquals;
16+
import static org.junit.Assert.fail;
17+
1418
public abstract class KubeServiceResolverTestBase extends ServiceResolverTestBase {
1519

1620
protected KubernetesMocking kubernetesMocking;
@@ -104,7 +108,19 @@ public void testDeletePod(TestContext should) throws Exception {
104108
}
105109
}
106110

107-
/*
111+
@Test
112+
public void testEmptyPods() throws Exception {
113+
String serviceName = "svc";
114+
kubernetesMocking.buildAndRegisterKubernetesService(serviceName, kubernetesMocking.defaultNamespace(), KubeOp.CREATE, Collections.emptyList());
115+
try {
116+
get(ServiceAddress.of("svc"));
117+
fail();
118+
} catch (IllegalStateException e) {
119+
assertEquals("No results for svc", e.getMessage());
120+
}
121+
}
122+
123+
/*
108124
@Test
109125
public void testDispose(TestContext should) throws Exception {
110126
Handler<HttpServerRequest> server = req -> {

0 commit comments

Comments
 (0)