Add externalname support (#1243)

This commit is contained in:
erabii
2023-03-08 23:53:35 +02:00
committed by GitHub
parent b9f9780e5a
commit 8438ff60da
18 changed files with 738 additions and 134 deletions

View File

@@ -102,6 +102,10 @@ public record DefaultKubernetesServiceInstance(String instanceId, String service
}
private URI createUri(String scheme, String host, int port) {
// assume ExternalName type of service
if (port == -1) {
return URI.create(host);
}
return URI.create(scheme + "://" + host + ":" + port);
}
}

View File

@@ -67,4 +67,14 @@ public final class KubernetesDiscoveryConstants {
*/
public static final String ENDPOINT_SLICE = "EndpointSlice";
/**
* ExternalName type of service.
*/
public static final String EXTERNAL_NAME = "ExternalName";
/**
* Type of the service.
*/
public static final String SERVICE_TYPE = "type";
}

View File

@@ -20,6 +20,7 @@ import java.util.Map;
import java.util.Set;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.bind.ConstructorBinding;
import org.springframework.boot.context.properties.bind.DefaultValue;
import static org.springframework.cloud.client.discovery.DiscoveryClient.DEFAULT_ORDER;
@@ -43,6 +44,8 @@ import static org.springframework.cloud.client.discovery.DiscoveryClient.DEFAULT
* @param primaryPortName If set then the port with a given name is used as primary when
* multiple ports are defined for a service.
* @param useEndpointSlices use EndpointSlice instead of Endpoints
* @param includeExternalNameServices should the discovery also search for services that
* have "type: ExternalName" in their spec.
*/
// @formatter:off
@ConfigurationProperties("spring.cloud.kubernetes.discovery")
@@ -56,14 +59,31 @@ public record KubernetesDiscoveryProperties(
@DefaultValue Map<String, String> serviceLabels, String primaryPortName,
@DefaultValue Metadata metadata,
@DefaultValue("" + DEFAULT_ORDER) int order,
boolean useEndpointSlices) {
boolean useEndpointSlices,
boolean includeExternalNameServices) {
// @formatter:on
@ConstructorBinding
public KubernetesDiscoveryProperties {
}
public KubernetesDiscoveryProperties(@DefaultValue("true") boolean enabled, boolean allNamespaces,
@DefaultValue Set<String> namespaces, @DefaultValue("true") boolean waitCacheReady,
@DefaultValue("60") long cacheLoadingTimeoutSeconds, boolean includeNotReadyAddresses, String filter,
@DefaultValue({ "443", "8443" }) Set<Integer> knownSecurePorts,
@DefaultValue Map<String, String> serviceLabels, String primaryPortName, @DefaultValue Metadata metadata,
@DefaultValue("" + DEFAULT_ORDER) int order, boolean useEndpointSlices) {
this(enabled, allNamespaces, namespaces, waitCacheReady, cacheLoadingTimeoutSeconds, includeNotReadyAddresses,
filter, knownSecurePorts, serviceLabels, primaryPortName, metadata, order, useEndpointSlices, false);
}
/**
* Default instance.
*/
public static final KubernetesDiscoveryProperties DEFAULT = new KubernetesDiscoveryProperties(true, false, Set.of(),
true, 60, false, null, Set.of(), Map.of(), null, KubernetesDiscoveryProperties.Metadata.DEFAULT, 0, false);
true, 60, false, null, Set.of(), Map.of(), null, KubernetesDiscoveryProperties.Metadata.DEFAULT, 0, false,
false);
/**
* @param addLabels include labels as metadata

View File

@@ -53,6 +53,7 @@ class KubernetesDiscoveryPropertiesTests {
assertThat(props.primaryPortName()).isNull();
assertThat(props.order()).isZero();
assertThat(props.useEndpointSlices()).isFalse();
assertThat(props.includeExternalNameServices()).isFalse();
});
}
@@ -64,7 +65,8 @@ class KubernetesDiscoveryPropertiesTests {
"spring.cloud.kubernetes.discovery.metadata.labelsPrefix=labelsPrefix",
"spring.cloud.kubernetes.discovery.use-endpoint-slices=true",
"spring.cloud.kubernetes.discovery.namespaces[0]=ns1",
"spring.cloud.kubernetes.discovery.namespaces[1]=ns2")
"spring.cloud.kubernetes.discovery.namespaces[1]=ns2",
"spring.cloud.kubernetes.discovery.include-external-name-services=true")
.run(context -> {
KubernetesDiscoveryProperties props = context.getBean(KubernetesDiscoveryProperties.class);
assertThat(props).isNotNull();
@@ -84,6 +86,7 @@ class KubernetesDiscoveryPropertiesTests {
assertThat(props.primaryPortName()).isNull();
assertThat(props.order()).isZero();
assertThat(props.useEndpointSlices()).isTrue();
assertThat(props.includeExternalNameServices()).isTrue();
});
}