Inject config into a KubernetesDependentResource
I'm trying to inject some dependencies into my KubernetesDependentResource.
For dependent resources I usually do this by implementing DependentResourceConfigurator:
// Dependent resource
public class MyDependentResource
extends PerResourcePollingDependentResource<MyDependent, MyPrimary>
implements DependentResourceConfigurator<MyConfig> {
@Override
public void configureWith(MyConfig config) {
this.dependency1 = config.dependency1();
}
}
// Startup code
Operator operator = new Operator();
operator.register(reconciler, r -> r.replaceNamedDependentResourceConfig(MyDependentResource.NAME, new MyConfig(/*...*/)));
But KubernetesDependentResource already implements DependentResourceConfigurator, so I can't configure my own config.
Currently working around this issue by using a singleton pattern. I inject the dependencies into the singleton and initialise it before the dependent resource reconciler is created. The dependent resource reconciler then fetches its dependencies from the singleton.
Is there a different way to do this?
HI @BramMeerten , updated the webpage sample on this branch, this seems to be working nicely:
https://github.com/operator-framework/java-operator-sdk/pull/2088
Thus, just extending the KubernetesDependentResourceConfig:
public static class MyConfig extends KubernetesDependentResourceConfig<ConfigMap> {
public MyConfig(String customValue) {
this.customValue = customValue;
}
public MyConfig() {}
public MyConfig(Set<String> namespaces, String labelSelector,
boolean configuredNS, ResourceDiscriminator<ConfigMap, ?> resourceDiscriminator,
OnAddFilter<ConfigMap> onAddFilter, OnUpdateFilter<ConfigMap> onUpdateFilter,
OnDeleteFilter<ConfigMap> onDeleteFilter, GenericFilter<ConfigMap> genericFilter,
String customValue) {
super(namespaces, labelSelector, configuredNS, resourceDiscriminator, onAddFilter,
onUpdateFilter, onDeleteFilter, genericFilter);
this.customValue = customValue;
}
private String customValue;
public String getCustomValue() {
return customValue;
}
public void setCustomValue(String customValue) {
this.customValue = customValue;
}
}
Then this is just used when registering the reconciler:
operator.register(new WebPageManagedDependentsReconciler(), o -> {
o.replacingNamedDependentResourceConfig(CONFIG_MAP_DEPENDENT,
new ConfigMapDependentResource.MyConfig("customValue"));
Thanks! Extending the KubernetesDependentResourceConfig works for me.
The only downside is the casting to MyConfig, but that's not that big of a deal.
Re-opening this to document this.
This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 14 days.
This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 14 days.