Skip to content

Commit

Permalink
Namespace filter for service discover - resolve git conflicts (issue s…
Browse files Browse the repository at this point in the history
  • Loading branch information
mbialkowski1 committed Oct 20, 2022
1 parent b983d3a commit 548b277
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 129 deletions.
Expand Up @@ -108,7 +108,8 @@ public void testServiceWithUnsetPortNames() {
Lister<V1Endpoints> endpointsLister = setupEndpointsLister(testEndpointWithUnsetPortName);

KubernetesDiscoveryProperties kubernetesDiscoveryProperties = new KubernetesDiscoveryProperties(true, true,
true, 60, false, null, Set.of(), Map.of(), null, KubernetesDiscoveryProperties.Metadata.DEFAULT, 0);
Set.of(), true, 60, false, null, Set.of(), Map.of(), null,
KubernetesDiscoveryProperties.Metadata.DEFAULT, 0);

KubernetesInformerDiscoveryClient discoveryClient = new KubernetesInformerDiscoveryClient("",
sharedInformerFactory, serviceLister, endpointsLister, null, null, kubernetesDiscoveryProperties);
Expand Down Expand Up @@ -141,7 +142,7 @@ public void testDiscoveryWithServiceLabels() {
labels.put("spring", "true");

KubernetesDiscoveryProperties kubernetesDiscoveryProperties = new KubernetesDiscoveryProperties(true, true,
true, 60, false, null, Set.of(), labels, null, null, 0);
Set.of(), true, 60, false, null, Set.of(), labels, null, null, 0);

KubernetesInformerDiscoveryClient discoveryClient = new KubernetesInformerDiscoveryClient("",
sharedInformerFactory, serviceLister, null, null, null, kubernetesDiscoveryProperties);
Expand All @@ -160,7 +161,7 @@ public void testDiscoveryInstancesWithServiceLabels() {
labels.put("spring", "true");

KubernetesDiscoveryProperties kubernetesDiscoveryProperties = new KubernetesDiscoveryProperties(true, true,
true, 60, false, null, Set.of(), labels, null, null, 0);
Set.of(), true, 60, false, null, Set.of(), labels, null, null, 0);

KubernetesInformerDiscoveryClient discoveryClient = new KubernetesInformerDiscoveryClient("",
sharedInformerFactory, serviceLister, endpointsLister, null, null, kubernetesDiscoveryProperties);
Expand Down Expand Up @@ -188,7 +189,7 @@ public void testDiscoveryGetInstanceAllNamespaceShouldWork() {
Lister<V1Endpoints> endpointsLister = setupEndpointsLister(testEndpoints1);

KubernetesDiscoveryProperties kubernetesDiscoveryProperties = new KubernetesDiscoveryProperties(true, true,
true, 60, false, null, Set.of(), null, null, null, 0);
Set.of(), true, 60, false, null, Set.of(), null, null, null, 0);

KubernetesInformerDiscoveryClient discoveryClient = new KubernetesInformerDiscoveryClient("",
sharedInformerFactory, serviceLister, endpointsLister, null, null, kubernetesDiscoveryProperties);
Expand All @@ -203,7 +204,7 @@ public void testDiscoveryGetInstanceOneNamespaceShouldWork() {
Lister<V1Endpoints> endpointsLister = setupEndpointsLister(testEndpoints1);

KubernetesDiscoveryProperties kubernetesDiscoveryProperties = new KubernetesDiscoveryProperties(true, false,
true, 60, false, null, Set.of(), null, null, null, 0);
Set.of(), true, 60, false, null, Set.of(), null, null, null, 0);

KubernetesInformerDiscoveryClient discoveryClient = new KubernetesInformerDiscoveryClient("namespace1",
sharedInformerFactory, serviceLister, endpointsLister, null, null, kubernetesDiscoveryProperties);
Expand All @@ -230,7 +231,7 @@ public void testDiscoveryGetInstanceWithNotReadyAddressesIncludedShouldWork() {
Lister<V1Endpoints> endpointsLister = setupEndpointsLister(testEndpointWithoutReadyAddresses);

KubernetesDiscoveryProperties kubernetesDiscoveryProperties = new KubernetesDiscoveryProperties(true, false,
true, 60, true, null, Set.of(), null, null, null, 0);
Set.of(), true, 60, true, null, Set.of(), null, null, null, 0);

KubernetesInformerDiscoveryClient discoveryClient = new KubernetesInformerDiscoveryClient("namespace1",
sharedInformerFactory, serviceLister, endpointsLister, null, null, kubernetesDiscoveryProperties);
Expand Down Expand Up @@ -271,7 +272,7 @@ public void instanceWithMultiplePortsAndPrimaryPortNameConfiguredWithLabelShould
Lister<V1Endpoints> endpointsLister = setupEndpointsLister(testEndpointWithMultiplePorts);

KubernetesDiscoveryProperties kubernetesDiscoveryProperties = new KubernetesDiscoveryProperties(true, false,
true, 60, false, null, Set.of(), null, null, null, 0);
Set.of(), true, 60, false, null, Set.of(), null, null, null, 0);

