K8s client lb cleanup 1 (#1625)
This commit is contained in:
@@ -28,23 +28,31 @@ import io.kubernetes.client.openapi.models.V1ServicePort;
|
||||
import org.springframework.cloud.kubernetes.commons.discovery.DefaultKubernetesServiceInstance;
|
||||
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
|
||||
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesServiceInstance;
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author Ryan Baxter
|
||||
*/
|
||||
public class KubernetesClientServiceInstanceMapper implements KubernetesServiceInstanceMapper<V1Service> {
|
||||
|
||||
private KubernetesLoadBalancerProperties properties;
|
||||
private final KubernetesLoadBalancerProperties properties;
|
||||
|
||||
private KubernetesDiscoveryProperties discoveryProperties;
|
||||
private final KubernetesDiscoveryProperties discoveryProperties;
|
||||
|
||||
private final ServicePortSecureResolver resolver;
|
||||
|
||||
public KubernetesClientServiceInstanceMapper(KubernetesLoadBalancerProperties properties,
|
||||
KubernetesDiscoveryProperties discoveryProperties) {
|
||||
this.properties = properties;
|
||||
this.discoveryProperties = discoveryProperties;
|
||||
resolver = new ServicePortSecureResolver(discoveryProperties);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -66,10 +74,11 @@ public class KubernetesClientServiceInstanceMapper implements KubernetesServiceI
|
||||
if (port == null) {
|
||||
return null;
|
||||
}
|
||||
final String host = KubernetesServiceInstanceMapper.createHost(service.getMetadata().getName(),
|
||||
String host = KubernetesServiceInstanceMapper.createHost(service.getMetadata().getName(),
|
||||
service.getMetadata().getNamespace(), properties.getClusterDomain());
|
||||
final boolean secure = KubernetesServiceInstanceMapper.isSecure(service.getMetadata().getLabels(),
|
||||
service.getMetadata().getAnnotations(), port.getName(), port.getPort());
|
||||
|
||||
boolean secure = secure(port, service);
|
||||
|
||||
return new DefaultKubernetesServiceInstance(meta.getUid(), meta.getName(), host, port.getPort(),
|
||||
getServiceMetadata(service), secure);
|
||||
}
|
||||
@@ -78,17 +87,24 @@ public class KubernetesClientServiceInstanceMapper implements KubernetesServiceI
|
||||
final Map<String, String> serviceMetadata = new HashMap<>();
|
||||
KubernetesDiscoveryProperties.Metadata metadataProps = this.discoveryProperties.metadata();
|
||||
if (metadataProps.addLabels()) {
|
||||
Map<String, String> labelMetadata = KubernetesServiceInstanceMapper
|
||||
.getMapWithPrefixedKeys(service.getMetadata().getLabels(), metadataProps.labelsPrefix());
|
||||
Map<String, String> labelMetadata = keysWithPrefix(service.getMetadata().getLabels(),
|
||||
metadataProps.labelsPrefix());
|
||||
serviceMetadata.putAll(labelMetadata);
|
||||
}
|
||||
if (metadataProps.addAnnotations()) {
|
||||
Map<String, String> annotationMetadata = KubernetesServiceInstanceMapper
|
||||
.getMapWithPrefixedKeys(service.getMetadata().getAnnotations(), metadataProps.annotationsPrefix());
|
||||
Map<String, String> annotationMetadata = keysWithPrefix(service.getMetadata().getAnnotations(),
|
||||
metadataProps.annotationsPrefix());
|
||||
serviceMetadata.putAll(annotationMetadata);
|
||||
}
|
||||
|
||||
return serviceMetadata;
|
||||
}
|
||||
|
||||
private boolean secure(V1ServicePort port, V1Service service) {
|
||||
V1ObjectMeta metadata = service.getMetadata();
|
||||
ServicePortNameAndNumber portNameAndNumber = new ServicePortNameAndNumber(port.getPort(), port.getName());
|
||||
Input input = new Input(portNameAndNumber, metadata.getName(), metadata.getLabels(), metadata.getAnnotations());
|
||||
return resolver.resolve(input);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,119 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.kubernetes.client.loadbalancer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
|
||||
import io.kubernetes.client.openapi.ApiClient;
|
||||
import io.kubernetes.client.openapi.apis.CoreV1Api;
|
||||
import io.kubernetes.client.util.ClientBuilder;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalancerRequest;
|
||||
import org.springframework.cloud.kubernetes.client.discovery.KubernetesInformerDiscoveryClient;
|
||||
import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider;
|
||||
import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.mock.http.client.MockClientHttpResponse;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author Ryan Baxter
|
||||
*/
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
||||
classes = KubernetesClientLoadBalancerPodModeTests.App.class)
|
||||
class KubernetesClientLoadBalancerPodModeTests {
|
||||
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@Test
|
||||
void testLoadBalancer() {
|
||||
String resp = restTemplate.getForObject("http://servicea-wiremock", String.class);
|
||||
assertThat(resp).isEqualTo("hello");
|
||||
}
|
||||
|
||||
@SpringBootApplication
|
||||
static class App {
|
||||
|
||||
@Bean
|
||||
CoreV1Api coreV1Api(ApiClient apiClient) {
|
||||
return new CoreV1Api(apiClient);
|
||||
}
|
||||
|
||||
@Bean
|
||||
ApiClient apiClient() {
|
||||
return new ClientBuilder().build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
BlockingLoadBalancerClient blockingLoadBalancerClient() {
|
||||
BlockingLoadBalancerClient client = mock(BlockingLoadBalancerClient.class);
|
||||
try {
|
||||
ClientHttpResponse response = new MockClientHttpResponse("hello".getBytes(), HttpStatus.OK);
|
||||
when(client.execute(eq("servicea-wiremock"), any(LoadBalancerRequest.class))).thenReturn(response);
|
||||
when(client.execute(eq("servicea-wiremock"), any(ServiceInstance.class),
|
||||
any(LoadBalancerRequest.class))).thenReturn(response);
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return client;
|
||||
}
|
||||
|
||||
@Bean
|
||||
KubernetesNamespaceProvider kubernetesNamespaceProvider() {
|
||||
KubernetesNamespaceProvider provider = mock(KubernetesNamespaceProvider.class);
|
||||
when(provider.getNamespace()).thenReturn("test");
|
||||
return provider;
|
||||
}
|
||||
|
||||
@Bean
|
||||
KubernetesInformerDiscoveryClient kubernetesInformerDiscoveryClient() {
|
||||
KubernetesInformerDiscoveryClient client = mock(KubernetesInformerDiscoveryClient.class);
|
||||
ServiceInstance instance = new DefaultServiceInstance("servicea-wiremock1", "servicea-wiremock", "fake",
|
||||
8888, false);
|
||||
given(client.getInstances(eq("servicea-wiremock"))).willReturn(Collections.singletonList(instance));
|
||||
return client;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@LoadBalanced
|
||||
RestTemplate restTemplate() {
|
||||
return new RestTemplateBuilder().build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,148 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.kubernetes.client.loadbalancer;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import io.kubernetes.client.openapi.ApiClient;
|
||||
import io.kubernetes.client.openapi.ApiException;
|
||||
import io.kubernetes.client.openapi.apis.CoreV1Api;
|
||||
import io.kubernetes.client.openapi.models.V1ObjectMetaBuilder;
|
||||
import io.kubernetes.client.openapi.models.V1ServiceBuilder;
|
||||
import io.kubernetes.client.openapi.models.V1ServiceList;
|
||||
import io.kubernetes.client.openapi.models.V1ServiceListBuilder;
|
||||
import io.kubernetes.client.openapi.models.V1ServicePortBuilder;
|
||||
import io.kubernetes.client.openapi.models.V1ServiceSpecBuilder;
|
||||
import io.kubernetes.client.util.ClientBuilder;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalancerRequest;
|
||||
import org.springframework.cloud.kubernetes.client.discovery.KubernetesInformerDiscoveryClient;
|
||||
import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider;
|
||||
import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.mock.http.client.MockClientHttpResponse;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author Ryan Baxter
|
||||
*/
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
||||
classes = KubernetesClientLoadBalancerServiceModeTests.App.class,
|
||||
properties = { "spring.cloud.kubernetes.loadbalancer.mode=SERVICE" })
|
||||
public class KubernetesClientLoadBalancerServiceModeTests {
|
||||
|
||||
private static final V1ServiceList SERVICE_LIST = new V1ServiceListBuilder()
|
||||
.addToItems(
|
||||
new V1ServiceBuilder()
|
||||
.withMetadata(new V1ObjectMetaBuilder().withName("servicea-wiremock")
|
||||
.withNamespace("default").withResourceVersion("1").addToLabels("beta", "true")
|
||||
.addToAnnotations("org.springframework.cloud", "true").withUid("0").build())
|
||||
.withSpec(new V1ServiceSpecBuilder().withClusterIP("10.96.0.1").withSessionAffinity("None")
|
||||
.withType("ClusterIP")
|
||||
.addToPorts(new V1ServicePortBuilder().withPort(80).withName("http")
|
||||
.withProtocol("TCP").withNewTargetPort(8080).build())
|
||||
.build())
|
||||
.build())
|
||||
.build();
|
||||
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@Test
|
||||
public void testLoadBalancer() {
|
||||
String resp = restTemplate.getForObject("http://servicea-wiremock", String.class);
|
||||
assertThat(resp).isEqualTo("hello");
|
||||
}
|
||||
|
||||
@SpringBootApplication
|
||||
static class App {
|
||||
|
||||
@Bean
|
||||
public ApiClient apiClient() {
|
||||
return new ClientBuilder().build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CoreV1Api coreV1Api() {
|
||||
CoreV1Api coreV1Api = mock(CoreV1Api.class);
|
||||
try {
|
||||
when(coreV1Api.listNamespacedService(eq("default"), eq(null), eq(null), eq(null),
|
||||
eq("metadata.name=servicea-wiremock"), eq(null), eq(null), eq(null), eq(null), eq(null),
|
||||
eq(null), eq(null))).thenReturn(SERVICE_LIST);
|
||||
}
|
||||
catch (ApiException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return coreV1Api;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public BlockingLoadBalancerClient blockingLoadBalancerClient() {
|
||||
BlockingLoadBalancerClient client = mock(BlockingLoadBalancerClient.class);
|
||||
try {
|
||||
ClientHttpResponse response = new MockClientHttpResponse("hello".getBytes(), HttpStatus.OK);
|
||||
when(client.execute(eq("servicea-wiremock"), any(LoadBalancerRequest.class))).thenReturn(response);
|
||||
when(client.execute(eq("servicea-wiremock"), any(ServiceInstance.class),
|
||||
any(LoadBalancerRequest.class))).thenReturn(response);
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return client;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public KubernetesInformerDiscoveryClient kubernetesInformerDiscoveryClient() {
|
||||
// Mock this so the real implementation does not try to connect to the K8S API
|
||||
// Server
|
||||
KubernetesInformerDiscoveryClient client = mock(KubernetesInformerDiscoveryClient.class);
|
||||
Mockito.when(client.getOrder()).thenReturn(0);
|
||||
return client;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public KubernetesNamespaceProvider kubernetesNamespaceProvider() {
|
||||
KubernetesNamespaceProvider provider = mock(KubernetesNamespaceProvider.class);
|
||||
when(provider.getNamespace()).thenReturn("test");
|
||||
return provider;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@LoadBalanced
|
||||
RestTemplate restTemplate() {
|
||||
return new RestTemplateBuilder().build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,12 +16,13 @@
|
||||
|
||||
package org.springframework.cloud.kubernetes.client.loadbalancer;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import io.kubernetes.client.openapi.models.V1ObjectMetaBuilder;
|
||||
import io.kubernetes.client.openapi.models.V1Service;
|
||||
import io.kubernetes.client.openapi.models.V1ServiceBuilder;
|
||||
import io.kubernetes.client.openapi.models.V1ServicePort;
|
||||
import io.kubernetes.client.openapi.models.V1ServicePortBuilder;
|
||||
import io.kubernetes.client.openapi.models.V1ServiceSpecBuilder;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -39,48 +40,75 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
class KubernetesClientServiceInstanceMapperTests {
|
||||
|
||||
@Test
|
||||
void basicMap() {
|
||||
void singlePortNonSecure() {
|
||||
KubernetesLoadBalancerProperties loadBalancerProperties = new KubernetesLoadBalancerProperties();
|
||||
KubernetesClientServiceInstanceMapper mapper = new KubernetesClientServiceInstanceMapper(loadBalancerProperties,
|
||||
KubernetesDiscoveryProperties.DEFAULT);
|
||||
|
||||
V1Service service = new V1ServiceBuilder()
|
||||
.withMetadata(new V1ObjectMetaBuilder().withName("database").withUid("0").withResourceVersion("0")
|
||||
.withNamespace("default").addToAnnotations("org.springframework.cloud", "true")
|
||||
.addToLabels("beta", "true").build())
|
||||
.withSpec(new V1ServiceSpecBuilder()
|
||||
.addToPorts(new V1ServicePortBuilder().withPort(80).withName("http").build()).build())
|
||||
.build();
|
||||
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()
|
||||
);
|
||||
V1Service service = createService("database", "default", annotations, labels, servicePorts);
|
||||
|
||||
KubernetesServiceInstance serviceInstance = mapper.map(service);
|
||||
Map<String, String> metadata = new HashMap<>();
|
||||
metadata.put("org.springframework.cloud", "true");
|
||||
metadata.put("beta", "true");
|
||||
Map<String, String> metadata = Map.of("org.springframework.cloud", "true", "beta", "true");
|
||||
DefaultKubernetesServiceInstance result = new DefaultKubernetesServiceInstance("0", "database",
|
||||
"database.default.svc.cluster.local", 80, metadata, false);
|
||||
assertThat(serviceInstance).isEqualTo(result);
|
||||
}
|
||||
|
||||
// has an annotation 'secured=true'
|
||||
@Test
|
||||
void multiportMap() {
|
||||
void singlePortSecure() {
|
||||
KubernetesLoadBalancerProperties loadBalancerProperties = new KubernetesLoadBalancerProperties();
|
||||
KubernetesClientServiceInstanceMapper mapper = new KubernetesClientServiceInstanceMapper(loadBalancerProperties,
|
||||
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()
|
||||
);
|
||||
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");
|
||||
DefaultKubernetesServiceInstance result = new DefaultKubernetesServiceInstance("0", "database",
|
||||
"database.default.svc.cluster.local", 80, metadata, true);
|
||||
assertThat(serviceInstance).isEqualTo(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void multiplePortsSecure() {
|
||||
KubernetesLoadBalancerProperties loadBalancerProperties = new KubernetesLoadBalancerProperties();
|
||||
loadBalancerProperties.setPortName("https");
|
||||
KubernetesClientServiceInstanceMapper mapper = new KubernetesClientServiceInstanceMapper(loadBalancerProperties,
|
||||
KubernetesDiscoveryProperties.DEFAULT);
|
||||
|
||||
V1Service service = new V1ServiceBuilder()
|
||||
.withMetadata(new V1ObjectMetaBuilder().withName("database").withUid("0").withResourceVersion("0")
|
||||
.withNamespace("default").build())
|
||||
.withSpec(new V1ServiceSpecBuilder()
|
||||
.addToPorts(new V1ServicePortBuilder().withPort(80).withName("http").build(),
|
||||
new V1ServicePortBuilder().withPort(443).withName("https").build())
|
||||
.build())
|
||||
.build();
|
||||
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()
|
||||
);
|
||||
V1Service service = createService("database", "default", annotations, labels, servicePorts);
|
||||
|
||||
Map<String, String> metadata = Map.of("org.springframework.cloud", "true", "beta", "true");
|
||||
KubernetesServiceInstance serviceInstance = mapper.map(service);
|
||||
DefaultKubernetesServiceInstance result = new DefaultKubernetesServiceInstance("0", "database",
|
||||
"database.default.svc.cluster.local", 443, Map.of(), true);
|
||||
"database.default.svc.cluster.local", 443, metadata, true);
|
||||
assertThat(serviceInstance).isEqualTo(result);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.cloud.kubernetes.commons.loadbalancer;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
@@ -25,8 +24,6 @@ import org.springframework.cloud.kubernetes.commons.discovery.KubernetesServiceI
|
||||
import org.springframework.core.log.LogAccessor;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.keysWithPrefix;
|
||||
|
||||
/**
|
||||
* @author Ryan Baxter
|
||||
*/
|
||||
@@ -44,41 +41,4 @@ public interface KubernetesServiceInstanceMapper<T> {
|
||||
return new StringJoiner(".").add(serviceName).add(namespaceToUse).add("svc").add(clusterDomain).toString();
|
||||
}
|
||||
|
||||
static boolean isSecure(Map<String, String> labels, Map<String, String> annotations, String servicePortName,
|
||||
Integer servicePort) {
|
||||
|
||||
if (hasTrueSecuredValue(labels)) {
|
||||
LOG.debug(() -> "Service has a true 'secured' label");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (hasTrueSecuredValue(annotations)) {
|
||||
LOG.debug(() -> "Service has a true 'secured' annotation");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (servicePortName != null && servicePortName.endsWith("https")) {
|
||||
LOG.debug(() -> "Service port name ends with 'https'");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (servicePort != null && servicePort.toString().endsWith("443")) {
|
||||
LOG.debug(() -> "Service port ends with '443'");
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static Map<String, String> getMapWithPrefixedKeys(Map<String, String> map, String prefix) {
|
||||
return keysWithPrefix(map, prefix);
|
||||
}
|
||||
|
||||
private static boolean hasTrueSecuredValue(Map<String, String> input) {
|
||||
if (input != null) {
|
||||
return "true".equals(input.get("secured"));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2023 the original author or authors.
|
||||
* Copyright 2013-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,12 +16,9 @@
|
||||
|
||||
package org.springframework.cloud.kubernetes.commons.loadbalancer;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.boot.test.system.CapturedOutput;
|
||||
import org.springframework.boot.test.system.OutputCaptureExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -32,14 +29,6 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
@ExtendWith(OutputCaptureExtension.class)
|
||||
class KubernetesServiceInstanceMapperTests {
|
||||
|
||||
private static final String SECURED_LABEL_MESSAGE = "Service has a true 'secured' label";
|
||||
|
||||
private static final String SECURED_ANNOTATION_MESSAGE = "Service has a true 'secured' annotation";
|
||||
|
||||
private static final String NAME_ENDS_IN_HTTPS_MESSAGE = "Service port name ends with 'https'";
|
||||
|
||||
private static final String PORT_ENDS_IN_443_MESSAGE = "Service port ends with '443'";
|
||||
|
||||
@Test
|
||||
void testCreateHostWithNamespace() {
|
||||
String namespace = "customNamespace";
|
||||
@@ -59,136 +48,4 @@ class KubernetesServiceInstanceMapperTests {
|
||||
assertThat(host).isEqualTo("serviceName.default.svc.clusterDomain");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIsSecureWithTrueLabel(CapturedOutput output) {
|
||||
Map<String, String> labels = Map.of("secured", "true");
|
||||
Map<String, String> annotations = Map.of();
|
||||
String servicePortName = null;
|
||||
Integer servicePort = null;
|
||||
assertThat(KubernetesServiceInstanceMapper.isSecure(labels, annotations, servicePortName, servicePort))
|
||||
.isTrue();
|
||||
|
||||
assertThat(output.getOut().contains(SECURED_LABEL_MESSAGE)).isTrue();
|
||||
assertThat(output.getOut().contains(SECURED_ANNOTATION_MESSAGE)).isFalse();
|
||||
assertThat(output.getOut().contains(NAME_ENDS_IN_HTTPS_MESSAGE)).isFalse();
|
||||
assertThat(output.getOut().contains(PORT_ENDS_IN_443_MESSAGE)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIsSecureWithTrueAnnotation(CapturedOutput output) {
|
||||
// empty labels
|
||||
Map<String, String> labels = Map.of();
|
||||
Map<String, String> annotations = Map.of("secured", "true");
|
||||
String servicePortName = null;
|
||||
Integer servicePort = null;
|
||||
assertThat(KubernetesServiceInstanceMapper.isSecure(labels, annotations, servicePortName, servicePort))
|
||||
.isTrue();
|
||||
|
||||
assertThat(output.getOut().contains(SECURED_LABEL_MESSAGE)).isFalse();
|
||||
assertThat(output.getOut().contains(SECURED_ANNOTATION_MESSAGE)).isTrue();
|
||||
assertThat(output.getOut().contains(NAME_ENDS_IN_HTTPS_MESSAGE)).isFalse();
|
||||
assertThat(output.getOut().contains(PORT_ENDS_IN_443_MESSAGE)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIsSecureWithTrueAnnotationNullLabels(CapturedOutput output) {
|
||||
// null labels
|
||||
Map<String, String> labels = null;
|
||||
Map<String, String> annotations = Map.of("secured", "true");
|
||||
String servicePortName = null;
|
||||
Integer servicePort = null;
|
||||
assertThat(KubernetesServiceInstanceMapper.isSecure(labels, annotations, servicePortName, servicePort))
|
||||
.isTrue();
|
||||
|
||||
assertThat(output.getOut().contains(SECURED_LABEL_MESSAGE)).isFalse();
|
||||
assertThat(output.getOut().contains(SECURED_ANNOTATION_MESSAGE)).isTrue();
|
||||
assertThat(output.getOut().contains(NAME_ENDS_IN_HTTPS_MESSAGE)).isFalse();
|
||||
assertThat(output.getOut().contains(PORT_ENDS_IN_443_MESSAGE)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIsNotSecureServicePortNameAndServicePortAreNull(CapturedOutput output) {
|
||||
// null labels
|
||||
Map<String, String> labels = null;
|
||||
// null annotations
|
||||
Map<String, String> annotations = null;
|
||||
String servicePortName = null;
|
||||
Integer servicePort = null;
|
||||
assertThat(KubernetesServiceInstanceMapper.isSecure(labels, annotations, servicePortName, servicePort))
|
||||
.isFalse();
|
||||
|
||||
assertThat(output.getOut().contains(SECURED_LABEL_MESSAGE)).isFalse();
|
||||
assertThat(output.getOut().contains(SECURED_ANNOTATION_MESSAGE)).isFalse();
|
||||
assertThat(output.getOut().contains(NAME_ENDS_IN_HTTPS_MESSAGE)).isFalse();
|
||||
assertThat(output.getOut().contains(PORT_ENDS_IN_443_MESSAGE)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIsNotSecureServicePortNameDoesNotMatch(CapturedOutput output) {
|
||||
// null labels
|
||||
Map<String, String> labels = null;
|
||||
// null annotations
|
||||
Map<String, String> annotations = null;
|
||||
String servicePortName = "abc_https_def";
|
||||
Integer servicePort = null;
|
||||
assertThat(KubernetesServiceInstanceMapper.isSecure(labels, annotations, servicePortName, servicePort))
|
||||
.isFalse();
|
||||
|
||||
assertThat(output.getOut().contains(SECURED_LABEL_MESSAGE)).isFalse();
|
||||
assertThat(output.getOut().contains(SECURED_ANNOTATION_MESSAGE)).isFalse();
|
||||
assertThat(output.getOut().contains(NAME_ENDS_IN_HTTPS_MESSAGE)).isFalse();
|
||||
assertThat(output.getOut().contains(PORT_ENDS_IN_443_MESSAGE)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIsSecureServicePortNameMatches(CapturedOutput output) {
|
||||
// null labels
|
||||
Map<String, String> labels = null;
|
||||
// null annotations
|
||||
Map<String, String> annotations = null;
|
||||
String servicePortName = "abc_https";
|
||||
Integer servicePort = null;
|
||||
assertThat(KubernetesServiceInstanceMapper.isSecure(labels, annotations, servicePortName, servicePort))
|
||||
.isTrue();
|
||||
|
||||
assertThat(output.getOut().contains(SECURED_LABEL_MESSAGE)).isFalse();
|
||||
assertThat(output.getOut().contains(SECURED_ANNOTATION_MESSAGE)).isFalse();
|
||||
assertThat(output.getOut().contains(NAME_ENDS_IN_HTTPS_MESSAGE)).isTrue();
|
||||
assertThat(output.getOut().contains(PORT_ENDS_IN_443_MESSAGE)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIsNotSecureServicePortDoesNotMatch(CapturedOutput output) {
|
||||
// null labels
|
||||
Map<String, String> labels = null;
|
||||
// null annotations
|
||||
Map<String, String> annotations = null;
|
||||
String servicePortName = null;
|
||||
Integer servicePort = 444;
|
||||
assertThat(KubernetesServiceInstanceMapper.isSecure(labels, annotations, servicePortName, servicePort))
|
||||
.isFalse();
|
||||
|
||||
assertThat(output.getOut().contains(SECURED_LABEL_MESSAGE)).isFalse();
|
||||
assertThat(output.getOut().contains(SECURED_ANNOTATION_MESSAGE)).isFalse();
|
||||
assertThat(output.getOut().contains(NAME_ENDS_IN_HTTPS_MESSAGE)).isFalse();
|
||||
assertThat(output.getOut().contains(PORT_ENDS_IN_443_MESSAGE)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIsSecureServicePortMatches(CapturedOutput output) {
|
||||
// null labels
|
||||
Map<String, String> labels = null;
|
||||
// null annotations
|
||||
Map<String, String> annotations = null;
|
||||
String servicePortName = null;
|
||||
Integer servicePort = 443;
|
||||
assertThat(KubernetesServiceInstanceMapper.isSecure(labels, annotations, servicePortName, servicePort))
|
||||
.isTrue();
|
||||
|
||||
assertThat(output.getOut().contains(SECURED_LABEL_MESSAGE)).isFalse();
|
||||
assertThat(output.getOut().contains(SECURED_ANNOTATION_MESSAGE)).isFalse();
|
||||
assertThat(output.getOut().contains(NAME_ENDS_IN_HTTPS_MESSAGE)).isFalse();
|
||||
assertThat(output.getOut().contains(PORT_ENDS_IN_443_MESSAGE)).isTrue();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -122,7 +122,7 @@ public class Fabric8ServiceInstanceMapper implements KubernetesServiceInstanceMa
|
||||
return DiscoveryClientUtils.serviceInstanceMetadata(PORTS_DATA, serviceMetadata, discoveryProperties);
|
||||
}
|
||||
|
||||
boolean secure(ServicePort port, Service service) {
|
||||
private boolean secure(ServicePort port, Service service) {
|
||||
ObjectMeta metadata = service.getMetadata();
|
||||
ServicePortNameAndNumber portNameAndNumber = new ServicePortNameAndNumber(port.getPort(), port.getName());
|
||||
Input input = new Input(portNameAndNumber, metadata.getName(), metadata.getLabels(), metadata.getAnnotations());
|
||||
|
||||
Reference in New Issue
Block a user