Cleanup fabric8 loadbalancer mapper part 4 (#1609)
This commit is contained in:
@@ -16,8 +16,10 @@
|
||||
|
||||
package org.springframework.cloud.kubernetes.commons.discovery;
|
||||
|
||||
import jakarta.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* @author wind57
|
||||
*/
|
||||
public record ServicePortNameAndNumber(int portNumber, String portName) {
|
||||
public record ServicePortNameAndNumber(@Nullable Integer portNumber, @Nullable String portName) {
|
||||
}
|
||||
|
||||
@@ -54,6 +54,8 @@ public final class ServicePortSecureResolver {
|
||||
String serviceName = input.serviceName();
|
||||
ServicePortNameAndNumber portData = input.portData();
|
||||
|
||||
Integer portNumber = portData.portNumber();
|
||||
|
||||
Optional<String> securedLabelValue = Optional.ofNullable(input.serviceLabels().get(SECURED));
|
||||
if (securedLabelValue.isPresent() && TRUTHY_STRINGS.contains(securedLabelValue.get())) {
|
||||
logEntry(serviceName, portData.portNumber(), "the service contains a true value for the 'secured' label");
|
||||
@@ -67,7 +69,7 @@ public final class ServicePortSecureResolver {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (properties.knownSecurePorts().contains(portData.portNumber())) {
|
||||
if (portNumber != null && properties.knownSecurePorts().contains(portData.portNumber())) {
|
||||
logEntry(serviceName, portData.portNumber(), "port is known to be a https port");
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -30,10 +30,14 @@ import org.springframework.cloud.kubernetes.commons.discovery.DiscoveryClientUti
|
||||
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.cloud.kubernetes.fabric8.Fabric8Utils;
|
||||
|
||||
import static org.springframework.cloud.kubernetes.commons.discovery.ServicePortSecureResolver.Input;
|
||||
|
||||
/**
|
||||
* Class for mapping Kubernetes Service object into {@link KubernetesServiceInstance}.
|
||||
*
|
||||
@@ -50,10 +54,13 @@ public class Fabric8ServiceInstanceMapper implements KubernetesServiceInstanceMa
|
||||
|
||||
private final KubernetesDiscoveryProperties discoveryProperties;
|
||||
|
||||
private final ServicePortSecureResolver resolver;
|
||||
|
||||
Fabric8ServiceInstanceMapper(KubernetesLoadBalancerProperties properties,
|
||||
KubernetesDiscoveryProperties discoveryProperties) {
|
||||
this.properties = properties;
|
||||
this.discoveryProperties = discoveryProperties;
|
||||
resolver = new ServicePortSecureResolver(discoveryProperties);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -76,8 +83,9 @@ public class Fabric8ServiceInstanceMapper implements KubernetesServiceInstanceMa
|
||||
}
|
||||
String host = KubernetesServiceInstanceMapper.createHost(service.getMetadata().getName(),
|
||||
service.getMetadata().getNamespace(), properties.getClusterDomain());
|
||||
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(),
|
||||
serviceMetadata(service), secure);
|
||||
}
|
||||
@@ -87,4 +95,11 @@ public class Fabric8ServiceInstanceMapper implements KubernetesServiceInstanceMa
|
||||
return DiscoveryClientUtils.serviceInstanceMetadata(PORTS_DATA, serviceMetadata, discoveryProperties);
|
||||
}
|
||||
|
||||
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());
|
||||
return resolver.resolve(input);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -66,8 +66,13 @@ class Fabric8ServiceInstanceMapperTests {
|
||||
void testMapperSecure() {
|
||||
KubernetesLoadBalancerProperties properties = new KubernetesLoadBalancerProperties();
|
||||
Service service = buildService("test", "test-namespace", "abc", 443, null, Map.of());
|
||||
KubernetesServiceInstance instance = new Fabric8ServiceInstanceMapper(properties,
|
||||
KubernetesDiscoveryProperties.DEFAULT).map(service);
|
||||
|
||||
KubernetesDiscoveryProperties discoveryProperties = new KubernetesDiscoveryProperties(true, false, Set.of(),
|
||||
true, 60, false, null, Set.of(443, 8443), Map.of(), null,
|
||||
KubernetesDiscoveryProperties.Metadata.DEFAULT, 0, false, false, null);
|
||||
|
||||
KubernetesServiceInstance instance = new Fabric8ServiceInstanceMapper(properties, discoveryProperties)
|
||||
.map(service);
|
||||
Assertions.assertNotNull(instance);
|
||||
Assertions.assertEquals("test", instance.getServiceId());
|
||||
Assertions.assertEquals("abc", instance.getInstanceId());
|
||||
@@ -78,8 +83,8 @@ class Fabric8ServiceInstanceMapperTests {
|
||||
void testMapperSecureNullLabelsAndAnnotations() {
|
||||
KubernetesLoadBalancerProperties properties = new KubernetesLoadBalancerProperties();
|
||||
KubernetesDiscoveryProperties discoveryProperties = new KubernetesDiscoveryProperties(true, true, Set.of(),
|
||||
true, 60, false, null, Set.of(), Map.of(), null, KubernetesDiscoveryProperties.Metadata.DEFAULT, 0,
|
||||
false);
|
||||
true, 60, false, null, Set.of(443, 8443), Map.of(), null,
|
||||
KubernetesDiscoveryProperties.Metadata.DEFAULT, 0, false);
|
||||
List<ServicePort> ports = new ArrayList<>();
|
||||
ports.add(new ServicePortBuilder().withPort(443).build());
|
||||
Service service = buildService("test", "test-namespace", "abc", ports, null, null);
|
||||
|
||||
Reference in New Issue
Block a user