java-operator-sdk icon indicating copy to clipboard operation
java-operator-sdk copied to clipboard

Inject config into a KubernetesDependentResource

Open BramMeerten opened this issue 2 years ago • 5 comments

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?

BramMeerten avatar Oct 11 '23 11:10 BramMeerten

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"));

csviri avatar Oct 11 '23 11:10 csviri

Thanks! Extending the KubernetesDependentResourceConfig works for me. The only downside is the casting to MyConfig, but that's not that big of a deal.

BramMeerten avatar Oct 11 '23 12:10 BramMeerten

Re-opening this to document this.

metacosm avatar Oct 11 '23 15:10 metacosm

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.

github-actions[bot] avatar Dec 11 '23 01:12 github-actions[bot]

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.

github-actions[bot] avatar Feb 13 '24 01:02 github-actions[bot]