Skip to content

Commit 8aecb2c

Browse files
author
wlanboy
committed
refactoring
1 parent d56e9ef commit 8aecb2c

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

src/main/java/com/wlanboy/javahttpclient/client/K8sDiagnosticService.java

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,30 @@ public class K8sDiagnosticService {
2323
private static final String ISTIO_GROUP = "networking.istio.io";
2424
private static final String ISTIO_VERSION = "v1alpha3";
2525

26-
private final ApiClient apiClient;
27-
private final CustomObjectsApi customObjectsApi;
26+
private volatile ApiClient apiClient;
27+
private volatile CustomObjectsApi customObjectsApi;
2828
private final RestTemplate restTemplate;
29+
private volatile boolean k8sInitialized = false;
30+
private volatile String k8sInitError = null;
2931

30-
public K8sDiagnosticService() throws IOException {
31-
this.apiClient = Config.defaultClient();
32-
this.customObjectsApi = new CustomObjectsApi(apiClient);
32+
public K8sDiagnosticService() {
3333
this.restTemplate = new RestTemplate();
34-
logger.info("K8s Diagnostic Service erfolgreich initialisiert.");
34+
initializeK8sClient();
35+
logger.info("K8s Diagnostic Service initialisiert (K8s API verfügbar: {}).", k8sInitialized);
36+
}
37+
38+
private synchronized void initializeK8sClient() {
39+
if (k8sInitialized) {
40+
return;
41+
}
42+
try {
43+
this.apiClient = Config.defaultClient();
44+
this.customObjectsApi = new CustomObjectsApi(apiClient);
45+
this.k8sInitialized = true;
46+
} catch (IOException e) {
47+
logger.warn("K8s API Client konnte nicht initialisiert werden: {}. Istio-Ressourcen nicht verfügbar.", e.getMessage());
48+
this.k8sInitError = e.getMessage();
49+
}
3550
}
3651

3752
public Map<String, Object> getContext() {
@@ -65,7 +80,6 @@ public Map<String, Object> getFullSidecarDetails() {
6580
String clusters = restTemplate.getForObject(ENVOY_ADMIN_URL + "/clusters", String.class);
6681
reachability.put("activeEndpoints", clusters);
6782
reachability.put("summary", summarizeClusters(clusters));
68-
reachability.put("envoyConfig", restTemplate.getForObject(ENVOY_ADMIN_URL + "/config_dump", Map.class));
6983
report.put("reachability", reachability);
7084

7185
// Sektion B: Gesundheit & Fehler
@@ -92,13 +106,16 @@ private Map<String, String> parseErrorStatsOnly(String rawStats) {
92106
rawStats.lines()
93107
.filter(line -> line.contains(":"))
94108
.forEach(line -> {
95-
String[] parts = line.split(":");
96-
String val = parts[1].trim();
97-
try {
98-
if (Long.parseLong(val) > 0) {
99-
errorMap.put(parts[0].trim(), val);
109+
int colonIndex = line.lastIndexOf(':');
110+
if (colonIndex > 0 && colonIndex < line.length() - 1) {
111+
String key = line.substring(0, colonIndex).trim();
112+
String val = line.substring(colonIndex + 1).trim();
113+
try {
114+
if (Long.parseLong(val) > 0) {
115+
errorMap.put(key, val);
116+
}
117+
} catch (NumberFormatException ignored) {
100118
}
101-
} catch (Exception ignored) {
102119
}
103120
});
104121
}
@@ -127,13 +144,16 @@ private Map<String, Object> getEnvoyDetails() {
127144
return envoy;
128145
}
129146

130-
// K8sDiagnosticService.java
131147
@SuppressWarnings("unchecked")
132148
public List<Object> getIstioResources(String namespace, String type) {
149+
if (!k8sInitialized) {
150+
logger.warn("K8s API nicht verfügbar: {}", k8sInitError);
151+
return Collections.emptyList();
152+
}
153+
133154
try {
134155
String plural = type.toLowerCase().endsWith("s") ? type.toLowerCase() : type.toLowerCase() + "s";
135156

136-
// Logge den Namespace für das Debugging
137157
logger.info("Abfrage Istio API: Group={}, Namespace={}, Plural={}", ISTIO_GROUP, namespace, plural);
138158

139159
Object result = customObjectsApi
@@ -164,8 +184,11 @@ private Map<String, String> parseStats(String rawStats) {
164184
rawStats.lines()
165185
.filter(line -> line.contains(":"))
166186
.forEach(line -> {
167-
String[] parts = line.split(":");
168-
statsMap.put(parts[0].trim(), parts[1].trim());
187+
int colonIndex = line.lastIndexOf(':');
188+
if (colonIndex > 0 && colonIndex < line.length() - 1) {
189+
statsMap.put(line.substring(0, colonIndex).trim(),
190+
line.substring(colonIndex + 1).trim());
191+
}
169192
});
170193
}
171194
return statsMap;

0 commit comments

Comments
 (0)