KubernetesInformerDiscoveryClient discoveryClient = new KubernetesInformerDiscoveryClient("namespace1",
sharedInformerFactory, serviceLister, endpointsLister, null, null, kubernetesDiscoveryProperties);
Expand All @@ -290,7 +291,7 @@ public void instanceWithMultiplePortsAndMisconfiguredPrimaryPortNameInLabelShoul
testEndpointWithMultiplePortsWithoutSupportedPortNames);

KubernetesDiscoveryProperties kubernetesDiscoveryProperties = new KubernetesDiscoveryProperties(true, false,
true, 60, false, null, Set.of(), null, null, null, 0);
Set.of(), true, 60, false, null, Set.of(), null, null, null, 0);

KubernetesInformerDiscoveryClient discoveryClient = new KubernetesInformerDiscoveryClient("namespace1",
sharedInformerFactory, serviceLister, endpointsLister, null, null, kubernetesDiscoveryProperties);
Expand All @@ -306,7 +307,7 @@ public void instanceWithMultiplePortsAndGenericPrimaryPortNameConfiguredShouldWo
Lister<V1Endpoints> endpointsLister = setupEndpointsLister(testEndpointWithMultiplePorts);

KubernetesDiscoveryProperties kubernetesDiscoveryProperties = new KubernetesDiscoveryProperties(true, false,
true, 60, false, null, Set.of(), null, "https", null, 0);
Set.of(), true, 60, false, null, Set.of(), null, "https", null, 0);

KubernetesInformerDiscoveryClient discoveryClient = new KubernetesInformerDiscoveryClient("namespace1",
sharedInformerFactory, serviceLister, endpointsLister, null, null, kubernetesDiscoveryProperties);
Expand All @@ -322,7 +323,7 @@ public void instanceWithMultiplePortsAndMisconfiguredGenericPrimaryPortNameShoul
testEndpointWithMultiplePortsWithoutSupportedPortNames);

KubernetesDiscoveryProperties kubernetesDiscoveryProperties = new KubernetesDiscoveryProperties(true, false,
true, 60, false, null, Set.of(), null, "oops", null, 0);
Set.of(), true, 60, false, null, Set.of(), null, "oops", null, 0);

KubernetesInformerDiscoveryClient discoveryClient = new KubernetesInformerDiscoveryClient("namespace1",
sharedInformerFactory, serviceLister, endpointsLister, null, null, kubernetesDiscoveryProperties);
Expand All @@ -337,7 +338,7 @@ public void instanceWithMultiplePortsAndWithoutPrimaryPortNameSpecifiedShouldFal
Lister<V1Endpoints> endpointsLister = setupEndpointsLister(testEndpointWithMultiplePorts);

KubernetesDiscoveryProperties kubernetesDiscoveryProperties = new KubernetesDiscoveryProperties(true, false,
true, 60, false, null, Set.of(), null, null, null, 0);
Set.of(), true, 60, false, null, Set.of(), null, null, null, 0);

