Skip to content

Commit 7c251f9

Browse files
TYsewynRyan Baxter
authored and
Ryan Baxter
committed
Added the instance id to the KubernetesServiceInstance (spring-cloud#279)
* Added the instance id to the `KubernetesServiceInstance` * Added integration test to verify that the instanceId is not empty * Fixed wrong import order * Fixed wrong service name in IT * Changed assertions since we want to verify the list of service instances * Updated assertion to validate a collection instead of an array * Use URI.create() instead of try-catch
1 parent 4ab2616 commit 7c251f9

File tree

5 files changed

+56
-19
lines changed

5 files changed

+56
-19
lines changed

spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClient.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,13 @@ public List<ServiceInstance> getInstances(String serviceId) {
149149

150150
List<EndpointAddress> addresses = s.getAddresses();
151151
for (EndpointAddress endpointAddress : addresses) {
152+
String instanceId = null;
153+
if (endpointAddress.getTargetRef() != null) {
154+
instanceId = endpointAddress.getTargetRef().getUid();
155+
}
152156
final EndpointPort endpointPort = s.getPorts().stream().findFirst()
153157
.orElseThrow(IllegalStateException::new);
154-
instances.add(new KubernetesServiceInstance(serviceId,
158+
instances.add(new KubernetesServiceInstance(instanceId, serviceId,
155159
endpointAddress, endpointPort, endpointMetadata,
156160
this.isServicePortSecureResolver
157161
.resolve(new DefaultIsServicePortSecureResolver.Input(

spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesServiceInstance.java

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.cloud.kubernetes.discovery;
1818

1919
import java.net.URI;
20-
import java.net.URISyntaxException;
2120
import java.util.Map;
2221

2322
import io.fabric8.kubernetes.api.model.EndpointAddress;
@@ -38,6 +37,8 @@ public class KubernetesServiceInstance implements ServiceInstance {
3837

3938
private static final String COLN = ":";
4039

40+
private final String instanceId;
41+
4142
private final String serviceId;
4243

4344
private final EndpointAddress endpointAddress;
@@ -48,15 +49,36 @@ public class KubernetesServiceInstance implements ServiceInstance {
4849

4950
private final Map<String, String> metadata;
5051

52+
/**
53+
* @param serviceId the id of the service.
54+
* @param endpointAddress the address where the service instance can be found.
55+
* @param endpointPort the port on which the service is running.
56+
* @param metadata a map containing metadata.
57+
* @param secure indicates whether or not the connection needs to be secure.
58+
* @deprecated - use other constructor
59+
*/
60+
@Deprecated
5161
public KubernetesServiceInstance(String serviceId, EndpointAddress endpointAddress,
5262
EndpointPort endpointPort, Map<String, String> metadata, Boolean secure) {
63+
this(null, serviceId, endpointAddress, endpointPort, metadata, secure);
64+
}
65+
66+
public KubernetesServiceInstance(String instanceId, String serviceId,
67+
EndpointAddress endpointAddress, EndpointPort endpointPort,
68+
Map<String, String> metadata, Boolean secure) {
69+
this.instanceId = instanceId;
5370
this.serviceId = serviceId;
5471
this.endpointAddress = endpointAddress;
5572
this.endpointPort = endpointPort;
5673
this.metadata = metadata;
5774
this.secure = secure;
5875
}
5976

77+
@Override
78+
public String getInstanceId() {
79+
return this.instanceId;
80+
}
81+
6082
@Override
6183
public String getServiceId() {
6284
return this.serviceId;
@@ -80,25 +102,17 @@ public boolean isSecure() {
80102
@Override
81103
public URI getUri() {
82104
StringBuilder sb = new StringBuilder();
83-
84-
if (isSecure()) {
85-
sb.append(HTTPS_PREFIX);
86-
}
87-
else {
88-
sb.append(HTTP_PREFIX);
89-
}
90-
91-
sb.append(getHost()).append(COLN).append(getPort());
92-
try {
93-
return new URI(sb.toString());
94-
}
95-
catch (URISyntaxException e) {
96-
throw new RuntimeException(e);
97-
}
105+
sb.append(getScheme()).append(getHost()).append(COLN).append(getPort());
106+
return URI.create(sb.toString());
98107
}
99108

100109
public Map<String, String> getMetadata() {
101110
return this.metadata;
102111
}
103112

113+
@Override
114+
public String getScheme() {
115+
return isSecure() ? HTTPS_PREFIX : HTTP_PREFIX;
116+
}
117+
104118
}

spring-cloud-kubernetes-discovery/src/test/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClientTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public void getInstancesShouldBeAbleToHandleEndpointsSingleAddress() {
6060
.andReturn(200,
6161
new EndpointsBuilder().withNewMetadata().withName("endpoint")
6262
.endMetadata().addNewSubset().addNewAddress()
63-
.withIp("ip1").endAddress().addNewPort("http", 80, "TCP")
63+
.withIp("ip1").withNewTargetRef().withUid("uid").endTargetRef().endAddress()
64+
.addNewPort("http", 80, "TCP")
6465
.endSubset().build())
6566
.once();
6667

@@ -81,7 +82,8 @@ public void getInstancesShouldBeAbleToHandleEndpointsSingleAddress() {
8182
final List<ServiceInstance> instances = discoveryClient.getInstances("endpoint");
8283

8384
assertThat(instances).hasSize(1)
84-
.filteredOn(s -> s.getHost().equals("ip1") && !s.isSecure()).hasSize(1);
85+
.filteredOn(s -> s.getHost().equals("ip1") && !s.isSecure()).hasSize(1)
86+
.filteredOn(s -> s.getInstanceId().equals("uid")).hasSize(1);
8587
}
8688

8789
@Test

spring-cloud-kubernetes-integration-tests/discovery/discovery-client/src/main/java/org/springframework/cloud/kubernetes/it/DiscoveryClientApplication.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
import org.springframework.beans.factory.annotation.Autowired;
2222
import org.springframework.boot.SpringApplication;
2323
import org.springframework.boot.autoconfigure.SpringBootApplication;
24+
import org.springframework.cloud.client.ServiceInstance;
2425
import org.springframework.cloud.client.discovery.DiscoveryClient;
2526
import org.springframework.web.bind.annotation.GetMapping;
27+
import org.springframework.web.bind.annotation.PathVariable;
2628
import org.springframework.web.bind.annotation.RestController;
2729

2830
@SpringBootApplication
@@ -41,4 +43,9 @@ public List<String> services() {
4143
return this.discoveryClient.getServices();
4244
}
4345

46+
@GetMapping("/services/{service}/instances")
47+
public List<ServiceInstance> instances(@PathVariable("service") String service) {
48+
return this.discoveryClient.getInstances(service);
49+
}
50+
4451
}

spring-cloud-kubernetes-integration-tests/discovery/tests/src/test/java/org/springframework/cloud/kubernetes/it/ServicesIT.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.junit.runner.RunWith;
2424

2525
import static io.restassured.RestAssured.given;
26+
import static org.hamcrest.Matchers.hasItems;
27+
import static org.hamcrest.Matchers.hasSize;
2628

2729
@RequiresKubernetes
2830
@RunWith(Arquillian.class)
@@ -44,4 +46,12 @@ protected boolean evalSubstringOf(String s) {
4446
});
4547
}
4648

49+
@Test
50+
public void testInstancesEndpoint() {
51+
given().baseUri(String.format("http://%s:%d", HOST, PORT))
52+
.get("services/discovery-service-a/instances").then().statusCode(200)
53+
.body("instanceId", hasSize(1))
54+
.body("serviceId", hasItems("discovery-service-a"));
55+
}
56+
4757
}

0 commit comments

Comments
 (0)