-
Notifications
You must be signed in to change notification settings - Fork 16.3k
Move container-related functions from PodManager to a separate file #56700
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
Merged
jscheffl
merged 3 commits into
apache:main
from
boschglobal:feature/prepare-asyncpodmanager-uses-hook
Oct 21, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/utils/container.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| """Helper functions for inspecting and interacting with containers in a Kubernetes Pod.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from contextlib import suppress | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from kubernetes.client.models.v1_container_status import V1ContainerStatus | ||
| from kubernetes.client.models.v1_pod import V1Pod | ||
|
|
||
|
|
||
| def get_container_status(pod: V1Pod, container_name: str) -> V1ContainerStatus | None: | ||
| """Retrieve container status.""" | ||
| if pod and pod.status: | ||
| container_statuses = [] | ||
| if pod.status.container_statuses: | ||
| container_statuses.extend(pod.status.container_statuses) | ||
| if pod.status.init_container_statuses: | ||
| container_statuses.extend(pod.status.init_container_statuses) | ||
|
|
||
| else: | ||
| container_statuses = None | ||
|
|
||
| if container_statuses: | ||
| # In general the variable container_statuses can store multiple items matching different containers. | ||
| # The following generator expression yields all items that have name equal to the container_name. | ||
| # The function next() here calls the generator to get only the first value. If there's nothing found | ||
| # then None is returned. | ||
| return next((x for x in container_statuses if x.name == container_name), None) | ||
| return None | ||
|
|
||
|
|
||
| def container_is_running(pod: V1Pod, container_name: str) -> bool: | ||
| """ | ||
| Examine V1Pod ``pod`` to determine whether ``container_name`` is running. | ||
|
|
||
| If that container is present and running, returns True. Returns False otherwise. | ||
| """ | ||
| container_status = get_container_status(pod, container_name) | ||
| if not container_status: | ||
| return False | ||
| return container_status.state.running is not None | ||
|
|
||
|
|
||
| def container_is_completed(pod: V1Pod, container_name: str) -> bool: | ||
| """ | ||
| Examine V1Pod ``pod`` to determine whether ``container_name`` is completed. | ||
|
|
||
| If that container is present and completed, returns True. Returns False otherwise. | ||
| """ | ||
| container_status = get_container_status(pod, container_name) | ||
| if not container_status: | ||
| return False | ||
| return container_status.state.terminated is not None | ||
|
|
||
|
|
||
| def container_is_succeeded(pod: V1Pod, container_name: str) -> bool: | ||
| """ | ||
| Examine V1Pod ``pod`` to determine whether ``container_name`` is completed and succeeded. | ||
|
|
||
| If that container is present and completed and succeeded, returns True. Returns False otherwise. | ||
| """ | ||
| container_status = get_container_status(pod, container_name) | ||
| if not container_status or container_status.state.terminated is None: | ||
| return False | ||
| return container_status.state.terminated.exit_code == 0 | ||
|
|
||
|
|
||
| def container_is_wait(pod: V1Pod, container_name: str) -> bool: | ||
| """ | ||
| Examine V1Pod ``pod`` to determine whether ``container_name`` is waiting. | ||
|
|
||
| If that container is present and waiting, returns True. Returns False otherwise. | ||
| """ | ||
| container_status = get_container_status(pod, container_name) | ||
| if not container_status: | ||
| return False | ||
|
|
||
| return container_status.state.waiting is not None | ||
|
|
||
|
|
||
| def container_is_terminated(pod: V1Pod, container_name: str) -> bool: | ||
| """ | ||
| Examine V1Pod ``pod`` to determine whether ``container_name`` is terminated. | ||
|
|
||
| If that container is present and terminated, returns True. Returns False otherwise. | ||
| """ | ||
| container_statuses = pod.status.container_statuses if pod and pod.status else None | ||
| if not container_statuses: | ||
| return False | ||
| container_status = next((x for x in container_statuses if x.name == container_name), None) | ||
| if not container_status: | ||
| return False | ||
| return container_status.state.terminated is not None | ||
|
|
||
|
|
||
| def get_container_termination_message(pod: V1Pod, container_name: str): | ||
| with suppress(AttributeError, TypeError): | ||
| container_statuses = pod.status.container_statuses | ||
| container_status = next((x for x in container_statuses if x.name == container_name), None) | ||
| return container_status.state.terminated.message if container_status else None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this considered part of the public API?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does not look breaking to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The question is if it's part of our public API or not.
I don't know if this can be used in some user custom code (operator, executor)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this question is not the one that should be asked - hence I answered the righ one. I don't think we ever specified what is and what is not public API.
But generally it's pretty much the same - Breaking = Public API change in a breaking way. I have not found it to be used elsewhere in our code except cncf.kubernetes - so I assume the intention was to not be used outside of it -> hence not breaking -> hence no public API.
BTW, Semver is not about whether something is a public API or not (because often it is not clearly specified) - but about whether intention of it was to be used as such.
So actually the proper question (if we want to be picky and ask really good question) to ask is "was that intended to be used outside of the provider and changes it in a breaking way?" , And my answer is "I don't think so".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry was distracted all day.
Technically it was not decleared private but I agree to Jarek that we did not define the "public API" on providers. Had the same question if this is breaking as well and did not find any other reference in "our" codebose.
The Protocol is not documented elsewhere and is not needed if you implement operators/Dags or so. So a "normal user" should not see this being removed. I assume every "Advanced user" that potentially builds around the package and extends the operators and needs this would know how to adjust but would be a very nice use case/extension. As GKE was also not using it I think it is safe to remove.