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

Simplify KubernetesPodOperator #19572

Merged
merged 25 commits into from
Dec 29, 2021
Merged

Simplify KubernetesPodOperator #19572

merged 25 commits into from
Dec 29, 2021

Conversation

dstandish
Copy link
Contributor

@dstandish dstandish commented Nov 13, 2021

Refactor of KPO with the following goals:

Notes on changes

pod_launcher.py

changes to launcher

start_pod split up into two steps:

  • create_pod asynchronously creates the pod
  • await_pod_start waits for it to leave the Pending phase

monitor_pod split up into two components

  • follow_container_logs: this bit of logic was previously in monitor_pod and invoked only if get_logs is true. it also monitored for completion of the base container in this case. here we pull that if self.get_logs decisionmaking up to the execute method for greater transparency, and we parameterize the follow_container_logs method so that it could be used for any container (and also make it more transparent which container it is following).
  • monitor_pod remains and does two things: wait for pod to reach a phase that is terminal, and harvest xcom if applicable.

PodStatus -> PodPhase

I rename the PodStatus enums to have the same case the actual kubernetes phase values. I do not think that we need to lower them and doing so introduces a need for more status evaluation methods.

I also do away with the way pod phase was always translated to task states. It's more transparent to handle the phases directly. In the old code sometimes you would look at the "task state" -- which the pod phase had been translated to -- in order to infer the pod phase; better to be more direct. In any case these two changes allow us to chop a few methods.

misc

kubernetes_pod.py

simplified execute

The main goal here was simplifying the execute method so it's easier to follow. I tried to better reveal the key orchesration decisions that have to be made (e.g. do we get logs or not; do we get xcom or not; do we find a pod or create one), and provide a clearer separation of the processess that can be separated.

other

I moved the "get client" logic to a cached property.

I did away with mutation of the self.pod attribute, and none of the methods reference the attribute directly -- the object is always passed to them in the method. And I made a clear separation between the pod request object (which is used when a new pod is created) and pod -- the pod we're actually monitoring e.g. if we find a pod after failure and resume..

Similar for self.namespace which was mutated in the run without an obvious reason. The reason was that sometimes operator namespace is none but through templates or whatever you have a namespace and when you go to find a pod you need the namespace.

@dstandish
Copy link
Contributor Author

@jedcunningham one thing i have not given a ton of thought is what should go in PodLauncher vs what should go in the operator. do you think that's something that i need to look into?

@jedcunningham
Copy link
Member

Yeah, probably worth giving that some thought while we are at it.

@dstandish
Copy link
Contributor Author

@jedcunningham one thing i have not given a ton of thought is what should go in PodLauncher vs what should go in the operator. do you think that's something that i need to look into?

Yeah, probably worth giving that some thought while we are at it.

yeah i took another look and gave it some more thought. i think it's ok.

i think PodLauncher is sort of an instance of a kind of "manager" design pattern which just helps to make the operator more about orchestration, more higher level, and can make it easier to write tests sometimes. it's like equivalent to a hook but specific to this operator.

we could shuffle things around but i'm not sure there is much value to be gained there.

let me know if you think otherwise.

@@ -51,10 +54,26 @@ def should_retry_start_pod(exception: Exception) -> bool:
class PodStatus:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason not to go ahead and rename this to PodPhase while we're at it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for PodPhase!

@dstandish
Copy link
Contributor Author

dstandish commented Dec 22, 2021

Just to confirm, is this going to be major release @kaxil?

(and since we're renaming "public" methods i think it has to be, right?)

If so I think we also rename PodLauncher -> PodManager since it does more general pod management, not only launching. (could also do this in folllowup, lmk)

Do we need to also keep and deprecate old names PodStatus and PodLauncher?

@BasPH
Copy link
Contributor

BasPH commented Dec 22, 2021

@dstandish I know at least 1 company that would be impacted by changing start_pod to create_pod.

PodLauncher doesn't cover the behaviour anymore so renaming that makes sense IMO.

@dstandish
Copy link
Contributor Author

@dstandish I know at least 1 company that would be impacted by changing start_pod to create_pod.

PodLauncher doesn't cover the behaviour anymore so renaming that makes sense IMO.

Thanks @BasPH

So major it is.

Do we need to keep the old names (with deprecation)? Or is it fine to simply rename. E.g. pod_launcher.py will move to pod_manager.py

@dstandish
Copy link
Contributor Author

I will save PodLauncher -> PodManager for a folllowup PR

@kaxil
Copy link
Member

kaxil commented Dec 22, 2021

Yeah, major version is fine as this is a big refactor with breaking changes. As long as we have documented it, we are good

@kaxil kaxil merged commit f200bb1 into apache:main Dec 29, 2021
@potiuk
Copy link
Member

potiuk commented Dec 29, 2021

Ah... .That means I will release it today :)

@dstandish
Copy link
Contributor Author

no!

@dstandish
Copy link
Contributor Author

there were fast folllows i intended before release

@dstandish
Copy link
Contributor Author

dstandish commented Dec 29, 2021

chatted with @potiuk he agreed to postpone release of this provider so we can get in those followups, which for the record include the following:

rename PodLauncher -> PodManager
set "delete pods"  to true by default
allow usage of k8s hook
possibly, remove ability for PodManager to build client itself (currently can either receive client or be  given same params as  kpo and fetch its own client)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:providers okay to merge It's ok to merge this PR as it does not require more tests provider:cncf-kubernetes Kubernetes provider related issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants