Skip to content

Commit 50f28d9

Browse files
authored
fix: dependent resource docs improvements (#1108)
1 parent 9feec0a commit 50f28d9

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

docs/documentation/dependent-resources.md

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,14 @@ dependent resources. You can annotate your reconciler with
143143
appropriate steps to wire everything together and call your
144144
`DependentResource` implementations `reconcile` method before your primary resource is reconciled. This makes sense in
145145
most use cases where the logic associated with the primary resource is usually limited to status handling based on the
146-
state of the secondary resources. This behavior and automated handling is referred to as "managed" because
147-
the `DependentResource`
148-
implementations are managed by JOSDK.
146+
state of the secondary resources and the resources are not dependent on each other.
147+
148+
Note that all dependents will be reconciled in order. If an exception happens in one or more reconciliations, the
149+
followup resources will be reconciled.
150+
151+
This behavior and automated handling is referred to as "managed" because the `DependentResource` instances
152+
are managed by JOSDK.
153+
149154
See [related sample](https://github.com/java-operator-sdk/java-operator-sdk/blob/main/sample-operators/webpage/src/main/java/io/javaoperatorsdk/operator/sample/WebPageManagedDependentsReconciler.java):
150155

151156
```java
@@ -181,14 +186,15 @@ sample [here](https://github.com/java-operator-sdk/java-operator-sdk/blob/main/s
181186

182187
## Standalone Dependent Resources
183188

184-
To use dependent resources in more complex workflows, when the reconciliation requires additional logic, the standalone
185-
mode is available. In practice this means that the developer is responsible to initializing and managing and
186-
calling `reconcile` method. However, this gives possibility for developers to fully customize the workflow for
187-
reconciliation. Like setting conditions (if creation of a resource is desired only in certain situations). Also, for
188-
example if calling an API needs to happen if a service is already up and running
189+
To use dependent resources in more complex workflows, when there are some resources needs to be created only in certain
190+
conditions the standalone mode is available or the dependent resources are not independent of each other.
191+
For example if calling an API needs to happen if a service is already up and running
189192
(think configuring a running DB instance).
193+
In practice this means that the developer is responsible to initializing and managing and
194+
calling `reconcile` method. However, this gives possibility for developers to fully customize the workflow for
195+
reconciliation. Use standalone dependent resources for cases when managed does not fit.
190196

191-
The following sample is equivalent to the one above with managed dependent resources:
197+
The sample is similar to one above it just performs additional checks, and conditionally creates an `Ingress`:
192198

193199
```java
194200

@@ -199,7 +205,8 @@ public class WebPageStandaloneDependentsReconciler
199205
private KubernetesDependentResource<ConfigMap, WebPage> configMapDR;
200206
private KubernetesDependentResource<Deployment, WebPage> deploymentDR;
201207
private KubernetesDependentResource<Service, WebPage> serviceDR;
202-
208+
private KubernetesDependentResource<Service, WebPage> ingressDR;
209+
203210
public WebPageStandaloneDependentsReconciler(KubernetesClient kubernetesClient) {
204211
// 1.
205212
createDependentResources(kubernetesClient);
@@ -217,13 +224,23 @@ public class WebPageStandaloneDependentsReconciler
217224
@Override
218225
public UpdateControl<WebPage> reconcile(WebPage webPage, Context<WebPage> context)
219226
throws Exception {
220-
221-
// 3.
227+
228+
// 3.
229+
if (!isValidHtml(webPage.getHtml())) {
230+
return UpdateControl.updateStatus(setInvalidHtmlErrorMessage(webPage));
231+
}
232+
233+
// 4.
222234
configMapDR.reconcile(webPage, context);
223235
deploymentDR.reconcile(webPage, context);
224236
serviceDR.reconcile(webPage, context);
225-
226-
// 4.
237+
238+
// 5.
239+
if (webPage.isExposed()) {
240+
ingressDR.reconcile();
241+
}
242+
243+
// 6.
227244
webPage.setStatus(
228245
createStatus(configMapDR.getResource(webPage).orElseThrow().getMetadata().getName()));
229246
return UpdateControl.updateStatus(webPage);
@@ -233,8 +250,9 @@ public class WebPageStandaloneDependentsReconciler
233250
this.configMapDR = new ConfigMapDependentResource();
234251
this.deploymentDR = new DeploymentDependentResource();
235252
this.serviceDR = new ServiceDependentResource();
253+
this.ingressDR = new IngressDependentResource();
236254

237-
Arrays.asList(configMapDR, deploymentDR, serviceDR).forEach(dr -> {
255+
Arrays.asList(configMapDR, deploymentDR, serviceDR, ingressDR).forEach(dr -> {
238256
dr.setKubernetesClient(client);
239257
dr.configureWith(new KubernetesDependentResourceConfig()
240258
.setLabelSelector(DEPENDENT_RESOURCE_LABEL_SELECTOR));
@@ -247,10 +265,12 @@ public class WebPageStandaloneDependentsReconciler
247265

248266
There are multiple things happening here:
249267

250-
1. Dependent resources are explicitly created and can be access later by reference.
268+
1. Dependent resources are explicitly created and can be access later by reference.
251269
2. Event sources are produced by the dependent resources, but needs to be explicitly registered in this case.
252-
3. Reconciliation is called explicitly, but here the workflow customization is fully in the hand of the developer.
253-
4. Status is set in a different way, this is just an alternative way to show, that the actual state can be read using
270+
3. The input html is validated, and error message is set in case it is invalid.
271+
4. Reconciliation is called explicitly, but here the workflow customization is fully in the hand of the developer.
272+
5. An `Ingress` is created but only in case `exposed` flag set to true on custom resource.
273+
6. Status is set in a different way, this is just an alternative way to show, that the actual state can be read using
254274
the reference. This could be written in a same way as in the managed example.
255275

256276
See the full source code of

0 commit comments

Comments
 (0)