1919import io .fabric8 .kubernetes .api .model .ConfigMapBuilder ;
2020import io .fabric8 .kubernetes .api .model .KubernetesListBuilder ;
2121import io .fabric8 .maven .core .config .ConfigMapEntry ;
22- import io .fabric8 .maven .core .config .ResourceConfig ;
2322import io .fabric8 .maven .core .config .PlatformMode ;
23+ import io .fabric8 .maven .core .config .ResourceConfig ;
2424import io .fabric8 .maven .enricher .api .BaseEnricher ;
2525import io .fabric8 .maven .enricher .api .MavenEnricherContext ;
2626
2727import java .io .IOException ;
28+ import java .nio .ByteBuffer ;
29+ import java .nio .charset .CharacterCodingException ;
30+ import java .nio .charset .CodingErrorAction ;
31+ import java .nio .charset .StandardCharsets ;
2832import java .nio .file .Files ;
2933import java .nio .file .Paths ;
30- import java .util .HashMap ;
34+ import java .util .Base64 ;
3135import java .util .Iterator ;
3236import java .util .Map ;
3337import java .util .Set ;
3438
39+ import static java .util .Collections .singletonMap ;
40+
3541public class ConfigMapEnricher extends BaseEnricher {
3642
3743 protected static final String PREFIX_ANNOTATION = "maven.fabric8.io/cm/" ;
@@ -53,34 +59,40 @@ private void addAnnotations(KubernetesListBuilder builder) {
5359 public void visit (ConfigMapBuilder element ) {
5460 final Map <String , String > annotations = element .buildMetadata ().getAnnotations ();
5561 try {
56- final Map <String , String > configMapAnnotations = createConfigMapFromAnnotations (annotations );
57- element .addToData (configMapAnnotations );
62+ addConfigMapFromAnnotations (annotations , element );
5863 } catch (IOException e ) {
5964 throw new IllegalArgumentException (e );
6065 }
6166 }
6267 });
6368 }
6469
65- private Map < String , String > createConfigMapFromAnnotations (final Map <String , String > annotations ) throws IOException {
70+ private void addConfigMapFromAnnotations (final Map <String , String > annotations , final ConfigMapBuilder configMapBuilder ) throws IOException {
6671 final Set <Map .Entry <String , String >> entries = annotations .entrySet ();
67- final Map <String , String > configMapFileLocations = new HashMap <>();
68-
69- for (Iterator <Map .Entry <String , String >> it = entries .iterator (); it .hasNext (); ) {
72+ for (Iterator <Map .Entry <String , String >> it = entries .iterator (); it .hasNext (); ) {
7073 Map .Entry <String , String > entry = it .next ();
7174 final String key = entry .getKey ();
7275
73- if (key .startsWith (PREFIX_ANNOTATION )) {
74- configMapFileLocations . put ( getOutput (key ), readContent ( entry .getValue () ));
76+ if (key .startsWith (PREFIX_ANNOTATION )) {
77+ addConfigMapEntryFromFile ( configMapBuilder , getOutput (key ), entry .getValue ());
7578 it .remove ();
7679 }
7780 }
78-
79- return configMapFileLocations ;
8081 }
8182
82- private String readContent (String location ) throws IOException {
83- return new String (Files .readAllBytes (Paths .get (location )));
83+ private void addConfigMapEntryFromFile (final ConfigMapBuilder configMapBuilder , final String key , final String filePath ) throws IOException {
84+ final byte [] bytes = Files .readAllBytes (Paths .get (filePath ));
85+ try {
86+ StandardCharsets .UTF_8 .newDecoder ()
87+ .onMalformedInput (CodingErrorAction .REPORT )
88+ .onUnmappableCharacter (CodingErrorAction .REPORT )
89+ .decode (ByteBuffer .wrap (bytes ));
90+ final String value = new String (bytes );
91+ configMapBuilder .addToData (singletonMap (key , value ));
92+ } catch (CharacterCodingException e ) {
93+ final String value = Base64 .getEncoder ().encodeToString (bytes );
94+ configMapBuilder .addToBinaryData (singletonMap (key , value ));
95+ }
8496 }
8597
8698 private String getOutput (String key ) {
@@ -89,57 +101,51 @@ private String getOutput(String key) {
89101
90102 private void addConfigMapFromXmlConfigurations (KubernetesListBuilder builder ) {
91103 io .fabric8 .maven .core .config .ConfigMap configMap = getConfigMapFromXmlConfiguration ();
92- final Map <String , String > configMapFromConfiguration ;
93104 try {
94- configMapFromConfiguration = createConfigMapFromConfiguration (configMap );
95- String configMapName = (configMap == null || configMap .getName () == null || configMap .getName ().trim ().isEmpty ()) ? "xmlconfig" : configMap .getName ().trim ();
96- log .debug ("configMapName :: " .concat (configMapName ));
97-
98- if (!configMapFromConfiguration .isEmpty () && !checkIfItemExists (builder , configMapName )) {
99- ConfigMapBuilder element = new ConfigMapBuilder ();
100- element .withNewMetadata ().withName (configMapName ).endMetadata ();
101- element .addToData (configMapFromConfiguration );
102-
103- builder .addToConfigMapItems (element .build ());
105+ if (configMap == null ) {
106+ return ;
107+ }
108+ String configMapName = configMap .getName () == null || configMap .getName ().trim ().isEmpty () ? "xmlconfig" : configMap .getName ().trim ();
109+ if (checkIfItemExists (builder , configMapName )) {
110+ return ;
104111 }
105- } catch (IOException e ) {
106- throw new IllegalArgumentException (e );
107- }
108- }
109-
110- private boolean checkIfItemExists (KubernetesListBuilder builder , String name ) {
111- return builder .buildItems ().stream ().filter (item -> item .getKind ().equals ("ConfigMap" )).anyMatch (item -> item .getMetadata ().getName ().equals (name ));
112- }
113-
114- private io .fabric8 .maven .core .config .ConfigMap getConfigMapFromXmlConfiguration () {
115- ResourceConfig resourceConfig = getConfiguration ().getResource ().orElse (null );
116- if (resourceConfig != null && resourceConfig .getConfigMap () != null ) {
117- return resourceConfig .getConfigMap ();
118- }
119- return null ;
120- }
121112
122- private Map < String , String > createConfigMapFromConfiguration ( io . fabric8 . maven . core . config . ConfigMap configMap ) throws IOException {
123- final Map < String , String > configMapData = new HashMap <> ();
113+ ConfigMapBuilder configMapBuilder = new ConfigMapBuilder ();
114+ configMapBuilder . withNewMetadata (). withName ( configMapName ). endMetadata ();
124115
125- if (configMap != null ) {
126116 for (ConfigMapEntry configMapEntry : configMap .getEntries ()) {
127117 String name = configMapEntry .getName ();
128118 final String value = configMapEntry .getValue ();
129119 if (name != null && value != null ) {
130- configMapData . put (name , value );
120+ configMapBuilder . addToData (name , value );
131121 } else {
132122 final String file = configMapEntry .getFile ();
133123 if (file != null ) {
134124 if (name == null ) {
135125 name = Paths .get (file ).getFileName ().toString ();
136126 }
137- configMapData . put ( name , readContent ( file ) );
127+ addConfigMapEntryFromFile ( configMapBuilder , name , file );
138128 }
139129 }
140130 }
131+
132+ if (!configMapBuilder .getData ().isEmpty () || !configMapBuilder .getBinaryData ().isEmpty ()) {
133+ builder .addToConfigMapItems (configMapBuilder .build ());
134+ }
135+ } catch (IOException e ) {
136+ throw new IllegalArgumentException (e );
141137 }
142- return configMapData ;
143138 }
144139
140+ private boolean checkIfItemExists (KubernetesListBuilder builder , String name ) {
141+ return builder .buildItems ().stream ().filter (item -> item .getKind ().equals ("ConfigMap" )).anyMatch (item -> item .getMetadata ().getName ().equals (name ));
142+ }
143+
144+ private io .fabric8 .maven .core .config .ConfigMap getConfigMapFromXmlConfiguration () {
145+ ResourceConfig resourceConfig = getConfiguration ().getResource ().orElse (null );
146+ if (resourceConfig != null && resourceConfig .getConfigMap () != null ) {
147+ return resourceConfig .getConfigMap ();
148+ }
149+ return null ;
150+ }
145151}
0 commit comments