Skip to content
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

Merged
merged 9 commits into from Oct 4, 2020
Expand Up @@ -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,
Expand Down Expand Up @@ -71,12 +72,15 @@ protected J4pClient expandAndProbeUrl(KubernetesClient client,
if (POD_PATTERN.matcher(proxyPath).matches()) {
final Matcher matcher = POD_PATTERN.matcher(proxyPath);
Copy link
Member

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.

Copy link
Contributor Author

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.

if (matcher.find()) {
String namespace = matcher.group(1);
String podPattern = matcher.group(2);
String path = matcher.group(3);
String namespace = matcher.group("namespace");
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 (?)

Copy link
Member

Choose a reason for hiding this comment

The 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
Expand All @@ -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;
}
}
Expand All @@ -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>();
Expand All @@ -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);
Expand Down
Expand Up @@ -13,7 +13,7 @@ public class ManualTestConnection {
@Test(groups = "manual")
public void testConnect() throws IOException {
final JMXServiceURL jmxServiceURL = new JMXServiceURL(
"service:jmx:kubernetes:///che/petclinic-.+-.+/actuator/jolokia/"
"service:jmx:kubernetes:///che/petclinic-.+-.+:8778/jolokia/"
);
jmxServiceURL.getProtocol();

Expand Down