Skip to content

Commit 95e310b

Browse files
committed
merge 3.3.x
Signed-off-by: wind57 <eugen.rabii@gmail.com>
2 parents 8101ca2 + a43c1d0 commit 95e310b

File tree

7 files changed

+327
-10
lines changed

7 files changed

+327
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Copyright 2013-present 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.commons.config.reload;
18+
19+
import java.util.List;
20+
21+
import com.github.tomakehurst.wiremock.WireMockServer;
22+
import com.github.tomakehurst.wiremock.client.WireMock;
23+
import io.kubernetes.client.openapi.ApiClient;
24+
import io.kubernetes.client.openapi.JSON;
25+
import io.kubernetes.client.openapi.apis.CoreV1Api;
26+
import io.kubernetes.client.openapi.models.V1ConfigMapList;
27+
import io.kubernetes.client.openapi.models.V1ConfigMapListBuilder;
28+
import io.kubernetes.client.openapi.models.V1SecretList;
29+
import io.kubernetes.client.openapi.models.V1SecretListBuilder;
30+
import io.kubernetes.client.util.ClientBuilder;
31+
import org.junit.jupiter.api.AfterAll;
32+
import org.junit.jupiter.api.BeforeAll;
33+
import org.junit.jupiter.api.Test;
34+
35+
import org.springframework.cloud.kubernetes.client.config.KubernetesClientConfigContext;
36+
import org.springframework.cloud.kubernetes.client.config.KubernetesClientConfigMapPropertySource;
37+
import org.springframework.cloud.kubernetes.client.config.KubernetesClientSecretsPropertySource;
38+
import org.springframework.cloud.kubernetes.commons.config.NamedConfigMapNormalizedSource;
39+
import org.springframework.cloud.kubernetes.commons.config.NamedSecretNormalizedSource;
40+
import org.springframework.cloud.kubernetes.commons.config.ReadType;
41+
import org.springframework.core.env.MapPropertySource;
42+
import org.springframework.mock.env.MockEnvironment;
43+
44+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
45+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
46+
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
47+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
48+
import static org.assertj.core.api.Assertions.assertThat;
49+
50+
/**
51+
* @author wind57
52+
*/
53+
class ConfigReloadUtilTests {
54+
55+
private static final V1ConfigMapList CONFIGMAP_LIST = new V1ConfigMapListBuilder().build();
56+
57+
private static final V1SecretList SECRET_LIST = new V1SecretListBuilder().build();
58+
59+
private static WireMockServer wireMockServer;
60+
61+
private static ApiClient apiClient;
62+
63+
@BeforeAll
64+
static void beforeAll() {
65+
wireMockServer = new WireMockServer(options().dynamicPort());
66+
wireMockServer.start();
67+
WireMock.configureFor("localhost", wireMockServer.port());
68+
69+
apiClient = new ClientBuilder().setBasePath("http://localhost:" + wireMockServer.port()).build();
70+
71+
stubFor(get("/api/v1/namespaces/default/configmaps")
72+
.willReturn(aResponse().withStatus(200).withBody(JSON.serialize(CONFIGMAP_LIST))));
73+
74+
stubFor(get("/api/v1/namespaces/default/secrets")
75+
.willReturn(aResponse().withStatus(200).withBody(JSON.serialize(SECRET_LIST))));
76+
77+
}
78+
79+
@AfterAll
80+
static void afterAll() {
81+
wireMockServer.stop();
82+
}
83+
84+
/**
85+
* isInstance configmap matches.
86+
*/
87+
@Test
88+
void testIsInstanceConfigMapPasses() {
89+
90+
MockEnvironment environment = new MockEnvironment();
91+
KubernetesClientConfigMapPropertySource configMapPropertySource = configMapPropertySource();
92+
environment.getPropertySources().addFirst(configMapPropertySource);
93+
94+
List<MapPropertySource> propertySources = ConfigReloadUtil
95+
.findPropertySources(KubernetesClientConfigMapPropertySource.class, environment);
96+
97+
assertThat(propertySources).hasSize(1);
98+
}
99+
100+
/**
101+
* isInstance secret matches.
102+
*/
103+
@Test
104+
void testIsInstanceSecretPasses() {
105+
106+
MockEnvironment environment = new MockEnvironment();
107+
KubernetesClientSecretsPropertySource secretsPropertySource = secretsPropertySource();
108+
environment.getPropertySources().addFirst(secretsPropertySource);
109+
110+
List<MapPropertySource> propertySources = ConfigReloadUtil
111+
.findPropertySources(KubernetesClientSecretsPropertySource.class, environment);
112+
113+
assertThat(propertySources).hasSize(1);
114+
}
115+
116+
private KubernetesClientConfigMapPropertySource configMapPropertySource() {
117+
CoreV1Api api = new CoreV1Api(apiClient);
118+
NamedConfigMapNormalizedSource namedSecretNormalizedSource = new NamedConfigMapNormalizedSource("configmap",
119+
"default", true, true);
120+
MockEnvironment environment = new MockEnvironment();
121+
122+
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, namedSecretNormalizedSource,
123+
"default", environment, false, ReadType.BATCH);
124+
125+
return new KubernetesClientConfigMapPropertySource(context);
126+
}
127+
128+
private KubernetesClientSecretsPropertySource secretsPropertySource() {
129+
CoreV1Api api = new CoreV1Api(apiClient);
130+
NamedSecretNormalizedSource namedSecretNormalizedSource = new NamedSecretNormalizedSource("secret", "default",
131+
true, true);
132+
MockEnvironment environment = new MockEnvironment();
133+
134+
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, namedSecretNormalizedSource,
135+
"default", environment, false, ReadType.BATCH);
136+
137+
return new KubernetesClientSecretsPropertySource(context);
138+
}
139+
140+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2013-present 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.commons.config;
18+
19+
import java.util.Map;
20+
21+
import org.springframework.core.env.MapPropertySource;
22+
23+
/**
24+
* Kubernetes property source for configmaps.
25+
*
26+
* @author wind57
27+
*/
28+
public class ConfigMapPropertySource extends MapPropertySource {
29+
30+
public ConfigMapPropertySource(String name, Map<String, Object> source) {
31+
super(name, source);
32+
}
33+
34+
}

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SourceDataEntriesProcessor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.apache.commons.logging.LogFactory;
3131

