operator-framework / java-operator-sdk

Java SDK for building Kubernetes Operators
https://javaoperatorsdk.io/
Apache License 2.0
797 stars 215 forks source link

Custom Invocation of Managed Workflows #1898

Closed csviri closed 7 months ago

csviri commented 1 year ago

In some use cases it would handy to implement custom code before the managed workflows executes, see for example: https://github.com/java-operator-sdk/java-operator-sdk/issues/1896

Currently the managed workflow always execute before the reconcile(...) method automatically. What we could provide is a method invoke the managed workflow:


 public UpdateControl<MyCustomResource> reconcile(MyCustomResource primary, Context<MyCustomResource> context) {
    simulateErrorIfRequested(webPage);

    // custom code before workflow execution

    context.managedDependentResourceContext().invokeWorkflow();

   // custom code after workflow execution

    return UpdateControl.patchStatus(primary);
  }

Probably would be nicer to do this just in some cases, so NOT require explicit invocation by default.

So such a feature flag could look like:

@ControllerConfiguration(
    workflow = @Workflow( 
          explicitInvocation = true,
          dependents = {
              @Dependent(...),
              @Dependent(...)}
    )
public class MyCustomResourceReconciler { }

see also related issue about naming: https://github.com/java-operator-sdk/java-operator-sdk/issues/1773

metacosm commented 9 months ago

Would it make sense to have pre- and post-reconcile workflows that would get executed before and after calling the main reconciler method, respectively?

csviri commented 9 months ago

Not sure how the user would then update the status, based on the results of the flow after the second workflow. Was thinking on other variants also, like having a list of managed workflows.

But would not complicate it, there are still standalone workflows for complex use cases. And usually I guess users have a set of resources they want to reconcile, with custom invocation we give possibility to check the precondition / pre-processing and set the status after, this is both general and powerful enough.

csviri commented 9 months ago

Typically this would be useful also to set some default values as discussed here: https://discordapp.com/channels/723455000604573736/723455000604573739/1201920896080543824

csviri commented 9 months ago

Note that this is little bit more cumbersome for cleanup, if Cleaner interface is not implemented probably should be called implicitly even the flag is there (when not all dependents are garbage collected), if cleaner is implemented it should be called explicitly when the flag is on.

jcechace commented 8 months ago

What about making the @Workflow annotation a top level and directly appleid to the Reconciler?

IMHO this would improve readability as personally I'm not a fun of overusing nested annotations.

csviri commented 7 months ago

I wonder if this is not misleading.

context.managedDependentResourceContext().invokeWorkflow();

Maybe should be: context.managedWorkflowContext().invokeWorkflow()