Skip to content

Commit ac44f23

Browse files
committed
first run on github
Signed-off-by: wind57 <[email protected]>
1 parent 1676bc0 commit ac44f23

File tree

30 files changed

+397
-1059
lines changed

30 files changed

+397
-1059
lines changed

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

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,31 @@
2626
import io.kubernetes.client.openapi.apis.CoreV1Api;
2727
import io.kubernetes.client.openapi.models.V1ConfigMap;
2828
import io.kubernetes.client.openapi.models.V1Secret;
29-
import org.apache.commons.logging.Log;
3029
import org.apache.commons.logging.LogFactory;
3130

3231
import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider;
33-
import org.springframework.cloud.kubernetes.commons.config.ConfigUtils;
3432
import org.springframework.cloud.kubernetes.commons.config.MultipleSourcesContainer;
33+
import org.springframework.cloud.kubernetes.commons.config.ReadType;
3534
import org.springframework.cloud.kubernetes.commons.config.StrippedSourceContainer;
3635
import org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadProperties;
3736
import org.springframework.core.env.Environment;
37+
import org.springframework.core.log.LogAccessor;
3838
import org.springframework.util.ObjectUtils;
3939

4040
import static org.springframework.cloud.kubernetes.client.KubernetesClientUtils.getApplicationNamespace;
4141
import static org.springframework.cloud.kubernetes.client.config.KubernetesClientSourcesBatchRead.strippedConfigMaps;
4242
import static org.springframework.cloud.kubernetes.client.config.KubernetesClientSourcesBatchRead.strippedSecrets;
4343
import static org.springframework.cloud.kubernetes.client.config.KubernetesClientSourcesSingleRead.strippedConfigMaps;
4444
import static org.springframework.cloud.kubernetes.client.config.KubernetesClientSourcesSingleRead.strippedSecrets;
45+
import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.processLabeledData;
46+
import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.processNamedData;
4547