3232
import org.springframework.core.env.Environment;
33-
import org.springframework.core.env.MapPropertySource;
3433

3534
import static org.springframework.cloud.kubernetes.commons.config.PropertySourceUtils.KEY_VALUE_TO_PROPERTIES;
3635
import static org.springframework.cloud.kubernetes.commons.config.PropertySourceUtils.PROPERTIES_TO_MAP;
@@ -44,7 +43,7 @@
4443
* @author Ali Shahbour
4544
* @author Michael Moudatsos
4645
*/
47-
public class SourceDataEntriesProcessor extends MapPropertySource {
46+
public class SourceDataEntriesProcessor extends ConfigMapPropertySource {
4847

4948
private static final Log LOG = LogFactory.getLog(SourceDataEntriesProcessor.class);
5049

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/reload/ConfigReloadUtil.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,10 @@ public static boolean reload(PropertySourceLocator locator, ConfigurableEnvironm
8585
* @param sourceClass class for which property sources will be found
8686
* @return finds all registered property sources of the given type
8787
*/
88-
static <S extends PropertySource<?>> List<S> findPropertySources(Class<S> sourceClass,
88+
static <S extends MapPropertySource> List<MapPropertySource> findPropertySources(Class<S> sourceClass,
89+
8990
ConfigurableEnvironment environment) {
90-
List<S> managedSources = new ArrayList<>();
91+
List<MapPropertySource> managedSources = new ArrayList<>();
9192

9293
List<PropertySource<?>> sources = environment.getPropertySources()
9394
.stream()

spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/reload/ConfigReloadUtilTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ void testFindPropertySources() {
126126
MockEnvironment environment = new MockEnvironment();
127127
MutablePropertySources propertySources = environment.getPropertySources();
128128
propertySources.addFirst(new OneComposite());
129-
propertySources.addFirst(new PlainPropertySource<>("plain"));
129+
propertySources.addFirst(new PlainMapPropertySource<>("plain", Map.of()));
130130
propertySources.addFirst(new OneBootstrap<>(new EnumerablePropertySource<>("enumerable") {
131131
@Override
132132
public String[] getPropertyNames() {
@@ -139,7 +139,7 @@ public Object getProperty(String name) {
139139
}
140140
}));
141141

142-
List<? extends PropertySource> result = ConfigReloadUtil.findPropertySources(PlainPropertySource.class,
142+
List<MapPropertySource> result = ConfigReloadUtil.findPropertySources(PlainMapPropertySource.class,
143143
environment);
144144

145145
Assertions.assertThat(result.size()).isEqualTo(2);
@@ -169,15 +169,15 @@ private TwoComposite() {
169169

170170
@Override
171171
public Collection<PropertySource<?>> getPropertySources() {
172-
return List.of(new PlainPropertySource<>("from-inner-two-composite"));
172+
return List.of(new PlainMapPropertySource<>("from-inner-two-composite", Map.of()));
173173
}
174174

175175
}
176176

177-
private static final class PlainPropertySource<T> extends PropertySource<T> {
177+
private static final class PlainMapPropertySource<T> extends MapPropertySource {
178178

179-
private PlainPropertySource(String name) {
180-
super(name);
179+
PlainMapPropertySource(String name, Map<String, Object> source) {
180+
super(name, source);
181181
}
182182

183183
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2013-present 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.commons.config.reload;
18+
19+
import java.util.List;
20+
21+
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
22+
import io.fabric8.kubernetes.client.KubernetesClient;
23+
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
24+
import org.junit.jupiter.api.AfterAll;
25+
import org.junit.jupiter.api.BeforeAll;
26+
import org.junit.jupiter.api.Test;
27+
28+
import org.springframework.cloud.kubernetes.fabric8.config.Fabric8ConfigMapPropertySource;
29+
import org.springframework.cloud.kubernetes.fabric8.config.Fabric8SecretsPropertySource;
30+
import org.springframework.core.env.MapPropertySource;
31+
import org.springframework.mock.env.MockEnvironment;
32+
33+
import static org.assertj.core.api.Assertions.assertThat;
34+
import static org.springframework.cloud.kubernetes.fabric8.config.Fabric8ConfigMapPropertySourceProvider.configMapPropertySource;
35+
import static org.springframework.cloud.kubernetes.fabric8.config.Fabric8ConfigMapPropertySourceProvider.secretPropertySource;
36+
37+
/**
38+
* @author wind57
39+
*/
40+
@EnableKubernetesMockClient(crud = true, https = false)
41+
class ConfigReloadUtilTests {
42+
43+
private static KubernetesClient kubernetesClient;
44+
45+
@BeforeAll
46+
static void beforeAll() {
47+
kubernetesClient.configMaps().inNamespace("default").resource(new ConfigMapBuilder().build());
48+
}
49+
50+
@AfterAll
51+
static void afterAll() {
52+
kubernetesClient.configMaps().inAnyNamespace().delete();
53+
}
54+
55+
/**
56+
* isInstance configmap matches.
57+
*/
58+
@Test
59+
void testIsInstanceConfigMapPasses() {
60+
61+
MockEnvironment environment = new MockEnvironment();
62+
Fabric8ConfigMapPropertySource configMapPropertySource = configMapPropertySource(kubernetesClient);
63+
environment.getPropertySources().addFirst(configMapPropertySource);
64+
65+
List<MapPropertySource> propertySources = ConfigReloadUtil
66+
.findPropertySources(Fabric8ConfigMapPropertySource.class, environment);
67+
68+
assertThat(propertySources).hasSize(1);
69+
}
70+
71+
/**
72+
* isInstance secret matches.
73+
*/
74+
@Test
75+
void testIsInstanceSecretPasses() {
76+
77+
MockEnvironment environment = new MockEnvironment();
78+
Fabric8SecretsPropertySource secretsPropertySource = secretPropertySource(kubernetesClient);
79+
environment.getPropertySources().addFirst(secretsPropertySource);
80+
81+
List<MapPropertySource> propertySources = ConfigReloadUtil
82+
.findPropertySources(Fabric8SecretsPropertySource.class, environment);
83+
84+
assertThat(propertySources).hasSize(1);
85+
}
86+
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2013-present 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.fabric8.config;
18+
19+
import io.fabric8.kubernetes.client.KubernetesClient;
20+
21+
import org.springframework.cloud.kubernetes.commons.config.NamedConfigMapNormalizedSource;
22+
import org.springframework.cloud.kubernetes.commons.config.NamedSecretNormalizedSource;
23+
import org.springframework.cloud.kubernetes.commons.config.ReadType;
24+
import org.springframework.mock.env.MockEnvironment;
25+
26+
/**
27+
* Only needed to get a hold of {@link Fabric8ConfigMapPropertySource}.
28+
*
29+
* @author wind57
30+
*/
31+
public final class Fabric8ConfigMapPropertySourceProvider {
32+
33+
private Fabric8ConfigMapPropertySourceProvider() {
34+
35+
}
36+
37+
public static Fabric8ConfigMapPropertySource configMapPropertySource(KubernetesClient kubernetesClient) {
38+
NamedConfigMapNormalizedSource namedConfigMapNormalizedSource = new NamedConfigMapNormalizedSource("configmap",
39+
"default", true, true);
40+
41+
Fabric8ConfigContext context = new Fabric8ConfigContext(kubernetesClient, namedConfigMapNormalizedSource,
42+
"default", new MockEnvironment(), ReadType.BATCH);
43+
return new Fabric8ConfigMapPropertySource(context);
44+
}
45+
46+
public static Fabric8SecretsPropertySource secretPropertySource(KubernetesClient kubernetesClient) {
47+
48+
NamedSecretNormalizedSource namedSecretNormalizedSource = new NamedSecretNormalizedSource("secret", "default",
49+
true, true);
50+
51+
Fabric8ConfigContext context = new Fabric8ConfigContext(kubernetesClient, namedSecretNormalizedSource,
52+
"default", new MockEnvironment(), ReadType.BATCH);
53+
return new Fabric8SecretsPropertySource(context);
54+
}
55+
56+
}

0 commit comments

Comments
 (0)