KubernetesInformerDiscoveryClient discoveryClient = new KubernetesInformerDiscoveryClient("namespace1",
sharedInformerFactory, serviceLister, endpointsLister, null, null, kubernetesDiscoveryProperties);
Expand All @@ -352,7 +353,7 @@ public void instanceWithMultiplePortsAndWithoutPrimaryPortNameSpecifiedOrHttpsPo
Lister<V1Endpoints> endpointsLister = setupEndpointsLister(testEndpointWithMultiplePortsWithoutHttps);

KubernetesDiscoveryProperties kubernetesDiscoveryProperties = new KubernetesDiscoveryProperties(true, false,
true, 60, false, null, Set.of(), null, null, null, 0);
Set.of(), true, 60, false, null, Set.of(), null, null, null, 0);

KubernetesInformerDiscoveryClient discoveryClient = new KubernetesInformerDiscoveryClient("namespace1",
sharedInformerFactory, serviceLister, endpointsLister, null, null, kubernetesDiscoveryProperties);
Expand All @@ -368,7 +369,7 @@ public void instanceWithMultiplePortsAndWithoutAnyConfigurationShouldPickTheFirs
testEndpointWithMultiplePortsWithoutSupportedPortNames);

KubernetesDiscoveryProperties kubernetesDiscoveryProperties = new KubernetesDiscoveryProperties(true, false,
true, 60, false, null, Set.of(), null, null, null, 0);
Set.of(), true, 60, false, null, Set.of(), null, null, null, 0);

KubernetesInformerDiscoveryClient discoveryClient = new KubernetesInformerDiscoveryClient("namespace1",
sharedInformerFactory, serviceLister, endpointsLister, null, null, kubernetesDiscoveryProperties);
Expand Down
Expand Up @@ -101,7 +101,7 @@ public void testDiscoveryGetInstanceAllNamespaceShouldWork() {
Lister<V1Endpoints> endpointsLister = setupEndpointsLister(testEndpoints1);

KubernetesDiscoveryProperties kubernetesDiscoveryProperties = new KubernetesDiscoveryProperties(true, true,
true, 60, false, null, Set.of(), null, null, null, 0);
Set.of(), true, 60, false, null, Set.of(), null, null, null, 0);

