move subsets to a record + add default instance for metadata (#1112)

This commit is contained in:
erabii
2022-10-18 23:17:08 +03:00
committed by GitHub
parent ab8aa793cf
commit b30d4a4257
5 changed files with 19 additions and 54 deletions

View File

@@ -113,7 +113,7 @@ public class KubernetesInformerDiscoveryClientTests {
Lister<V1Endpoints> endpointsLister = setupEndpointsLister(testEndpointWithUnsetPortName);
when(kubernetesDiscoveryProperties.isAllNamespaces()).thenReturn(true);
when(kubernetesDiscoveryProperties.getMetadata()).thenReturn(new KubernetesDiscoveryProperties.Metadata());
when(kubernetesDiscoveryProperties.getMetadata()).thenReturn(KubernetesDiscoveryProperties.Metadata.DEFAULT);
KubernetesInformerDiscoveryClient discoveryClient = new KubernetesInformerDiscoveryClient("",
sharedInformerFactory, serviceLister, endpointsLister, null, null, kubernetesDiscoveryProperties);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2019 the original author or authors.
* Copyright 2013-2022 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.
@@ -24,7 +24,6 @@ import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.core.style.ToStringCreator;
@@ -77,7 +76,7 @@ public class KubernetesDiscoveryProperties {
*/
private String primaryPortName;
private Metadata metadata = new Metadata();
private Metadata metadata = Metadata.DEFAULT;
private int order = DEFAULT_ORDER;
@@ -188,15 +187,10 @@ public class KubernetesDiscoveryProperties {
@DefaultValue("true") boolean addAnnotations, String annotationsPrefix,
@DefaultValue("true") boolean addPorts, @DefaultValue("port.") String portsPrefix) {
@ConstructorBinding
public Metadata {
}
// needed in order to get the defaults for some fields
public Metadata() {
this(true, null, true, null, true, "port.");
}
/**
* Default instance.
*/
public static final Metadata DEFAULT = new Metadata(true, null, true, null, true, "port.");
}

View File

@@ -32,7 +32,7 @@ class KubernetesDiscoveryPropertiesMetadataTests {
@Test
void testDefaultConstructor() {
Metadata m = new Metadata();
Metadata m = Metadata.DEFAULT;
assertThat(m.addLabels()).isTrue();
assertThat(m.labelsPrefix()).isNull();
assertThat(m.addAnnotations()).isTrue();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2022 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,7 +16,6 @@
package org.springframework.cloud.kubernetes.fabric8.discovery;
import java.util.ArrayList;
import java.util.List;
import io.fabric8.kubernetes.api.model.EndpointSubset;
@@ -24,36 +23,11 @@ import io.fabric8.kubernetes.api.model.EndpointSubset;
/**
* @author Haytham Mohamed
**/
public class EndpointSubsetNS {
private String namespace;
private List<EndpointSubset> endpointSubset = new ArrayList<>();
public String getNamespace() {
return namespace;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
public List<EndpointSubset> getEndpointSubset() {
return endpointSubset;
}
public void setEndpointSubset(List<EndpointSubset> endpointSubset) {
this.endpointSubset = endpointSubset;
}
@Override
public boolean equals(Object o) {
return this.endpointSubset.equals(o);
}
@Override
public int hashCode() {
return this.endpointSubset.hashCode();
record EndpointSubsetNS(String namespace, List<EndpointSubset> endpointSubset) {
EndpointSubsetNS {
if (endpointSubset == null) {
endpointSubset = List.of();
}
}
}

View File

@@ -129,8 +129,8 @@ public class KubernetesDiscoveryClient implements DiscoveryClient {
}
private List<ServiceInstance> getNamespaceServiceInstances(EndpointSubsetNS es, String serviceId) {
String namespace = es.getNamespace();
List<EndpointSubset> subsets = es.getEndpointSubset();
String namespace = es.namespace();
List<EndpointSubset> subsets = es.endpointSubset();
List<ServiceInstance> instances = new ArrayList<>();
if (!subsets.isEmpty()) {
final Service service = this.client.services().inNamespace(namespace).withName(serviceId).get();
@@ -249,13 +249,10 @@ public class KubernetesDiscoveryClient implements DiscoveryClient {
}
private EndpointSubsetNS getSubsetsFromEndpoints(Endpoints endpoints) {
EndpointSubsetNS es = new EndpointSubsetNS();
es.setNamespace(this.client.getNamespace()); // start with the default that comes
// with the client
// start with the default that comes with the client
EndpointSubsetNS es = new EndpointSubsetNS(this.client.getNamespace(), null);
if (endpoints != null && endpoints.getSubsets() != null) {
es.setNamespace(endpoints.getMetadata().getNamespace());
es.setEndpointSubset(endpoints.getSubsets());
es = new EndpointSubsetNS(endpoints.getMetadata().getNamespace(), endpoints.getSubsets());
}
return es;