From 2d12979934629652a189fa4deda43ea318baccdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20Andr=C3=A9?= Date: Thu, 3 Dec 2026 21:24:56 -0300 Subject: [PATCH] docs: document custom config for KubernetesDependentResource MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matheus André --- .../documentation/operations/configuration.md | 58 ++++++++++++++++++- .../ControllerConfigurationOverriderTest.java | 41 +++++++++++++ 2 files changed, 98 insertions(+), 1 deletion(-) diff --git a/docs/content/en/docs/documentation/operations/configuration.md b/docs/content/en/docs/documentation/operations/configuration.md index c41d0a2e13..2c06d67d41 100644 --- a/docs/content/en/docs/documentation/operations/configuration.md +++ b/docs/content/en/docs/documentation/operations/configuration.md @@ -8,7 +8,7 @@ The Java Operator SDK (JOSDK) provides abstractions that work great out of the b Configuration options operate at several levels: - **Operator-level** using `ConfigurationService` - **Reconciler-level** using `ControllerConfiguration` -- **DependentResource-level** using the `DependentResourceConfigurator` interface +- **DependentResource-level** using the `ConfiguredDependentResource` interface - **EventSource-level** where some event sources (like `InformerEventSource`) need fine-tuning to identify which events trigger the associated reconciler ## Operator-Level Configuration @@ -223,6 +223,62 @@ For more information on how to use this feature, we recommend looking at how thi `KubernetesDependentResource` in the core framework, `SchemaDependentResource` in the samples or `CustomAnnotationDep` in the `BaseConfigurationServiceTest` test class. +### Providing a custom configuration to a `KubernetesDependentResource` + +`KubernetesDependentResource` already implements `ConfiguredDependentResource`, parameterized with +`KubernetesDependentResourceConfig`, so a subclass cannot implement the interface a second time with its own +configuration type. If your Kubernetes dependent needs additional, programmatically provided values (for example a +service or a setting that is only known when the operator starts), extend `KubernetesDependentResourceConfig` instead: + +```java +public class MyConfig extends KubernetesDependentResourceConfig { + + private final String customValue; + + public MyConfig(String customValue, InformerConfiguration informerConfig) { + super(null, DEFAULT_CREATE_RESOURCE_ONLY_IF_NOT_EXISTING_WITH_SSA, informerConfig); + this.customValue = customValue; + } + + public String customValue() { + return customValue; + } +} +``` + +Then replace the configuration of the dependent resource, identified by its name, when registering the reconciler: + +```java +Operator operator = new Operator(); +operator.register( + new WebPageReconciler(), + overrider -> + overrider.replacingNamedDependentResourceConfig( + "configmap", + new MyConfig("customValue", InformerConfiguration.builder(ConfigMap.class).build()))); +``` + +The framework injects this object into the dependent resource, which can retrieve it using `configuration()` and cast it +to the custom type: + +```java +public class ConfigMapDependentResource + extends CRUDKubernetesDependentResource { + + @Override + protected ConfigMap desired(WebPage primary, Context context) { + var customValue = + configuration().map(MyConfig.class::cast).map(MyConfig::customValue).orElseThrow(); + // use customValue to create the desired state + } +} +``` + +Note that the provided object replaces the configuration that would otherwise be created from the +`@KubernetesDependent` annotation, it is not merged with it. Settings such as the informer's namespaces or label +selector, the SSA options or the matcher therefore need to be set on the configuration object you provide. An +`InformerConfiguration` created without explicit namespaces, as above, watches the same namespaces as the controller. + ## Loading Configuration from External Sources JOSDK ships a `ConfigLoader` that bridges any key-value configuration source to the operator and diff --git a/operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/config/ControllerConfigurationOverriderTest.java b/operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/config/ControllerConfigurationOverriderTest.java index 86b8de441b..77bfa9509c 100644 --- a/operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/config/ControllerConfigurationOverriderTest.java +++ b/operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/config/ControllerConfigurationOverriderTest.java @@ -36,6 +36,7 @@ import io.javaoperatorsdk.operator.api.reconciler.Workflow; import io.javaoperatorsdk.operator.api.reconciler.dependent.Dependent; import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource; +import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResourceFactory; import io.javaoperatorsdk.operator.api.reconciler.dependent.GarbageCollected; import io.javaoperatorsdk.operator.api.reconciler.dependent.ReconcileResult; import io.javaoperatorsdk.operator.api.reconciler.dependent.managed.ConfiguredDependentResource; @@ -342,6 +343,46 @@ void replaceNamedDependentResourceConfigShouldWork() { assertInstanceOf(TestCondition.class, dependentSpec.getReadyCondition()); } + @Test + @SuppressWarnings("unchecked") + void replacingWithExtendedKubernetesDependentResourceConfigShouldWork() { + var configuration = createConfiguration(new OneDepReconciler()); + final var dependentResourceName = DependentResource.defaultNameFor(ReadOnlyDependent.class); + final var customValue = "customValue"; + + final var overridden = + ControllerConfigurationOverrider.override(configuration) + .replacingNamedDependentResourceConfig( + dependentResourceName, + new CustomConfig( + customValue, InformerConfiguration.builder(ConfigMap.class).build())) + .build(); + + var dependent = + (ReadOnlyDependent) + DependentResourceFactory.DEFAULT.createFrom( + overridden.getWorkflowSpec().orElseThrow().getDependentResourceSpecs().get(0), + overridden); + + var config = dependent.configuration().map(CustomConfig.class::cast).orElseThrow(); + assertEquals(customValue, config.customValue()); + assertTrue(config.informerConfig().inheritsNamespacesFromController()); + } + + private static class CustomConfig extends KubernetesDependentResourceConfig { + + private final String customValue; + + CustomConfig(String customValue, InformerConfiguration informerConfig) { + super(null, DEFAULT_CREATE_RESOURCE_ONLY_IF_NOT_EXISTING_WITH_SSA, informerConfig); + this.customValue = customValue; + } + + String customValue() { + return customValue; + } + } + private static class MyItemStore extends BasicItemStore { public MyItemStore() {