KubernetesInformerReactiveDiscoveryClient discoveryClient = new KubernetesInformerReactiveDiscoveryClient(
new KubernetesNamespaceProvider(new MockEnvironment()), sharedInformerFactory, serviceLister,
Expand All @@ -120,7 +120,7 @@ public void testDiscoveryGetInstanceOneNamespaceShouldWork() {
Lister<V1Endpoints> endpointsLister = setupEndpointsLister(testEndpoints1);

KubernetesDiscoveryProperties kubernetesDiscoveryProperties = new KubernetesDiscoveryProperties(true, false,
true, 60, false, null, Set.of(), null, null, null, 0);
Set.of(), true, 60, false, null, Set.of(), null, null, null, 0);

KubernetesNamespaceProvider kubernetesNamespaceProvider = mock(KubernetesNamespaceProvider.class);
when(kubernetesNamespaceProvider.getNamespace()).thenReturn("namespace1");
Expand Down
Expand Up @@ -146,7 +146,8 @@ void getListAllNamespaces() {
KubernetesNamespaceProvider kubernetesNamespaceProvider = mock(KubernetesNamespaceProvider.class);
when(kubernetesNamespaceProvider.getNamespace()).thenReturn("default");
KubernetesDiscoveryProperties kubernetesDiscoveryProperties = new KubernetesDiscoveryProperties(true, true,
true, 60, false, null, Set.of(), Map.of(), null, KubernetesDiscoveryProperties.Metadata.DEFAULT, 0);
Set.of(), true, 60, false, null, Set.of(), Map.of(), null,
KubernetesDiscoveryProperties.Metadata.DEFAULT, 0);
CoreV1Api coreV1Api = new CoreV1Api();
KubernetesClientServiceInstanceMapper mapper = new KubernetesClientServiceInstanceMapper(
new KubernetesLoadBalancerProperties(), kubernetesDiscoveryProperties);
Expand Down
Expand Up @@ -27,6 +27,8 @@
/**
* @param enabled if kubernetes discovery is enabled
* @param allNamespaces if discover is enabled for all namespaces
* @param namespaces If set and allNamespaces is false, then only the services and
* endpoints matching these namespaces will be fetched from the Kubernetes API server.
* @param waitCacheReady wait for the discovery cache (service and endpoints) to be fully
* loaded, otherwise aborts the application on starting
* @param cacheLoadingTimeoutSeconds timeout for initializing discovery cache, will abort
Expand All @@ -45,6 +47,7 @@
@ConfigurationProperties("spring.cloud.kubernetes.discovery")
public record KubernetesDiscoveryProperties(
@DefaultValue("true") boolean enabled, boolean allNamespaces,
@DefaultValue Set<String> namespaces,
@DefaultValue("true") boolean waitCacheReady,
@DefaultValue("60") long cacheLoadingTimeoutSeconds,
boolean includeNotReadyAddresses, String filter,
Expand All @@ -57,8 +60,8 @@ public record KubernetesDiscoveryProperties(
/**
* Default instance.
*/
public static final KubernetesDiscoveryProperties DEFAULT = new KubernetesDiscoveryProperties(true, false, true, 60,
false, null, Set.of(), Map.of(), null, KubernetesDiscoveryProperties.Metadata.DEFAULT, 0);
public static final KubernetesDiscoveryProperties DEFAULT = new KubernetesDiscoveryProperties(true, false, Set.of(),
true, 60, false, null, Set.of(), Map.of(), null, KubernetesDiscoveryProperties.Metadata.DEFAULT, 0);

/**
* @param addLabels include labels as metadata
Expand Down
Expand Up @@ -43,6 +43,7 @@ void testBindingWhenNoPropertiesProvided() {

assertThat(props.enabled()).isTrue();
assertThat(props.allNamespaces()).isFalse();
assertThat(props.namespaces()).isEmpty();
assertThat(props.waitCacheReady()).isTrue();
assertThat(props.cacheLoadingTimeoutSeconds()).isEqualTo(60);
assertThat(props.includeNotReadyAddresses()).isFalse();
Expand All @@ -59,7 +60,9 @@ void testBindingWhenSomePropertiesProvided() {
new ApplicationContextRunner().withUserConfiguration(KubernetesDiscoveryPropertiesMetadataTests.Config.class)
.withPropertyValues("spring.cloud.kubernetes.discovery.filter=some-filter",
"spring.cloud.kubernetes.discovery.knownSecurePorts[0]=222",
"spring.cloud.kubernetes.discovery.metadata.labelsPrefix=labelsPrefix")
"spring.cloud.kubernetes.discovery.metadata.labelsPrefix=labelsPrefix",
"spring.cloud.kubernetes.discovery.namespaces[0]=ns1",
"spring.cloud.kubernetes.discovery.namespaces[1]=ns2")
.run(context -> {
KubernetesDiscoveryProperties props = context.getBean(KubernetesDiscoveryProperties.class);
assertThat(props).isNotNull();
Expand All @@ -69,6 +72,7 @@ void testBindingWhenSomePropertiesProvided() {

assertThat(props.enabled()).isTrue();
assertThat(props.allNamespaces()).isFalse();
assertThat(props.namespaces()).containsExactlyInAnyOrder("ns1", "ns2");
assertThat(props.waitCacheReady()).isTrue();
assertThat(props.cacheLoadingTimeoutSeconds()).isEqualTo(60);
assertThat(props.includeNotReadyAddresses()).isFalse();
Expand Down
Expand Up @@ -52,14 +52,14 @@ public String description() {
@Override
public List<ServiceInstance> getInstances(String serviceId) {
List<ServiceInstance> response = Collections.emptyList();
KubernetesServiceInstance[] responseBody = rest
.getForEntity(properties.getDiscoveryServerUrl() + "/apps/" + serviceId, KubernetesServiceInstance[].class)
.getBody();
KubernetesServiceInstance[] responseBody = rest.getForEntity(
properties.getDiscoveryServerUrl() + "/apps/" + serviceId, KubernetesServiceInstance[].class).getBody();
if (responseBody != null && responseBody.length > 0) {
response = Arrays.stream(responseBody).filter(this::matchNamespaces).collect(Collectors.toList());
}
return response;
}

@Override
public List<String> getServices() {
List<String> response = Collections.emptyList();
Expand Down
Expand Up @@ -32,8 +32,8 @@ public class KubernetesDiscoveryClientProperties {
private boolean enabled = true;

/**
* If set then only the services and endpoints matching
* these namespaces will be fetched from the Kubernetes API server.
* If set then only the services and endpoints matching these namespaces will be
* fetched from the Kubernetes API server.
*/
private List<String> namespaces = new ArrayList<>();

Expand All @@ -60,4 +60,5 @@ List<String> getNamespaces() {
void setNamespaces(List<String> namespaces) {
this.namespaces = namespaces;
}

}
Expand Up @@ -124,4 +124,5 @@ static Stream<Arguments> instancesFilteredByNamespacesSource() {
Arguments.of(List.of("namespace1"), "test-svc-3", List.of()),
Arguments.of(List.of("namespace2"), "test-svc-3", List.of("uid2")));
}

}
Expand Up @@ -121,25 +121,22 @@ public List<ServiceInstance> getInstances(String serviceId) {
}

