|
19 | 19 | import java.net.HttpURLConnection; |
20 | 20 | import java.net.URL; |
21 | 21 | import java.nio.charset.StandardCharsets; |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.nio.file.Path; |
22 | 24 | import java.time.Duration; |
23 | 25 | import java.util.ArrayDeque; |
24 | 26 | import java.util.Deque; |
@@ -61,49 +63,195 @@ class MetricsHandlingE2E { |
61 | 63 | public static final String NAME_LABEL_KEY = "app.kubernetes.io/name"; |
62 | 64 |
|
63 | 65 | private LocalPortForward prometheusPortForward; |
| 66 | + private Process prometheusProcess; |
64 | 67 |
|
65 | 68 | static final KubernetesClient client = new KubernetesClientBuilder().build(); |
66 | 69 |
|
67 | | - MetricsHandlingE2E() throws FileNotFoundException {} |
68 | | - |
69 | | - @RegisterExtension |
70 | | - AbstractOperatorExtension operator = |
71 | | - isLocal() |
72 | | - ? LocallyRunOperatorExtension.builder() |
73 | | - .withReconciler(new MetricsHandlingReconciler1()) |
74 | | - .withReconciler(new MetricsHandlingReconciler2()) |
75 | | - .withConfigurationService( |
76 | | - c -> { |
77 | | - var registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT); |
78 | | - try { |
79 | | - MetricsHandlingSampleOperator.startMetricsServer(registry); |
80 | | - } catch (IOException e) { |
81 | | - throw new UncheckedIOException(e); |
82 | | - } |
83 | | - c.withMetrics(MetricsHandlingSampleOperator.initMetrics(registry)); |
84 | | - }) |
85 | | - .build() |
86 | | - : ClusterDeployedOperatorExtension.builder() |
87 | | - .withOperatorDeployment( |
88 | | - new KubernetesClientBuilder() |
89 | | - .build() |
90 | | - .load(new FileInputStream("k8s/operator.yaml")) |
91 | | - .items()) |
92 | | - .build(); |
| 70 | + MetricsHandlingE2E() {} |
| 71 | + |
| 72 | + static final PrometheusMeterRegistry prometheusRegistry = |
| 73 | + isLocal() ? new PrometheusMeterRegistry(PrometheusConfig.DEFAULT) : null; |
| 74 | + |
| 75 | + @RegisterExtension AbstractOperatorExtension operator = createOperatorExtension(); |
| 76 | + |
| 77 | + private static AbstractOperatorExtension createOperatorExtension() { |
| 78 | + if (!isLocal()) { |
| 79 | + try { |
| 80 | + return ClusterDeployedOperatorExtension.builder() |
| 81 | + .withOperatorDeployment( |
| 82 | + new KubernetesClientBuilder() |
| 83 | + .build() |
| 84 | + .load(new FileInputStream("k8s/operator.yaml")) |
| 85 | + .items()) |
| 86 | + .build(); |
| 87 | + } catch (FileNotFoundException e) { |
| 88 | + throw new UncheckedIOException(e); |
| 89 | + } |
| 90 | + } |
| 91 | + try { |
| 92 | + MetricsHandlingSampleOperator.startMetricsServer(prometheusRegistry); |
| 93 | + } catch (IOException e) { |
| 94 | + throw new UncheckedIOException(e); |
| 95 | + } |
| 96 | + return LocallyRunOperatorExtension.builder() |
| 97 | + .withReconciler(new MetricsHandlingReconciler1()) |
| 98 | + .withReconciler(new MetricsHandlingReconciler2()) |
| 99 | + .withConfigurationService( |
| 100 | + c -> c.withMetrics(MetricsHandlingSampleOperator.initMetrics(prometheusRegistry))) |
| 101 | + .build(); |
| 102 | + } |
93 | 103 |
|
94 | 104 | @BeforeAll |
95 | | - void setupObservability() { |
96 | | - log.info("Setting up observability stack..."); |
97 | | - installObservabilityServices(); |
98 | | - createOperatorServiceMonitor(); |
99 | | - prometheusPortForward = portForward(NAME_LABEL_KEY, "prometheus", PROMETHEUS_PORT); |
| 105 | + void setupObservability() throws Exception { |
| 106 | + if (isLocal()) { |
| 107 | + log.info("Starting local Prometheus..."); |
| 108 | + startLocalPrometheus(); |
| 109 | + } else { |
| 110 | + log.info("Setting up observability stack in cluster..."); |
| 111 | + installObservabilityServices(); |
| 112 | + createOperatorServiceMonitor(); |
| 113 | + prometheusPortForward = portForward(NAME_LABEL_KEY, "prometheus", PROMETHEUS_PORT); |
| 114 | + } |
100 | 115 | } |
101 | 116 |
|
102 | 117 | @AfterAll |
103 | 118 | void cleanup() throws IOException { |
| 119 | + if (prometheusProcess != null) { |
| 120 | + log.info("Stopping local Prometheus..."); |
| 121 | + prometheusProcess.destroyForcibly(); |
| 122 | + } |
104 | 123 | closePortForward(prometheusPortForward); |
105 | 124 | } |
106 | 125 |
|
| 126 | + private void startLocalPrometheus() throws Exception { |
| 127 | + // Get the prometheus binary via the download script |
| 128 | + String promBinary = getPrometheusBinary(); |
| 129 | + |
| 130 | + // Create a temporary prometheus.yml that scrapes the local operator metrics endpoint |
| 131 | + Path promDir = Files.createTempDirectory("prometheus-test"); |
| 132 | + Path promConfig = promDir.resolve("prometheus.yml"); |
| 133 | + Files.writeString( |
| 134 | + promConfig, |
| 135 | + """ |
| 136 | + global: |
| 137 | + scrape_interval: 5s |
| 138 | + evaluation_interval: 5s |
| 139 | + scrape_configs: |
| 140 | + - job_name: 'operator' |
| 141 | + static_configs: |
| 142 | + - targets: ['localhost:%d'] |
| 143 | + """ |
| 144 | + .formatted(MetricsHandlingSampleOperator.METRICS_PORT)); |
| 145 | + log.info("Prometheus config written to {}", promConfig); |
| 146 | + |
| 147 | + Path dataDir = promDir.resolve("data"); |
| 148 | + Files.createDirectories(dataDir); |
| 149 | + |
| 150 | + ProcessBuilder pb = |
| 151 | + new ProcessBuilder( |
| 152 | + promBinary, |
| 153 | + "--config.file=" + promConfig, |
| 154 | + "--storage.tsdb.path=" + dataDir, |
| 155 | + "--web.listen-address=0.0.0.0:" + PROMETHEUS_PORT, |
| 156 | + "--log.level=warn"); |
| 157 | + pb.redirectErrorStream(true); |
| 158 | + prometheusProcess = pb.start(); |
| 159 | + |
| 160 | + // Log Prometheus output in a background daemon thread |
| 161 | + var outputReader = prometheusProcess.getInputStream(); |
| 162 | + Thread logThread = |
| 163 | + new Thread( |
| 164 | + () -> { |
| 165 | + try (var reader = new BufferedReader(new InputStreamReader(outputReader))) { |
| 166 | + String line; |
| 167 | + while ((line = reader.readLine()) != null) { |
| 168 | + log.info("Prometheus: {}", line); |
| 169 | + } |
| 170 | + } catch (IOException e) { |
| 171 | + // process terminated |
| 172 | + } |
| 173 | + }, |
| 174 | + "prometheus-output"); |
| 175 | + logThread.setDaemon(true); |
| 176 | + logThread.start(); |
| 177 | + |
| 178 | + // Wait for Prometheus to be ready |
| 179 | + await() |
| 180 | + .atMost(Duration.ofSeconds(30)) |
| 181 | + .pollInterval(Duration.ofSeconds(1)) |
| 182 | + .untilAsserted( |
| 183 | + () -> { |
| 184 | + var conn = |
| 185 | + (HttpURLConnection) |
| 186 | + new URL("http://localhost:" + PROMETHEUS_PORT + "/-/ready").openConnection(); |
| 187 | + conn.setConnectTimeout(1000); |
| 188 | + conn.setReadTimeout(1000); |
| 189 | + assertThat(conn.getResponseCode()).isEqualTo(200); |
| 190 | + }); |
| 191 | + log.info("Local Prometheus is ready on port {}", PROMETHEUS_PORT); |
| 192 | + } |
| 193 | + |
| 194 | + private String getPrometheusBinary() throws Exception { |
| 195 | + File scriptFile = findObservabilityFile("get-prometheus.sh"); |
| 196 | + log.info("Running get-prometheus.sh to obtain Prometheus binary..."); |
| 197 | + |
| 198 | + ProcessBuilder pb = new ProcessBuilder("/bin/sh", scriptFile.getAbsolutePath()); |
| 199 | + pb.redirectErrorStream(false); |
| 200 | + Process process = pb.start(); |
| 201 | + |
| 202 | + // Read stderr for progress messages |
| 203 | + Thread stderrThread = |
| 204 | + new Thread( |
| 205 | + () -> { |
| 206 | + try (var reader = |
| 207 | + new BufferedReader(new InputStreamReader(process.getErrorStream()))) { |
| 208 | + String line; |
| 209 | + while ((line = reader.readLine()) != null) { |
| 210 | + log.info("get-prometheus: {}", line); |
| 211 | + } |
| 212 | + } catch (IOException e) { |
| 213 | + // done |
| 214 | + } |
| 215 | + }, |
| 216 | + "get-prometheus-stderr"); |
| 217 | + stderrThread.setDaemon(true); |
| 218 | + stderrThread.start(); |
| 219 | + |
| 220 | + // Read stdout — last line is the binary path |
| 221 | + String binaryPath; |
| 222 | + try (var reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) { |
| 223 | + binaryPath = reader.lines().reduce((first, second) -> second).orElse(null); |
| 224 | + } |
| 225 | + |
| 226 | + int exitCode = process.waitFor(); |
| 227 | + if (exitCode != 0 || binaryPath == null || binaryPath.isBlank()) { |
| 228 | + throw new IllegalStateException( |
| 229 | + "get-prometheus.sh failed (exit code " + exitCode + "), output: " + binaryPath); |
| 230 | + } |
| 231 | + |
| 232 | + log.info("Prometheus binary: {}", binaryPath); |
| 233 | + return binaryPath; |
| 234 | + } |
| 235 | + |
| 236 | + private File findObservabilityFile(String fileName) { |
| 237 | + try { |
| 238 | + File projectRoot = new File(".").getCanonicalFile(); |
| 239 | + while (projectRoot != null && !new File(projectRoot, "observability").exists()) { |
| 240 | + projectRoot = projectRoot.getParentFile(); |
| 241 | + } |
| 242 | + if (projectRoot == null) { |
| 243 | + throw new IllegalStateException("Could not find observability directory"); |
| 244 | + } |
| 245 | + File file = new File(projectRoot, "observability/" + fileName); |
| 246 | + if (!file.exists()) { |
| 247 | + throw new IllegalStateException("File not found: " + file.getAbsolutePath()); |
| 248 | + } |
| 249 | + return file; |
| 250 | + } catch (IOException e) { |
| 251 | + throw new UncheckedIOException(e); |
| 252 | + } |
| 253 | + } |
| 254 | + |
107 | 255 | private LocalPortForward portForward(String labelKey, String labelValue, int port) { |
108 | 256 | log.info("Waiting for pod with label {}={} to be ready...", labelKey, labelValue); |
109 | 257 | AtomicReference<Pod> portForwardPod = new AtomicReference<>(); |
@@ -163,7 +311,7 @@ void testPropagatedMetrics() throws Exception { |
163 | 311 | operator.create(createResource(MetricsHandlingCustomResource1.class, "test-fail-1", 1)); |
164 | 312 | operator.create(createResource(MetricsHandlingCustomResource2.class, "test-fail-2", 1)); |
165 | 313 |
|
166 | | - // Continuously trigger reconciliations for ~50 seconds by alternating between |
| 314 | + // Continuously trigger reconciliations for ~60 seconds by alternating between |
167 | 315 | // creating new resources, updating specs of existing ones, and deleting older dynamic ones |
168 | 316 | long deadline = System.currentTimeMillis() + TEST_DURATION.toMillis(); |
169 | 317 | int counter = 0; |
@@ -226,9 +374,7 @@ private void verifyPrometheusMetrics() { |
226 | 374 | assertMetricPresent(prometheusUrl, "reconciliations_success_total", Duration.ofSeconds(30)); |
227 | 375 | assertMetricPresent(prometheusUrl, "reconciliations_failure_total", Duration.ofSeconds(30)); |
228 | 376 | assertMetricPresent( |
229 | | - prometheusUrl, |
230 | | - "reconciliations_execution_duration_milliseconds_count", |
231 | | - Duration.ofSeconds(30)); |
| 377 | + prometheusUrl, "reconciliations_execution_duration_seconds_count", Duration.ofSeconds(30)); |
232 | 378 |
|
233 | 379 | log.info("All metrics verified successfully in Prometheus"); |
234 | 380 | } |
@@ -286,8 +432,8 @@ private String queryPrometheus(String prometheusUrl, String query) throws IOExce |
286 | 432 | } |
287 | 433 |
|
288 | 434 | /** |
289 | | - * Creates a ServiceMonitor so Prometheus scrapes the operator's /metrics endpoint. For local |
290 | | - * runs, Prometheus scrapes localhost:8080; for remote, it scrapes the operator service. |
| 435 | + * Creates a ServiceMonitor so Prometheus scrapes the operator's /metrics endpoint (remote mode |
| 436 | + * only). |
291 | 437 | */ |
292 | 438 | private void createOperatorServiceMonitor() { |
293 | 439 | String namespace = operator.getNamespace(); |
@@ -333,27 +479,11 @@ private void createOperatorServiceMonitor() { |
333 | 479 |
|
334 | 480 | private void installObservabilityServices() { |
335 | 481 | try { |
336 | | - // Find the observability script relative to project root |
337 | | - File projectRoot = new File(".").getCanonicalFile(); |
338 | | - while (projectRoot != null && !new File(projectRoot, "observability").exists()) { |
339 | | - projectRoot = projectRoot.getParentFile(); |
340 | | - } |
341 | | - |
342 | | - if (projectRoot == null) { |
343 | | - throw new IllegalStateException("Could not find observability directory"); |
344 | | - } |
345 | | - |
346 | | - File scriptFile = new File(projectRoot, "observability/install-observability.sh"); |
347 | | - if (!scriptFile.exists()) { |
348 | | - throw new IllegalStateException( |
349 | | - "Observability script not found at: " + scriptFile.getAbsolutePath()); |
350 | | - } |
| 482 | + File scriptFile = findObservabilityFile("install-observability.sh"); |
351 | 483 | log.info("Running observability setup script: {}", scriptFile.getAbsolutePath()); |
352 | 484 |
|
353 | | - // Run the install-observability.sh script |
354 | 485 | ProcessBuilder processBuilder = new ProcessBuilder("/bin/sh", scriptFile.getAbsolutePath()); |
355 | 486 | processBuilder.redirectErrorStream(true); |
356 | | - |
357 | 487 | processBuilder.environment().putAll(System.getenv()); |
358 | 488 | Process process = processBuilder.start(); |
359 | 489 | BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); |
|
0 commit comments