Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion docs/content/en/docs/documentation/operations/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<ConfigMap> {

private final String customValue;

public MyConfig(String customValue, InformerConfiguration<ConfigMap> 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<ConfigMap, WebPage> {

@Override
protected ConfigMap desired(WebPage primary, Context<WebPage> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ConfigMap> {

private final String customValue;

CustomConfig(String customValue, InformerConfiguration<ConfigMap> informerConfig) {
super(null, DEFAULT_CREATE_RESOURCE_ONLY_IF_NOT_EXISTING_WITH_SSA, informerConfig);
this.customValue = customValue;
}

String customValue() {
return customValue;
}
}

private static class MyItemStore<T extends HasMetadata> extends BasicItemStore<T> {

public MyItemStore() {
Expand Down