public List<Endpoints> getEndPointsList(String serviceId) {
if (properties.getNamespaces().isEmpty()) {
return this.properties.isAllNamespaces()
? this.client.endpoints().inAnyNamespace()
.withField("metadata.name", serviceId)
.withLabels(properties.getServiceLabels()).list().getItems()
: this.client.endpoints().withField("metadata.name", serviceId)
.withLabels(properties.getServiceLabels()).list().getItems();
if (this.properties.allNamespaces()) {
return this.client.endpoints().inAnyNamespace().withField("metadata.name", serviceId)
.withLabels(properties.serviceLabels()).list().getItems();
}
else {
return findEndPointsFilteredByNamespaces(serviceId);
if (properties.namespaces().isEmpty()) {
return this.client.endpoints().withField("metadata.name", serviceId).withLabels(properties.serviceLabels())
.list().getItems();
}
return findEndPointsFilteredByNamespaces(serviceId);
}

private List<Endpoints> findEndPointsFilteredByNamespaces(String serviceId) {
List<Endpoints> endpoints = new ArrayList<>();
for (String ns : properties.getNamespaces()) {
endpoints.addAll(getClient().endpoints().inNamespace(ns)
.withField("metadata.name", serviceId)
.withLabels(properties.getServiceLabels()).list().getItems());
for (String ns : properties.namespaces()) {
endpoints.addAll(getClient().endpoints().inNamespace(ns).withField("metadata.name", serviceId)
.withLabels(properties.serviceLabels()).list().getItems());
}
return endpoints;
}
Expand Down Expand Up @@ -313,21 +310,18 @@ public List<String> getServices() {
}

public List<String> getServices(Predicate<Service> filter) {
if (properties.getNamespaces().isEmpty()) {
return this.kubernetesClientServicesFunction.apply(this.client).list()
.getItems().stream().filter(filter)
if (properties.namespaces().isEmpty()) {
return this.kubernetesClientServicesFunction.apply(this.client).list().getItems().stream().filter(filter)
.map(s -> s.getMetadata().getName()).collect(Collectors.toList());
}
List<String> services = new ArrayList<>();
for (String ns : properties.getNamespaces()) {
services.addAll(
getClient().services().inNamespace(ns).list().getItems().stream()
.filter(filter).map(s -> s.getMetadata().getName()).toList());
for (String ns : properties.namespaces()) {
services.addAll(getClient().services().inNamespace(ns).list().getItems().stream().filter(filter)
.map(s -> s.getMetadata().getName()).toList());
}
return services;
}


@Override
public int getOrder() {
return this.properties.order();
Expand Down

0 comments on commit 548b277

Please sign in to comment.