-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathClusterHelper.java
89 lines (75 loc) · 3.24 KB
/
ClusterHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*******************************************************************************
* Copyright (c) 2021-2023 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package com.redhat.devtools.intellij.common.kubernetes;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.VersionInfo;
import io.fabric8.openshift.client.OpenShiftClient;
import java.net.HttpURLConnection;
public class ClusterHelper {
private ClusterHelper() {
//avoid instanciation
}
public static boolean isOpenShift(KubernetesClient client) {
OpenShiftClient osClient = client.adapt(OpenShiftClient.class);
try {
return osClient.isSupported();
} catch (KubernetesClientException e) {
return e.getCode() == HttpURLConnection.HTTP_UNAUTHORIZED;
}
}
public static ClusterInfo getClusterInfo(KubernetesClient client) {
if (client instanceof OpenShiftClient) {
return new ClusterInfo(
getKubernetesVersion((OpenShiftClient) client),
true,
getOpenShiftVersion((OpenShiftClient) client));
} else if (client.adapt(OpenShiftClient.class) != null && client.adapt(OpenShiftClient.class).isSupported()){
return new ClusterInfo(
getKubernetesVersion(client),
true,
getOpenShiftVersion(client));
} else {
return new ClusterInfo(
getKubernetesVersion(client),
false,
"");
}
}
private static String getKubernetesVersion(OpenShiftClient client) {
try {
KubernetesClient kclient = new KubernetesClientBuilder().withConfig(client.getConfiguration()).build();
return getKubernetesVersion(kclient);
} catch (KubernetesClientException e) {
return null;
}
}
private static String getKubernetesVersion(KubernetesClient client) {
VersionInfo version = client.getKubernetesVersion();
return version != null ? version.getGitVersion() : "";
}
private static String getOpenShiftVersion(KubernetesClient client) {
try {
OpenShiftClient oclient = client.adapt(OpenShiftClient.class);
return getOpenShiftVersion(oclient);
} catch (KubernetesClientException e) {
return null;
}
}
private static String getOpenShiftVersion(OpenShiftClient client) {
VersionInfo version = client.getVersion();
return version != null && version.getMajor() != null ? getVersion(version.getMajor(), version.getMinor()) : "";
}
private static String getVersion(String major, String minor) {
return major + '.' + minor;
}
}