19
19
import io .fabric8 .kubernetes .api .model .ConfigBuilder ;
20
20
import io .fabric8 .kubernetes .api .model .Context ;
21
21
import io .fabric8 .kubernetes .api .model .NamedContext ;
22
+ import io .fabric8 .kubernetes .client .KubernetesClientException ;
22
23
import io .fabric8 .kubernetes .client .internal .KubeConfigUtils ;
23
24
24
25
import java .io .File ;
@@ -32,34 +33,23 @@ public class ConfigHelper {
32
33
private static final ObjectMapper mapper = new ObjectMapper (new YAMLFactory ());
33
34
34
35
public static String getKubeConfigPath () {
35
- return io .fabric8 .kubernetes .client .Config .getKubeconfigFilename ();
36
- }
37
-
38
- public static void saveKubeConfig (Config config ) throws IOException {
39
- mapper .writeValue (new File (getKubeConfigPath ()), config );
36
+ Collection <String > filenames = io .fabric8 .kubernetes .client .Config .getKubeconfigFilenames ();
37
+ if (filenames == null || filenames .isEmpty ()) {
38
+ return null ;
39
+ }
40
+ return filenames .stream ()
41
+ .filter (filename -> filename .endsWith ("config" ))
42
+ .findAny ()
43
+ .orElse (null );
40
44
}
41
-
42
45
public static Config safeLoadKubeConfig () {
43
46
try {
44
- return loadKubeConfig ();
45
- } catch (IOException e ) {
47
+ return io . fabric8 . kubernetes . client . Config . builder (). build ();
48
+ } catch (KubernetesClientException e ) {
46
49
return null ;
47
50
}
48
51
}
49
52
50
- public static Config loadKubeConfig () throws IOException {
51
- return loadKubeConfig (getKubeConfigPath ());
52
- }
53
-
54
- public static Config loadKubeConfig (String path ) throws IOException {
55
- File f = new File (path );
56
- if (f .exists ()) {
57
- return KubeConfigUtils .parseConfig (f );
58
- } else {
59
- return new ConfigBuilder ().build ();
60
- }
61
- }
62
-
63
53
public static boolean isKubeConfigParsable () {
64
54
return isKubeConfigParsable (new File (getKubeConfigPath ()));
65
55
}
@@ -85,63 +75,24 @@ public static ToolsConfig loadToolsConfig(URL url) throws IOException {
85
75
}
86
76
}
87
77
88
- public static NamedContext getCurrentContext () {
89
- try {
90
- Config config = loadKubeConfig ();
91
- return getCurrentContext (config );
92
- } catch (IOException e ) {
93
- return null ;
94
- }
95
- }
96
-
97
- public static NamedContext getCurrentContext (Config config ) {
98
- if (config == null ) {
78
+ public static String getCurrentContextName (io .fabric8 .kubernetes .client .Config config ) {
79
+ NamedContext current = config .getCurrentContext ();
80
+ if (current == null ) {
99
81
return null ;
82
+ } else {
83
+ return current .getName ();
100
84
}
101
- return KubeConfigUtils .getCurrentContext (config );
102
- }
103
-
104
- public static String getCurrentContextName (Config config ) {
105
- String current = null ;
106
- NamedContext currentContext = getCurrentContext (config );
107
- if (currentContext != null
108
- && currentContext .getName () != null ) {
109
- current = currentContext .getName ();
110
- }
111
- return current ;
112
85
}
113
86
114
- public static String getCurrentContextName () {
115
- try {
116
- return getCurrentContextName (loadKubeConfig ());
117
- } catch (IOException e ) {
118
- return null ;
87
+ public static boolean areEqual (io .fabric8 .kubernetes .client .Config thisConfig , io .fabric8 .kubernetes .client .Config thatConfig ) {
88
+ if (thisConfig == null ) {
89
+ return thatConfig == null ;
119
90
}
120
- }
121
-
122
- /**
123
- * Returns {@code true} if the given {@link io.fabric8.kubernetes.api.model.Config} and
124
- * the new {@link io.fabric8.kubernetes.api.model.Config} are equal. They are considered equal if they're
125
- * equal in
126
- * <ul>
127
- * <li>current context (cluster, user, current namespace, extensions)</li>
128
- * <li>(authentication) token</li>
129
- * </ul>
130
- *
131
- * @param kubeConfig the (file) config to compare
132
- * @param newKubeConfig the (client, runtime) config to compare
133
- * @return true if both configs are equal in context, contexts and token
134
- */
135
- public static boolean areEqualCurrentContext (Config kubeConfig , Config newKubeConfig ) {
136
- if (newKubeConfig == null ) {
137
- return kubeConfig == null ;
138
- } else if (kubeConfig == null ) {
91
+ if (thatConfig == null ) {
139
92
return false ;
140
93
}
141
- return areEqual (KubeConfigUtils .getCurrentContext (newKubeConfig ), KubeConfigUtils .getCurrentContext (kubeConfig ))
142
- && areEqualToken (kubeConfig , newKubeConfig );
94
+ return thisConfig .equals (thatConfig );
143
95
}
144
-
145
96
/**
146
97
* Returns {@code true} if the given {@link io.fabric8.kubernetes.api.model.Config} and
147
98
* (client runtime) {@link io.fabric8.kubernetes.client.Config} are equal. They are considered equal if they're
@@ -157,14 +108,16 @@ public static boolean areEqualCurrentContext(Config kubeConfig, Config newKubeCo
157
108
* @return true if both configs are equal in context, contexts and token
158
109
*/
159
110
public static boolean areEqual (Config kubeConfig , io .fabric8 .kubernetes .client .Config clientConfig ) {
160
- if (clientConfig == null ) {
111
+ /* if (clientConfig == null) {
161
112
return kubeConfig == null;
162
113
} else if (kubeConfig == null) {
163
114
return false;
164
115
}
165
116
return areEqual(clientConfig.getCurrentContext(), KubeConfigUtils.getCurrentContext(kubeConfig))
166
117
&& areEqual(clientConfig.getContexts(), kubeConfig.getContexts())
167
118
&& areEqualToken(kubeConfig, clientConfig);
119
+ */
120
+ return false ;
168
121
}
169
122
170
123
/**
@@ -181,6 +134,7 @@ && areEqual(clientConfig.getContexts(), kubeConfig.getContexts())
181
134
* @param thatConfig the second (file) config to compare
182
135
* @return true if both configs are equal in context, contexts and token
183
136
*/
137
+ /*
184
138
public static boolean areEqual(Config thisConfig, Config thatConfig) {
185
139
if (thisConfig == null) {
186
140
return thatConfig == null;
@@ -191,7 +145,7 @@ public static boolean areEqual(Config thisConfig, Config thatConfig) {
191
145
&& areEqual(thisConfig.getContexts(), thatConfig.getContexts())
192
146
&& areEqualToken(getAuthInfo(thisConfig), getAuthInfo(thatConfig));
193
147
}
194
-
148
+ */
195
149
/**
196
150
* Returns {@code true} if both given contexts are equal. They are considered equal if they're equal in
197
151
* <ul>
@@ -266,10 +220,11 @@ private static boolean contains(NamedContext namedContext, Collection<NamedConte
266
220
* @param clientConfig the (client) config that contains the token
267
221
* @return true if both tokens are equal, false otherwise
268
222
*/
223
+ /*
269
224
public static boolean areEqualToken(Config kubeConfig, io.fabric8.kubernetes.client.Config clientConfig) {
270
225
return areEqualToken(getAuthInfo(kubeConfig), clientConfig);
271
226
}
272
-
227
+ */
273
228
/**
274
229
* Returns {@code true} if the token in the given (kubernetes file) {@link io.fabric8.kubernetes.api.model.Config}
275
230
* and the one in the new Kubernetes file {@link io.fabric8.kubernetes.api.model.Config} are equal.
@@ -279,18 +234,20 @@ public static boolean areEqualToken(Config kubeConfig, io.fabric8.kubernetes.cli
279
234
* @param newKubeConfig the (client) config that contains the token
280
235
* @return true if both tokens are equal, false otherwise
281
236
*/
237
+ /*
282
238
public static boolean areEqualToken(Config kubeConfig, Config newKubeConfig) {
283
239
return areEqualToken(getAuthInfo(kubeConfig), getAuthInfo(newKubeConfig));
284
240
}
285
-
241
+ */
242
+ /*
286
243
private static AuthInfo getAuthInfo(Config kubeConfig) {
287
244
NamedContext current = KubeConfigUtils.getCurrentContext(kubeConfig);
288
245
if (current == null) {
289
246
return null;
290
247
}
291
248
return KubeConfigUtils.getUserAuthInfo(kubeConfig, current.getContext());
292
249
}
293
-
250
+ */
294
251
/**
295
252
* Returns {@code true} if the token in the given {@link AuthInfo} (that's retrieved from the kube config file)
296
253
* and {@link Config} (that's contains the runtime settings that the kubernetes-client is using) are equal.
0 commit comments