Skip to content

Commit 698bacc

Browse files
committed
bump kubernetes-client to 7.0.0 (redhat-developer#247)
Signed-off-by: Andre Dietisheim <[email protected]>
1 parent 9f738b1 commit 698bacc

File tree

6 files changed

+88
-161
lines changed

6 files changed

+88
-161
lines changed

src/main/java/com/redhat/devtools/intellij/common/kubernetes/ClusterHelper.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,32 @@
1616
import io.fabric8.kubernetes.client.VersionInfo;
1717
import io.fabric8.openshift.client.OpenShiftClient;
1818

19-
import java.net.HttpURLConnection;
20-
2119
public class ClusterHelper {
2220

2321
private ClusterHelper() {
2422
//avoid instanciation
2523
}
2624

2725
public static boolean isOpenShift(KubernetesClient client) {
28-
return client.hasApiGroup(OpenShiftClient.BASE_API_GROUP, false);
26+
try {
27+
return client.hasApiGroup(OpenShiftClient.BASE_API_GROUP, false);
28+
} catch (KubernetesClientException e) {
29+
return false;
30+
}
2931
}
3032

3133
public static ClusterInfo getClusterInfo(KubernetesClient client) {
32-
if (client instanceof OpenShiftClient) {
33-
return new ClusterInfo(
34-
getKubernetesVersion((OpenShiftClient) client),
35-
true,
36-
getOpenShiftVersion((OpenShiftClient) client));
37-
38-
} else if (client.adapt(OpenShiftClient.class) != null && client.adapt(OpenShiftClient.class).isSupported()){
34+
OpenShiftClient openShiftClient = getOpenShiftClient(client);
35+
if (openShiftClient != null) {
3936
return new ClusterInfo(
40-
getKubernetesVersion(client),
37+
getKubernetesVersion(openShiftClient),
4138
true,
42-
getOpenShiftVersion(client));
39+
getOpenShiftVersion(openShiftClient));
4340
} else {
4441
return new ClusterInfo(
4542
getKubernetesVersion(client),
4643
false,
4744
"");
48-
4945
}
5046
}
5147

@@ -63,18 +59,21 @@ private static String getKubernetesVersion(KubernetesClient client) {
6359
return version != null ? version.getGitVersion() : "";
6460
}
6561

66-
private static String getOpenShiftVersion(KubernetesClient client) {
67-
try {
68-
OpenShiftClient oclient = client.adapt(OpenShiftClient.class);
69-
return getOpenShiftVersion(oclient);
70-
} catch (KubernetesClientException e) {
62+
private static OpenShiftClient getOpenShiftClient(KubernetesClient client) {
63+
if (client instanceof OpenShiftClient) {
64+
return (OpenShiftClient) client;
65+
} else if (isOpenShift(client)) {
66+
return client.adapt(OpenShiftClient.class);
67+
} else {
7168
return null;
7269
}
7370
}
7471

7572
private static String getOpenShiftVersion(OpenShiftClient client) {
7673
VersionInfo version = client.getVersion();
77-
return version != null && version.getMajor() != null ? getVersion(version.getMajor(), version.getMinor()) : "";
74+
return version != null && version.getMajor() != null ?
75+
getVersion(version.getMajor(), version.getMinor())
76+
: "";
7877
}
7978

8079
private static String getVersion(String major, String minor) {

src/main/java/com/redhat/devtools/intellij/common/utils/ConfigHelper.java

Lines changed: 32 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.fabric8.kubernetes.api.model.ConfigBuilder;
2020
import io.fabric8.kubernetes.api.model.Context;
2121
import io.fabric8.kubernetes.api.model.NamedContext;
22+
import io.fabric8.kubernetes.client.KubernetesClientException;
2223
import io.fabric8.kubernetes.client.internal.KubeConfigUtils;
2324

2425
import java.io.File;
@@ -32,34 +33,23 @@ public class ConfigHelper {
3233
private static final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
3334

3435
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);
4044
}
41-
4245
public static Config safeLoadKubeConfig() {
4346
try {
44-
return loadKubeConfig();
45-
} catch (IOException e) {
47+
return io.fabric8.kubernetes.client.Config.builder().build();
48+
} catch (KubernetesClientException e) {
4649
return null;
4750
}
4851
}
4952

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-
6353
public static boolean isKubeConfigParsable() {
6454
return isKubeConfigParsable(new File(getKubeConfigPath()));
6555
}
@@ -85,63 +75,24 @@ public static ToolsConfig loadToolsConfig(URL url) throws IOException {
8575
}
8676
}
8777

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) {
9981
return null;
82+
} else {
83+
return current.getName();
10084
}
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;
11285
}
11386

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;
11990
}
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) {
13992
return false;
14093
}
141-
return areEqual(KubeConfigUtils.getCurrentContext(newKubeConfig), KubeConfigUtils.getCurrentContext(kubeConfig))
142-
&& areEqualToken(kubeConfig, newKubeConfig);
94+
return thisConfig.equals(thatConfig);
14395
}
144-
14596
/**
14697
* Returns {@code true} if the given {@link io.fabric8.kubernetes.api.model.Config} and
14798
* (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
157108
* @return true if both configs are equal in context, contexts and token
158109
*/
159110
public static boolean areEqual(Config kubeConfig, io.fabric8.kubernetes.client.Config clientConfig) {
160-
if (clientConfig == null) {
111+
/* if (clientConfig == null) {
161112
return kubeConfig == null;
162113
} else if (kubeConfig == null) {
163114
return false;
164115
}
165116
return areEqual(clientConfig.getCurrentContext(), KubeConfigUtils.getCurrentContext(kubeConfig))
166117
&& areEqual(clientConfig.getContexts(), kubeConfig.getContexts())
167118
&& areEqualToken(kubeConfig, clientConfig);
119+
*/
120+
return false;
168121
}
169122

170123
/**
@@ -181,6 +134,7 @@ && areEqual(clientConfig.getContexts(), kubeConfig.getContexts())
181134
* @param thatConfig the second (file) config to compare
182135
* @return true if both configs are equal in context, contexts and token
183136
*/
137+
/*
184138
public static boolean areEqual(Config thisConfig, Config thatConfig) {
185139
if (thisConfig == null) {
186140
return thatConfig == null;
@@ -191,7 +145,7 @@ public static boolean areEqual(Config thisConfig, Config thatConfig) {
191145
&& areEqual(thisConfig.getContexts(), thatConfig.getContexts())
192146
&& areEqualToken(getAuthInfo(thisConfig), getAuthInfo(thatConfig));
193147
}
194-
148+
*/
195149
/**
196150
* Returns {@code true} if both given contexts are equal. They are considered equal if they're equal in
197151
* <ul>
@@ -266,10 +220,11 @@ private static boolean contains(NamedContext namedContext, Collection<NamedConte
266220
* @param clientConfig the (client) config that contains the token
267221
* @return true if both tokens are equal, false otherwise
268222
*/
223+
/*
269224
public static boolean areEqualToken(Config kubeConfig, io.fabric8.kubernetes.client.Config clientConfig) {
270225
return areEqualToken(getAuthInfo(kubeConfig), clientConfig);
271226
}
272-
227+
*/
273228
/**
274229
* Returns {@code true} if the token in the given (kubernetes file) {@link io.fabric8.kubernetes.api.model.Config}
275230
* 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
279234
* @param newKubeConfig the (client) config that contains the token
280235
* @return true if both tokens are equal, false otherwise
281236
*/
237+
/*
282238
public static boolean areEqualToken(Config kubeConfig, Config newKubeConfig) {
283239
return areEqualToken(getAuthInfo(kubeConfig), getAuthInfo(newKubeConfig));
284240
}
285-
241+
*/
242+
/*
286243
private static AuthInfo getAuthInfo(Config kubeConfig) {
287244
NamedContext current = KubeConfigUtils.getCurrentContext(kubeConfig);
288245
if (current == null) {
289246
return null;
290247
}
291248
return KubeConfigUtils.getUserAuthInfo(kubeConfig, current.getContext());
292249
}
293-
250+
*/
294251
/**
295252
* Returns {@code true} if the token in the given {@link AuthInfo} (that's retrieved from the kube config file)
296253
* and {@link Config} (that's contains the runtime settings that the kubernetes-client is using) are equal.

0 commit comments

Comments
 (0)