Skip to content

Commit 0d429a7

Browse files
committed
fix
1 parent 2ff03da commit 0d429a7

15 files changed

+2201
-20
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ public static Set<String> namespaces(KubernetesNamespaceProvider provider, Confi
7474
* </pre>
7575
*/
7676
static MultipleSourcesContainer configMapsDataByName(CoreV1Api client, String namespace,
77-
LinkedHashSet<String> sourceNames, Environment environment, boolean namespacedBatchRead) {
77+
LinkedHashSet<String> sourceNames, Environment environment, boolean includeDefaultProfileData,
78+
boolean namespacedBatchRead) {
7879

7980
List<StrippedSourceContainer> strippedConfigMaps;
8081

@@ -87,7 +88,8 @@ static MultipleSourcesContainer configMapsDataByName(CoreV1Api client, String na
8788
strippedConfigMaps = strippedConfigMapsNonBatchRead(client, namespace, sourceNames);
8889
}
8990

90-
return ConfigUtils.processNamedData(strippedConfigMaps, environment, sourceNames, namespace, false);
91+
return ConfigUtils.processNamedData(strippedConfigMaps, environment, sourceNames, namespace, false,
92+
includeDefaultProfileData);
9193
}
9294

9395
/**
@@ -99,7 +101,8 @@ static MultipleSourcesContainer configMapsDataByName(CoreV1Api client, String na
99101
* </pre>
100102
*/
101103
static MultipleSourcesContainer secretsDataByName(CoreV1Api client, String namespace,
102-
LinkedHashSet<String> sourceNames, Environment environment, boolean namespacedBatchRead) {
104+
LinkedHashSet<String> sourceNames, Environment environment, boolean includeDefaultProfileData,
105+
boolean namespacedBatchRead) {
103106

104107
List<StrippedSourceContainer> strippedSecrets;
105108

@@ -112,7 +115,8 @@ static MultipleSourcesContainer secretsDataByName(CoreV1Api client, String names
112115
strippedSecrets = strippedSecretsNonBatchRead(client, namespace, sourceNames);
113116
}
114117

115-
return ConfigUtils.processNamedData(strippedSecrets, environment, sourceNames, namespace, false);
118+
return ConfigUtils.processNamedData(strippedSecrets, environment, sourceNames, namespace, false,
119+
includeDefaultProfileData);
116120
}
117121

118122
/**

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ static List<StrippedSourceContainer> strippedConfigMapsNonBatchRead(CoreV1Api cl
4949
List<V1ConfigMap> configMaps = new ArrayList<>(sourceNames.size());
5050

5151
for (String sourceName : sourceNames) {
52-
V1ConfigMap configMap;
52+
V1ConfigMap configMap = null;
5353
try {
54-
configMap = client.readNamespacedConfigMap(namespace, sourceName, null);
54+
configMap = client.readNamespacedConfigMap(sourceName, namespace, null);
5555
}
5656
catch (ApiException e) {
57-
throw new RuntimeException(e.getResponseBody(), e);
57+
KubernetesClientSourcesStripper.handleApiException(e, sourceName);
5858
}
5959
if (configMap != null) {
6060
LOG.debug("Loaded config map '" + sourceName + "'");
@@ -81,12 +81,12 @@ static List<StrippedSourceContainer> strippedSecretsNonBatchRead(CoreV1Api clien
8181
List<V1Secret> secrets = new ArrayList<>(sourceNames.size());
8282

8383
for (String sourceName : sourceNames) {
84-
V1Secret secret;
84+
V1Secret secret = null;
8585
try {
8686
secret = client.readNamespacedSecret(sourceName, namespace, null);
8787
}
8888
catch (ApiException e) {
89-
throw new RuntimeException(e.getResponseBody(), e);
89+
KubernetesClientSourcesStripper.handleApiException(e, sourceName);
9090
}
9191
if (secret != null) {
9292
LOG.debug("Loaded config map '" + sourceName + "'");

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,22 @@
2020
import java.util.Map;
2121
import java.util.stream.Collectors;
2222

23+
import io.kubernetes.client.openapi.ApiException;
2324
import io.kubernetes.client.openapi.models.V1ConfigMap;
2425
import io.kubernetes.client.openapi.models.V1Secret;
26+
import org.apache.commons.logging.LogFactory;
2527

2628
import org.springframework.cloud.kubernetes.commons.config.StrippedSourceContainer;
29+
import org.springframework.core.log.LogAccessor;
2730
import org.springframework.util.ObjectUtils;
2831

2932
/**
3033
* @author wind57
3134
*/
3235
interface KubernetesClientSourcesStripper {
3336

37+
LogAccessor LOG = new LogAccessor(LogFactory.getLog(KubernetesClientSourcesStripper.class));
38+
3439
static List<StrippedSourceContainer> strippedSecrets(List<V1Secret> secrets) {
3540
return secrets.stream()
3641
.map(secret -> new StrippedSourceContainer(secret.getMetadata().getLabels(), secret.getMetadata().getName(),
@@ -45,6 +50,15 @@ static List<StrippedSourceContainer> strippedConfigMaps(List<V1ConfigMap> config
4550
.toList();
4651
}
4752

53+
static void handleApiException(ApiException e, String sourceName) {
54+
if (e.getCode() == 404) {
55+
LOG.warn("source with name : " + sourceName + " not found. Ignoring");
56+
}
57+
else {
58+
throw new RuntimeException(e.getResponseBody(), e);
59+
}
60+
}
61+
4862
private static Map<String, String> transform(Map<String, byte[]> in) {
4963
return ObjectUtils.isEmpty(in) ? Map.of()
5064
: in.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, en -> new String(en.getValue())));

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ protected String generateSourceName(String target, String sourceName, String nam
5555
@Override
5656
public MultipleSourcesContainer dataSupplier(LinkedHashSet<String> sourceNames) {
5757
return KubernetesClientConfigUtils.configMapsDataByName(context.client(), context.namespace(),
58-
sourceNames, context.environment(), context.namespacedBatchRead());
58+
sourceNames, context.environment(), context.includeDefaultProfileData(),
59+
context.namespacedBatchRead());
5960
}
6061
}.compute(source.name().orElseThrow(), source.prefix(), source.target(), source.profileSpecificSources(),
6162
source.failFast(), context.namespace(), context.environment().getActiveProfiles());

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ protected String generateSourceName(String target, String sourceName, String nam
5454
@Override
5555
public MultipleSourcesContainer dataSupplier(LinkedHashSet<String> sourceNames) {
5656
return KubernetesClientConfigUtils.secretsDataByName(context.client(), context.namespace(),
57-
sourceNames, context.environment(), context.namespacedBatchRead());
57+
sourceNames, context.environment(), context.includeDefaultProfileData(),
58+
context.namespacedBatchRead());
5859
}
5960
}.compute(source.name().orElseThrow(), source.prefix(), source.target(), source.profileSpecificSources(),
6061
source.failFast(), context.namespace(), context.environment().getActiveProfiles());
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2022 the original author or authors.
2+
* Copyright 2013-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
import io.kubernetes.client.openapi.models.V1ConfigMapList;
3333
import io.kubernetes.client.openapi.models.V1ObjectMetaBuilder;
3434
import io.kubernetes.client.util.ClientBuilder;
35+
import org.junit.jupiter.api.AfterAll;
3536
import org.junit.jupiter.api.AfterEach;
3637
import org.junit.jupiter.api.Assertions;
3738
import org.junit.jupiter.api.BeforeAll;
@@ -55,7 +56,7 @@
5556
* @author wind57
5657
*/
5758
@ExtendWith(OutputCaptureExtension.class)
58-
class LabeledConfigMapContextToSourceDataProviderTests {
59+
class LabeledConfigMapContextToSourceDataProviderNamespacedBatchReadTests {
5960

6061
private static final boolean NAMESPACED_BATCH_READ = true;
6162

@@ -92,6 +93,11 @@ void afterEach() {
9293
new KubernetesClientSourcesNamespaceBatched().discardConfigMaps();
9394
}
9495

96+
@AfterAll
97+
static void afterAll() {
98+
WireMock.shutdownServer();
99+
}
100+
95101
/**
96102
* we have a single config map deployed. it has two labels and these match against our
97103
* queries.
@@ -580,7 +586,9 @@ void cache(CapturedOutput output) {
580586
Assertions.assertEquals(redSourceData.sourceData().size(), 1);
581587
Assertions.assertEquals(redSourceData.sourceData().get("color"), "red");
582588
Assertions.assertEquals(redSourceData.sourceName(), "configmap.red-configmap.default");
589+
583590
Assertions.assertTrue(output.getAll().contains("Loaded all config maps in namespace '" + NAMESPACE + "'"));
591+
Assertions.assertFalse(output.getAll().contains("Will read individual configmaps in namespace"));
584592

585593
NormalizedSource greenSource = new LabeledConfigMapNormalizedSource(NAMESPACE, Map.of("color", "green"), false,
586594
ConfigUtils.Prefix.DEFAULT, false);

0 commit comments

Comments
 (0)