add pod metadata and annotations as part of the service discovery response (#1254)

This commit is contained in:
erabii
2023-03-15 01:57:41 +02:00
committed by GitHub
parent f320f56b1f
commit e099d73cac
12 changed files with 536 additions and 26 deletions

View File

@@ -35,8 +35,8 @@ import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesD
* @param cluster the cluster the service resides in.
*/
public record DefaultKubernetesServiceInstance(String instanceId, String serviceId, String host, int port,
Map<String, String> metadata, boolean secure, String namespace,
String cluster) implements KubernetesServiceInstance {
Map<String, String> metadata, boolean secure, String namespace, String cluster,
Map<String, Map<String, String>> podMetadata) implements KubernetesServiceInstance {
/**
* @param instanceId the id of the instance.
@@ -48,7 +48,12 @@ public record DefaultKubernetesServiceInstance(String instanceId, String service
*/
public DefaultKubernetesServiceInstance(String instanceId, String serviceId, String host, int port,
Map<String, String> metadata, boolean secure) {
this(instanceId, serviceId, host, port, metadata, secure, null, null);
this(instanceId, serviceId, host, port, metadata, secure, null, null, Map.of());
}
public DefaultKubernetesServiceInstance(String instanceId, String serviceId, String host, int port,
Map<String, String> metadata, boolean secure, String namespace, String cluster) {
this(instanceId, serviceId, host, port, metadata, secure, namespace, cluster, Map.of());
}
@Override
@@ -101,6 +106,11 @@ public record DefaultKubernetesServiceInstance(String instanceId, String service
return this.cluster;
}
@Override
public Map<String, Map<String, String>> podMetadata() {
return podMetadata;
}
private URI createUri(String scheme, String host, int port) {
// assume ExternalName type of service
if (port == -1) {

View File

@@ -92,15 +92,30 @@ public record KubernetesDiscoveryProperties(
* @param annotationsPrefix prefix for the annotations
* @param addPorts include ports as metadata
* @param portsPrefix prefix for the ports, by default it is "port."
* @param addPodLabels add pod labels as part of the response.
* @param addPodAnnotations add pod annotations as part of the response.
*/
public record Metadata(@DefaultValue("true") boolean addLabels, String labelsPrefix,
@DefaultValue("true") boolean addAnnotations, String annotationsPrefix,
@DefaultValue("true") boolean addPorts, @DefaultValue("port.") String portsPrefix) {
@DefaultValue("true") boolean addPorts, @DefaultValue("port.") String portsPrefix, boolean addPodLabels,
boolean addPodAnnotations) {
@ConstructorBinding
public Metadata {
}
public Metadata(@DefaultValue("true") boolean addLabels, String labelsPrefix,
@DefaultValue("true") boolean addAnnotations, String annotationsPrefix,
@DefaultValue("true") boolean addPorts, @DefaultValue("port.") String portsPrefix) {
this(addLabels, labelsPrefix, addAnnotations, annotationsPrefix, addPorts, portsPrefix, false, false);
}
/**
* Default instance.
*/
public static final Metadata DEFAULT = new Metadata(true, null, true, null, true, "port.");
public static final Metadata DEFAULT = new Metadata(true, null, true, null, true, "port.", false, false);
}

View File

@@ -16,6 +16,8 @@
package org.springframework.cloud.kubernetes.commons.discovery;
import java.util.Map;
import org.springframework.cloud.client.ServiceInstance;
/**
@@ -29,4 +31,8 @@ sealed public interface KubernetesServiceInstance extends ServiceInstance permit
String getCluster();
default Map<String, Map<String, String>> podMetadata() {
return Map.of();
}
}

View File

@@ -39,18 +39,24 @@ class KubernetesDiscoveryPropertiesMetadataTests {
assertThat(m.annotationsPrefix()).isNull();
assertThat(m.addPorts()).isTrue();
assertThat(m.portsPrefix()).isEqualTo("port.");
assertThat(m.addPodLabels()).isFalse();
assertThat(m.addPodAnnotations()).isFalse();
}
@Test
void testSpringBindingFields() {
new ApplicationContextRunner().withUserConfiguration(Config.class)
.withPropertyValues("spring.cloud.kubernetes.discovery.metadata.labelsPrefix=labelsPrefix")
.withPropertyValues("spring.cloud.kubernetes.discovery.metadata.labelsPrefix=labelsPrefix",
"spring.cloud.kubernetes.discovery.metadata.add-pod-annotations=true",
"spring.cloud.kubernetes.discovery.metadata.add-pod-labels=true")
.run(context -> {
KubernetesDiscoveryProperties props = context.getBean(KubernetesDiscoveryProperties.class);
assertThat(props).isNotNull();
assertThat(props.metadata().labelsPrefix()).isEqualTo("labelsPrefix");
assertThat(props.metadata().addPorts()).isTrue();
assertThat(props.metadata().portsPrefix()).isEqualTo("port.");
assertThat(props.metadata().addPodLabels()).isTrue();
assertThat(props.metadata().addPodAnnotations()).isTrue();
});
}

View File

@@ -44,6 +44,7 @@ class KubernetesServiceInstanceTests {
assertThat(instance.getScheme()).isEqualTo("https");
assertThat(instance.getNamespace()).isEqualTo("spring-k8s");
assertThat(instance.getCluster()).isNull();
assertThat(instance.podMetadata()).isEqualTo(Map.of());
}
@Test
@@ -61,6 +62,27 @@ class KubernetesServiceInstanceTests {
assertThat(instance.getScheme()).isEqualTo("https");
assertThat(instance.getNamespace()).isEqualTo("spring-k8s");
assertThat(instance.getCluster()).isEqualTo("cluster");
assertThat(instance.podMetadata()).isEqualTo(Map.of());
}
@Test
void testThirdConstructor() {
DefaultKubernetesServiceInstance instance = new DefaultKubernetesServiceInstance("instanceId", "serviceId",
"host", 8080, Map.of("a", "b"), true, "spring-k8s", "cluster",
Map.of("labels", Map.of("a", "b"), "annotations", Map.of("c", "d")));
assertThat(instance.getInstanceId()).isEqualTo("instanceId");
assertThat(instance.getServiceId()).isEqualTo("serviceId");
assertThat(instance.getHost()).isEqualTo("host");
assertThat(instance.getPort()).isEqualTo(8080);
assertThat(instance.isSecure()).isTrue();
assertThat(instance.getUri()).isEqualTo(URI.create("https://host:8080"));
assertThat(instance.getMetadata()).isEqualTo(Map.of("a", "b"));
assertThat(instance.getScheme()).isEqualTo("https");
assertThat(instance.getNamespace()).isEqualTo("spring-k8s");
assertThat(instance.getCluster()).isEqualTo("cluster");
assertThat(instance.podMetadata())
.isEqualTo(Map.of("labels", Map.of("a", "b"), "annotations", Map.of("c", "d")));
}
@Test