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+ }
0 commit comments