-
Notifications
You must be signed in to change notification settings - Fork 218
/
KubernetesJmxConnector.java
158 lines (143 loc) · 5.72 KB
/
KubernetesJmxConnector.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package org.jolokia.kubernetes.client;
import com.squareup.okhttp.Credentials;
import com.squareup.okhttp.Response;
import io.kubernetes.client.ApiClient;
import io.kubernetes.client.ApiException;
import io.kubernetes.client.Configuration;
import io.kubernetes.client.Pair;
import io.kubernetes.client.apis.CoreV1Api;
import io.kubernetes.client.models.V1Pod;
import io.kubernetes.client.util.ClientBuilder;
import io.kubernetes.client.util.KubeConfig;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXServiceURL;
import org.jolokia.client.J4pClient;
import org.jolokia.client.jmxadapter.JolokiaJmxConnector;
import org.jolokia.client.jmxadapter.RemoteJmxAdapter;
public class KubernetesJmxConnector extends JolokiaJmxConnector {
private static Pattern POD_PATTERN = Pattern
.compile("/namespaces/([^/]+)/pods/([^/]+)/(.+)");
private static ApiClient apiClient;
public KubernetesJmxConnector(JMXServiceURL serviceURL,
Map<String, ?> environment) {
super(serviceURL, environment);
}
@Override
public void connect(Map<String, ?> env) throws IOException {
if (!"kubernetes".equals(this.serviceUrl.getProtocol())) {
throw new MalformedURLException(String
.format("Invalid URL %s : Only protocol \"kubernetes\" is supported (not %s)", serviceUrl,
serviceUrl.getProtocol()));
}
final Map<String, Object> mergedEnvironment = this.mergedEnvironment(env);
ApiClient client = getApiClient(mergedEnvironment);
this.adapter = createAdapter(expandAndProbeUrl(client, mergedEnvironment));
this.postCreateAdapter();
}
protected RemoteJmxAdapter createAdapter(J4pClient client) throws IOException {
return new RemoteJmxAdapter(client);
}
public static ApiClient getApiClient(Map<String, ?> env) throws IOException {
if (apiClient != null) {
return apiClient;
}
return buildApiClient(env);
}
public static ApiClient buildApiClient(Map<String, ?> env) throws IOException {
// file path to your KubeConfig
final Object configPath = env != null ? env.get("kube.config.path") : null;
String kubeConfigPath = configPath != null ? configPath.toString()
: String.format("%s/.kube/config", System.getProperty("user.home"));
// loading the out-of-cluster config, a kubeconfig from file-system
return apiClient = ClientBuilder
.kubeconfig(KubeConfig.loadKubeConfig(new FileReader(kubeConfigPath))).build();
}
/**
* @return a connection if successful
*/
protected J4pClient expandAndProbeUrl(ApiClient client,
Map<String, Object> env) throws MalformedURLException {
Configuration.setDefaultApiClient(client);
CoreV1Api api = new CoreV1Api();
String proxyPath = this.serviceUrl.getURLPath();
J4pClient connection;
final HashMap<String, String> headersForProbe = createHeadersForProbe(env);
try {
if (POD_PATTERN.matcher(proxyPath).matches()) {
final Matcher matcher = POD_PATTERN.matcher(proxyPath);
if (matcher.find()) {
String namespace = matcher.group(1);
String podPattern = matcher.group(2);
String path = matcher.group(3);
//check if podname pans out directly
if ((connection = probeProxyPath(env, client, namespace, podPattern, path,
headersForProbe)) != null) {
return connection;
} else { //scan through pods in namespace if podname is a pattern
for (final V1Pod pod : api
.listNamespacedPod(namespace,
false,
null,
null,
null,
null,
null,
null,
10,
null)
.getItems()) {
if (pod.getMetadata().getNamespace().matches(namespace) && pod.getMetadata()
.getName().matches(podPattern)) {
if ((connection = probeProxyPath(env, client, namespace,
pod.getMetadata().getName(), path, headersForProbe)) != null) {
return connection;
}
}
}
}
}
}
} catch (ApiException ignore) {
}
throw new MalformedURLException("Unable to connect to proxypath " + proxyPath);
}
private HashMap<String, String> createHeadersForProbe(
Map<String, Object> env) {
final HashMap<String, String> headers = new HashMap<String, String>();
String[] credentials = (String[]) env.get(JMXConnector.CREDENTIALS);
if (credentials != null) {
headers.put("Authorization", Credentials.basic(credentials[0], credentials[1]));
}
return headers;
}
/**
* Probe whether we find Jolokia in given namespace, pod and path
*/
public static J4pClient probeProxyPath(Map<String, Object> env, ApiClient client,
String namespace, String podName, String path,
HashMap<String, String> headers)
throws ApiException {
final String proxyPath = String
.format("/api/v1/namespaces/%s/pods/%s/proxy/%s", namespace, podName, path);
try {
Response response = MinimalHttpClientAdapter
.performRequest(client, proxyPath, Collections.singletonMap("type", "version"),
Collections.<Pair>emptyList(), "POST", headers);
response.body().close();
if (response.isSuccessful()) {
return new J4pClient(
proxyPath, new MinimalHttpClientAdapter(client, proxyPath, env));
}
} catch (IOException ignore) {
}
return null;
}
}