Skip to content

Commit d09f194

Browse files
csvirimetacosm
andcommitted
improve: remove EventSourceInitializer (#2257)
Signed-off-by: Attila Mészáros <[email protected]> Signed-off-by: Chris Laprun <[email protected]> Co-authored-by: Chris Laprun <[email protected]> Signed-off-by: Attila Mészáros <[email protected]>
1 parent 30841db commit d09f194

File tree

43 files changed

+252
-368
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+252
-368
lines changed

Diff for: bootstrapper-maven-plugin/src/main/resources/templates/Reconciler.java

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
66
import io.javaoperatorsdk.operator.api.config.informer.InformerConfiguration;
77
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
8-
import io.javaoperatorsdk.operator.api.reconciler.EventSourceInitializer;
98
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
109
import io.javaoperatorsdk.operator.api.reconciler.Context;
1110
import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext;

Diff for: caffeine-bounded-cache-support/src/test/java/io/javaoperatorsdk/operator/processing/event/source/cache/sample/AbstractTestReconciler.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import com.github.benmanes.caffeine.cache.Caffeine;
2929

3030
public abstract class AbstractTestReconciler<P extends CustomResource<BoundedCacheTestSpec, BoundedCacheTestStatus>>
31-
implements Reconciler<P>, EventSourceInitializer<P> {
31+
implements Reconciler<P> {
3232

3333
private static final Logger log =
3434
LoggerFactory.getLogger(BoundedCacheClusterScopeTestReconciler.class);
@@ -82,7 +82,7 @@ public Map<String, EventSource> prepareEventSources(
8282
Mappers.fromOwnerReference(this instanceof BoundedCacheClusterScopeTestReconciler))
8383
.build(), context);
8484

85-
return EventSourceInitializer.nameEventSources(es);
85+
return EventSourceUtils.nameEventSources(es);
8686
}
8787

8888
private void ensureStatus(P resource) {

Diff for: docs/documentation/dependent-resources.md

+59-149
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ and labels, which are ignored by default:
101101

102102
```java
103103
public class MyDependentResource extends KubernetesDependentResource<MyDependent, MyPrimary>
104-
implements Matcher<MyDependent, MyPrimary> {
105-
// your implementation
104+
implements Matcher<MyDependent, MyPrimary> {
105+
// your implementation
106106

107-
public Result<MyDependent> match(MyDependent actualResource, MyPrimary primary,
108-
Context<MyPrimary> context) {
109-
return GenericKubernetesResourceMatcher.match(this, actualResource, primary, context, true);
110-
}
107+
public Result<MyDependent> match(MyDependent actualResource, MyPrimary primary,
108+
Context<MyPrimary> context) {
109+
return GenericKubernetesResourceMatcher.match(this, actualResource, primary, context, true);
110+
}
111111
}
112112
```
113113

@@ -141,24 +141,24 @@ Deleted (or set to be garbage collected). The following example shows how to cre
141141
@KubernetesDependent(labelSelector = WebPageManagedDependentsReconciler.SELECTOR)
142142
class DeploymentDependentResource extends CRUDKubernetesDependentResource<Deployment, WebPage> {
143143

144-
public DeploymentDependentResource() {
145-
super(Deployment.class);
146-
}
147-
148-
@Override
149-
protected Deployment desired(WebPage webPage, Context<WebPage> context) {
150-
var deploymentName = deploymentName(webPage);
151-
Deployment deployment = loadYaml(Deployment.class, getClass(), "deployment.yaml");
152-
deployment.getMetadata().setName(deploymentName);
153-
deployment.getMetadata().setNamespace(webPage.getMetadata().getNamespace());
154-
deployment.getSpec().getSelector().getMatchLabels().put("app", deploymentName);
155-
156-
deployment.getSpec().getTemplate().getMetadata().getLabels()
157-
.put("app", deploymentName);
158-
deployment.getSpec().getTemplate().getSpec().getVolumes().get(0)
159-
.setConfigMap(new ConfigMapVolumeSourceBuilder().withName(configMapName(webPage)).build());
160-
return deployment;
161-
}
144+
public DeploymentDependentResource() {
145+
super(Deployment.class);
146+
}
147+
148+
@Override
149+
protected Deployment desired(WebPage webPage, Context<WebPage> context) {
150+
var deploymentName = deploymentName(webPage);
151+
Deployment deployment = loadYaml(Deployment.class, getClass(), "deployment.yaml");
152+
deployment.getMetadata().setName(deploymentName);
153+
deployment.getMetadata().setNamespace(webPage.getMetadata().getNamespace());
154+
deployment.getSpec().getSelector().getMatchLabels().put("app", deploymentName);
155+
156+
deployment.getSpec().getTemplate().getMetadata().getLabels()
157+
.put("app", deploymentName);
158+
deployment.getSpec().getTemplate().getSpec().getVolumes().get(0)
159+
.setConfigMap(new ConfigMapVolumeSourceBuilder().withName(configMapName(webPage)).build());
160+
return deployment;
161+
}
162162
}
163163
```
164164

@@ -194,25 +194,25 @@ instances are managed by JOSDK, an example of which can be seen below:
194194
```java
195195

196196
@ControllerConfiguration(
197-
labelSelector = SELECTOR,
198-
dependents = {
199-
@Dependent(type = ConfigMapDependentResource.class),
200-
@Dependent(type = DeploymentDependentResource.class),
201-
@Dependent(type = ServiceDependentResource.class)
202-
})
197+
labelSelector = SELECTOR,
198+
dependents = {
199+
@Dependent(type = ConfigMapDependentResource.class),
200+
@Dependent(type = DeploymentDependentResource.class),
201+
@Dependent(type = ServiceDependentResource.class)
202+
})
203203
public class WebPageManagedDependentsReconciler
204-
implements Reconciler<WebPage>, ErrorStatusHandler<WebPage> {
204+
implements Reconciler<WebPage>, ErrorStatusHandler<WebPage> {
205205

206-
// omitted code
206+
// omitted code
207207

208-
@Override
209-
public UpdateControl<WebPage> reconcile(WebPage webPage, Context<WebPage> context) {
208+
@Override
209+
public UpdateControl<WebPage> reconcile(WebPage webPage, Context<WebPage> context) {
210210

211-
final var name = context.getSecondaryResource(ConfigMap.class).orElseThrow()
212-
.getMetadata().getName();
213-
webPage.setStatus(createStatus(name));
214-
return UpdateControl.patchStatus(webPage);
215-
}
211+
final var name = context.getSecondaryResource(ConfigMap.class).orElseThrow()
212+
.getMetadata().getName();
213+
webPage.setStatus(createStatus(name));
214+
return UpdateControl.patchStatus(webPage);
215+
}
216216

217217
}
218218
```
@@ -227,104 +227,11 @@ It is also possible to wire dependent resources programmatically. In practice th
227227
developer is responsible for initializing and managing the dependent resources as well as calling
228228
their `reconcile` method. However, this makes it possible for developers to fully customize the
229229
reconciliation process. Standalone dependent resources should be used in cases when the managed use
230-
case does not fit.
231-
232-
Note that [Workflows](https://javaoperatorsdk.io/docs/workflows) also can be invoked from standalone
233-
resources.
234-
235-
The following sample is similar to the one above, simply performing additional checks, and
236-
conditionally creating an `Ingress`:
237-
238-
```java
239-
240-
@ControllerConfiguration
241-
public class WebPageStandaloneDependentsReconciler
242-
implements Reconciler<WebPage>, ErrorStatusHandler<WebPage>,
243-
EventSourceInitializer<WebPage> {
244-
245-
private KubernetesDependentResource<ConfigMap, WebPage> configMapDR;
246-
private KubernetesDependentResource<Deployment, WebPage> deploymentDR;
247-
private KubernetesDependentResource<Service, WebPage> serviceDR;
248-
private KubernetesDependentResource<Service, WebPage> ingressDR;
249-
250-
public WebPageStandaloneDependentsReconciler(KubernetesClient kubernetesClient) {
251-
// 1.
252-
createDependentResources(kubernetesClient);
253-
}
254-
255-
@Override
256-
public List<EventSource> prepareEventSources(EventSourceContext<WebPage> context) {
257-
// 2.
258-
return List.of(
259-
configMapDR.initEventSource(context),
260-
deploymentDR.initEventSource(context),
261-
serviceDR.initEventSource(context));
262-
}
263-
264-
@Override
265-
public UpdateControl<WebPage> reconcile(WebPage webPage, Context<WebPage> context) {
266-
267-
// 3.
268-
if (!isValidHtml(webPage.getHtml())) {
269-
return UpdateControl.patchStatus(setInvalidHtmlErrorMessage(webPage));
270-
}
230+
case does not fit. You can, of course, also use [Workflows](https://javaoperatorsdk.io/docs/workflows) when managing
231+
resources programmatically.
271232

272-
// 4.
273-
configMapDR.reconcile(webPage, context);
274-
deploymentDR.reconcile(webPage, context);
275-
serviceDR.reconcile(webPage, context);
276-
277-
// 5.
278-
if (Boolean.TRUE.equals(webPage.getSpec().getExposed())) {
279-
ingressDR.reconcile(webPage, context);
280-
} else {
281-
ingressDR.delete(webPage, context);
282-
}
283-
284-
// 6.
285-
webPage.setStatus(
286-
createStatus(configMapDR.getResource(webPage).orElseThrow().getMetadata().getName()));
287-
return UpdateControl.patchStatus(webPage);
288-
}
289-
290-
private void createDependentResources(KubernetesClient client) {
291-
this.configMapDR = new ConfigMapDependentResource();
292-
this.deploymentDR = new DeploymentDependentResource();
293-
this.serviceDR = new ServiceDependentResource();
294-
this.ingressDR = new IngressDependentResource();
295-
296-
Arrays.asList(configMapDR, deploymentDR, serviceDR, ingressDR).forEach(dr -> {
297-
dr.setKubernetesClient(client);
298-
dr.configureWith(new KubernetesDependentResourceConfig()
299-
.setLabelSelector(DEPENDENT_RESOURCE_LABEL_SELECTOR));
300-
});
301-
}
302-
303-
// omitted code
304-
}
305-
```
306-
307-
There are multiple things happening here:
308-
309-
1. Dependent resources are explicitly created and can be access later by reference.
310-
2. Event sources are produced by the dependent resources, but needs to be explicitly registered in
311-
this case by implementing
312-
the [`EventSourceInitializer`](https://github.com/java-operator-sdk/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/EventSourceInitializer.java)
313-
interface.
314-
3. The input html is validated, and error message is set in case it is invalid.
315-
4. Reconciliation of dependent resources is called explicitly, but here the workflow
316-
customization is fully in the hand of the developer.
317-
5. An `Ingress` is created but only in case `exposed` flag set to true on custom resource. Tries to
318-
delete it if not.
319-
6. Status is set in a different way, this is just an alternative way to show, that the actual state
320-
can be read using the reference. This could be written in a same way as in the managed example.
321-
322-
See the full source code of
323-
sample [here](https://github.com/operator-framework/java-operator-sdk/blob/main/sample-operators/webpage/src/main/java/io/javaoperatorsdk/operator/sample/WebPageStandaloneDependentsReconciler.java)
324-
.
325-
326-
Note also the Workflows feature makes it possible to also support this conditional creation use
327-
case in managed dependent resources.
233+
You can see a commented example of how to do
234+
so [here](https://github.com/operator-framework/java-operator-sdk/blob/main/sample-operators/webpage/src/main/java/io/javaoperatorsdk/operator/sample/WebPageStandaloneDependentsReconciler.java).
328235

329236
## Creating/Updating Kubernetes Resources
330237

@@ -357,17 +264,17 @@ Since SSA is a complex feature, JOSDK implements a feature flag allowing users t
357264
these implementations. See
358265
in [ConfigurationService](https://github.com/java-operator-sdk/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ConfigurationService.java#L332-L358).
359266

360-
It is, however, important to note that these implementations are default, generic
361-
implementations that the framework can provide expected behavior out of the box. In many
362-
situations, these will work just fine but it is also possible to provide matching algorithms
267+
It is, however, important to note that these implementations are default, generic
268+
implementations that the framework can provide expected behavior out of the box. In many
269+
situations, these will work just fine but it is also possible to provide matching algorithms
363270
optimized for specific use cases. This is easily done by simply overriding
364-
the `match(...)` [method](https://github.com/java-operator-sdk/java-operator-sdk/blob/e16559fd41bbb8bef6ce9d1f47bffa212a941b09/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/KubernetesDependentResource.java#L156-L156).
271+
the `match(...)` [method](https://github.com/java-operator-sdk/java-operator-sdk/blob/e16559fd41bbb8bef6ce9d1f47bffa212a941b09/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/KubernetesDependentResource.java#L156-L156).
365272

366-
It is also possible to bypass the matching logic altogether to simply rely on the server-side
273+
It is also possible to bypass the matching logic altogether to simply rely on the server-side
367274
apply mechanism if always sending potentially unchanged resources to the cluster is not an issue.
368275
JOSDK's matching mechanism allows to spare some potentially useless calls to the Kubernetes API
369-
server. To bypass the matching feature completely, simply override the `match` method to always
370-
return `false`, thus telling JOSDK that the actual state never matches the desired one, making
276+
server. To bypass the matching feature completely, simply override the `match` method to always
277+
return `false`, thus telling JOSDK that the actual state never matches the desired one, making
371278
it always update the resources using SSA.
372279

373280
WARNING: Older versions of Kubernetes before 1.25 would create an additional resource version for every SSA update
@@ -489,15 +396,18 @@ also be created, one per dependent resource.
489396
See [integration test](https://github.com/java-operator-sdk/java-operator-sdk/blob/main/operator-framework/src/test/java/io/javaoperatorsdk/operator/ExternalStateBulkIT.java)
490397
as a sample.
491398

492-
493399
## GenericKubernetesResource based Dependent Resources
494400

495-
In rare circumstances resource handling where there is no class representation or just typeless handling might be needed.
496-
Fabric8 Client provides [GenericKubernetesResource](https://github.com/fabric8io/kubernetes-client/blob/main/doc/CHEATSHEET.md#resource-typeless-api)
497-
to support that.
401+
In rare circumstances resource handling where there is no class representation or just typeless handling might be
402+
needed.
403+
Fabric8 Client
404+
provides [GenericKubernetesResource](https://github.com/fabric8io/kubernetes-client/blob/main/doc/CHEATSHEET.md#resource-typeless-api)
405+
to support that.
498406

499-
For dependent resource this is supported by [GenericKubernetesDependentResource](https://github.com/java-operator-sdk/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/GenericKubernetesDependentResource.java#L8-L8)
500-
. See samples [here](https://github.com/java-operator-sdk/java-operator-sdk/tree/main/operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/generickubernetesresource).
407+
For dependent resource this is supported
408+
by [GenericKubernetesDependentResource](https://github.com/java-operator-sdk/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/GenericKubernetesDependentResource.java#L8-L8)
409+
. See
410+
samples [here](https://github.com/java-operator-sdk/java-operator-sdk/tree/main/operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/generickubernetesresource).
501411

502412
## Other Dependent Resource Features
503413

0 commit comments

Comments
 (0)