New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Kubernetes connector #434
Kubernetes connector #434
Changes from 1 commit
cb7fca4
c5d7ee2
2c1af94
25b31a4
12164cb
b0f9875
373fb85
29d250a
a2ff078
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,8 @@ | |
public class KubernetesJmxConnector extends JolokiaJmxConnector { | ||
|
||
private static Pattern POD_PATTERN = Pattern | ||
.compile("/?([^/]+)/([^/]+)/(.+)"); | ||
.compile( | ||
"/?(?<namespace>[^/]+)/(?<protocol>https?:)?(?<podPattern>[^/^:]+)(?<port>:[^/]+)?/(?<path>.+)"); | ||
private static KubernetesClient apiClient; | ||
|
||
public KubernetesJmxConnector(JMXServiceURL serviceURL, | ||
|
@@ -71,12 +72,15 @@ protected J4pClient expandAndProbeUrl(KubernetesClient client, | |
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); | ||
String namespace = matcher.group("namespace"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note, this requires Java 1.7 , I can happily replace with numeric groups, however was not sure if Java 1.6 is feasible anymore (?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We abandoned Java 1.6 already afair (my gosh, really not deep in Jolokia anymore :(. Lets keep it ... |
||
String podPattern = matcher.group("podPattern"); | ||
String path = matcher.group("path"); | ||
String protocol = matcher.group("protocol"); | ||
String port = matcher.group("port"); | ||
final Pod exactPod = client.pods().inNamespace(namespace).withName(podPattern).get(); | ||
//check if podname pans out directly | ||
if (exactPod != null && (connection = probeProxyPath(env, client, exactPod, path, | ||
if (exactPod != null | ||
&& (connection = probeProxyPath(env, client, buildProxyPath(exactPod, protocol, port, path), | ||
headersForProbe)) != null) { | ||
return connection; | ||
} else { //scan through pods in namespace if podname is a pattern | ||
|
@@ -85,7 +89,8 @@ protected J4pClient expandAndProbeUrl(KubernetesClient client, | |
client.pods().inNamespace(namespace).list().getItems()) { | ||
if (pod.getMetadata() | ||
.getName().matches(podPattern)) { | ||
if ((connection = probeProxyPath(env, client, pod, path, headersForProbe)) != null) { | ||
if ((connection = probeProxyPath(env, client, buildProxyPath(pod, protocol, port, path), | ||
headersForProbe)) != null) { | ||
return connection; | ||
} | ||
} | ||
|
@@ -98,6 +103,27 @@ protected J4pClient expandAndProbeUrl(KubernetesClient client, | |
throw new MalformedURLException("Unable to connect to proxypath " + proxyPath); | ||
} | ||
|
||
private StringBuilder buildProxyPath(Pod pod, String protocol, String port, String path) { | ||
final StringBuilder url = new StringBuilder(pod.getMetadata().getSelfLink()); | ||
if (protocol != null && !protocol.equals("http:")) { | ||
//if we need to specify protocol, inject in url | ||
final int index = url.lastIndexOf(pod.getMetadata().getName()); | ||
if (index > -1) { | ||
url.insert(index, protocol); | ||
} | ||
} | ||
if(port!=null){ | ||
url.append(port); | ||
} | ||
url.append("/proxy"); | ||
|
||
if(!path.startsWith("/")) { | ||
url.append('/'); | ||
} | ||
url.append(path); | ||
return url; | ||
} | ||
|
||
private static HashMap<String, String> createHeadersForProbe( | ||
Map<String, Object> env) { | ||
final HashMap<String, String> headers = new HashMap<String, String>(); | ||
|
@@ -112,10 +138,10 @@ private static HashMap<String, String> createHeadersForProbe( | |
* Probe whether we find Jolokia in given namespace, pod and path | ||
*/ | ||
public static J4pClient probeProxyPath(Map<String, Object> env, KubernetesClient client, | ||
Pod pod, String path, | ||
StringBuilder url, | ||
HashMap<String, String> headers) { | ||
try { | ||
final String proxyPath = pod.getMetadata().getSelfLink() + "/proxy/" + path; | ||
final String proxyPath = url.toString(); | ||
Response response = MinimalHttpClientAdapter | ||
.performRequest((BaseClient) client, proxyPath, "{\"type\":\"version\"}".getBytes(), null | ||
, headers); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this method is already very long. I would make sense to move the service and pod specific handling in different methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Service handling is removed. Only one loop left now.