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

BugFix: CLI 'kubernetes cleanup-pods' should only clean up Airflow-created Pods #15204

Merged
merged 4 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion airflow/cli/cli_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1400,7 +1400,11 @@ class GroupCommand(NamedTuple):
KUBERNETES_COMMANDS = (
ActionCommand(
name='cleanup-pods',
help="Clean up Kubernetes pods in evicted/failed/succeeded states",
help=(
"Clean up Kubernetes pods "
"(created by KubernetesExecutor/KubernetesPodOperator) "
"in evicted/failed/succeeded states"
),
func=lazy_load_command('airflow.cli.commands.kubernetes_command.cleanup_pods'),
args=(ARG_NAMESPACE,),
),
Expand Down
18 changes: 17 additions & 1 deletion airflow/cli/commands/kubernetes_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,23 @@ def cleanup_pods(args):
print('Loading Kubernetes configuration')
kube_client = get_kube_client()
print(f'Listing pods in namespace {namespace}')
list_kwargs = {"namespace": namespace, "limit": 500}
airflow_pod_labels = [
'dag_id',
'task_id',
'execution_date',
'try_number',
'airflow_version',
]
kaxil marked this conversation as resolved.
Show resolved Hide resolved
list_kwargs = {
"namespace": namespace,
"limit": 500,
"label_selector": client.V1LabelSelector(
match_expressions=[
client.V1LabelSelectorRequirement(key=label, operator="Exists")
XD-DENG marked this conversation as resolved.
Show resolved Hide resolved
for label in airflow_pod_labels
]
),
}
while True: # pylint: disable=too-many-nested-blocks
pod_list = kube_client.list_namespaced_pod(**list_kwargs)
XD-DENG marked this conversation as resolved.
Show resolved Hide resolved
for pod in pod_list.items:
Expand Down
40 changes: 32 additions & 8 deletions tests/cli/commands/test_kubernetes_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ def test_generate_dag_yaml(self):


class TestCleanUpPodsCommand(unittest.TestCase):
label_selector = kubernetes.client.V1LabelSelector(
match_expressions=[
kubernetes.client.V1LabelSelectorRequirement(key=label, operator="Exists")
for label in ['dag_id', 'task_id', 'execution_date', 'try_number', 'airflow_version']
]
)

@classmethod
def setUpClass(cls):
cls.parser = cli_parser.get_parser()
Expand All @@ -79,7 +86,9 @@ def test_running_pods_are_not_cleaned(self, load_incluster_config, list_namespac
kubernetes_command.cleanup_pods(
self.parser.parse_args(['kubernetes', 'cleanup-pods', '--namespace', 'awesome-namespace'])
)
list_namespaced_pod.assert_called_once_with(namespace='awesome-namespace', limit=500)
list_namespaced_pod.assert_called_once_with(
namespace='awesome-namespace', limit=500, label_selector=self.label_selector
)
delete_pod.assert_not_called()
load_incluster_config.assert_called_once()

Expand All @@ -98,7 +107,9 @@ def test_cleanup_succeeded_pods(self, load_incluster_config, list_namespaced_pod
kubernetes_command.cleanup_pods(
self.parser.parse_args(['kubernetes', 'cleanup-pods', '--namespace', 'awesome-namespace'])
)
list_namespaced_pod.assert_called_once_with(namespace='awesome-namespace', limit=500)
list_namespaced_pod.assert_called_once_with(
namespace='awesome-namespace', limit=500, label_selector=self.label_selector
)
delete_pod.assert_called_with('dummy', 'awesome-namespace')
load_incluster_config.assert_called_once()

Expand All @@ -120,7 +131,9 @@ def test_no_cleanup_failed_pods_wo_restart_policy_never(
kubernetes_command.cleanup_pods(
self.parser.parse_args(['kubernetes', 'cleanup-pods', '--namespace', 'awesome-namespace'])
)
list_namespaced_pod.assert_called_once_with(namespace='awesome-namespace', limit=500)
list_namespaced_pod.assert_called_once_with(
namespace='awesome-namespace', limit=500, label_selector=self.label_selector
)
delete_pod.assert_not_called()
load_incluster_config.assert_called_once()

Expand All @@ -142,7 +155,9 @@ def test_cleanup_failed_pods_w_restart_policy_never(
kubernetes_command.cleanup_pods(
self.parser.parse_args(['kubernetes', 'cleanup-pods', '--namespace', 'awesome-namespace'])
)
list_namespaced_pod.assert_called_once_with(namespace='awesome-namespace', limit=500)
list_namespaced_pod.assert_called_once_with(
namespace='awesome-namespace', limit=500, label_selector=self.label_selector
)
delete_pod.assert_called_with('dummy3', 'awesome-namespace')
load_incluster_config.assert_called_once()

Expand All @@ -162,7 +177,9 @@ def test_cleanup_evicted_pods(self, load_incluster_config, list_namespaced_pod,
kubernetes_command.cleanup_pods(
self.parser.parse_args(['kubernetes', 'cleanup-pods', '--namespace', 'awesome-namespace'])
)
list_namespaced_pod.assert_called_once_with(namespace='awesome-namespace', limit=500)
list_namespaced_pod.assert_called_once_with(
namespace='awesome-namespace', limit=500, label_selector=self.label_selector
)
delete_pod.assert_called_with('dummy4', 'awesome-namespace')
load_incluster_config.assert_called_once()

Expand All @@ -182,7 +199,9 @@ def test_cleanup_api_exception_continue(self, load_incluster_config, list_namesp
kubernetes_command.cleanup_pods(
self.parser.parse_args(['kubernetes', 'cleanup-pods', '--namespace', 'awesome-namespace'])
)
list_namespaced_pod.assert_called_once_with(namespace='awesome-namespace', limit=500)
list_namespaced_pod.assert_called_once_with(
namespace='awesome-namespace', limit=500, label_selector=self.label_selector
)
load_incluster_config.assert_called_once()

@mock.patch('airflow.cli.commands.kubernetes_command._delete_pod')
Expand All @@ -204,8 +223,13 @@ def test_list_pod_with_continue_token(self, load_incluster_config, list_namespac
self.parser.parse_args(['kubernetes', 'cleanup-pods', '--namespace', 'awesome-namespace'])
)
calls = [
call.first(namespace='awesome-namespace', limit=500),
call.second(namespace='awesome-namespace', limit=500, _continue='dummy-token'),
call.first(namespace='awesome-namespace', limit=500, label_selector=self.label_selector),
call.second(
namespace='awesome-namespace',
limit=500,
label_selector=self.label_selector,
_continue='dummy-token',
),
]
list_namespaced_pod.assert_has_calls(calls)
delete_pod.assert_called_with('dummy', 'awesome-namespace')
Expand Down