|
| 1 | +package io.javaoperatorsdk.operator.sample.complexdependent; |
| 2 | + |
| 3 | +import java.util.Map; |
| 4 | +import java.util.Objects; |
| 5 | + |
| 6 | +import io.fabric8.kubernetes.api.model.Service; |
| 7 | +import io.fabric8.kubernetes.api.model.apps.StatefulSet; |
| 8 | +import io.javaoperatorsdk.operator.api.config.informer.InformerConfiguration; |
| 9 | +import io.javaoperatorsdk.operator.api.reconciler.*; |
| 10 | +import io.javaoperatorsdk.operator.api.reconciler.dependent.Dependent; |
| 11 | +import io.javaoperatorsdk.operator.processing.event.source.EventSource; |
| 12 | +import io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource; |
| 13 | +import io.javaoperatorsdk.operator.sample.complexdependent.dependent.*; |
| 14 | + |
| 15 | +import static io.javaoperatorsdk.operator.sample.complexdependent.ComplexDependentReconciler.SERVICE_EVENT_SOURCE_NAME; |
| 16 | +import static io.javaoperatorsdk.operator.sample.complexdependent.ComplexDependentReconciler.STATEFUL_SET_EVENT_SOURCE_NAME; |
| 17 | + |
| 18 | +@ControllerConfiguration( |
| 19 | + name = "project-operator", |
| 20 | + dependents = { |
| 21 | + @Dependent(name = "first-svc", type = FirstService.class, |
| 22 | + useEventSourceWithName = SERVICE_EVENT_SOURCE_NAME), |
| 23 | + @Dependent(name = "second-svc", type = SecondService.class, |
| 24 | + useEventSourceWithName = SERVICE_EVENT_SOURCE_NAME), |
| 25 | + @Dependent(name = "first", type = FirstStatefulSet.class, |
| 26 | + useEventSourceWithName = STATEFUL_SET_EVENT_SOURCE_NAME, |
| 27 | + dependsOn = {"first-svc"}, |
| 28 | + readyPostcondition = StatefulSetReadyCondition.class), |
| 29 | + @Dependent(name = "second", |
| 30 | + type = SecondStatefulSet.class, |
| 31 | + useEventSourceWithName = STATEFUL_SET_EVENT_SOURCE_NAME, |
| 32 | + dependsOn = {"second-svc", "first"}, |
| 33 | + readyPostcondition = StatefulSetReadyCondition.class), |
| 34 | + }) |
| 35 | +public class ComplexDependentReconciler implements Reconciler<ComplexDependentCustomResource>, |
| 36 | + EventSourceInitializer<ComplexDependentCustomResource> { |
| 37 | + |
| 38 | + public static final String SERVICE_EVENT_SOURCE_NAME = "serviceEventSource"; |
| 39 | + public static final String STATEFUL_SET_EVENT_SOURCE_NAME = "statefulSetEventSource"; |
| 40 | + |
| 41 | + @Override |
| 42 | + public UpdateControl<ComplexDependentCustomResource> reconcile( |
| 43 | + ComplexDependentCustomResource resource, |
| 44 | + Context<ComplexDependentCustomResource> context) throws Exception { |
| 45 | + var ready = context.managedDependentResourceContext().getWorkflowReconcileResult() |
| 46 | + .orElseThrow().allDependentResourcesReady(); |
| 47 | + |
| 48 | + var status = Objects.requireNonNullElseGet(resource.getStatus(), ComplexDependentStatus::new); |
| 49 | + status.setStatus(ready ? RECONCILE_STATUS.READY : RECONCILE_STATUS.NOT_READY); |
| 50 | + resource.setStatus(status); |
| 51 | + |
| 52 | + return UpdateControl.updateStatus(resource); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public Map<String, EventSource> prepareEventSources( |
| 57 | + EventSourceContext<ComplexDependentCustomResource> context) { |
| 58 | + InformerEventSource<Service, ComplexDependentCustomResource> serviceEventSource = |
| 59 | + new InformerEventSource<>(InformerConfiguration.from(Service.class, context).build(), |
| 60 | + context); |
| 61 | + InformerEventSource<StatefulSet, ComplexDependentCustomResource> statefulSetEventSource = |
| 62 | + new InformerEventSource<>(InformerConfiguration.from(StatefulSet.class, context).build(), |
| 63 | + context); |
| 64 | + return Map.of(SERVICE_EVENT_SOURCE_NAME, serviceEventSource, STATEFUL_SET_EVENT_SOURCE_NAME, |
| 65 | + statefulSetEventSource); |
| 66 | + } |
| 67 | + |
| 68 | + public enum RECONCILE_STATUS { |
| 69 | + READY, NOT_READY |
| 70 | + } |
| 71 | +} |
0 commit comments