|
7 | 7 | import java.util.concurrent.ExecutionException; |
8 | 8 | import java.util.concurrent.TimeUnit; |
9 | 9 | import java.util.concurrent.TimeoutException; |
| 10 | +import java.util.stream.Collectors; |
10 | 11 |
|
11 | 12 | import org.slf4j.Logger; |
12 | 13 | import org.slf4j.LoggerFactory; |
|
16 | 17 | import io.fabric8.kubernetes.client.KubernetesClient; |
17 | 18 | import io.fabric8.kubernetes.client.dsl.ExecListener; |
18 | 19 | import io.fabric8.kubernetes.client.dsl.ExecWatch; |
19 | | -import io.javaoperatorsdk.operator.api.config.Dependent; |
20 | 20 | import io.javaoperatorsdk.operator.api.reconciler.Context; |
21 | 21 | import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration; |
22 | 22 | import io.javaoperatorsdk.operator.api.reconciler.DeleteControl; |
| 23 | +import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext; |
| 24 | +import io.javaoperatorsdk.operator.api.reconciler.EventSourceInitializer; |
23 | 25 | import io.javaoperatorsdk.operator.api.reconciler.Reconciler; |
24 | 26 | import io.javaoperatorsdk.operator.api.reconciler.UpdateControl; |
| 27 | +import io.javaoperatorsdk.operator.processing.event.ResourceID; |
| 28 | +import io.javaoperatorsdk.operator.processing.event.source.EventSource; |
| 29 | +import io.javaoperatorsdk.operator.processing.event.source.informer.InformerConfiguration; |
| 30 | +import io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource; |
25 | 31 |
|
26 | | -@ControllerConfiguration( |
27 | | - eventFilters = CustomFilter.class, |
28 | | - dependents = @Dependent(resourceType = Tomcat.class, type = TomcatDependentResource.class)) |
29 | | -public class WebappReconciler implements Reconciler<Webapp> { |
| 32 | +@ControllerConfiguration(eventFilters = {CustomFilter.class}) |
| 33 | +public class WebappReconciler implements Reconciler<Webapp>, EventSourceInitializer<Webapp> { |
30 | 34 |
|
31 | | - private final KubernetesClient kubernetesClient; |
| 35 | + private static final Logger log = LoggerFactory.getLogger(WebappReconciler.class); |
32 | 36 |
|
33 | | - private final Logger log = LoggerFactory.getLogger(getClass()); |
| 37 | + private final KubernetesClient kubernetesClient; |
34 | 38 |
|
35 | 39 | public WebappReconciler(KubernetesClient kubernetesClient) { |
36 | 40 | this.kubernetesClient = kubernetesClient; |
37 | 41 | } |
38 | 42 |
|
| 43 | + @Override |
| 44 | + public List<EventSource> prepareEventSources(EventSourceContext<Webapp> context) { |
| 45 | + return List.of( |
| 46 | + new InformerEventSource<>( |
| 47 | + new InformerConfiguration<>(context.getConfigurationService(), |
| 48 | + null, |
| 49 | + Tomcat.class, |
| 50 | + // To create an event to a related WebApp resource and trigger the reconciliation |
| 51 | + // we need to find which WebApp this Tomcat custom resource is related to. |
| 52 | + // To find the related customResourceId of the WebApp resource we traverse the cache |
| 53 | + // and identify it based on naming convention. |
| 54 | + (Tomcat t) -> context.getPrimaryCache() |
| 55 | + .list(webApp -> webApp.getSpec().getTomcat().equals(t.getMetadata().getName())) |
| 56 | + .map(ResourceID::fromResource) |
| 57 | + .collect(Collectors.toSet()), |
| 58 | + (Webapp webapp) -> new ResourceID(webapp.getSpec().getTomcat(), |
| 59 | + webapp.getMetadata().getNamespace()), |
| 60 | + true), |
| 61 | + context)); |
| 62 | + } |
| 63 | + |
39 | 64 | /** |
40 | 65 | * This method will be called not only on changes to Webapp objects but also when Tomcat objects |
41 | 66 | * change. |
|
0 commit comments