Skip to content

Commit e3ae419

Browse files
authored
fix (#1171)
1 parent d1e7c63 commit e3ae419

File tree

28 files changed

+193
-102
lines changed

28 files changed

+193
-102
lines changed

spring-cloud-kubernetes-client-autoconfig/src/main/java/org/springframework/cloud/kubernetes/client/KubernetesClientUtils.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
import org.apache.commons.logging.Log;
2222
import org.apache.commons.logging.LogFactory;
2323

24+
import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider;
25+
import org.springframework.cloud.kubernetes.commons.config.NamespaceResolutionFailedException;
26+
import org.springframework.util.StringUtils;
27+
2428
/**
2529
* @author Ryan Baxter
2630
*/
@@ -61,4 +65,42 @@ public static ApiClient kubernetesApiClient() {
6165
}
6266
}
6367

68+
/**
69+
* this method does the namespace resolution for both config map and secrets
70+
* implementations. It tries these places to find the namespace:
71+
*
72+
* <pre>
73+
* 1. from a normalized source (which can be null)
74+
* 2. from a property 'spring.cloud.kubernetes.client.namespace', if such is present
75+
* 3. from a String residing in a file denoted by `spring.cloud.kubernetes.client.serviceAccountNamespacePath`
76+
* property, if such is present
77+
* 4. from a String residing in `/var/run/secrets/kubernetes.io/serviceaccount/namespace` file,
78+
* if such is present (kubernetes default path)
79+
* </pre>
80+
*
81+
* If any of the above fail, we throw a NamespaceResolutionFailedException.
82+
* @param namespace normalized namespace
83+
* @param configurationTarget Config Map/Secret
84+
* @param provider the provider which computes the namespace
85+
* @return application namespace
86+
* @throws NamespaceResolutionFailedException when namespace could not be resolved
87+
*/
88+
public static String getApplicationNamespace(String namespace, String configurationTarget,
89+
KubernetesNamespaceProvider provider) {
90+
if (StringUtils.hasText(namespace)) {
91+
LOG.debug(configurationTarget + " namespace : " + namespace);
92+
return namespace;
93+
}
94+
95+
if (provider != null) {
96+
String providerNamespace = provider.getNamespace();
97+
if (StringUtils.hasText(providerNamespace)) {
98+
LOG.debug(configurationTarget + " namespace from provider : " + namespace);
99+
return providerNamespace;
100+
}
101+
}
102+
103+
throw new NamespaceResolutionFailedException("unresolved namespace");
104+
}
105+
64106
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2013-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.client;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.mockito.Mockito;
21+
22+
import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider;
23+
import org.springframework.cloud.kubernetes.commons.config.NamespaceResolutionFailedException;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
27+
28+
/**
29+
* @author wind57
30+
*/
31+
class KubernetesClientUtilsTests {
32+
33+
private final KubernetesNamespaceProvider provider = Mockito.mock(KubernetesNamespaceProvider.class);
34+
35+
@Test
36+
void testNamespaceFromNormalizedSource() {
37+
String result = KubernetesClientUtils.getApplicationNamespace("abc", "target", null);
38+
assertThat(result).isEqualTo("abc");
39+
}
40+
41+
@Test
42+
void testNamespaceFromProvider() {
43+
Mockito.when(provider.getNamespace()).thenReturn("def");
44+
String result = KubernetesClientUtils.getApplicationNamespace("", "target", provider);
45+
assertThat(result).isEqualTo("def");
46+
}
47+
48+
@Test
49+
void testNamespaceResolutionFailed() {
50+
assertThatThrownBy(() -> KubernetesClientUtils.getApplicationNamespace("", "target", null))
51+
.isInstanceOf(NamespaceResolutionFailedException.class);
52+
}
53+
54+
}

spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapPropertySourceLocator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.springframework.core.env.ConfigurableEnvironment;
2626
import org.springframework.core.env.MapPropertySource;
2727

28-
import static org.springframework.cloud.kubernetes.client.config.KubernetesClientConfigUtils.getApplicationNamespace;
28+
import static org.springframework.cloud.kubernetes.client.KubernetesClientUtils.getApplicationNamespace;
2929

