Skip to content

Commit faa5811

Browse files
committed
docs: added sample dependent implementation
1 parent c763561 commit faa5811

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

docs/documentation/dependent-resources.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,44 @@ We also provide implementations that make it very easy to cache
9494
resources (`PollingDependentResource`, `PerResourcePollingDependentResource`). All the provided implementations can be
9595
found in the `io/javaoperatorsdk/operator/processing/dependent` package of the `operator-framework-core` module.
9696

97+
### Sample Kubernetes Dependent Resource
98+
99+
A typical use case, when a Kubernetes resource is fully managed - Created, Read, Updated and Deleted (or set to be garbage
100+
collected). The following example shows how to create a `Deployment` dependent resource:
101+
102+
```java
103+
@KubernetesDependent(labelSelector = WebPageManagedDependentsReconciler.SELECTOR)
104+
class DeploymentDependentResource extends CRUKubernetesDependentResource<Deployment, WebPage> {
105+
106+
public DeploymentDependentResource() {
107+
super(Deployment.class);
108+
}
109+
110+
@Override
111+
protected Deployment desired(WebPage webPage, Context<WebPage> context) {
112+
var deploymentName = deploymentName(webPage);
113+
Deployment deployment = loadYaml(Deployment.class, getClass(), "deployment.yaml");
114+
deployment.getMetadata().setName(deploymentName);
115+
deployment.getMetadata().setNamespace(webPage.getMetadata().getNamespace());
116+
deployment.getSpec().getSelector().getMatchLabels().put("app", deploymentName);
117+
118+
deployment.getSpec().getTemplate().getMetadata().getLabels()
119+
.put("app", deploymentName);
120+
deployment.getSpec().getTemplate().getSpec().getVolumes().get(0)
121+
.setConfigMap(new ConfigMapVolumeSourceBuilder().withName(configMapName(webPage)).build());
122+
return deployment;
123+
}
124+
}
125+
```
126+
127+
The only thing needs to be done is to extend the `CRUKubernetesDependentResource` and specify the desired state.
128+
Note that it is `CRU` instead of `CRUD`, since it not explicitly manages the delete operation. That is handled by
129+
the Kubernetes garbage collector through owner references, what is automatically set to the resource.
130+
`CRUKubernetesDependentResource` is only an adaptor class that already implements the`Creator` and `Updater` but
131+
not the `Deleter` interface.
132+
133+
See the full source code [here](https://github.com/java-operator-sdk/java-operator-sdk/blob/main/sample-operators/webpage/src/main/java/io/javaoperatorsdk/operator/sample/DeploymentDependentResource.java).
134+
97135
## Managed Dependent Resources
98136

99137
As mentioned previously, one goal of this implementation is to make it possible to semi-declaratively create and wire
@@ -211,7 +249,7 @@ public class WebPageStandaloneDependentsReconciler
211249

212250
There are multiple things happening here:
213251

214-
1. Dependent resources are explicitly created, and can be access later by reference.
252+
1. Dependent resources are explicitly created and can be access later by reference.
215253
2. Event sources are produced by the dependent resources, but needs to be explicitly registered in this case.
216254
3. Reconciliation is called explicitly, but here the workflow customization is fully in the hand of the developer.
217255
4. Status is set in a different way, this is just an alternative way to show, that the actual state can be read using

sample-operators/webpage/src/main/java/io/javaoperatorsdk/operator/sample/DeploymentDependentResource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import io.fabric8.kubernetes.api.model.ConfigMapVolumeSourceBuilder;
44
import io.fabric8.kubernetes.api.model.apps.Deployment;
55
import io.javaoperatorsdk.operator.api.reconciler.Context;
6-
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.CRUDKubernetesDependentResource;
6+
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.CRUKubernetesDependentResource;
77
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependent;
88

99
import static io.javaoperatorsdk.operator.ReconcilerUtils.loadYaml;
@@ -12,7 +12,7 @@
1212

1313
// this annotation only activates when using managed dependents and is not otherwise needed
1414
@KubernetesDependent(labelSelector = WebPageManagedDependentsReconciler.SELECTOR)
15-
class DeploymentDependentResource extends CRUDKubernetesDependentResource<Deployment, WebPage> {
15+
class DeploymentDependentResource extends CRUKubernetesDependentResource<Deployment, WebPage> {
1616

1717
public DeploymentDependentResource() {
1818
super(Deployment.class);

0 commit comments

Comments
 (0)