Skip to content

Commit ea6be10

Browse files
committed
wip
Signed-off-by: wind57 <[email protected]>
1 parent fb86ff5 commit ea6be10

File tree

3 files changed

+121
-123
lines changed

3 files changed

+121
-123
lines changed

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

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,10 @@
1616

1717
package org.springframework.cloud.kubernetes.commons.config;
1818

19-
import java.util.ArrayList;
2019
import java.util.List;
2120
import java.util.Map;
22-
import java.util.stream.Stream;
2321

2422
import org.springframework.boot.context.properties.ConfigurationProperties;
25-
import org.springframework.boot.context.properties.bind.DefaultValue;
26-
import org.springframework.core.env.Environment;
27-
import org.springframework.util.StringUtils;
28-
29-
import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.getApplicationName;
3023

3124
/**
3225
* Config map configuration properties.
@@ -49,27 +42,4 @@ public ConfigMapConfigProperties(boolean enabled, List<Source> sources, Map<Stri
4942
includeProfileSpecificSources, failFast, retry, readType);
5043
}
5144

52-
/**
53-
* @return A list of config map source(s) to use.
54-
*/
55-
public List<NormalizedSource> determineSources(Environment environment) {
56-
if (this.sources.isEmpty()) {
57-
List<NormalizedSource> result = new ArrayList<>(2);
58-
String name = getApplicationName(environment, this.name, "ConfigMap");
59-
result.add(new NamedConfigMapNormalizedSource(name, this.namespace, this.failFast,
60-
this.includeProfileSpecificSources));
61-
62-
if (!labels.isEmpty()) {
63-
result.add(new LabeledConfigMapNormalizedSource(this.namespace, this.labels, this.failFast,
64-
ConfigUtils.Prefix.DEFAULT, false));
65-
}
66-
return result;
67-
}
68-
69-
return this.sources.stream()
70-
.flatMap(s -> s.normalize(this.name, this.namespace, this.labels, this.includeProfileSpecificSources,
71-
this.failFast, this.useNameAsPrefix, environment))
72-
.toList();
73-
}
74-
7545
}

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

Lines changed: 4 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -37,81 +37,14 @@
3737
*/
3838
@ConfigurationProperties(SecretsConfigProperties.PREFIX)
3939
public record SecretsConfigProperties(@DefaultValue("false") boolean enabled,
40-
@DefaultValue List<Source> sources, @DefaultValue Map<String, String> labels,
41-
String name, String namespace, boolean useNameAsPrefix,
42-
@DefaultValue("true") boolean includeProfileSpecificSources, boolean failFast,
43-
@DefaultValue RetryProperties retry, @DefaultValue("BATCH") ReadType readType) {
40+
@DefaultValue List<SourceConfigProperties.Source> sources, @DefaultValue Map<String, String> labels,
41+
String name, String namespace, boolean useNameAsPrefix,
42+
@DefaultValue("true") boolean includeProfileSpecificSources, boolean failFast,
43+
@DefaultValue RetryProperties retry, @DefaultValue("BATCH") ReadType readType) {
4444

4545
/**
4646
* Prefix for Kubernetes secrets configuration properties.
4747
*/
4848
public static final String PREFIX = "spring.cloud.kubernetes.secrets";
4949

50-
/**
51-
* @return A list of Secret source(s) to use.
52-
*/
53-
public List<NormalizedSource> determineSources(Environment environment) {
54-
if (this.sources.isEmpty()) {
55-
List<NormalizedSource> result = new ArrayList<>(2);
56-
String name = getApplicationName(environment, this.name, "Secret");
57-
result.add(new NamedSecretNormalizedSource(name, this.namespace, this.failFast,
58-
this.includeProfileSpecificSources));
59-
60-
if (!labels.isEmpty()) {
61-
result.add(new LabeledSecretNormalizedSource(this.namespace, this.labels, this.failFast,
62-
ConfigUtils.Prefix.DEFAULT));
63-
}
64-
return result;
65-
}
66-
67-
return this.sources.stream()
68-
.flatMap(s -> s.normalize(this.name, this.namespace, this.labels, this.includeProfileSpecificSources,
69-
this.failFast, this.useNameAsPrefix, environment))
70-
.toList();
71-
}
72-
73-
/**
74-
* @param name The name of the Secret.
75-
* @param namespace The namespace where the Secret is found.
76-
* @param labels The labels of the Secret to find.
77-
* @param explicitPrefix An explicit prefix to be used for properties.
78-
* @param useNameAsPrefix Use secret name as prefix for properties.
79-
* @param includeProfileSpecificSources Use profile name to append to a config map
80-
* name.
81-
*/
82-
public record Source(String name, String namespace, @DefaultValue Map<String, String> labels, String explicitPrefix,
83-
Boolean useNameAsPrefix, Boolean includeProfileSpecificSources) {
84-
85-
private Stream<NormalizedSource> normalize(String defaultName, String defaultNamespace,
86-
Map<String, String> defaultLabels, boolean defaultIncludeProfileSpecificSources, boolean failFast,
87-
boolean defaultUseNameAsPrefix, Environment environment) {
88-
89-
Stream.Builder<NormalizedSource> normalizedSources = Stream.builder();
90-
91-
String normalizedName = StringUtils.hasLength(this.name) ? this.name : defaultName;
92-
String normalizedNamespace = StringUtils.hasLength(this.namespace) ? this.namespace : defaultNamespace;
93-
Map<String, String> normalizedLabels = this.labels.isEmpty() ? defaultLabels : this.labels;
94-
95-
String secretName = getApplicationName(environment, normalizedName, "Secret");
96-
97-
ConfigUtils.Prefix prefix = ConfigUtils.findPrefix(this.explicitPrefix, this.useNameAsPrefix,
98-
defaultUseNameAsPrefix, normalizedName);
99-
100-
boolean includeProfileSpecificSources = ConfigUtils.includeProfileSpecificSources(
101-
defaultIncludeProfileSpecificSources, this.includeProfileSpecificSources);
102-
NormalizedSource namedBasedSource = new NamedSecretNormalizedSource(secretName, normalizedNamespace,
103-
failFast, prefix, includeProfileSpecificSources);
104-
normalizedSources.add(namedBasedSource);
105-
106-
if (!normalizedLabels.isEmpty()) {
107-
NormalizedSource labeledBasedSource = new LabeledSecretNormalizedSource(normalizedNamespace, labels,
108-
failFast, prefix);
109-
normalizedSources.add(labeledBasedSource);
110-
}
111-
112-
return normalizedSources.build();
113-
}
114-
115-
}
116-
11750
}

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

