diff --git a/spring-cloud-kubernetes-client-loadbalancer/src/main/java/org/springframework/cloud/kubernetes/client/loadbalancer/KubernetesClientServiceInstanceMapper.java b/spring-cloud-kubernetes-client-loadbalancer/src/main/java/org/springframework/cloud/kubernetes/client/loadbalancer/KubernetesClientServiceInstanceMapper.java index 297d102332..83293741d8 100644 --- a/spring-cloud-kubernetes-client-loadbalancer/src/main/java/org/springframework/cloud/kubernetes/client/loadbalancer/KubernetesClientServiceInstanceMapper.java +++ b/spring-cloud-kubernetes-client-loadbalancer/src/main/java/org/springframework/cloud/kubernetes/client/loadbalancer/KubernetesClientServiceInstanceMapper.java @@ -16,7 +16,6 @@ package org.springframework.cloud.kubernetes.client.loadbalancer; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; @@ -24,17 +23,19 @@ import io.kubernetes.client.openapi.models.V1ObjectMeta; import io.kubernetes.client.openapi.models.V1Service; import io.kubernetes.client.openapi.models.V1ServicePort; +import io.kubernetes.client.openapi.models.V1ServiceSpec; import org.springframework.cloud.kubernetes.commons.discovery.DefaultKubernetesServiceInstance; +import org.springframework.cloud.kubernetes.commons.discovery.DiscoveryClientUtils; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesServiceInstance; +import org.springframework.cloud.kubernetes.commons.discovery.ServiceMetadata; import org.springframework.cloud.kubernetes.commons.discovery.ServicePortNameAndNumber; import org.springframework.cloud.kubernetes.commons.discovery.ServicePortSecureResolver; import org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesLoadBalancerProperties; import org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesServiceInstanceMapper; import org.springframework.util.StringUtils; -import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.keysWithPrefix; import static org.springframework.cloud.kubernetes.commons.discovery.ServicePortSecureResolver.Input; /** @@ -42,6 +43,11 @@ */ public class KubernetesClientServiceInstanceMapper implements KubernetesServiceInstanceMapper { + /** + * empty on purpose, load balancer implementation does not need them. + */ + private static final Map PORTS_DATA = Map.of(); + private final KubernetesLoadBalancerProperties properties; private final KubernetesDiscoveryProperties discoveryProperties; @@ -80,24 +86,15 @@ else if (ports.size() > 1 && StringUtils.hasText(this.properties.getPortName())) boolean secure = secure(port, service); return new DefaultKubernetesServiceInstance(meta.getUid(), meta.getName(), host, port.getPort(), - getServiceMetadata(service), secure); + serviceMetadata(service), secure); } - private Map getServiceMetadata(V1Service service) { - final Map serviceMetadata = new HashMap<>(); - KubernetesDiscoveryProperties.Metadata metadataProps = this.discoveryProperties.metadata(); - if (metadataProps.addLabels()) { - Map labelMetadata = keysWithPrefix(service.getMetadata().getLabels(), - metadataProps.labelsPrefix()); - serviceMetadata.putAll(labelMetadata); - } - if (metadataProps.addAnnotations()) { - Map annotationMetadata = keysWithPrefix(service.getMetadata().getAnnotations(), - metadataProps.annotationsPrefix()); - serviceMetadata.putAll(annotationMetadata); - } - - return serviceMetadata; + private Map serviceMetadata(V1Service service) { + V1ObjectMeta metadata = service.getMetadata(); + V1ServiceSpec serviceSpec = service.getSpec(); + ServiceMetadata serviceMetadata = new ServiceMetadata(metadata.getName(), metadata.getNamespace(), + serviceSpec.getType(), metadata.getLabels(), metadata.getAnnotations()); + return DiscoveryClientUtils.serviceInstanceMetadata(PORTS_DATA, serviceMetadata, discoveryProperties); } private boolean secure(V1ServicePort port, V1Service service) { diff --git a/spring-cloud-kubernetes-client-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/client/loadbalancer/KubernetesClientServiceInstanceMapperTests.java b/spring-cloud-kubernetes-client-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/client/loadbalancer/KubernetesClientServiceInstanceMapperTests.java index b3a4273227..c825641b08 100644 --- a/spring-cloud-kubernetes-client-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/client/loadbalancer/KubernetesClientServiceInstanceMapperTests.java +++ b/spring-cloud-kubernetes-client-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/client/loadbalancer/KubernetesClientServiceInstanceMapperTests.java @@ -47,13 +47,12 @@ void singlePortNonSecure() { Map annotations = Map.of("org.springframework.cloud", "true"); Map labels = Map.of("beta", "true"); - List servicePorts = List.of( - new V1ServicePortBuilder().withName("http").withPort(80).build() - ); + List servicePorts = List.of(new V1ServicePortBuilder().withName("http").withPort(80).build()); V1Service service = createService("database", "default", annotations, labels, servicePorts); KubernetesServiceInstance serviceInstance = mapper.map(service); - Map metadata = Map.of("org.springframework.cloud", "true", "beta", "true"); + Map metadata = Map.of("org.springframework.cloud", "true", "beta", "true", "k8s_namespace", + "default", "type", "V1Service"); DefaultKubernetesServiceInstance result = new DefaultKubernetesServiceInstance("0", "database", "database.default.svc.cluster.local", 80, metadata, false); assertThat(serviceInstance).isEqualTo(result); @@ -64,19 +63,18 @@ void singlePortNonSecure() { void singlePortSecure() { KubernetesLoadBalancerProperties loadBalancerProperties = new KubernetesLoadBalancerProperties(); KubernetesClientServiceInstanceMapper mapper = new KubernetesClientServiceInstanceMapper(loadBalancerProperties, - KubernetesDiscoveryProperties.DEFAULT); + KubernetesDiscoveryProperties.DEFAULT); Map annotations = Map.of("org.springframework.cloud", "true", "secured", "true"); Map labels = Map.of("beta", "true"); - List servicePorts = List.of( - new V1ServicePortBuilder().withName("http").withPort(80).build() - ); + List servicePorts = List.of(new V1ServicePortBuilder().withName("http").withPort(80).build()); V1Service service = createService("database", "default", annotations, labels, servicePorts); KubernetesServiceInstance serviceInstance = mapper.map(service); - Map metadata = Map.of("org.springframework.cloud", "true", "beta", "true", "secured", "true"); + Map metadata = Map.of("org.springframework.cloud", "true", "beta", "true", "secured", "true", + "k8s_namespace", "default", "type", "V1Service"); DefaultKubernetesServiceInstance result = new DefaultKubernetesServiceInstance("0", "database", - "database.default.svc.cluster.local", 80, metadata, true); + "database.default.svc.cluster.local", 80, metadata, true); assertThat(serviceInstance).isEqualTo(result); } @@ -89,13 +87,12 @@ void multiplePortsSecure() { Map annotations = Map.of("org.springframework.cloud", "true"); Map labels = Map.of("beta", "true"); - List servicePorts = List.of( - new V1ServicePortBuilder().withName("http").withPort(80).build(), - new V1ServicePortBuilder().withName("https").withPort(443).build() - ); + List servicePorts = List.of(new V1ServicePortBuilder().withName("http").withPort(80).build(), + new V1ServicePortBuilder().withName("https").withPort(443).build()); V1Service service = createService("database", "default", annotations, labels, servicePorts); - Map metadata = Map.of("org.springframework.cloud", "true", "beta", "true"); + Map metadata = Map.of("org.springframework.cloud", "true", "beta", "true", "k8s_namespace", + "default", "type", "V1Service"); KubernetesServiceInstance serviceInstance = mapper.map(service); DefaultKubernetesServiceInstance result = new DefaultKubernetesServiceInstance("0", "database", "database.default.svc.cluster.local", 443, metadata, true); @@ -105,10 +102,9 @@ void multiplePortsSecure() { private V1Service createService(String name, String namespace, Map annotations, Map labels, List servicePorts) { return new V1ServiceBuilder() - .withMetadata(new V1ObjectMetaBuilder().withName(name).withUid("0") - .withNamespace(namespace).addToAnnotations(annotations) - .addToLabels(labels).build()) - .withSpec(new V1ServiceSpecBuilder().addAllToPorts(servicePorts).build()).build(); + .withMetadata(new V1ObjectMetaBuilder().withName(name).withUid("0").withNamespace(namespace) + .addToAnnotations(annotations).addToLabels(labels).build()) + .withSpec(new V1ServiceSpecBuilder().addAllToPorts(servicePorts).withType("V1Service").build()).build(); } } diff --git a/spring-cloud-kubernetes-client-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/client/loadbalancer/KubernetesClientServicesListSupplierTests.java b/spring-cloud-kubernetes-client-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/client/loadbalancer/KubernetesClientServicesListSupplierTests.java index a7d23e2697..cbfa69a7b8 100644 --- a/spring-cloud-kubernetes-client-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/client/loadbalancer/KubernetesClientServicesListSupplierTests.java +++ b/spring-cloud-kubernetes-client-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/client/loadbalancer/KubernetesClientServicesListSupplierTests.java @@ -71,16 +71,17 @@ class KubernetesClientServicesListSupplierTests { private static final V1Service SERVICE_A_DEFAULT_NAMESPACE = new V1ServiceBuilder() .withMetadata(new V1ObjectMetaBuilder().withName("service-a").withNamespace("default").withUid("0") .addToLabels("beta", "true").addToAnnotations("org.springframework.cloud", "true").build()) - .withSpec(new V1ServiceSpecBuilder() + .withSpec(new V1ServiceSpecBuilder().withType("V1Service") .addToPorts(new V1ServicePortBuilder().withPort(80).withName("http").build()).build()) .build(); private static final V1Service SERVICE_A_TEST_NAMESPACE = new V1ServiceBuilder() .withMetadata(new V1ObjectMetaBuilder().withName("service-a").withNamespace("test").withUid("1").build()) - .withSpec(new V1ServiceSpecBuilder() - .addToPorts(new V1ServicePortBuilder().withPort(80).withName("http").build(), - new V1ServicePortBuilder().withPort(443).withName("https").build()) - .build()) + .withSpec( + new V1ServiceSpecBuilder().withType("V1Service") + .addToPorts(new V1ServicePortBuilder().withPort(80).withName("http").build(), + new V1ServicePortBuilder().withPort(443).withName("https").build()) + .build()) .build(); private static final V1ServiceList SINGLE_NAMESPACE_SERVICES = new V1ServiceList() @@ -142,7 +143,8 @@ void singleNamespaceTest(CapturedOutput output) { Flux> instances = listSupplier.get(); - Map metadata = Map.of("org.springframework.cloud", "true", "beta", "true"); + Map metadata = Map.of("org.springframework.cloud", "true", "beta", "true", "k8s_namespace", + "default", "type", "V1Service"); DefaultKubernetesServiceInstance serviceA = new DefaultKubernetesServiceInstance("0", "service-a", "service-a.default.svc.cluster.local", 80, metadata, false); List services = new ArrayList<>(); @@ -209,11 +211,12 @@ void allNamespacesTest(CapturedOutput output) { Flux> instances = listSupplier.get(); - Map metadata = Map.of("org.springframework.cloud", "true", "beta", "true"); + Map metadata = Map.of("org.springframework.cloud", "true", "beta", "true", "k8s_namespace", + "default", "type", "V1Service"); DefaultKubernetesServiceInstance serviceADefaultNamespace = new DefaultKubernetesServiceInstance("0", "service-a", "service-a.default.svc.cluster.local", 80, metadata, false); DefaultKubernetesServiceInstance serviceATestNamespace = new DefaultKubernetesServiceInstance("1", "service-a", - "service-a.test.svc.cluster.local", 80, Map.of(), false); + "service-a.test.svc.cluster.local", 80, Map.of("k8s_namespace", "test", "type", "V1Service"), false); List services = new ArrayList<>(); services.add(serviceADefaultNamespace); services.add(serviceATestNamespace); @@ -253,11 +256,12 @@ void selectiveNamespacesTest(CapturedOutput output) { Flux> instances = listSupplier.get(); - Map metadata = Map.of("org.springframework.cloud", "true", "beta", "true"); + Map metadata = Map.of("org.springframework.cloud", "true", "beta", "true", "k8s_namespace", + "default", "type", "V1Service"); DefaultKubernetesServiceInstance serviceADefaultNamespace = new DefaultKubernetesServiceInstance("0", "service-a", "service-a.default.svc.cluster.local", 80, metadata, false); DefaultKubernetesServiceInstance serviceATestNamespace = new DefaultKubernetesServiceInstance("1", "service-a", - "service-a.test.svc.cluster.local", 80, Map.of(), false); + "service-a.test.svc.cluster.local", 80, Map.of("k8s_namespace", "test", "type", "V1Service"), false); List services = new ArrayList<>(); services.add(serviceADefaultNamespace); services.add(serviceATestNamespace);