Skip to content

Commit 7d86c29

Browse files
authored
docs: document annotation-based dependent resource configuration (#2809)
* docs: document annotation-based dependent resource configuration Fixes #2791 Signed-off-by: Chris Laprun <claprun@redhat.com> * fix: remove now unneeded interface Signed-off-by: Chris Laprun <claprun@redhat.com> * docs: expand KubernetesDependentResource example Signed-off-by: Chris Laprun <claprun@redhat.com> --------- Signed-off-by: Chris Laprun <claprun@redhat.com>
1 parent 494e063 commit 7d86c29

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

docs/content/en/docs/documentation/configuration.md

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,53 @@ for this feature.
113113

114114
## DependentResource-level configuration
115115

116-
`DependentResource` implementations can implement the `DependentResourceConfigurator` interface
117-
to pass information to the implementation. For example, the SDK
118-
provides specific support for the `KubernetesDependentResource`, which can be configured via the
119-
`@KubernetesDependent` annotation. This annotation is, in turn, converted into a
120-
`KubernetesDependentResourceConfig` instance, which is then passed to the `configureWith` method
121-
implementation.
116+
It is possible to define custom annotations to configure custom `DependentResource` implementations. In order to provide
117+
such a configuration mechanism for your own `DependentResource` implementations, they must be annotated with the
118+
`@Configured` annotation. This annotation defines 3 fields that tie everything together:
119+
120+
- `by`, which specifies which annotation class will be used to configure your dependents,
121+
- `with`, which specifies the class holding the configuration object for your dependents and
122+
- `converter`, which specifies the `ConfigurationConverter` implementation in charge of converting the annotation
123+
specified by the `by` field into objects of the class specified by the `with` field.
124+
125+
`ConfigurationConverter` instances implement a single `configFrom` method, which will receive, as expected, the
126+
annotation instance annotating the dependent resource instance to be configured, but it can also extract information
127+
from the `DependentResourceSpec` instance associated with the `DependentResource` class so that metadata from it can be
128+
used in the configuration, as well as the parent `ControllerConfiguration`, if needed. The role of
129+
`ConfigurationConverter` implementations is to extract the annotation information, augment it with metadata from the
130+
`DependentResourceSpec` and the configuration from the parent controller on which the dependent is defined, to finally
131+
create the configuration object that the `DependentResource` instances will use.
132+
133+
However, one last element is required to finish the configuration process: the target `DependentResource` class must
134+
implement the `ConfiguredDependentResource` interface, parameterized with the annotation class defined by the
135+
`@Configured` annotation `by` field. This interface is called by the framework to inject the configuration at the
136+
appropriate time and retrieve the configuration, if it's available.
137+
138+
For example, `KubernetesDependentResource`, a core implementation that the framework provides, can be configured via the
139+
`@KubernetesDependent` annotation. This set up is configured as follows:
122140

123-
TODO
141+
```java
142+
143+
@Configured(
144+
by = KubernetesDependent.class,
145+
with = KubernetesDependentResourceConfig.class,
146+
converter = KubernetesDependentConverter.class)
147+
public abstract class KubernetesDependentResource<R extends HasMetadata, P extends HasMetadata>
148+
extends AbstractEventSourceHolderDependentResource<R, P, InformerEventSource<R, P>>
149+
implements ConfiguredDependentResource<KubernetesDependentResourceConfig<R>> {
150+
// code omitted
151+
}
152+
```
153+
154+
The `@Configured` annotation specifies that `KubernetesDependentResource` instances can be configured by using the
155+
`@KubernetesDependent` annotation, which gets converted into a `KubernetesDependentResourceConfig` object by a
156+
`KubernetesDependentConverter`. That configuration object is then injected by the framework in the
157+
`KubernetesDependentResource` instance, after it's been created, because the class implements the
158+
`ConfiguredDependentResource` interface, properly parameterized.
159+
160+
For more information on how to use this feature, we recommend looking at how this mechanism is implemented for
161+
`KubernetesDependentResource` in the core framework, `SchemaDependentResource` in the samples or `CustomAnnotationDep`
162+
in the `BaseConfigurationServiceTest` test class.
124163

125164
## EventSource-level configuration
126165

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/dependent/DependentResourceConfigurationProvider.java

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)