Skip to content

Commit 0d9ecdf

Browse files
committed
docs: document custom config for KubernetesDependentResource
Signed-off-by: Matheus André <matheusandr2@gmail.com>
1 parent 43d925a commit 0d9ecdf

2 files changed

Lines changed: 99 additions & 1 deletion

File tree

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

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ The Java Operator SDK (JOSDK) provides abstractions that work great out of the b
88
Configuration options operate at several levels:
99
- **Operator-level** using `ConfigurationService`
1010
- **Reconciler-level** using `ControllerConfiguration`
11-
- **DependentResource-level** using the `DependentResourceConfigurator` interface
11+
- **DependentResource-level** using the `ConfiguredDependentResource` interface
1212
- **EventSource-level** where some event sources (like `InformerEventSource`) need fine-tuning to identify which events trigger the associated reconciler
1313

14+
1415
## Operator-Level Configuration
1516

1617
Configuration that impacts the entire operator is performed via the `ConfigurationService` class. `ConfigurationService` is an abstract class with different implementations based on which framework flavor you use (e.g., Quarkus Operator SDK replaces the default implementation). Configurations initialize with sensible defaults but can be changed during initialization.
@@ -223,6 +224,62 @@ For more information on how to use this feature, we recommend looking at how thi
223224
`KubernetesDependentResource` in the core framework, `SchemaDependentResource` in the samples or `CustomAnnotationDep`
224225
in the `BaseConfigurationServiceTest` test class.
225226

227+
### Providing a custom configuration to a `KubernetesDependentResource`
228+
229+
`KubernetesDependentResource` already implements `ConfiguredDependentResource`, parameterized with
230+
`KubernetesDependentResourceConfig`, so a subclass cannot implement the interface a second time with its own
231+
configuration type. If your Kubernetes dependent needs additional, programmatically provided values (for example a
232+
service or a setting that is only known when the operator starts), extend `KubernetesDependentResourceConfig` instead:
233+
234+
```java
235+
public class MyConfig extends KubernetesDependentResourceConfig<ConfigMap> {
236+
237+
private final String customValue;
238+
239+
public MyConfig(String customValue, InformerConfiguration<ConfigMap> informerConfig) {
240+
super(null, DEFAULT_CREATE_RESOURCE_ONLY_IF_NOT_EXISTING_WITH_SSA, informerConfig);
241+
this.customValue = customValue;
242+
}
243+
244+
public String customValue() {
245+
return customValue;
246+
}
247+
}
248+
```
249+
250+
Then replace the configuration of the dependent resource, identified by its name, when registering the reconciler:
251+
252+
```java
253+
Operator operator = new Operator();
254+
operator.register(
255+
new WebPageReconciler(),
256+
overrider ->
257+
overrider.replacingNamedDependentResourceConfig(
258+
"configmap",
259+
new MyConfig("customValue", InformerConfiguration.builder(ConfigMap.class).build())));
260+
```
261+
262+
The framework injects this object into the dependent resource, which can retrieve it using `configuration()` and cast it
263+
to the custom type:
264+
265+
```java
266+
public class ConfigMapDependentResource
267+
extends CRUDKubernetesDependentResource<ConfigMap, WebPage> {
268+
269+
@Override
270+
protected ConfigMap desired(WebPage primary, Context<WebPage> context) {
271+
var customValue =
272+
configuration().map(MyConfig.class::cast).map(MyConfig::customValue).orElseThrow();
273+
// use customValue to create the desired state
274+
}
275+
}
276+
```
277+
278+
Note that the provided object replaces the configuration that would otherwise be created from the
279+
`@KubernetesDependent` annotation, it is not merged with it. Settings such as the informer's namespaces or label
280+
selector, the SSA options or the matcher therefore need to be set on the configuration object you provide. An
281+
`InformerConfiguration` created without explicit namespaces, as above, watches the same namespaces as the controller.
282+
226283
## Loading Configuration from External Sources
227284

228285
JOSDK ships a `ConfigLoader` that bridges any key-value configuration source to the operator and

‎operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/config/ControllerConfigurationOverriderTest.java‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import io.javaoperatorsdk.operator.api.reconciler.Workflow;
3737
import io.javaoperatorsdk.operator.api.reconciler.dependent.Dependent;
3838
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
39+
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResourceFactory;
3940
import io.javaoperatorsdk.operator.api.reconciler.dependent.GarbageCollected;
4041
import io.javaoperatorsdk.operator.api.reconciler.dependent.ReconcileResult;
4142
import io.javaoperatorsdk.operator.api.reconciler.dependent.managed.ConfiguredDependentResource;
@@ -342,6 +343,46 @@ void replaceNamedDependentResourceConfigShouldWork() {
342343
assertInstanceOf(TestCondition.class, dependentSpec.getReadyCondition());
343344
}
344345

346+
@Test
347+
@SuppressWarnings("unchecked")
348+
void replacingWithExtendedKubernetesDependentResourceConfigShouldWork() {
349+
var configuration = createConfiguration(new OneDepReconciler());
350+
final var dependentResourceName = DependentResource.defaultNameFor(ReadOnlyDependent.class);
351+
final var customValue = "customValue";
352+
353+
final var overridden =
354+
ControllerConfigurationOverrider.override(configuration)
355+
.replacingNamedDependentResourceConfig(
356+
dependentResourceName,
357+
new CustomConfig(
358+
customValue, InformerConfiguration.builder(ConfigMap.class).build()))
359+
.build();
360+
361+
var dependent =
362+
(ReadOnlyDependent)
363+
DependentResourceFactory.DEFAULT.createFrom(
364+
overridden.getWorkflowSpec().orElseThrow().getDependentResourceSpecs().get(0),
365+
overridden);
366+
367+
var config = dependent.configuration().map(CustomConfig.class::cast).orElseThrow();
368+
assertEquals(customValue, config.customValue());
369+
assertTrue(config.informerConfig().inheritsNamespacesFromController());
370+
}
371+
372+
private static class CustomConfig extends KubernetesDependentResourceConfig<ConfigMap> {
373+
374+
private final String customValue;
375+
376+
CustomConfig(String customValue, InformerConfiguration<ConfigMap> informerConfig) {
377+
super(null, DEFAULT_CREATE_RESOURCE_ONLY_IF_NOT_EXISTING_WITH_SSA, informerConfig);
378+
this.customValue = customValue;
379+
}
380+
381+
String customValue() {
382+
return customValue;
383+
}
384+
}
385+
345386
private static class MyItemStore<T extends HasMetadata> extends BasicItemStore<T> {
346387

347388
public MyItemStore() {

0 commit comments

Comments
 (0)