|
2 | 2 |
|
3 | 3 | import org.springframework.boot.context.properties.bind.DefaultValue; |
4 | 4 | import org.springframework.core.env.Environment; |
5 | | -import org.springframework.util.StringUtils; |
6 | 5 |
|
| 6 | +import java.util.ArrayList; |
7 | 7 | import java.util.List; |
8 | 8 | import java.util.Map; |
9 | 9 | import java.util.stream.Stream; |
10 | 10 |
|
11 | 11 | 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; |
12 | 15 |
|
13 | 16 | abstract sealed class SourceConfigProperties permits ConfigMapConfigProperties { |
14 | 17 |
|
@@ -54,37 +57,129 @@ public SourceConfigProperties(@DefaultValue("true") boolean enabled, |
54 | 57 | */ |
55 | 58 | public record Source(String name, String namespace, @DefaultValue Map<String, String> labels, String explicitPrefix, |
56 | 59 | Boolean useNameAsPrefix, Boolean includeProfileSpecificSources) { |
57 | | - } |
58 | 60 |
|
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 | + } |
62 | 89 |
|
63 | | - Stream.Builder<NormalizedSource> normalizedSources = Stream.builder(); |
| 90 | + normalizedSources.add(namedSource); |
64 | 91 |
|
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); |
68 | 103 |
|
69 | | - String configMapName = getApplicationName(environment, normalizedName, "Config Map"); |
| 104 | + } |
70 | 105 |
|
71 | | - ConfigUtils.Prefix prefix = ConfigUtils.findPrefix(this.explicitPrefix, this.useNameAsPrefix, |
72 | | - defaultUseNameAsPrefix, normalizedName); |
| 106 | + return normalizedSources.build(); |
73 | 107 |
|
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 | + } |
79 | 110 |
|
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; |
84 | 138 | } |
85 | 139 |
|
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 | + } |
87 | 153 |
|
| 154 | + public Map<String, String> getLabels() { |
| 155 | + return labels; |
88 | 156 | } |
89 | 157 |
|
| 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 | + } |
90 | 185 | } |
0 commit comments