From c42a085735c9f083bb4ec63fc4e9e76466eedfea Mon Sep 17 00:00:00 2001 From: erabii Date: Fri, 29 Dec 2023 22:52:51 +0200 Subject: [PATCH] fix issue 1558 (#1559) Fixes #1558 --- ...rSelectiveNamespacesAutoConfiguration.java | 4 +- ...ctiveNamespacesAutoConfigurationTests.java | 113 ++++++++++++++++++ 2 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 spring-cloud-kubernetes-client-discovery/src/test/java/org/springframework/cloud/kubernetes/client/discovery/KubernetesClientInformerSelectiveNamespacesAutoConfigurationTests.java diff --git a/spring-cloud-kubernetes-client-discovery/src/main/java/org/springframework/cloud/kubernetes/client/discovery/KubernetesClientInformerSelectiveNamespacesAutoConfiguration.java b/spring-cloud-kubernetes-client-discovery/src/main/java/org/springframework/cloud/kubernetes/client/discovery/KubernetesClientInformerSelectiveNamespacesAutoConfiguration.java index 5f44ed9b21..9dfd058085 100644 --- a/spring-cloud-kubernetes-client-discovery/src/main/java/org/springframework/cloud/kubernetes/client/discovery/KubernetesClientInformerSelectiveNamespacesAutoConfiguration.java +++ b/spring-cloud-kubernetes-client-discovery/src/main/java/org/springframework/cloud/kubernetes/client/discovery/KubernetesClientInformerSelectiveNamespacesAutoConfiguration.java @@ -67,10 +67,8 @@ public class KubernetesClientInformerSelectiveNamespacesAutoConfiguration { LogFactory.getLog(KubernetesClientInformerSelectiveNamespacesAutoConfiguration.class)); // we rely on the order of namespaces to enable listers, as such provide a bean of - // namespaces - // as a list, instead of the incoming Set. + // namespaces as a list, instead of the incoming Set. @Bean - @ConditionalOnMissingBean public List selectiveNamespaces(KubernetesDiscoveryProperties properties) { List selectiveNamespaces = properties.namespaces().stream().sorted().toList(); LOG.debug(() -> "using selective namespaces : " + selectiveNamespaces); diff --git a/spring-cloud-kubernetes-client-discovery/src/test/java/org/springframework/cloud/kubernetes/client/discovery/KubernetesClientInformerSelectiveNamespacesAutoConfigurationTests.java b/spring-cloud-kubernetes-client-discovery/src/test/java/org/springframework/cloud/kubernetes/client/discovery/KubernetesClientInformerSelectiveNamespacesAutoConfigurationTests.java new file mode 100644 index 0000000000..c65544cc4c --- /dev/null +++ b/spring-cloud-kubernetes-client-discovery/src/test/java/org/springframework/cloud/kubernetes/client/discovery/KubernetesClientInformerSelectiveNamespacesAutoConfigurationTests.java @@ -0,0 +1,113 @@ +/* + * Copyright 2019-2023 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes.client.discovery; + +import java.util.List; +import java.util.Set; + +import io.kubernetes.client.openapi.ApiClient; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mockito; + +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; +import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author wind57 + */ +@ExtendWith(OutputCaptureExtension.class) +class KubernetesClientInformerSelectiveNamespacesAutoConfigurationTests { + + private static final String NAMESPACE_A = "a"; + + private static final String NAMESPACE_B = "b"; + + private static final String NAMESPACE_C = "c"; + + private static final String NAMESPACE_D = "d"; + + @Test + void testBeansCreates(CapturedOutput output) { + + new ApplicationContextRunner() + .withPropertyValues("spring.cloud.discovery.enabled=true", + "spring.cloud.kubernetes.discovery.enabled=true", + "spring.cloud.kubernetes.discovery.namespaces[0]=" + NAMESPACE_A, + "spring.cloud.kubernetes.discovery.namespaces[1]=" + NAMESPACE_B, + "spring.main.cloud-platform=kubernetes") + .withConfiguration( + AutoConfigurations.of(KubernetesClientInformerSelectiveNamespacesAutoConfiguration.class)) + .withUserConfiguration(Config.class).run(context -> { + assertThat(context.getBean("selectiveNamespaces")).isNotNull(); + + @SuppressWarnings("unchecked") + Set selectiveNamespaces = context.getBean("selectiveNamespaces", Set.class); + Assertions.assertEquals(selectiveNamespaces, Set.of("a", "b")); + + @SuppressWarnings("unchecked") + Set namespaces = context.getBean("namespaces", Set.class); + Assertions.assertEquals(namespaces, Set.of("c", "d")); + }); + + assertThat(output.getOut().contains("registering lister (for services) in namespace : " + NAMESPACE_A)) + .isTrue(); + assertThat(output.getOut().contains("registering lister (for services) in namespace : " + NAMESPACE_B)) + .isTrue(); + + assertThat(output.getOut().contains("registering lister (for services) in namespace : " + NAMESPACE_C)) + .isFalse(); + assertThat(output.getOut().contains("registering lister (for services) in namespace : " + NAMESPACE_D)) + .isFalse(); + + assertThat(output.getOut().contains("registering lister (for endpoints) in namespace : " + NAMESPACE_A)) + .isTrue(); + assertThat(output.getOut().contains("registering lister (for endpoints) in namespace : " + NAMESPACE_B)) + .isTrue(); + + assertThat(output.getOut().contains("registering lister (for endpoints) in namespace : " + NAMESPACE_C)) + .isFalse(); + assertThat(output.getOut().contains("registering lister (for endpoints) in namespace : " + NAMESPACE_D)) + .isFalse(); + } + + @Configuration + @EnableConfigurationProperties(KubernetesDiscoveryProperties.class) + static class Config { + + @Bean + ApiClient apiClient() { + return Mockito.mock(ApiClient.class); + } + + @Bean + List namespaces() { + return List.of(NAMESPACE_C, NAMESPACE_D); + } + + } + +}