18
18
import io .fabric8 .kubernetes .api .model .AuthInfo ;
19
19
import io .fabric8 .kubernetes .api .model .Cluster ;
20
20
import io .fabric8 .kubernetes .api .model .Config ;
21
+ import io .fabric8 .kubernetes .api .model .ConfigBuilder ;
21
22
import io .fabric8 .kubernetes .api .model .Context ;
22
23
import io .fabric8 .kubernetes .api .model .NamedAuthInfo ;
23
24
import io .fabric8 .kubernetes .api .model .NamedCluster ;
24
25
import io .fabric8 .kubernetes .api .model .NamedContext ;
26
+ import io .fabric8 .kubernetes .api .model .NamedExtension ;
27
+ import io .fabric8 .kubernetes .api .model .PreferencesBuilder ;
25
28
import io .fabric8 .kubernetes .client .utils .Serialization ;
29
+ import io .fabric8 .kubernetes .client .utils .Utils ;
26
30
27
31
import java .io .File ;
28
- import java .io .FileInputStream ;
29
32
import java .io .FileWriter ;
30
33
import java .io .IOException ;
34
+ import java .nio .file .Files ;
31
35
import java .util .List ;
32
36
33
37
/**
@@ -40,7 +44,7 @@ private KubeConfigUtils() {
40
44
}
41
45
42
46
public static Config parseConfig (File file ) throws IOException {
43
- return Serialization .unmarshal (new FileInputStream (file ), Config .class );
47
+ return Serialization .unmarshal (Files . newInputStream (file . toPath () ), Config .class );
44
48
}
45
49
46
50
public static Config parseConfigFromString (String contents ) {
@@ -56,16 +60,31 @@ public static Config parseConfigFromString(String contents) {
56
60
public static NamedContext getCurrentContext (Config config ) {
57
61
String contextName = config .getCurrentContext ();
58
62
if (contextName != null ) {
63
+ return getContext (config , contextName );
64
+ }
65
+ return null ;
66
+ }
67
+
68
+ /**
69
+ * Returns the {@link NamedContext} with the given name.
70
+ * Returns {@code null} otherwise
71
+ *
72
+ * @param config the config to search
73
+ * @param name the context name to match
74
+ * @return the context with the the given name
75
+ */
76
+ public static NamedContext getContext (Config config , String name ) {
77
+ NamedContext context = null ;
78
+ if (config != null && name != null ) {
59
79
List <NamedContext > contexts = config .getContexts ();
60
80
if (contexts != null ) {
61
- for (NamedContext context : contexts ) {
62
- if (contextName .equals (context .getName ())) {
63
- return context ;
64
- }
65
- }
81
+ context = contexts .stream ()
82
+ .filter (toInspect -> name .equals (toInspect .getName ()))
83
+ .findAny ()
84
+ .orElse (null );
66
85
}
67
86
}
68
- return null ;
87
+ return context ;
69
88
}
70
89
71
90
/**
@@ -91,23 +110,49 @@ public static String getUserToken(Config config, Context context) {
91
110
* @return {@link AuthInfo} for current context
92
111
*/
93
112
public static AuthInfo getUserAuthInfo (Config config , Context context ) {
94
- AuthInfo authInfo = null ;
95
- if (config != null && context != null ) {
96
- String user = context .getUser ();
97
- if (user != null ) {
98
- List <NamedAuthInfo > users = config .getUsers ();
99
- if (users != null ) {
100
- authInfo = users .stream ()
101
- .filter (u -> u .getName ().equals (user ))
102
- .findAny ()
103
- .map (NamedAuthInfo ::getUser )
104
- .orElse (null );
105
- }
113
+ NamedAuthInfo namedAuthInfo = getAuthInfo (config , context .getUser ());
114
+ return (namedAuthInfo != null ) ? namedAuthInfo .getUser () : null ;
115
+ }
116
+
117
+ /**
118
+ * Returns the {@link NamedAuthInfo} with the given name.
119
+ * Returns {@code null} otherwise
120
+ *
121
+ * @param config the config to search
122
+ * @param name
123
+ * @return
124
+ */
125
+ public static NamedAuthInfo getAuthInfo (Config config , String name ) {
126
+ NamedAuthInfo authInfo = null ;
127
+ if (config != null && name != null ) {
128
+ List <NamedAuthInfo > users = config .getUsers ();
129
+ if (users != null ) {
130
+ authInfo = users .stream ()
131
+ .filter (toInspect -> name .equals (toInspect .getName ()))
132
+ .findAny ()
133
+ .orElse (null );
106
134
}
107
135
}
108
136
return authInfo ;
109
137
}
110
138
139
+ /**
140
+ * Returns {@code true} if the given {@link Config} has a {@link NamedAuthInfo} with the given name.
141
+ * Returns {@code false} otherwise.
142
+ *
143
+ * @param name the name of the NamedAuthInfo that we are looking for
144
+ * @param config the Config to search
145
+ * @return true if it contains a NamedAuthInfo with the given name
146
+ */
147
+ public static boolean hasAuthInfoNamed (Config config , String name ) {
148
+ if (Utils .isNullOrEmpty (name )
149
+ || config == null
150
+ || config .getUsers () == null ) {
151
+ return false ;
152
+ }
153
+ return getAuthInfo (config , name ) != null ;
154
+ }
155
+
111
156
/**
112
157
* Returns the current {@link Cluster} for the current context
113
158
*
@@ -161,4 +206,39 @@ public static void persistKubeConfigIntoFile(Config kubeConfig, String kubeConfi
161
206
writer .write (Serialization .asYaml (kubeConfig ));
162
207
}
163
208
}
209
+
210
+ public static Config merge (Config thisConfig , Config thatConfig ) {
211
+ if (thisConfig == null ) {
212
+ return thatConfig ;
213
+ }
214
+ ConfigBuilder builder = new ConfigBuilder (thatConfig );
215
+ if (thisConfig .getClusters () != null ) {
216
+ builder .addAllToClusters (thisConfig .getClusters ());
217
+ }
218
+ if (thisConfig .getContexts () != null ) {
219
+ builder .addAllToContexts (thisConfig .getContexts ());
220
+ }
221
+ if (thisConfig .getUsers () != null ) {
222
+ builder .addAllToUsers (thisConfig .getUsers ());
223
+ }
224
+ if (thisConfig .getExtensions () != null ) {
225
+ builder .addAllToExtensions (thisConfig .getExtensions ());
226
+ }
227
+ if (!builder .hasCurrentContext ()
228
+ && Utils .isNotNullOrEmpty (thisConfig .getCurrentContext ())) {
229
+ builder .withCurrentContext (thisConfig .getCurrentContext ());
230
+ }
231
+ Config merged = builder .build ();
232
+ mergePreferences (thisConfig , merged );
233
+ return merged ;
234
+ }
235
+
236
+ public static void mergePreferences (io .fabric8 .kubernetes .api .model .Config source ,
237
+ io .fabric8 .kubernetes .api .model .Config destination ) {
238
+ if (source .getPreferences () != null ) {
239
+ PreferencesBuilder builder = new PreferencesBuilder (destination .getPreferences ());
240
+ builder .addToExtensions (source .getExtensions ().toArray (new NamedExtension [] {}));
241
+ destination .setPreferences (builder .build ());
242
+ }
243
+ }
164
244
}
0 commit comments