|
15 | 15 | */ |
16 | 16 | package io.javaoperatorsdk.operator.sample.metrics; |
17 | 17 |
|
18 | | -import java.util.List; |
19 | | - |
20 | 18 | import org.slf4j.Logger; |
21 | 19 | import org.slf4j.LoggerFactory; |
22 | 20 |
|
23 | 21 | import io.javaoperatorsdk.operator.api.reconciler.Context; |
24 | 22 | import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration; |
25 | | -import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext; |
26 | 23 | import io.javaoperatorsdk.operator.api.reconciler.Reconciler; |
27 | 24 | import io.javaoperatorsdk.operator.api.reconciler.UpdateControl; |
28 | | -import io.javaoperatorsdk.operator.processing.event.source.EventSource; |
29 | | -import io.javaoperatorsdk.operator.sample.metrics.customresource.MetricsHandlingCustomResource1; |
| 25 | +import io.javaoperatorsdk.operator.sample.metrics.customresource.MetricsHandlingCustomResource2; |
| 26 | +import io.javaoperatorsdk.operator.sample.metrics.customresource.MetricsHandlingStatus; |
30 | 27 |
|
31 | 28 | @ControllerConfiguration |
32 | | -public class MetricsHandlingReconciler2 implements Reconciler<MetricsHandlingCustomResource1> { |
33 | | - |
34 | | - public static final String INDEX_HTML = "index.html"; |
| 29 | +public class MetricsHandlingReconciler2 implements Reconciler<MetricsHandlingCustomResource2> { |
35 | 30 |
|
36 | 31 | private static final Logger log = LoggerFactory.getLogger(MetricsHandlingReconciler2.class); |
37 | 32 |
|
38 | 33 | public MetricsHandlingReconciler2() {} |
39 | 34 |
|
40 | 35 | @Override |
41 | | - public List<EventSource<?, MetricsHandlingCustomResource1>> prepareEventSources( |
42 | | - EventSourceContext<MetricsHandlingCustomResource1> context) { |
| 36 | + public UpdateControl<MetricsHandlingCustomResource2> reconcile( |
| 37 | + MetricsHandlingCustomResource2 resource, Context<MetricsHandlingCustomResource2> context) { |
43 | 38 |
|
44 | | - return List.of(); |
45 | | - } |
| 39 | + String name = resource.getMetadata().getName(); |
| 40 | + log.info("Reconciling resource: {}", name); |
46 | 41 |
|
47 | | - @Override |
48 | | - public UpdateControl<MetricsHandlingCustomResource1> reconcile( |
49 | | - MetricsHandlingCustomResource1 metricsHandlingCustomResource1, |
50 | | - Context<MetricsHandlingCustomResource1> context) { |
| 42 | + // Simulate some work (slightly different timing than reconciler1) |
| 43 | + try { |
| 44 | + Thread.sleep(150); |
| 45 | + } catch (InterruptedException e) { |
| 46 | + Thread.currentThread().interrupt(); |
| 47 | + throw new RuntimeException("Interrupted during reconciliation", e); |
| 48 | + } |
| 49 | + |
| 50 | + // Throw exception for resources with names containing "fail" or "error" |
| 51 | + if (name.toLowerCase().contains("fail") || name.toLowerCase().contains("error")) { |
| 52 | + log.error("Simulating failure for resource: {}", name); |
| 53 | + throw new IllegalStateException("Simulated reconciliation failure for resource: " + name); |
| 54 | + } |
| 55 | + |
| 56 | + // Update status |
| 57 | + var status = resource.getStatus(); |
| 58 | + if (status == null) { |
| 59 | + status = new MetricsHandlingStatus(); |
| 60 | + resource.setStatus(status); |
| 61 | + } |
| 62 | + |
| 63 | + var spec = resource.getSpec(); |
| 64 | + if (spec != null) { |
| 65 | + status.setObservedNumber(spec.getObservedNumber()); |
| 66 | + } |
51 | 67 |
|
52 | | - return UpdateControl.noUpdate(); |
| 68 | + log.info("Successfully reconciled resource: {}", name); |
| 69 | + return UpdateControl.patchStatus(resource); |
53 | 70 | } |
54 | 71 | } |
0 commit comments