K8s client lb cleanup 2 (#1626)

This commit is contained in:
erabii
2024-04-05 00:10:10 +03:00
committed by GitHub
parent ff469eba9a
commit 4d0b4fd1a4
3 changed files with 44 additions and 47 deletions

View File

@@ -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 java.util.Optional;
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 @@ import static org.springframework.cloud.kubernetes.commons.discovery.ServicePort
*/
public class KubernetesClientServiceInstanceMapper implements KubernetesServiceInstanceMapper<V1Service> {
/**
* empty on purpose, load balancer implementation does not need them.
*/
private static final Map<String, Integer> PORTS_DATA = Map.of();
private final KubernetesLoadBalancerProperties properties;
private final KubernetesDiscoveryProperties discoveryProperties;
@@ -80,24 +86,15 @@ public class KubernetesClientServiceInstanceMapper implements KubernetesServiceI
boolean secure = secure(port, service);
return new DefaultKubernetesServiceInstance(meta.getUid(), meta.getName(), host, port.getPort(),
getServiceMetadata(service), secure);
serviceMetadata(service), secure);
}
private Map<String, String> getServiceMetadata(V1Service service) {
final Map<String, String> serviceMetadata = new HashMap<>();
KubernetesDiscoveryProperties.Metadata metadataProps = this.discoveryProperties.metadata();
if (metadataProps.addLabels()) {
Map<String, String> labelMetadata = keysWithPrefix(service.getMetadata().getLabels(),
metadataProps.labelsPrefix());
serviceMetadata.putAll(labelMetadata);
}
if (metadataProps.addAnnotations()) {
Map<String, String> annotationMetadata = keysWithPrefix(service.getMetadata().getAnnotations(),
metadataProps.annotationsPrefix());
serviceMetadata.putAll(annotationMetadata);
}
return serviceMetadata;
private Map<String, String> 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) {

View File

@@ -47,13 +47,12 @@ class KubernetesClientServiceInstanceMapperTests {
Map<String, String> annotations = Map.of("org.springframework.cloud", "true");
Map<String, String> labels = Map.of("beta", "true");
List<V1ServicePort> servicePorts = List.of(
new V1ServicePortBuilder().withName("http").withPort(80).build()
);
List<V1ServicePort> servicePorts = List.of(new V1ServicePortBuilder().withName("http").withPort(80).build());
V1Service service = createService("database", "default", annotations, labels, servicePorts);
KubernetesServiceInstance serviceInstance = mapper.map(service);
Map<String, String> metadata = Map.of("org.springframework.cloud", "true", "beta", "true");
Map<String, String> 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 @@ class KubernetesClientServiceInstanceMapperTests {
void singlePortSecure() {
KubernetesLoadBalancerProperties loadBalancerProperties = new KubernetesLoadBalancerProperties();
KubernetesClientServiceInstanceMapper mapper = new KubernetesClientServiceInstanceMapper(loadBalancerProperties,
KubernetesDiscoveryProperties.DEFAULT);
KubernetesDiscoveryProperties.DEFAULT);
Map<String, String> annotations = Map.of("org.springframework.cloud", "true", "secured", "true");
Map<String, String> labels = Map.of("beta", "true");
List<V1ServicePort> servicePorts = List.of(
new V1ServicePortBuilder().withName("http").withPort(80).build()
);
List<V1ServicePort> servicePorts = List.of(new V1ServicePortBuilder().withName("http").withPort(80).build());
V1Service service = createService("database", "default", annotations, labels, servicePorts);
KubernetesServiceInstance serviceInstance = mapper.map(service);
Map<String, String> metadata = Map.of("org.springframework.cloud", "true", "beta", "true", "secured", "true");
Map<String, String> 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 @@ class KubernetesClientServiceInstanceMapperTests {
Map<String, String> annotations = Map.of("org.springframework.cloud", "true");
Map<String, String> labels = Map.of("beta", "true");
List<V1ServicePort> servicePorts = List.of(
new V1ServicePortBuilder().withName("http").withPort(80).build(),
new V1ServicePortBuilder().withName("https").withPort(443).build()
);
List<V1ServicePort> 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<String, String> metadata = Map.of("org.springframework.cloud", "true", "beta", "true");
Map<String, String> 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 @@ class KubernetesClientServiceInstanceMapperTests {
private V1Service createService(String name, String namespace, Map<String, String> annotations,
Map<String, String> labels, List<V1ServicePort> 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();
}
}

View File

@@ -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 @@ class KubernetesClientServicesListSupplierTests {
Flux<List<ServiceInstance>> instances = listSupplier.get();
Map<String, String> metadata = Map.of("org.springframework.cloud", "true", "beta", "true");
Map<String, String> 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<ServiceInstance> services = new ArrayList<>();
@@ -209,11 +211,12 @@ class KubernetesClientServicesListSupplierTests {
Flux<List<ServiceInstance>> instances = listSupplier.get();
Map<String, String> metadata = Map.of("org.springframework.cloud", "true", "beta", "true");
Map<String, String> 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<ServiceInstance> services = new ArrayList<>();
services.add(serviceADefaultNamespace);
services.add(serviceATestNamespace);
@@ -253,11 +256,12 @@ class KubernetesClientServicesListSupplierTests {
Flux<List<ServiceInstance>> instances = listSupplier.get();
Map<String, String> metadata = Map.of("org.springframework.cloud", "true", "beta", "true");
Map<String, String> 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<ServiceInstance> services = new ArrayList<>();
services.add(serviceADefaultNamespace);
services.add(serviceATestNamespace);