Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject config into a KubernetesDependentResource #2087

Open
BramMeerten opened this issue Oct 11, 2023 · 5 comments
Open

Inject config into a KubernetesDependentResource #2087

BramMeerten opened this issue Oct 11, 2023 · 5 comments
Assignees
Labels
kind/documentation Categorizes issue or PR as related to documentation. lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale.

Comments

@BramMeerten
Copy link
Contributor

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?

@csviri
Copy link
Collaborator

csviri commented Oct 11, 2023

HI @BramMeerten , updated the webpage sample on this branch, this seems to be working nicely:

#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 csviri linked a pull request Oct 11, 2023 that will close this issue
@BramMeerten
Copy link
Contributor Author

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

@csviri csviri self-assigned this Oct 11, 2023
@metacosm
Copy link
Collaborator

Re-opening this to document this.

@metacosm metacosm reopened this Oct 11, 2023
@metacosm metacosm self-assigned this Oct 11, 2023
Copy link

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 github-actions bot added the stale label Dec 11, 2023
@openshift-ci openshift-ci bot added lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. and removed stale labels Dec 13, 2023
@metacosm metacosm added kind/documentation Categorizes issue or PR as related to documentation. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Dec 14, 2023
Copy link

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 github-actions bot added the stale label Feb 13, 2024
@openshift-ci openshift-ci bot added lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. and removed stale labels Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/documentation Categorizes issue or PR as related to documentation. lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants