Skip to content

Commit

Permalink
Add command 'cpo cluster delete-terminated-pods' (#181)
Browse files Browse the repository at this point in the history
Signed-off-by: Jens Müller <[email protected]>
  • Loading branch information
jensmueller-com authored Mar 13, 2024
1 parent 80c7591 commit 81039ef
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
48 changes: 48 additions & 0 deletions cpo/commands/cluster/delete_terminated_pods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright 2024 IBM Corporation
#
# Licensed 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.

from typing import Optional

import click

import cpo.config.cluster_credentials_manager
import cpo.lib.click.utils
import cpo.utils.network

from cpo.lib.ansible.openshift_playbook_runner import OpenShiftPlaybookRunner
from cpo.lib.openshift.utils.click import openshift_server_options
from cpo.utils.logging import loglevel_command


@loglevel_command(
context_settings=cpo.lib.click.utils.create_default_map_from_dict(
cpo.config.cluster_credentials_manager.cluster_credentials_manager.get_current_credentials()
)
)
@openshift_server_options
@click.pass_context
def delete_terminated_pods(
ctx: click.Context,
server: Optional[str],
username: Optional[str],
password: Optional[str],
token: Optional[str],
insecure_skip_tls_verify: Optional[bool],
use_cluster: Optional[str],
):
"""Delete terminated pods"""

credentials = cpo.lib.click.utils.get_cluster_credentials(ctx, locals().copy())

OpenShiftPlaybookRunner("delete_terminated_pods_playbook.yaml", credentials).run_playbook()
47 changes: 47 additions & 0 deletions cpo/deps/playbooks/delete_terminated_pods_playbook.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2024 IBM Corporation
#
# Licensed 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.
---
- connection: local
hosts: localhost
gather_facts: false
tasks:
- name: "Get failed pods"
kubernetes.core.k8s_info:
field_selectors:
- status.phase=Failed
kind: Pod
kubeconfig: "{{ kube_config }}"
register: k8s_info_result

- name: "Delete failed pods"
kubernetes.core.k8s:
kubeconfig: "{{ kube_config }}"
resource_definition: "{{ k8s_info_result.resources }}"
state: absent
when: k8s_info_result.resources | length != 0

- name: "Get succeeded pods"
kubernetes.core.k8s_info:
field_selectors:
- status.phase=Succeeded
kind: Pod
kubeconfig: "{{ kube_config }}"
register: k8s_info_result

- name: "Delete succeeded pods"
kubernetes.core.k8s:
kubeconfig: "{{ kube_config }}"
resource_definition: "{{ k8s_info_result.resources }}"
state: absent
when: k8s_info_result.resources | length != 0

0 comments on commit 81039ef

Please sign in to comment.