3030
/**
3131
* @author Ryan Baxter

spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigUtils.java

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider;
3333
import org.springframework.cloud.kubernetes.commons.config.ConfigUtils;
3434
import org.springframework.cloud.kubernetes.commons.config.MultipleSourcesContainer;
35-
import org.springframework.cloud.kubernetes.commons.config.NamespaceResolutionFailedException;
3635
import org.springframework.cloud.kubernetes.commons.config.StrippedSourceContainer;
3736
import org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadProperties;
3837
import org.springframework.core.env.Environment;
39-
import org.springframework.util.StringUtils;
38+
39+
import static org.springframework.cloud.kubernetes.client.KubernetesClientUtils.getApplicationNamespace;
4040

4141
/**
4242
* @author Ryan Baxter
@@ -65,44 +65,6 @@ public static Set<String> namespaces(KubernetesNamespaceProvider provider, Confi
6565
return namespaces;
6666
}
6767

68-
/**
69-
* this method does the namespace resolution for both config map and secrets
70-
* implementations. It tries these places to find the namespace:
71-
*
72-
* <pre>
73-
* 1. from a normalized source (which can be null)
74-
* 2. from a property 'spring.cloud.kubernetes.client.namespace', if such is present
75-
* 3. from a String residing in a file denoted by `spring.cloud.kubernetes.client.serviceAccountNamespacePath`
76-
* property, if such is present
77-
* 4. from a String residing in `/var/run/secrets/kubernetes.io/serviceaccount/namespace` file,
78-
* if such is present (kubernetes default path)
79-
* </pre>
80-
*
81-
* If any of the above fail, we throw a NamespaceResolutionFailedException.
82-
* @param namespace normalized namespace
83-
* @param configurationTarget Config Map/Secret
84-
* @param provider the provider which computes the namespace
85-
* @return application namespace
86-
* @throws NamespaceResolutionFailedException when namespace could not be resolved
87-
*/
88-
static String getApplicationNamespace(String namespace, String configurationTarget,
89-
KubernetesNamespaceProvider provider) {
90-
if (StringUtils.hasText(namespace)) {
91-
LOG.debug(configurationTarget + " namespace : " + namespace);
92-
return namespace;
93-
}
94-
95-
if (provider != null) {
96-
String providerNamespace = provider.getNamespace();
97-
if (StringUtils.hasText(providerNamespace)) {
98-
LOG.debug(configurationTarget + " namespace from provider : " + namespace);
99-
return providerNamespace;
100-
}
101-
}
102-
103-
throw new NamespaceResolutionFailedException("unresolved namespace");
104-
}
105-
10668
/**
10769
* <pre>
10870
* 1. read all secrets in the provided namespace

spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientSecretsPropertySourceLocator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.springframework.core.env.ConfigurableEnvironment;
2626
import org.springframework.core.env.MapPropertySource;
2727

28-
import static org.springframework.cloud.kubernetes.client.config.KubernetesClientConfigUtils.getApplicationNamespace;
28+
import static org.springframework.cloud.kubernetes.client.KubernetesClientUtils.getApplicationNamespace;
2929

3030
/**
3131
* @author Ryan Baxter

spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigUtilsTests.java

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,16 @@
2121

2222
import org.junit.jupiter.api.Assertions;
2323
import org.junit.jupiter.api.Test;
24-
import org.mockito.Mockito;
2524

2625
import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider;
27-
import org.springframework.cloud.kubernetes.commons.config.NamespaceResolutionFailedException;
2826
import org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadProperties;
2927
import org.springframework.mock.env.MockEnvironment;
3028

31-
import static org.assertj.core.api.Assertions.assertThat;
32-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
33-
3429
/**
3530
* @author wind57
3631
*/
3732
class KubernetesClientConfigUtilsTests {
3833

39-
private final KubernetesNamespaceProvider provider = Mockito.mock(KubernetesNamespaceProvider.class);
40-
41-
@Test
42-
void testNamespaceFromNormalizedSource() {
43-
String result = KubernetesClientConfigUtils.getApplicationNamespace("abc", "target", null);
44-
assertThat(result).isEqualTo("abc");
45-
}
46-
47-
@Test
48-
void testNamespaceFromProvider() {
49-
Mockito.when(provider.getNamespace()).thenReturn("def");
50-
String result = KubernetesClientConfigUtils.getApplicationNamespace("", "target", provider);
51-
assertThat(result).isEqualTo("def");
52-
}
53-
54-
@Test
55-
void testNamespaceResolutionFailed() {
56-
assertThatThrownBy(() -> KubernetesClientConfigUtils.getApplicationNamespace("", "target", null))
57-
.isInstanceOf(NamespaceResolutionFailedException.class);
58-
}
59-
6034
@Test
6135
void testNamespacesFromProperties() {
6236
ConfigReloadProperties properties = new ConfigReloadProperties(false, false, false,

spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_prefix/LabeledConfigMapWithPrefixBootstrapTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
classes = LabeledConfigMapWithPrefixApp.class,
2626
properties = { "spring.cloud.bootstrap.name=labeled-configmap-with-prefix",
2727
"labeled.config.map.with.prefix.stub=true", "spring.main.cloud-platform=KUBERNETES",
28-
"spring.cloud.bootstrap.enabled=true", "spring.cloud.kubernetes.client.namespace=spring-k8s" })
28+
"spring.cloud.bootstrap.enabled=true" })
2929
class LabeledConfigMapWithPrefixBootstrapTests extends LabeledConfigMapWithPrefixTests {
3030

3131
}

spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_prefix/LabeledConfigMapWithPrefixConfigDataTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.jupiter.api.AfterAll;
2323
import org.junit.jupiter.api.BeforeAll;
2424
import org.mockito.MockedStatic;
25+
import org.mockito.Mockito;
2526

2627
import org.springframework.boot.test.context.SpringBootTest;
2728
import org.springframework.cloud.kubernetes.client.KubernetesClientUtils;
@@ -37,8 +38,7 @@
3738
classes = LabeledConfigMapWithPrefixApp.class,
3839
properties = { "spring.cloud.application.name=labeled-configmap-with-prefix",
3940
"labeled.config.map.with.prefix.stub=true", "spring.main.cloud-platform=KUBERNETES",
40-
"spring.config.import=kubernetes:,classpath:./labeled-configmap-with-prefix.yaml",
41-
"spring.cloud.kubernetes.client.namespace=spring-k8s" })
41+
"spring.config.import=kubernetes:,classpath:./labeled-configmap-with-prefix.yaml" })
4242
class LabeledConfigMapWithPrefixConfigDataTests extends LabeledConfigMapWithPrefixTests {
4343

4444
private static MockedStatic<KubernetesClientUtils> clientUtilsMock;
@@ -51,6 +51,9 @@ static void wireMock() {
5151
clientUtilsMock = mockStatic(KubernetesClientUtils.class);
5252
clientUtilsMock.when(KubernetesClientUtils::kubernetesApiClient)
5353
.thenReturn(new ClientBuilder().setBasePath(server.baseUrl()).build());
54+
clientUtilsMock
55+
.when(() -> KubernetesClientUtils.getApplicationNamespace(Mockito.any(), Mockito.any(), Mockito.any()))
56+
.thenReturn("spring-k8s");
5457
stubData();
5558
}
5659

spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/LabeledConfigMapWithProfileConfigDataTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.jupiter.api.AfterAll;
2323
import org.junit.jupiter.api.BeforeAll;
2424
import org.mockito.MockedStatic;
25+
import org.mockito.Mockito;
2526

2627
import org.springframework.boot.test.context.SpringBootTest;
2728
import org.springframework.cloud.kubernetes.client.KubernetesClientUtils;
@@ -39,8 +40,7 @@
3940
classes = LabeledConfigMapWithProfileApp.class,
4041
properties = { "spring.cloud.application.name=labeled-configmap-with-profile",
4142
"labeled.config.map.with.profile.stub=true", "spring.main.cloud-platform=KUBERNETES",
42-
"spring.config.import=kubernetes:,classpath:./labeled-configmap-with-profile.yaml",
43-
"spring.cloud.kubernetes.client.namespace=spring-k8s" })
43+
"spring.config.import=kubernetes:,classpath:./labeled-configmap-with-profile.yaml" })
4444
class LabeledConfigMapWithProfileConfigDataTests extends LabeledConfigMapWithProfileTests {
4545

4646
private static MockedStatic<KubernetesClientUtils> clientUtilsMock;
@@ -53,6 +53,9 @@ static void wireMock() {
5353
clientUtilsMock = mockStatic(KubernetesClientUtils.class);
5454
clientUtilsMock.when(KubernetesClientUtils::kubernetesApiClient)
5555
.thenReturn(new ClientBuilder().setBasePath(server.baseUrl()).build());
56+
clientUtilsMock
57+
.when(() -> KubernetesClientUtils.getApplicationNamespace(Mockito.any(), Mockito.any(), Mockito.any()))
58+
.thenReturn("spring-k8s");
5659
stubData();
5760
}
5861

spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_prefix/LabeledSecretWithPrefixConfigDataTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.jupiter.api.AfterAll;
2323
import org.junit.jupiter.api.BeforeAll;
2424
import org.mockito.MockedStatic;
25+
import org.mockito.Mockito;
2526

2627
import org.springframework.boot.test.context.SpringBootTest;
2728
import org.springframework.cloud.kubernetes.client.KubernetesClientUtils;
@@ -36,8 +37,7 @@
3637
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = LabeledSecretWithPrefixApp.class,
3738
properties = { "spring.cloud.application.name=labeled-secret-with-prefix",
3839
"labeled.secret.with.prefix.stub=true", "spring.main.cloud-platform=KUBERNETES",
39-
"spring.config.import=kubernetes:,classpath:./labeled-secret-with-prefix.yaml",
40-
"spring.cloud.kubernetes.client.namespace=spring-k8s" })
40+
"spring.config.import=kubernetes:,classpath:./labeled-secret-with-prefix.yaml" })
4141
class LabeledSecretWithPrefixConfigDataTests extends LabeledSecretWithPrefixTests {
4242

4343
private static MockedStatic<KubernetesClientUtils> clientUtilsMock;
@@ -50,6 +50,9 @@ static void wireMock() {
5050
clientUtilsMock = mockStatic(KubernetesClientUtils.class);
5151
clientUtilsMock.when(KubernetesClientUtils::kubernetesApiClient)
5252
.thenReturn(new ClientBuilder().setBasePath(server.baseUrl()).build());
53+
clientUtilsMock
54+
.when(() -> KubernetesClientUtils.getApplicationNamespace(Mockito.any(), Mockito.any(), Mockito.any()))
55+
.thenReturn("spring-k8s");
5356
stubData();
5457
}
5558

0 commit comments

Comments
 (0)