17
17
18
18
package org .springframework .cloud .kubernetes .config ;
19
19
20
- import java .io .ByteArrayInputStream ;
21
- import java .io .IOException ;
20
+ import static org .springframework .cloud .kubernetes .config .PropertySourceUtils .KEY_VALUE_TO_PROPERTIES ;
21
+ import static org .springframework .cloud .kubernetes .config .PropertySourceUtils .PROPERTIES_TO_MAP ;
22
+ import static org .springframework .cloud .kubernetes .config .PropertySourceUtils .yamlParserGenerator ;
23
+
24
+ import io .fabric8 .kubernetes .api .model .ConfigMap ;
25
+ import io .fabric8 .kubernetes .client .KubernetesClient ;
22
26
import java .util .HashMap ;
23
27
import java .util .Map ;
24
28
import java .util .Map .Entry ;
25
- import java .util .Properties ;
26
29
import java .util .Set ;
27
- import java .util .function .Function ;
28
30
import java .util .stream .Collectors ;
29
-
30
- import io .fabric8 .kubernetes .api .model .ConfigMap ;
31
- import io .fabric8 .kubernetes .client .KubernetesClient ;
32
31
import org .apache .commons .logging .Log ;
33
32
import org .apache .commons .logging .LogFactory ;
34
-
35
- import org .springframework .beans .factory .config .YamlPropertiesFactoryBean ;
36
- import org .springframework .core .io .ByteArrayResource ;
33
+ import org .springframework .core .env .MapPropertySource ;
37
34
import org .springframework .util .StringUtils ;
38
35
39
- import static java .util .Arrays .asList ;
40
- import static org .springframework .beans .factory .config .YamlProcessor .MatchStatus .ABSTAIN ;
41
- import static org .springframework .beans .factory .config .YamlProcessor .MatchStatus .FOUND ;
42
- import static org .springframework .beans .factory .config .YamlProcessor .MatchStatus .NOT_FOUND ;
43
-
44
- public class ConfigMapPropertySource extends KubernetesPropertySource {
36
+ public class ConfigMapPropertySource extends MapPropertySource {
45
37
private static final Log LOG = LogFactory .getLog (ConfigMapPropertySource .class );
46
38
47
39
private static final String APPLICATION_YML = "application.yml" ;
@@ -50,20 +42,14 @@ public class ConfigMapPropertySource extends KubernetesPropertySource {
50
42
51
43
private static final String PREFIX = "configmap" ;
52
44
53
- public ConfigMapPropertySource (KubernetesClient client , String name ,
54
- ConfigMapConfigProperties config ) {
55
- this (client , name , null , config );
56
- }
57
-
58
- public ConfigMapPropertySource (KubernetesClient client , String name ,
59
- String [] profiles , ConfigMapConfigProperties config ) {
60
- this (client , name , null , profiles , config );
45
+ public ConfigMapPropertySource (KubernetesClient client , String name ) {
46
+ this (client , name , null , null );
61
47
}
62
48
63
49
public ConfigMapPropertySource (KubernetesClient client , String name , String namespace ,
64
- String [] profiles , ConfigMapConfigProperties config ) {
50
+ String [] profiles ) {
65
51
super (getName (client , name , namespace ),
66
- asObjectMap (getData (client , name , namespace , profiles , config )));
52
+ asObjectMap (getData (client , name , namespace , profiles )));
67
53
}
68
54
69
55
private static String getName (KubernetesClient client , String name ,
@@ -77,35 +63,31 @@ private static String getName(KubernetesClient client, String name,
77
63
}
78
64
79
65
private static Map <String , String > getData (KubernetesClient client , String name ,
80
- String namespace , String [] profiles , ConfigMapConfigProperties config ) {
81
- Map <String , String > result = new HashMap <>();
82
- if (config .isEnableApi ()) {
83
- try {
84
- ConfigMap map = StringUtils .isEmpty (namespace )
85
- ? client .configMaps ().withName (name ).get ()
86
- : client .configMaps ().inNamespace (namespace ).withName (name ).get ();
87
-
88
- if (map != null ) {
89
- result .putAll (processAllEntries (map .getData (), profiles ));
90
- }
91
- }
92
- catch (Exception e ) {
93
- LOG .warn ("Can't read configMap with name: [" + name + "] in namespace:["
94
- + namespace + "]. Ignoring" , e );
66
+ String namespace , String [] profiles ) {
67
+ try {
68
+ ConfigMap map = StringUtils .isEmpty (namespace )
69
+ ? client .configMaps ().withName (name ).get ()
70
+ : client .configMaps ().inNamespace (namespace ).withName (name ).get ();
71
+
72
+ if (map != null ) {
73
+ return processAllEntries (map .getData (), profiles );
95
74
}
96
75
}
76
+ catch (Exception e ) {
77
+ LOG .warn ("Can't read configMap with name: [" + name + "] in namespace:["
78
+ + namespace + "]. Ignoring" , e );
79
+ }
97
80
98
- Map <String , String > configsFromPaths = new HashMap <>();
99
- putPathConfig (configsFromPaths , config .getPaths ());
100
- result .putAll (processAllEntries (configsFromPaths , profiles ));
101
- return result ;
81
+ return new HashMap <>();
102
82
}
103
83
104
84
private static Map <String , String > processAllEntries (Map <String , String > input ,
105
85
String [] profiles ) {
106
86
107
87
Set <Entry <String , String >> entrySet = input .entrySet ();
108
88
if (entrySet .size () == 1 ) {
89
+ // we handle the case where the configmap contains a single "file"
90
+ // in this case we don't care what the name of t he file is
109
91
Entry <String , String > singleEntry = entrySet .iterator ().next ();
110
92
String propertyName = singleEntry .getKey ();
111
93
String propertyValue = singleEntry .getValue ();
@@ -115,7 +97,8 @@ private static Map<String, String> processAllEntries(Map<String, String> input,
115
97
+ "] will be treated as a yaml file" );
116
98
}
117
99
118
- return yamlParserGenerator (profiles ).andThen (PROPERTIES_TO_MAP )
100
+ return yamlParserGenerator (profiles ).andThen (
101
+ PROPERTIES_TO_MAP )
119
102
.apply (propertyValue );
120
103
}
121
104
else if (propertyName .endsWith (".properties" )) {
@@ -124,7 +107,8 @@ else if (propertyName.endsWith(".properties")) {
124
107
+ "] will be treated as a properties file" );
125
108
}
126
109
127
- return KEY_VALUE_TO_PROPERTIES .andThen (PROPERTIES_TO_MAP )
110
+ return KEY_VALUE_TO_PROPERTIES .andThen (
111
+ PROPERTIES_TO_MAP )
128
112
.apply (propertyValue );
129
113
}
130
114
else {
@@ -140,65 +124,29 @@ private static Map<String, String> defaultProcessAllEntries(Map<String, String>
140
124
141
125
return input .entrySet ().stream ()
142
126
.map (e -> extractProperties (e .getKey (), e .getValue (), profiles ))
143
- .filter (m -> !m .isEmpty ()).flatMap (m -> m .entrySet ().stream ())
144
- .collect (Collectors .toMap (e -> e .getKey (), e -> e .getValue ()));
127
+ .filter (m -> !m .isEmpty ())
128
+ .flatMap (m -> m .entrySet ().stream ())
129
+ .collect (Collectors .toMap (Entry ::getKey , Entry ::getValue ));
145
130
}
146
131
147
132
private static Map <String , String > extractProperties (String resourceName ,
148
133
String content , String [] profiles ) {
149
- Map <String , String > result = new HashMap <>();
150
134
151
- if (resourceName .equals (APPLICATION_YAML )
152
- || resourceName .equals (APPLICATION_YML )) {
153
- result .putAll (yamlParserGenerator (profiles ).andThen (PROPERTIES_TO_MAP )
154
- .apply (content ));
135
+ if (resourceName .equals (APPLICATION_YAML ) || resourceName .equals (APPLICATION_YML )) {
136
+ return yamlParserGenerator (profiles ).andThen (PROPERTIES_TO_MAP ).apply (content );
155
137
}
156
138
else if (resourceName .equals (APPLICATION_PROPERTIES )) {
157
- result .putAll (
158
- KEY_VALUE_TO_PROPERTIES .andThen (PROPERTIES_TO_MAP ).apply (content ));
139
+ return KEY_VALUE_TO_PROPERTIES .andThen (PROPERTIES_TO_MAP ).apply (content );
159
140
}
160
- else {
161
- result . put ( resourceName , content );
162
- }
163
- return result ;
141
+
142
+ return new HashMap < String , String >() {{
143
+ put ( resourceName , content );
144
+ }} ;
164
145
}
165
146
166
147
private static Map <String , Object > asObjectMap (Map <String , String > source ) {
167
148
return source .entrySet ().stream ()
168
149
.collect (Collectors .toMap (Map .Entry ::getKey , Map .Entry ::getValue ));
169
150
}
170
151
171
- private static Function <String , Properties > yamlParserGenerator (
172
- final String [] profiles ) {
173
- return s -> {
174
- YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean ();
175
- yamlFactory .setDocumentMatchers (properties -> {
176
- String profileProperty = properties .getProperty ("spring.profiles" );
177
- if (profileProperty != null && profileProperty .length () > 0 ) {
178
- return asList (profiles ).contains (profileProperty ) ? FOUND : NOT_FOUND ;
179
- }
180
- else {
181
- return ABSTAIN ;
182
- }
183
- });
184
- yamlFactory .setResources (new ByteArrayResource (s .getBytes ()));
185
- return yamlFactory .getObject ();
186
- };
187
- }
188
-
189
- private static final Function <String , Properties > KEY_VALUE_TO_PROPERTIES = s -> {
190
- Properties properties = new Properties ();
191
- try {
192
- properties .load (new ByteArrayInputStream (s .getBytes ()));
193
- return properties ;
194
- }
195
- catch (IOException e ) {
196
- throw new IllegalArgumentException ();
197
- }
198
- };
199
-
200
- private static final Function <Properties , Map <String , String >> PROPERTIES_TO_MAP = p -> p
201
- .entrySet ().stream ().collect (Collectors .toMap (e -> String .valueOf (e .getKey ()),
202
- e -> String .valueOf (e .getValue ())));
203
-
204
152
}
0 commit comments