2626import io .kubernetes .client .openapi .apis .CoreV1Api ;
2727import io .kubernetes .client .openapi .models .V1ConfigMap ;
2828import io .kubernetes .client .openapi .models .V1Secret ;
29- import org .apache .commons .logging .Log ;
3029import org .apache .commons .logging .LogFactory ;
3130
3231import org .springframework .cloud .kubernetes .commons .KubernetesNamespaceProvider ;
33- import org .springframework .cloud .kubernetes .commons .config .ConfigUtils ;
3432import org .springframework .cloud .kubernetes .commons .config .MultipleSourcesContainer ;
33+ import org .springframework .cloud .kubernetes .commons .config .ReadType ;
3534import org .springframework .cloud .kubernetes .commons .config .StrippedSourceContainer ;
3635import org .springframework .cloud .kubernetes .commons .config .reload .ConfigReloadProperties ;
3736import org .springframework .core .env .Environment ;
37+ import org .springframework .core .log .LogAccessor ;
3838import org .springframework .util .ObjectUtils ;
3939
4040import static org .springframework .cloud .kubernetes .client .KubernetesClientUtils .getApplicationNamespace ;
4141import static org .springframework .cloud .kubernetes .client .config .KubernetesClientSourcesBatchRead .strippedConfigMaps ;
4242import static org .springframework .cloud .kubernetes .client .config .KubernetesClientSourcesBatchRead .strippedSecrets ;
4343import static org .springframework .cloud .kubernetes .client .config .KubernetesClientSourcesSingleRead .strippedConfigMaps ;
4444import 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 */
4951public 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}
0 commit comments