Skip to content

Commit 2d12979

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

2 files changed

Lines changed: 98 additions & 1 deletion

File tree

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

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ 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

1414
## Operator-Level Configuration
@@ -223,6 +223,62 @@ For more information on how to use this feature, we recommend looking at how thi
223223
`KubernetesDependentResource` in the core framework, `SchemaDependentResource` in the samples or `CustomAnnotationDep`
224224
in the `BaseConfigurationServiceTest` test class.
225225

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

228284
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)