Merge branch '3.0.x'

This commit is contained in:
Ryan Baxter
2023-08-07 19:20:58 -04:00
6 changed files with 76 additions and 31 deletions

View File

@@ -36,7 +36,7 @@ final class Fabric8InstanceIdHostPodNameSupplier implements Supplier<InstanceIdH
private final Service service;
Fabric8InstanceIdHostPodNameSupplier(EndpointAddress endpointAddress, Service service) {
private Fabric8InstanceIdHostPodNameSupplier(EndpointAddress endpointAddress, Service service) {
this.endpointAddress = endpointAddress;
this.service = service;
}
@@ -46,6 +46,20 @@ final class Fabric8InstanceIdHostPodNameSupplier implements Supplier<InstanceIdH
return new InstanceIdHostPodName(instanceId(), host(), podName());
}
/**
* to be used when .spec.type of the Service is != 'ExternalName'.
*/
static Fabric8InstanceIdHostPodNameSupplier nonExternalName(EndpointAddress endpointAddress, Service service) {
return new Fabric8InstanceIdHostPodNameSupplier(endpointAddress, service);
}
/**
* to be used when .spec.type of the Service is == 'ExternalName'.
*/
static Fabric8InstanceIdHostPodNameSupplier externalName(Service service) {
return new Fabric8InstanceIdHostPodNameSupplier(null, service);
}
// instanceId is usually the pod-uid as seen in the .metadata.uid
private String instanceId() {
return Optional.ofNullable(endpointAddress).map(EndpointAddress::getTargetRef).map(ObjectReference::getUid)

View File

@@ -42,6 +42,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
import org.springframework.cloud.kubernetes.commons.discovery.ServiceMetadataForServiceInstance;
import org.springframework.cloud.kubernetes.fabric8.Fabric8Utils;
import org.springframework.core.log.LogAccessor;
import org.springframework.util.CollectionUtils;
@@ -214,6 +215,11 @@ final class Fabric8KubernetesDiscoveryClientUtils {
return result;
}
static ServiceMetadataForServiceInstance forServiceInstance(Service service) {
return new ServiceMetadataForServiceInstance(service.getMetadata().getName(), service.getMetadata().getLabels(),
service.getMetadata().getAnnotations());
}
/**
* serviceName can be null, in which case, such a filter will not be applied.
*/

View File

@@ -36,11 +36,25 @@ final class Fabric8PodLabelsAndAnnotationsSupplier implements Function<String, P
private final String namespace;
Fabric8PodLabelsAndAnnotationsSupplier(KubernetesClient client, String namespace) {
private Fabric8PodLabelsAndAnnotationsSupplier(KubernetesClient client, String namespace) {
this.client = client;
this.namespace = namespace;
}
/**
* to be used when .spec.type of the Service is != 'ExternalName'.
*/
static Fabric8PodLabelsAndAnnotationsSupplier nonExternalName(KubernetesClient client, String namespace) {
return new Fabric8PodLabelsAndAnnotationsSupplier(client, namespace);
}
/**
* to be used when .spec.type of the Service is == 'ExternalName'.
*/
static Fabric8PodLabelsAndAnnotationsSupplier externalName() {
return new Fabric8PodLabelsAndAnnotationsSupplier(null, null);
}
@Override
public PodLabelsAndAnnotations apply(String podName) {
ObjectMeta metadata = Optional.ofNullable(client.pods().inNamespace(namespace).withName(podName).get())

View File

@@ -44,11 +44,16 @@ import org.springframework.core.log.LogAccessor;
import static org.springframework.cloud.kubernetes.commons.discovery.DiscoveryClientUtils.serviceInstance;
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.EXTERNAL_NAME;
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8InstanceIdHostPodNameSupplier.externalName;
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8InstanceIdHostPodNameSupplier.nonExternalName;
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8KubernetesDiscoveryClientUtils.addresses;
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8KubernetesDiscoveryClientUtils.endpointSubsetPortsData;
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8KubernetesDiscoveryClientUtils.endpoints;
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8KubernetesDiscoveryClientUtils.forServiceInstance;
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8KubernetesDiscoveryClientUtils.portsData;
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8KubernetesDiscoveryClientUtils.services;
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8PodLabelsAndAnnotationsSupplier.externalName;
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8PodLabelsAndAnnotationsSupplier.nonExternalName;
/**
* Fabric8 Kubernetes implementation of {@link DiscoveryClient}.
@@ -129,14 +134,13 @@ public class KubernetesDiscoveryClient implements DiscoveryClient, EnvironmentAw
serviceMetadata.getLabels(), serviceMetadata.getAnnotations(), Map.of(), properties,
serviceMetadata.getNamespace(), service.getSpec().getType());
ServiceMetadataForServiceInstance forServiceInstance = new ServiceMetadataForServiceInstance(
service.getMetadata().getName(), service.getMetadata().getLabels(),
service.getMetadata().getAnnotations());
ServiceMetadataForServiceInstance forServiceInstance = forServiceInstance(service);
Fabric8InstanceIdHostPodNameSupplier supplierOne = externalName(service);
Fabric8PodLabelsAndAnnotationsSupplier supplierTwo = externalName();
ServiceInstance externalNameServiceInstance = serviceInstance(null, forServiceInstance,
new Fabric8InstanceIdHostPodNameSupplier(null, service),
new Fabric8PodLabelsAndAnnotationsSupplier(null, null), new ServicePortNameAndNumber(-1, null),
serviceId, result, service.getMetadata().getNamespace(), properties);
ServiceInstance externalNameServiceInstance = serviceInstance(null, forServiceInstance, supplierOne,
supplierTwo, new ServicePortNameAndNumber(-1, null), serviceId, result,
service.getMetadata().getNamespace(), properties);
instances.add(externalNameServiceInstance);
}
@@ -175,14 +179,12 @@ public class KubernetesDiscoveryClient implements DiscoveryClient, EnvironmentAw
List<EndpointAddress> 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);
}
}

View File

@@ -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);

View File

@@ -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"));