4648
/**
4749
* @author Ryan Baxter
4850
*/
4951
public final class KubernetesClientConfigUtils {
5052

51-
private static final Log LOG = LogFactory.getLog(KubernetesClientConfigUtils.class);
52-
53-
// k8s-native client already returns data from secrets as being decoded
54-
// this flags makes sure we use it everywhere
55-
private static final boolean DECODE = Boolean.FALSE;
53+
private static final LogAccessor LOG = new LogAccessor(LogFactory.getLog(KubernetesClientConfigUtils.class));
5654

5755
private KubernetesClientConfigUtils() {
5856
}
@@ -61,7 +59,7 @@ private KubernetesClientConfigUtils() {
6159
* finds namespaces to be used for the event based reloading.
6260
*/
6361
public static Set<String> namespaces(KubernetesNamespaceProvider provider, ConfigReloadProperties properties,
64-
String target) {
62+
String target) {
6563
Set<String> namespaces = properties.namespaces();
6664
if (namespaces.isEmpty()) {
6765
namespaces = Set.of(getApplicationNamespace(null, target, provider));
@@ -78,23 +76,24 @@ public static Set<String> namespaces(KubernetesNamespaceProvider provider, Confi
7876
* 4. gather all the names of the config maps + data they hold
7977
* </pre>
8078
*/
81-
static MultipleSourcesContainer configMapsDataByName(CoreV1Api client, String namespace,
82-
LinkedHashSet<String> sourceNames, Environment environment, boolean includeDefaultProfileData,
83-
boolean namespacedBatchRead) {
79+
static MultipleSourcesContainer configMapsByName(CoreV1Api client, String namespace,
80+
LinkedHashSet<String> sourceNames, Environment environment, boolean includeDefaultProfileData,
81+
ReadType readType) {
8482

8583
List<StrippedSourceContainer> strippedConfigMaps;
8684

87-
if (namespacedBatchRead) {
88-
LOG.debug("Will read all configmaps in namespace : " + namespace);
85+
if (readType.equals(ReadType.BATCH)) {
86+
LOG.debug(() -> "Will read all configmaps in namespace : " + namespace);
8987
strippedConfigMaps = strippedConfigMaps(client, namespace);
9088
}
9189
else {
92-
LOG.debug("Will read individual configmaps in namespace : " + namespace + " with names : " + sourceNames);
93-
strippedConfigMaps = KubernetesClientSourcesSingleRead.strippedConfigMaps(client, namespace, sourceNames);
90+
LOG.debug(() -> "Will read individual configmaps in namespace : " + namespace + " with names : "
91+
+ sourceNames);
92+
strippedConfigMaps = strippedConfigMaps(client, namespace, sourceNames);
9493
}
9594

96-
return ConfigUtils.processNamedData(strippedConfigMaps, environment, sourceNames, namespace, false,
97-
includeDefaultProfileData);
95+
return processNamedData(strippedConfigMaps, environment, sourceNames, namespace, false,
96+
includeDefaultProfileData);
9897
}
9998

10099
/**
@@ -105,23 +104,22 @@ static MultipleSourcesContainer configMapsDataByName(CoreV1Api client, String na
105104
* 4. gather all the names of the secrets + decoded data they hold
106105
* </pre>
107106
*/
108-
static MultipleSourcesContainer secretsDataByName(CoreV1Api client, String namespace,
109-
LinkedHashSet<String> sourceNames, Environment environment, boolean includeDefaultProfileData,
110-
boolean namespacedBatchRead) {
107+
static MultipleSourcesContainer secretsByName(CoreV1Api client, String namespace, LinkedHashSet<String> sourceNames,
108+
Environment environment, boolean includeDefaultProfileData, ReadType readType) {
111109

112110
List<StrippedSourceContainer> strippedSecrets;
113111

114-
if (namespacedBatchRead) {
115-
LOG.debug("Will read all secrets in namespace : " + namespace);
112+
if (readType.equals(ReadType.BATCH)) {
113+
LOG.debug(() -> "Will read all secrets in namespace : " + namespace);
116114
strippedSecrets = strippedSecrets(client, namespace);
117115
}
118116
else {
119-
LOG.debug("Will read individual secrets in namespace : " + namespace + " with names : " + sourceNames);
120-
strippedSecrets = KubernetesClientSourcesSingleRead.strippedSecrets(client, namespace, sourceNames);
117+
LOG.debug(
118+
() -> "Will read individual secrets in namespace : " + namespace + " with names : " + sourceNames);
119+
strippedSecrets = strippedSecrets(client, namespace, sourceNames);
121120
}
122121

123-
return ConfigUtils.processNamedData(strippedSecrets, environment, sourceNames, namespace, false,
124-
includeDefaultProfileData);
122+
return processNamedData(strippedSecrets, environment, sourceNames, namespace, false, includeDefaultProfileData);
125123
}
126124

127125
/**
@@ -132,21 +130,21 @@ static MultipleSourcesContainer secretsDataByName(CoreV1Api client, String names
132130
* 4. gather all the names of the config maps + data they hold
133131
* </pre>
134132
*/
135-
static MultipleSourcesContainer configMapsDataByLabels(CoreV1Api client, String namespace,
136-
Map<String, String> labels, Environment environment, boolean namespacedBatchRead) {
133+
static MultipleSourcesContainer configMapsByLabels(CoreV1Api client, String namespace, Map<String, String> labels,
134+
Environment environment, ReadType readType) {
137135

138136
List<StrippedSourceContainer> strippedConfigMaps;
139137

140-
if (namespacedBatchRead) {
141-
LOG.debug("Will read all configmaps in namespace : " + namespace);
138+
if (readType.equals(ReadType.BATCH)) {
139+
LOG.debug(() -> "Will read all configmaps in namespace : " + namespace);
142140
strippedConfigMaps = strippedConfigMaps(client, namespace);
143141
}
144142
else {
145-
LOG.debug("Will read individual configmaps in namespace : " + namespace + " with labels : " + labels);
146-
strippedConfigMaps = KubernetesClientSourcesSingleRead.strippedConfigMaps(client, namespace, labels);
143+
LOG.debug(() -> "Will read individual configmaps in namespace : " + namespace + " with labels : " + labels);
144+
strippedConfigMaps = strippedConfigMaps(client, namespace, labels);
147145
}
148146

149-
return ConfigUtils.processLabeledData(strippedConfigMaps, environment, labels, namespace, false);
147+
return processLabeledData(strippedConfigMaps, environment, labels, namespace, false);
150148
}
151149

152150
/**
@@ -157,40 +155,40 @@ static MultipleSourcesContainer configMapsDataByLabels(CoreV1Api client, String
157155
* 4. gather all the names of the secrets + data they hold
158156
* </pre>
159157
*/
160-
static MultipleSourcesContainer secretsDataByLabels(CoreV1Api client, String namespace, Map<String, String> labels,
161-
Environment environment, boolean namespacedBatchRead) {
158+
static MultipleSourcesContainer secretsByLabels(CoreV1Api client, String namespace, Map<String, String> labels,
159+
Environment environment, ReadType readType) {
162160

163161
List<StrippedSourceContainer> strippedSecrets;
164162

165-
if (namespacedBatchRead) {
166-
LOG.debug("Will read all secrets in namespace : " + namespace);
163+
if (readType.equals(ReadType.BATCH)) {
164+
LOG.debug(() -> "Will read all secrets in namespace : " + namespace);
167165
strippedSecrets = strippedSecrets(client, namespace);
168166
}
169167
else {
170-
LOG.debug("Will read individual secrets in namespace : " + namespace + " with labels : " + labels);
171-
strippedSecrets = KubernetesClientSourcesSingleRead.strippedSecrets(client, namespace, labels);
168+
LOG.debug(() -> "Will read individual secrets in namespace : " + namespace + " with labels : " + labels);
169+
strippedSecrets = strippedSecrets(client, namespace, labels);
172170
}
173171

174-
return ConfigUtils.processLabeledData(strippedSecrets, environment, labels, namespace, false);
172+
return processLabeledData(strippedSecrets, environment, labels, namespace, false);
175173
}
176174

177175
static List<StrippedSourceContainer> stripSecrets(List<V1Secret> secrets) {
178176
return secrets.stream()
179177
.map(secret -> new StrippedSourceContainer(secret.getMetadata().getLabels(), secret.getMetadata().getName(),
180-
transform(secret.getData())))
178+
transform(secret.getData())))
181179
.toList();
182180
}
183181

184182
static List<StrippedSourceContainer> stripConfigMaps(List<V1ConfigMap> configMaps) {
185183
return configMaps.stream()
186184
.map(configMap -> new StrippedSourceContainer(configMap.getMetadata().getLabels(),
187-
configMap.getMetadata().getName(), configMap.getData()))
185+
configMap.getMetadata().getName(), configMap.getData()))
188186
.toList();
189187
}
190188

191189
static void handleApiException(ApiException e, String sourceName) {
192190
if (e.getCode() == 404) {
193-
LOG.warn("source with name : " + sourceName + " not found. Ignoring");
191+
LOG.warn(() -> "source with name : " + sourceName + " not found. Ignoring");
194192
}
195193
else {
196194
throw new RuntimeException(e.getResponseBody(), e);
@@ -199,7 +197,7 @@ static void handleApiException(ApiException e, String sourceName) {
199197

200198
private static Map<String, String> transform(Map<String, byte[]> in) {
201199
return ObjectUtils.isEmpty(in) ? Map.of()
202-
: in.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, en -> new String(en.getValue())));
200+
: in.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, en -> new String(en.getValue())));
203201
}
204202

205203
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.core.env.PropertySource;
3131

3232
import static org.springframework.cloud.kubernetes.client.KubernetesClientUtils.getApplicationNamespace;
33+
import static org.springframework.cloud.kubernetes.client.config.KubernetesClientSourcesBatchRead.discardSecrets;
3334

3435
/**
3536
* @author Ryan Baxter
@@ -51,7 +52,9 @@ public KubernetesClientSecretsPropertySourceLocator(CoreV1Api coreV1Api,
5152

5253
@Override
5354
public PropertySource<?> locate(Environment environment) {
54-
Pro
55+
PropertySource<?> propertySource = super.locate(environment);
56+
discardSecrets();
57+
return propertySource;
5558
}
5659

5760
@Override

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
*/
3636
public final class KubernetesClientSourcesBatchRead {
3737

38+
private KubernetesClientSourcesBatchRead() {
39+
40+
}
41+
3842
/**
3943
* at the moment our loading of config maps is using a single thread, but might change
4044
* in the future, thus a thread safe structure.
@@ -43,11 +47,11 @@ public final class KubernetesClientSourcesBatchRead {
4347

4448
private static final ConcurrentHashMap<String, List<StrippedSourceContainer>> CONFIGMAPS_CACHE = new ConcurrentHashMap<>();
4549

46-
static void discardSecrets() {
50+
public static void discardSecrets() {
4751
SECRETS_CACHE.clear();
4852
}
4953

50-
static void discardConfigMaps() {
54+
public static void discardConfigMaps() {
5155
CONFIGMAPS_CACHE.clear();
5256
}
5357

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2024 the original author or authors.
2+
* Copyright 2013-present 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.
@@ -37,8 +37,7 @@
3737

3838
final class KubernetesClientSourcesSingleRead {
3939

40-
private static final LogAccessor LOG = new LogAccessor(
41-
LogFactory.getLog(KubernetesClientSourcesSingleRead.class));
40+
private static final LogAccessor LOG = new LogAccessor(LogFactory.getLog(KubernetesClientSourcesSingleRead.class));
4241

4342
private KubernetesClientSourcesSingleRead() {
4443

@@ -48,7 +47,7 @@ private KubernetesClientSourcesSingleRead() {
4847
* read configmaps by name, one by one, without caching them.
4948
*/
5049
static List<StrippedSourceContainer> strippedConfigMaps(CoreV1Api client, String namespace,
51-
LinkedHashSet<String> sourceNames) {
50+
LinkedHashSet<String> sourceNames) {
5251

5352
List<V1ConfigMap> configMaps = new ArrayList<>(sourceNames.size());
5453

@@ -79,7 +78,7 @@ static List<StrippedSourceContainer> strippedConfigMaps(CoreV1Api client, String
7978
* read secrets by name, one by one, without caching them.
8079
*/
8180
static List<StrippedSourceContainer> strippedSecrets(CoreV1Api client, String namespace,
82-
LinkedHashSet<String> sourceNames) {
81+
LinkedHashSet<String> sourceNames) {
8382

8483
List<V1Secret> secrets = new ArrayList<>(sourceNames.size());
8584

@@ -110,12 +109,14 @@ static List<StrippedSourceContainer> strippedSecrets(CoreV1Api client, String na
110109
* read configmaps by labels, without caching them.
111110
*/
112111
static List<StrippedSourceContainer> strippedConfigMaps(CoreV1Api client, String namespace,
113-
Map<String, String> labels) {
112+
Map<String, String> labels) {
114113

115114
List<V1ConfigMap> configMaps;
116115
try {
117-
configMaps = client
118-
.listNamespacedConfigMap(namespace).labelSelector(labelSelector(labels)).execute().getItems();
116+
configMaps = client.listNamespacedConfigMap(namespace)
117+
.labelSelector(labelSelector(labels))
118+
.execute()
119+
.getItems();
119120
}
120121
catch (ApiException e) {
121122
throw new RuntimeException(e.getResponseBody(), e);
@@ -136,12 +137,11 @@ static List<StrippedSourceContainer> strippedConfigMaps(CoreV1Api client, String
136137
* read secrets by labels, without caching them.
137138
*/
138139
static List<StrippedSourceContainer> strippedSecrets(CoreV1Api client, String namespace,
139-
Map<String, String> labels) {
140+
Map<String, String> labels) {
140141

141142
List<V1Secret> secrets;
142143
try {
143-
secrets = client
144-
.listNamespacedSecret(namespace).labelSelector(labelSelector(labels)).execute().getItems();
144+
secrets = client.listNamespacedSecret(namespace).labelSelector(labelSelector(labels)).execute().getItems();
145145
}
146146
catch (ApiException e) {
147147
throw new RuntimeException(e.getResponseBody(), e);
@@ -159,8 +159,7 @@ static List<StrippedSourceContainer> strippedSecrets(CoreV1Api client, String na
159159
}
160160

161161
private static String labelSelector(Map<String, String> labels) {
162-
return labels.entrySet().stream()
163-
.map(en -> en.getKey() + "=" + en.getValue()).collect(Collectors.joining("&"));
162+
return labels.entrySet().stream().map(en -> en.getKey() + "=" + en.getValue()).collect(Collectors.joining("&"));
164163
}
165164

166165
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.springframework.cloud.kubernetes.commons.config.LabeledSourceData;
2424
import org.springframework.cloud.kubernetes.commons.config.MultipleSourcesContainer;
2525

26+
import static org.springframework.cloud.kubernetes.client.config.KubernetesClientConfigUtils.configMapsByLabels;
27+
2628
class LabeledConfigMapContextToSourceDataProvider implements Supplier<KubernetesClientContextToSourceData> {
2729

2830
LabeledConfigMapContextToSourceDataProvider() {
@@ -48,8 +50,8 @@ public KubernetesClientContextToSourceData get() {
4850
return new LabeledSourceData() {
4951
@Override
5052
public MultipleSourcesContainer dataSupplier(Map<String, String> labels) {
51-
return KubernetesClientConfigUtils.configMapsDataByLabels(context.client(), context.namespace(),
52-
labels, context.environment(), context.namespacedBatchRead());
53+
return configMapsByLabels(context.client(), context.namespace(), labels, context.environment(),
54+
context.readType());
5355
}
5456

5557
}.compute(source.labels(), source.prefix(), source.target(), source.failFast(), context.namespace());

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.springframework.cloud.kubernetes.commons.config.LabeledSourceData;
2424
import org.springframework.cloud.kubernetes.commons.config.MultipleSourcesContainer;
2525

26+
import static org.springframework.cloud.kubernetes.client.config.KubernetesClientConfigUtils.secretsByLabels;
27+
2628
/**
2729
* Provides an implementation of {@link KubernetesClientContextToSourceData} for a labeled
2830
* secret.
@@ -55,8 +57,8 @@ public KubernetesClientContextToSourceData get() {
5557
return new LabeledSourceData() {
5658
@Override
5759
public MultipleSourcesContainer dataSupplier(Map<String, String> labels) {
58-
return KubernetesClientConfigUtils.secretsDataByLabels(context.client(), context.namespace(),
59-
labels, context.environment(), context.namespacedBatchRead());
60+
return secretsByLabels(context.client(), context.namespace(), labels, context.environment(),
61+
context.readType());
6062
}
6163

6264
}.compute(source.labels(), source.prefix(), source.target(), source.failFast(), context.namespace());

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.springframework.cloud.kubernetes.commons.config.NamedConfigMapNormalizedSource;
2525
import org.springframework.cloud.kubernetes.commons.config.NamedSourceData;
2626

27+
import static org.springframework.cloud.kubernetes.client.config.KubernetesClientConfigUtils.configMapsByName;
28+
2729
/**
2830
* Provides an implementation of {@link KubernetesClientContextToSourceData} for a named
2931
* config map.
@@ -54,9 +56,8 @@ protected String generateSourceName(String target, String sourceName, String nam
5456

5557
@Override
5658
public MultipleSourcesContainer dataSupplier(LinkedHashSet<String> sourceNames) {
57-
return KubernetesClientConfigUtils.configMapsDataByName(context.client(), context.namespace(),
58-
sourceNames, context.environment(), context.includeDefaultProfileData(),
59-
context.namespacedBatchRead());
59+
return configMapsByName(context.client(), context.namespace(), sourceNames, context.environment(),
60+
context.includeDefaultProfileData(), context.readType());
6061
}
6162
}.compute(source.name().orElseThrow(), source.prefix(), source.target(), source.profileSpecificSources(),
6263
source.failFast(), context.namespace(), context.environment().getActiveProfiles());

0 commit comments

Comments
 (0)