Skip to content

Commit 34a1b1a

Browse files
author
wlanboy
committed
first version of k8s and istio infos
1 parent f11b23b commit 34a1b1a

2 files changed

Lines changed: 110 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.wlanboy.javahttpclient.client;
2+
3+
import io.kubernetes.client.openapi.ApiClient;
4+
import io.kubernetes.client.openapi.apis.CoreV1Api;
5+
import io.kubernetes.client.openapi.apis.CustomObjectsApi;
6+
import io.kubernetes.client.util.Config;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
import org.springframework.stereotype.Service;
10+
11+
import java.io.IOException;
12+
import java.net.InetSocketAddress;
13+
import java.net.Socket;
14+
import java.nio.file.Files;
15+
import java.nio.file.Paths;
16+
import java.util.*;
17+
18+
@Service
19+
public class K8sDiagnosticService {
20+
21+
private static final Logger logger = LoggerFactory.getLogger(K8sDiagnosticService.class);
22+
23+
private final ApiClient apiClient;
24+
private final CoreV1Api coreV1Api;
25+
private final CustomObjectsApi customObjectsApi;
26+
27+
public K8sDiagnosticService() throws IOException {
28+
this.apiClient = Config.defaultClient();
29+
this.coreV1Api = new CoreV1Api(apiClient);
30+
this.customObjectsApi = new CustomObjectsApi(apiClient);
31+
logger.info("K8s ApiClient mit Fluent-API initialisiert.");
32+
}
33+
34+
public Map<String, Object> getContext() {
35+
Map<String, Object> details = new HashMap<>();
36+
details.put("podName", System.getenv().getOrDefault("HOSTNAME", "unknown"));
37+
details.put("namespace", getCurrentNamespace());
38+
details.put("istioSidecar", checkIstioSidecar());
39+
return details;
40+
}
41+
42+
@SuppressWarnings("unchecked")
43+
public List<Object> getIstioResources(String namespace, String type) {
44+
try {
45+
String plural = type.toLowerCase().endsWith("s") ? type.toLowerCase() : type.toLowerCase() + "s";
46+
47+
// Korrekte Verwendung der Fluent-API:
48+
// 1. Request-Objekt erstellen
49+
// 2. execute() aufrufen
50+
Object result = customObjectsApi.listNamespacedCustomObject(
51+
"networking.istio.io",
52+
"v1alpha3",
53+
namespace,
54+
plural
55+
).execute();
56+
57+
if (result instanceof Map) {
58+
return (List<Object>) ((Map<String, Object>) result).get("items");
59+
}
60+
return Collections.emptyList();
61+
62+
} catch (Exception e) {
63+
logger.error("Fehler beim Abrufen der Istio-Ressourcen ({}): {}", type, e.getMessage());
64+
return Collections.singletonList(Map.of("error", e.getMessage()));
65+
}
66+
}
67+
68+
private String getCurrentNamespace() {
69+
try {
70+
return Files.readString(Paths.get("/var/run/secrets/kubernetes.io/serviceaccount/namespace")).trim();
71+
} catch (Exception e) {
72+
return System.getenv().getOrDefault("KUBERNETES_NAMESPACE", "default");
73+
}
74+
}
75+
76+
private boolean checkIstioSidecar() {
77+
try (Socket socket = new Socket()) {
78+
socket.connect(new InetSocketAddress("127.0.0.1", 15021), 150);
79+
return true;
80+
} catch (Exception e) {
81+
return false;
82+
}
83+
}
84+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.wlanboy.javahttpclient.controller;
2+
3+
import com.wlanboy.javahttpclient.client.K8sDiagnosticService;
4+
import org.springframework.web.bind.annotation.*;
5+
import java.util.*;
6+
7+
@RestController
8+
@RequestMapping("/api/k8s")
9+
public class DiagnosticController {
10+
11+
private final K8sDiagnosticService diagnosticService;
12+
13+
public DiagnosticController(K8sDiagnosticService diagnosticService) {
14+
this.diagnosticService = diagnosticService;
15+
}
16+
17+
@GetMapping("/context")
18+
public Map<String, Object> getContext() {
19+
return diagnosticService.getContext();
20+
}
21+
22+
@GetMapping("/istio/{type}")
23+
public List<Object> getIstioConfig(@PathVariable String type, @RequestParam String namespace) {
24+
return diagnosticService.getIstioResources(namespace, type);
25+
}
26+
}

0 commit comments

Comments
 (0)