Skip to content

Commit 44a0064

Browse files
authored
Simplify new suppliers introduced previously (#1400)
1 parent ce9db7b commit 44a0064

File tree

6 files changed

+76
-31
lines changed

6 files changed

+76
-31
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ final class Fabric8InstanceIdHostPodNameSupplier implements Supplier<InstanceIdH
3636

3737
private final Service service;
3838

39-
Fabric8InstanceIdHostPodNameSupplier(EndpointAddress endpointAddress, Service service) {
39+
private Fabric8InstanceIdHostPodNameSupplier(EndpointAddress endpointAddress, Service service) {
4040
this.endpointAddress = endpointAddress;
4141
this.service = service;
4242
}
@@ -46,6 +46,20 @@ public InstanceIdHostPodName get() {
4646
return new InstanceIdHostPodName(instanceId(), host(), podName());
4747
}
4848

49+
/**
50+
* to be used when .spec.type of the Service is != 'ExternalName'.
51+
*/
52+
static Fabric8InstanceIdHostPodNameSupplier nonExternalName(EndpointAddress endpointAddress, Service service) {
53+
return new Fabric8InstanceIdHostPodNameSupplier(endpointAddress, service);
54+
}
55+
56+
/**
57+
* to be used when .spec.type of the Service is == 'ExternalName'.
58+
*/
59+
static Fabric8InstanceIdHostPodNameSupplier externalName(Service service) {
60+
return new Fabric8InstanceIdHostPodNameSupplier(null, service);
61+
}
62+
4963
// instanceId is usually the pod-uid as seen in the .metadata.uid
5064
private String instanceId() {
5165
return Optional.ofNullable(endpointAddress).map(EndpointAddress::getTargetRef).map(ObjectReference::getUid)

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider;
4444
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
45+
import org.springframework.cloud.kubernetes.commons.discovery.ServiceMetadataForServiceInstance;
4546
import org.springframework.cloud.kubernetes.fabric8.Fabric8Utils;
4647
import org.springframework.core.log.LogAccessor;
4748
import org.springframework.util.CollectionUtils;
@@ -214,6 +215,11 @@ static LinkedHashMap<String, Integer> endpointSubsetPortsData(EndpointSubset end
214215
return result;
215216
}
216217

218+
static ServiceMetadataForServiceInstance forServiceInstance(Service service) {
219+
return new ServiceMetadataForServiceInstance(service.getMetadata().getName(), service.getMetadata().getLabels(),
220+
service.getMetadata().getAnnotations());
221+
}
222+
217223
/**
218224
* serviceName can be null, in which case, such a filter will not be applied.
219225
*/

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,25 @@ final class Fabric8PodLabelsAndAnnotationsSupplier implements Function<String, P
3636

3737
private final String namespace;
3838

39-
Fabric8PodLabelsAndAnnotationsSupplier(KubernetesClient client, String namespace) {
39+
private Fabric8PodLabelsAndAnnotationsSupplier(KubernetesClient client, String namespace) {
4040
this.client = client;
4141
this.namespace = namespace;
4242
}
4343

44+
/**
45+
* to be used when .spec.type of the Service is != 'ExternalName'.
46+
*/
47+
static Fabric8PodLabelsAndAnnotationsSupplier nonExternalName(KubernetesClient client, String namespace) {
48+
return new Fabric8PodLabelsAndAnnotationsSupplier(client, namespace);
49+
}
50+
51+
/**
52+
* to be used when .spec.type of the Service is == 'ExternalName'.
53+
*/
54+
static Fabric8PodLabelsAndAnnotationsSupplier externalName() {
55+
return new Fabric8PodLabelsAndAnnotationsSupplier(null, null);
56+
}
57+
4458
@Override
4559
public PodLabelsAndAnnotations apply(String podName) {
4660
ObjectMeta metadata = Optional.ofNullable(client.pods().inNamespace(namespace).withName(podName).get())

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,16 @@
4444

4545
import static org.springframework.cloud.kubernetes.commons.discovery.DiscoveryClientUtils.serviceInstance;
4646
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.EXTERNAL_NAME;
47+
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8InstanceIdHostPodNameSupplier.externalName;
48+
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8InstanceIdHostPodNameSupplier.nonExternalName;
4749
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8KubernetesDiscoveryClientUtils.addresses;
4850
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8KubernetesDiscoveryClientUtils.endpointSubsetPortsData;
4951
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8KubernetesDiscoveryClientUtils.endpoints;
52+
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8KubernetesDiscoveryClientUtils.forServiceInstance;
5053
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8KubernetesDiscoveryClientUtils.portsData;
5154
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8KubernetesDiscoveryClientUtils.services;
55+
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8PodLabelsAndAnnotationsSupplier.externalName;
56+
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8PodLabelsAndAnnotationsSupplier.nonExternalName;
5257

5358
/**
5459
* Fabric8 Kubernetes implementation of {@link DiscoveryClient}.
@@ -129,14 +134,13 @@ public List<ServiceInstance> getInstances(String serviceId) {
129134
serviceMetadata.getLabels(), serviceMetadata.getAnnotations(), Map.of(), properties,
130135
serviceMetadata.getNamespace(), service.getSpec().getType());
131136

132-
ServiceMetadataForServiceInstance forServiceInstance = new ServiceMetadataForServiceInstance(
133-
service.getMetadata().getName(), service.getMetadata().getLabels(),
134-
service.getMetadata().getAnnotations());
137+
ServiceMetadataForServiceInstance forServiceInstance = forServiceInstance(service);
138+
Fabric8InstanceIdHostPodNameSupplier supplierOne = externalName(service);
139+
Fabric8PodLabelsAndAnnotationsSupplier supplierTwo = externalName();
135140

136-
ServiceInstance externalNameServiceInstance = serviceInstance(null, forServiceInstance,
137-
new Fabric8InstanceIdHostPodNameSupplier(null, service),
138-
new Fabric8PodLabelsAndAnnotationsSupplier(null, null), new ServicePortNameAndNumber(-1, null),
139-
serviceId, result, service.getMetadata().getNamespace(), properties);
141+
ServiceInstance externalNameServiceInstance = serviceInstance(null, forServiceInstance, supplierOne,
142+
supplierTwo, new ServicePortNameAndNumber(-1, null), serviceId, result,
143+
service.getMetadata().getNamespace(), properties);
140144

141145
instances.add(externalNameServiceInstance);
142146
}
@@ -175,14 +179,12 @@ private List<ServiceInstance> getNamespaceServiceInstances(EndpointSubsetNS es,
175179
List<EndpointAddress> addresses = addresses(endpointSubset, properties);
176180
for (EndpointAddress endpointAddress : addresses) {
177181

178-
ServiceMetadataForServiceInstance forServiceInstance = new ServiceMetadataForServiceInstance(
179-
service.getMetadata().getName(), service.getMetadata().getLabels(),
180-
service.getMetadata().getAnnotations());
182+
ServiceMetadataForServiceInstance forServiceInstance = forServiceInstance(service);
183+
Fabric8InstanceIdHostPodNameSupplier supplierOne = nonExternalName(endpointAddress, service);
184+
Fabric8PodLabelsAndAnnotationsSupplier supplierTwo = nonExternalName(client, namespace);
181185

182186
ServiceInstance serviceInstance = serviceInstance(servicePortSecureResolver, forServiceInstance,
183-
new Fabric8InstanceIdHostPodNameSupplier(endpointAddress, service),
184-
new Fabric8PodLabelsAndAnnotationsSupplier(client, namespace), portData, serviceId, result,
185-
namespace, properties);
187+
supplierOne, supplierTwo, portData, serviceId, result, namespace, properties);
186188
instances.add(serviceInstance);
187189
}
188190
}

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

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@ class Fabric8InstanceIdHostPodNameSupplierTests {
3636

3737
@Test
3838
void instanceIdNoEndpointAddress() {
39-
EndpointAddress endpointAddress = null;
4039
Service service = new ServiceBuilder().withSpec(new ServiceSpecBuilder().build())
4140
.withMetadata(new ObjectMetaBuilder().withUid("123").build()).build();
4241

43-
Fabric8InstanceIdHostPodNameSupplier supplier = new Fabric8InstanceIdHostPodNameSupplier(endpointAddress,
44-
service);
42+
Fabric8InstanceIdHostPodNameSupplier supplier = Fabric8InstanceIdHostPodNameSupplier.externalName(service);
4543
InstanceIdHostPodName result = supplier.get();
4644

4745
Assertions.assertNotNull(result);
@@ -55,8 +53,8 @@ void instanceIdWithEndpointAddress() {
5553
Service service = new ServiceBuilder().withSpec(new ServiceSpecBuilder().build())
5654
.withMetadata(new ObjectMetaBuilder().withUid("123").build()).build();
5755

58-
Fabric8InstanceIdHostPodNameSupplier supplier = new Fabric8InstanceIdHostPodNameSupplier(endpointAddress,
59-
service);
56+
Fabric8InstanceIdHostPodNameSupplier supplier = Fabric8InstanceIdHostPodNameSupplier
57+
.nonExternalName(endpointAddress, service);
6058
InstanceIdHostPodName result = supplier.get();
6159

6260
Assertions.assertNotNull(result);
@@ -65,13 +63,11 @@ void instanceIdWithEndpointAddress() {
6563

6664
@Test
6765
void hostNoEndpointAddress() {
68-
EndpointAddress endpointAddress = null;
6966
Service service = new ServiceBuilder()
7067
.withSpec(new ServiceSpecBuilder().withExternalName("external-name").build())
7168
.withMetadata(new ObjectMeta()).build();
7269

73-
Fabric8InstanceIdHostPodNameSupplier supplier = new Fabric8InstanceIdHostPodNameSupplier(endpointAddress,
74-
service);
70+
Fabric8InstanceIdHostPodNameSupplier supplier = Fabric8InstanceIdHostPodNameSupplier.externalName(service);
7571
InstanceIdHostPodName result = supplier.get();
7672

7773
Assertions.assertNotNull(result);
@@ -85,14 +81,25 @@ void hostWithEndpointAddress() {
8581
.withSpec(new ServiceSpecBuilder().withExternalName("external-name").build())
8682
.withMetadata(new ObjectMeta()).build();
8783

88-
Fabric8InstanceIdHostPodNameSupplier supplier = new Fabric8InstanceIdHostPodNameSupplier(endpointAddress,
89-
service);
84+
Fabric8InstanceIdHostPodNameSupplier supplier = Fabric8InstanceIdHostPodNameSupplier
85+
.nonExternalName(endpointAddress, service);
9086
InstanceIdHostPodName result = supplier.get();
9187

9288
Assertions.assertNotNull(result);
9389
Assertions.assertEquals(result.host(), "127.0.0.1");
9490
}
9591

92+
@Test
93+
void testPodNameIsNull() {
94+
Service service = new ServiceBuilder().withMetadata(new ObjectMetaBuilder().withUid("123").build())
95+
.withSpec(new ServiceSpecBuilder().withExternalName("external-name").build()).build();
96+
Fabric8InstanceIdHostPodNameSupplier supplier = Fabric8InstanceIdHostPodNameSupplier.externalName(service);
97+
InstanceIdHostPodName result = supplier.get();
98+
99+
Assertions.assertNotNull(result);
100+
Assertions.assertNull(result.podName());
101+
}
102+
96103
@Test
97104
void podNameKindNotPod() {
98105
EndpointAddress endpointAddress = new EndpointAddressBuilder()
@@ -101,8 +108,8 @@ void podNameKindNotPod() {
101108
.withSpec(new ServiceSpecBuilder().withExternalName("external-name").build())
102109
.withMetadata(new ObjectMeta()).build();
103110

104-
Fabric8InstanceIdHostPodNameSupplier supplier = new Fabric8InstanceIdHostPodNameSupplier(endpointAddress,
105-
service);
111+
Fabric8InstanceIdHostPodNameSupplier supplier = Fabric8InstanceIdHostPodNameSupplier
112+
.nonExternalName(endpointAddress, service);
106113
InstanceIdHostPodName result = supplier.get();
107114

108115
Assertions.assertNotNull(result);
@@ -117,8 +124,8 @@ void podNameKindIsPod() {
117124
.withSpec(new ServiceSpecBuilder().withExternalName("external-name").build())
118125
.withMetadata(new ObjectMeta()).build();
119126

120-
Fabric8InstanceIdHostPodNameSupplier supplier = new Fabric8InstanceIdHostPodNameSupplier(endpointAddress,
121-
service);
127+
Fabric8InstanceIdHostPodNameSupplier supplier = Fabric8InstanceIdHostPodNameSupplier
128+
.nonExternalName(endpointAddress, service);
122129
InstanceIdHostPodName result = supplier.get();
123130

124131
Assertions.assertNotNull(result);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ void noObjetMeta() {
5151
.resource(new PodBuilder().withMetadata(new ObjectMetaBuilder().withName(POD_NAME).build()).build())
5252
.create();
5353

54-
PodLabelsAndAnnotations result = new Fabric8PodLabelsAndAnnotationsSupplier(client, NAMESPACE).apply(POD_NAME);
54+
PodLabelsAndAnnotations result = Fabric8PodLabelsAndAnnotationsSupplier.nonExternalName(client, NAMESPACE)
55+
.apply(POD_NAME);
5556
Assertions.assertNotNull(result);
5657
Assertions.assertTrue(result.labels().isEmpty());
5758
Assertions.assertTrue(result.annotations().isEmpty());
@@ -63,7 +64,8 @@ void labelsAndAnnotationsPresent() {
6364
.withName(POD_NAME).withLabels(Map.of("a", "b")).withAnnotations(Map.of("c", "d")).build()).build())
6465
.create();
6566

66-
PodLabelsAndAnnotations result = new Fabric8PodLabelsAndAnnotationsSupplier(client, NAMESPACE).apply(POD_NAME);
67+
PodLabelsAndAnnotations result = Fabric8PodLabelsAndAnnotationsSupplier.nonExternalName(client, NAMESPACE)
68+
.apply(POD_NAME);
6769
Assertions.assertNotNull(result);
6870
Assertions.assertEquals(result.labels(), Map.of("a", "b"));
6971
Assertions.assertEquals(result.annotations(), Map.of("c", "d"));

0 commit comments

Comments
 (0)