Lines changed: 117 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
import org.springframework.boot.context.properties.bind.DefaultValue;
44
import org.springframework.core.env.Environment;
5-
import org.springframework.util.StringUtils;
65

6+
import java.util.ArrayList;
77
import java.util.List;
88
import java.util.Map;
99
import java.util.stream.Stream;
1010

1111
import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.getApplicationName;
12+
import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.Prefix;
13+
import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.findPrefix;
14+
import static org.springframework.util.StringUtils.hasLength;
1215

1316
abstract sealed class SourceConfigProperties permits ConfigMapConfigProperties {
1417

@@ -54,37 +57,129 @@ public SourceConfigProperties(@DefaultValue("true") boolean enabled,
5457
*/
5558
public record Source(String name, String namespace, @DefaultValue Map<String, String> labels, String explicitPrefix,
5659
Boolean useNameAsPrefix, Boolean includeProfileSpecificSources) {
57-
}
5860

59-
protected Stream<NormalizedSource> normalize(String defaultName, String defaultNamespace,
60-
Map<String, String> defaultLabels, boolean defaultIncludeProfileSpecificSources, boolean failFast,
61-
boolean defaultUseNameAsPrefix, Environment environment) {
61+
Stream<NormalizedSource> normalize(boolean configMap, String defaultName, String defaultNamespace,
62+
Map<String, String> defaultLabels, boolean defaultIncludeProfileSpecificSources, boolean failFast,
63+
boolean defaultUseNameAsPrefix, Environment environment) {
64+
65+
Stream.Builder<NormalizedSource> normalizedSources = Stream.builder();
66+
67+
String normalizedName = hasLength(name) ? name : defaultName;
68+
String normalizedNamespace = hasLength(namespace) ? namespace : defaultNamespace;
69+
Map<String, String> normalizedLabels = labels.isEmpty() ? defaultLabels : labels;
70+
71+
String configurationTarget = configMap ? "ConfigMap" : "Secret";
72+
String sourceName = getApplicationName(environment, normalizedName, configurationTarget);
73+
74+
Prefix prefix = findPrefix(explicitPrefix, useNameAsPrefix,
75+
defaultUseNameAsPrefix, normalizedName);
76+
77+
boolean includeProfileSpecificSources = ConfigUtils.includeProfileSpecificSources(
78+
defaultIncludeProfileSpecificSources, this.includeProfileSpecificSources);
79+
80+
NormalizedSource namedSource;
81+
if (configMap) {
82+
namedSource = new NamedConfigMapNormalizedSource(sourceName, normalizedNamespace,
83+
failFast, prefix, includeProfileSpecificSources);
84+
} else {
85+
namedSource = new NamedSecretNormalizedSource(sourceName, normalizedNamespace,
86+
failFast, prefix, includeProfileSpecificSources);
87+
normalizedSources.add(namedSource);
88+
}
6289

63-
Stream.Builder<NormalizedSource> normalizedSources = Stream.builder();
90+
normalizedSources.add(namedSource);
6491

65-
String normalizedName = StringUtils.hasLength(this.name) ? this.name : defaultName;
66-
String normalizedNamespace = StringUtils.hasLength(this.namespace) ? this.namespace : defaultNamespace;
67-
Map<String, String> normalizedLabels = this.labels.isEmpty() ? defaultLabels : this.labels;
92+
if (!normalizedLabels.isEmpty()) {
93+
NormalizedSource labeledSource;
94+
if (configMap) {
95+
labeledSource = new LabeledConfigMapNormalizedSource(normalizedNamespace, labels,
96+
failFast, prefix, includeProfileSpecificSources);
97+
} else {
98+
labeledSource = new LabeledSecretNormalizedSource(normalizedNamespace, labels,
99+
failFast, prefix);
100+
normalizedSources.add(labeledSource);
101+
}
102+
normalizedSources.add(labeledSource);
68103

69-
String configMapName = getApplicationName(environment, normalizedName, "Config Map");
104+
}
70105

71-
ConfigUtils.Prefix prefix = ConfigUtils.findPrefix(this.explicitPrefix, this.useNameAsPrefix,
72-
defaultUseNameAsPrefix, normalizedName);
106+
return normalizedSources.build();
73107

74-
boolean includeProfileSpecificSources = ConfigUtils.includeProfileSpecificSources(
75-
defaultIncludeProfileSpecificSources, this.includeProfileSpecificSources);
76-
NormalizedSource namedBasedSource = new NamedConfigMapNormalizedSource(configMapName, normalizedNamespace,
77-
failFast, prefix, includeProfileSpecificSources);
78-
normalizedSources.add(namedBasedSource);
108+
}
109+
}
79110

80-
if (!normalizedLabels.isEmpty()) {
81-
NormalizedSource labeledBasedSource = new LabeledConfigMapNormalizedSource(normalizedNamespace, labels,
82-
failFast, prefix, includeProfileSpecificSources);
83-
normalizedSources.add(labeledBasedSource);
111+
public List<NormalizedSource> determineSources(boolean configMap, Environment environment) {
112+
if (getSources().isEmpty()) {
113+
List<NormalizedSource> result = new ArrayList<>(2);
114+
String configurationTarget = configMap ? "ConfigMap" : "Secret";
115+
String name = getApplicationName(environment, getName(), configurationTarget);
116+
NormalizedSource normalizedSource;
117+
if (configMap) {
118+
normalizedSource = new NamedConfigMapNormalizedSource(name, getNamespace(),
119+
isFailFast(), isIncludeProfileSpecificSources());
120+
} else {
121+
normalizedSource = new NamedSecretNormalizedSource(name, this.namespace, this.failFast,
122+
this.includeProfileSpecificSources);
123+
}
124+
result.add(normalizedSource);
125+
126+
if (!getLabels().isEmpty()) {
127+
NormalizedSource labeledSource;
128+
if (configMap) {
129+
labeledSource = new LabeledConfigMapNormalizedSource(getNamespace(), getLabels(), isFailFast(),
130+
ConfigUtils.Prefix.DEFAULT, false);
131+
} else {
132+
labeledSource = new LabeledSecretNormalizedSource(this.namespace, this.labels, this.failFast,
133+
ConfigUtils.Prefix.DEFAULT);
134+
}
135+
result.add(labeledSource);
136+
}
137+
return result;
84138
}
85139

86-
return normalizedSources.build();
140+
return getSources().stream()
141+
.flatMap(s -> s.normalize(configMap, getName(), getNamespace(), getLabels(),
142+
isIncludeProfileSpecificSources(), isFailFast(), isUseNameAsPrefix(), environment))
143+
.toList();
144+
}
145+
146+
public boolean isEnabled() {
147+
return enabled;
148+
}
149+
150+
public List<Source> getSources() {
151+
return sources;
152+
}
87153

154+
public Map<String, String> getLabels() {
155+
return labels;
88156
}
89157

158+
public String getName() {
159+
return name;
160+
}
161+
162+
public String getNamespace() {
163+
return namespace;
164+
}
165+
166+
public boolean isUseNameAsPrefix() {
167+
return useNameAsPrefix;
168+
}
169+
170+
public boolean isIncludeProfileSpecificSources() {
171+
return includeProfileSpecificSources;
172+
}
173+
174+
public boolean isFailFast() {
175+
return failFast;
176+
}
177+
178+
public RetryProperties getRetry() {
179+
return retry;
180+
}
181+
182+
public ReadType getReadType() {
183+
return readType;
184+
}
90185
}

0 commit comments

Comments
 (0)