diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8InstanceIdHostPodNameSupplier.java b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8InstanceIdHostPodNameSupplier.java index df777ed9..2267e0ed 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8InstanceIdHostPodNameSupplier.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8InstanceIdHostPodNameSupplier.java @@ -36,7 +36,7 @@ final class Fabric8InstanceIdHostPodNameSupplier implements Supplier addresses = addresses(endpointSubset, properties); for (EndpointAddress endpointAddress : addresses) { - ServiceMetadataForServiceInstance forServiceInstance = new ServiceMetadataForServiceInstance( - service.getMetadata().getName(), service.getMetadata().getLabels(), - service.getMetadata().getAnnotations()); + ServiceMetadataForServiceInstance forServiceInstance = forServiceInstance(service); + Fabric8InstanceIdHostPodNameSupplier supplierOne = nonExternalName(endpointAddress, service); + Fabric8PodLabelsAndAnnotationsSupplier supplierTwo = nonExternalName(client, namespace); ServiceInstance serviceInstance = serviceInstance(servicePortSecureResolver, forServiceInstance, - new Fabric8InstanceIdHostPodNameSupplier(endpointAddress, service), - new Fabric8PodLabelsAndAnnotationsSupplier(client, namespace), portData, serviceId, result, - namespace, properties); + supplierOne, supplierTwo, portData, serviceId, result, namespace, properties); instances.add(serviceInstance); } } diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8InstanceIdHostPodNameSupplierTests.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8InstanceIdHostPodNameSupplierTests.java index 24bb82e0..c7b7e62b 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8InstanceIdHostPodNameSupplierTests.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8InstanceIdHostPodNameSupplierTests.java @@ -36,12 +36,10 @@ class Fabric8InstanceIdHostPodNameSupplierTests { @Test void instanceIdNoEndpointAddress() { - EndpointAddress endpointAddress = null; Service service = new ServiceBuilder().withSpec(new ServiceSpecBuilder().build()) .withMetadata(new ObjectMetaBuilder().withUid("123").build()).build(); - Fabric8InstanceIdHostPodNameSupplier supplier = new Fabric8InstanceIdHostPodNameSupplier(endpointAddress, - service); + Fabric8InstanceIdHostPodNameSupplier supplier = Fabric8InstanceIdHostPodNameSupplier.externalName(service); InstanceIdHostPodName result = supplier.get(); Assertions.assertNotNull(result); @@ -55,8 +53,8 @@ class Fabric8InstanceIdHostPodNameSupplierTests { Service service = new ServiceBuilder().withSpec(new ServiceSpecBuilder().build()) .withMetadata(new ObjectMetaBuilder().withUid("123").build()).build(); - Fabric8InstanceIdHostPodNameSupplier supplier = new Fabric8InstanceIdHostPodNameSupplier(endpointAddress, - service); + Fabric8InstanceIdHostPodNameSupplier supplier = Fabric8InstanceIdHostPodNameSupplier + .nonExternalName(endpointAddress, service); InstanceIdHostPodName result = supplier.get(); Assertions.assertNotNull(result); @@ -65,13 +63,11 @@ class Fabric8InstanceIdHostPodNameSupplierTests { @Test void hostNoEndpointAddress() { - EndpointAddress endpointAddress = null; Service service = new ServiceBuilder() .withSpec(new ServiceSpecBuilder().withExternalName("external-name").build()) .withMetadata(new ObjectMeta()).build(); - Fabric8InstanceIdHostPodNameSupplier supplier = new Fabric8InstanceIdHostPodNameSupplier(endpointAddress, - service); + Fabric8InstanceIdHostPodNameSupplier supplier = Fabric8InstanceIdHostPodNameSupplier.externalName(service); InstanceIdHostPodName result = supplier.get(); Assertions.assertNotNull(result); @@ -85,14 +81,25 @@ class Fabric8InstanceIdHostPodNameSupplierTests { .withSpec(new ServiceSpecBuilder().withExternalName("external-name").build()) .withMetadata(new ObjectMeta()).build(); - Fabric8InstanceIdHostPodNameSupplier supplier = new Fabric8InstanceIdHostPodNameSupplier(endpointAddress, - service); + Fabric8InstanceIdHostPodNameSupplier supplier = Fabric8InstanceIdHostPodNameSupplier + .nonExternalName(endpointAddress, service); InstanceIdHostPodName result = supplier.get(); Assertions.assertNotNull(result); Assertions.assertEquals(result.host(), "127.0.0.1"); } + @Test + void testPodNameIsNull() { + Service service = new ServiceBuilder().withMetadata(new ObjectMetaBuilder().withUid("123").build()) + .withSpec(new ServiceSpecBuilder().withExternalName("external-name").build()).build(); + Fabric8InstanceIdHostPodNameSupplier supplier = Fabric8InstanceIdHostPodNameSupplier.externalName(service); + InstanceIdHostPodName result = supplier.get(); + + Assertions.assertNotNull(result); + Assertions.assertNull(result.podName()); + } + @Test void podNameKindNotPod() { EndpointAddress endpointAddress = new EndpointAddressBuilder() @@ -101,8 +108,8 @@ class Fabric8InstanceIdHostPodNameSupplierTests { .withSpec(new ServiceSpecBuilder().withExternalName("external-name").build()) .withMetadata(new ObjectMeta()).build(); - Fabric8InstanceIdHostPodNameSupplier supplier = new Fabric8InstanceIdHostPodNameSupplier(endpointAddress, - service); + Fabric8InstanceIdHostPodNameSupplier supplier = Fabric8InstanceIdHostPodNameSupplier + .nonExternalName(endpointAddress, service); InstanceIdHostPodName result = supplier.get(); Assertions.assertNotNull(result); @@ -117,8 +124,8 @@ class Fabric8InstanceIdHostPodNameSupplierTests { .withSpec(new ServiceSpecBuilder().withExternalName("external-name").build()) .withMetadata(new ObjectMeta()).build(); - Fabric8InstanceIdHostPodNameSupplier supplier = new Fabric8InstanceIdHostPodNameSupplier(endpointAddress, - service); + Fabric8InstanceIdHostPodNameSupplier supplier = Fabric8InstanceIdHostPodNameSupplier + .nonExternalName(endpointAddress, service); InstanceIdHostPodName result = supplier.get(); Assertions.assertNotNull(result); diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8PodLabelsAndAnnotationsSupplierTests.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8PodLabelsAndAnnotationsSupplierTests.java index db530b53..08e5de08 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8PodLabelsAndAnnotationsSupplierTests.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8PodLabelsAndAnnotationsSupplierTests.java @@ -51,7 +51,8 @@ class Fabric8PodLabelsAndAnnotationsSupplierTests { .resource(new PodBuilder().withMetadata(new ObjectMetaBuilder().withName(POD_NAME).build()).build()) .create(); - PodLabelsAndAnnotations result = new Fabric8PodLabelsAndAnnotationsSupplier(client, NAMESPACE).apply(POD_NAME); + PodLabelsAndAnnotations result = Fabric8PodLabelsAndAnnotationsSupplier.nonExternalName(client, NAMESPACE) + .apply(POD_NAME); Assertions.assertNotNull(result); Assertions.assertTrue(result.labels().isEmpty()); Assertions.assertTrue(result.annotations().isEmpty()); @@ -63,7 +64,8 @@ class Fabric8PodLabelsAndAnnotationsSupplierTests { .withName(POD_NAME).withLabels(Map.of("a", "b")).withAnnotations(Map.of("c", "d")).build()).build()) .create(); - PodLabelsAndAnnotations result = new Fabric8PodLabelsAndAnnotationsSupplier(client, NAMESPACE).apply(POD_NAME); + PodLabelsAndAnnotations result = Fabric8PodLabelsAndAnnotationsSupplier.nonExternalName(client, NAMESPACE) + .apply(POD_NAME); Assertions.assertNotNull(result); Assertions.assertEquals(result.labels(), Map.of("a", "b")); Assertions.assertEquals(result.annotations(), Map.of("c", "d"));