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

Add add_resource_on_condition method to only add nodes when certain conditions are satisfied #69

Open
pit1sIBM opened this issue May 2, 2024 · 2 comments

Comments

@pit1sIBM
Copy link
Contributor

pit1sIBM commented May 2, 2024

Is your feature request related to a problem? Please describe.

Not a major problem, right now I'm implementing a feature to ignore certain nodes that are annotated in a certain way. This looks roughly like

ignore_resource = False
obj = fetch_k8s_resource(session)
if obj is not None:
    if ANNOTATION_IGNORE in obj["metadata"]["annotations"]:
       log.warn("Annotation set, ignoring resource")
       ignore_resource = True

if not ignore_resource:
    self.add_resource("job", k8s.Job(...))


# repeat for each resource

Describe the solution you'd like

I'd love to be more clear on whether a resource has some condition that needs to be satisfied first. In my head this looks like

def add_resource(self, name: str, obj: Any):
    ...

def add_resource_on_condition(self, name: str, obj: Any, cond: bool):
    if cond:
        self.add_resource(name, obj)

which turns the above example into

obj = fetch_k8s_resource(session)
self.add_resource_on_condition(
    "job", 
    k8s.Job(...), 
    obj is not None and ANNOTATION_IGNORE in obj["metadata"]["annotations"]
)

# repeat for each resource

Describe alternatives you've considered

Could also enhance existing add_resource with an additional param, I suggested a separate method to be more clear

Additional context

Add any other context about the feature request here.

@gabe-l-hart
Copy link
Member

This is a really interesting idea! In general, for this kind of thing, I've tended to do two things:

  1. Prototype it in my own operator's base component class (you could probably do this now)
  2. Add it to oper8.x.oper8x_component

In general, I've tried to avoid adding too many optional bells-and-whistles to the base Component, but since this would just wrap self.add_resource, it would be a natural fit for the "Component plus common patterns" base in oper8.x. I don't think you guys are using oper8.x though, are you? It also gets you other nice things like the deps_annotation to auto-rollover on mounted data changes.

@pit1sIBM
Copy link
Contributor Author

pit1sIBM commented May 2, 2024

Understood, that makes sense to me. I was in the middle of 1 prior to opening the issue, completely missed oper8.x. I'll dig more into that now as the deps_annotation was on my todo list as well. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants