Skip to content

Commit

Permalink
fix integration testing
Browse files Browse the repository at this point in the history
Update new feature - support deletion of all resources using delete_all: true
  • Loading branch information
abikouo committed Mar 20, 2023
1 parent 982b66f commit f9e4e70
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 288 deletions.
2 changes: 1 addition & 1 deletion changelogs/fragments/517-k8s-make-name-optional.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
---
minor_changes:
- k8s - make name optional when trying to delete or patch all resources from namespace. (https://github.com/ansible-collections/kubernetes.core/issues/504)
- k8s - add new option delete_all to support deletion of all resources when state is set to absent. (https://github.com/ansible-collections/kubernetes.core/issues/504)
63 changes: 24 additions & 39 deletions plugins/module_utils/k8s/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from ansible_collections.kubernetes.core.plugins.module_utils.selector import (
LabelSelectorFilter,
)
from ansible.module_utils.common.dict_transformations import dict_merge


def validate(client, module, resource):
Expand All @@ -47,65 +46,51 @@ def _prepend_resource_info(resource, msg):
return [_prepend_resource_info(resource, msg) for msg in warnings + errors]


def run_module(module) -> None:
results = []
changed = False
client = get_api_client(module)
svc = K8sService(client, module)
def get_definitions(svc, params):
try:
definitions = create_definitions(module.params)
definitions = create_definitions(params)
except Exception as e:
msg = "Failed to load resource definition: {0}".format(e)
raise CoreException(msg) from e

select_all = module.params.get("select_all")
src = module.params.get("src")
resource_definition = module.params.get("resource_definition")
name = module.params.get("name")
state = module.params.get("state")
delete_all = params.get("delete_all")
src = params.get("src")
resource_definition = params.get("resource_definition")
name = params.get("name")
state = params.get("state")

if (
state == "absent"
delete_all
and state == "absent"
and name is None
and resource_definition is None
and src is None
and select_all
):
# Delete all resources in the namespace for the specified resource type
if module.params.get("kind") is None:
if params.get("kind") is None:
raise CoreException(
"'kind' option is required to sepcify the resource type."
)

resource = svc.find_resource(
module.params.get("kind"), module.params.get("api_version"), fail=True
params.get("kind"), params.get("api_version"), fail=True
)
definitions = svc.retrieve_all(
resource,
module.params.get("namespace"),
module.params.get("label_selectors"),
params.get("namespace"),
params.get("label_selectors"),
)
elif (
state == "patched"
and select_all
and len(definitions) == 1
and definitions[0].get("metadata", {}).get("name") is None
):
kind = definitions[0].get("kind") or module.params.get("kind")
api_version = module.params.get("apiVersion") or definitions[0].get(
"api_version"
)
if kind is None:
raise CoreException(
"'kind' option is required to sepcify the resource type."
)
resource = svc.find_resource(kind, api_version, fail=True)
existing = svc.retrieve_all(
resource,
module.params.get("namespace"),
module.params.get("label_selectors"),
)
definitions = [dict_merge(d, definitions[0]) for d in existing]

return definitions


def run_module(module) -> None:
results = []
changed = False
client = get_api_client(module)
svc = K8sService(client, module)

definitions = get_definitions(svc, module.params)

for definition in definitions:
result = {"changed": False, "result": {}}
Expand Down
31 changes: 9 additions & 22 deletions plugins/modules/k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,16 @@
- When set to True, server-side apply will force the changes against conflicts.
type: bool
default: False
select_all:
delete_all:
description:
- When this option is set to I(true), action will be performed on all resources, in the namespace of the specified resource type.
- When I(select_all=True) and I(state=absent), C(kind) is required.
- When I(select_all=True) and I(state=patched) a valid YAML definition must be provided using option C(src) or C(resource_definition),
resource type must be defined using C(kind) option or into provided YAML definition.
- Ignored when I(state=present).
- Ignored when I(state=absent) and one of C(src), C(name) or C(resource_definition) is provided.
- Ignored when I(state=patched) and C(name) is provided or C(src) or C(resource_definition) is provided with a list containing
more than one YAML definition, or if the resource definition contains a metadata.name field.
- When this option is set to I(true) and I(state=absent),
module will delete on all resources, in the namespace of the specified resource type.
- Ignored when C(state) is not set to I(absent) or when one of (src),
C(name) or C(resource_definition) is provided.
- Parameter C(kind) is required to use this option.
type: bool
default: false
version_added: 2.5.0
aliases:
- all
Expand Down Expand Up @@ -364,18 +362,7 @@
api_version: apps/v1
namespace: testing
kind: Deployment
select_all: true
# Pause all Deployment from specified namespace
- name: Delete all Deployment from specified namespace
kubernetes.core.k8s:
api_version: apps/v1
namespace: testing
kind: Deployment
definition:
spec:
paused: True
select_all: true
delete_all: true
"""

RETURN = r"""
Expand Down Expand Up @@ -483,7 +470,7 @@ def argspec():
argument_spec["server_side_apply"] = dict(
type="dict", default=None, options=server_apply_spec()
)
argument_spec["select_all"] = dict(type="bool", default=False, aliases=["all"])
argument_spec["delete_all"] = dict(type="bool", default=False, aliases=["all"])

return argument_spec

Expand Down
15 changes: 12 additions & 3 deletions plugins/modules/k8s_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,19 @@ def execute_module(svc, params):
{"tailLines": params["tail_lines"]}
)

pod_containers = [None]
if params.get("all_containers"):
pod_containers = list_containers_in_pod(svc, resource, namespace, name)

log = ""
try:
response = resource.log.get(
name=name, namespace=namespace, serialize=False, **kwargs
)
for container in pod_containers:
if container is not None:
kwargs.setdefault("query_params", {}).update({"container": container})
response = resource.log.get(
name=name, namespace=namespace, serialize=False, **kwargs
)
log += response.data.decode("utf8")
except ApiException as exc:
if exc.reason == "Not Found":
raise CoreException("Pod {0}/{1} not found.".format(namespace, name))
Expand Down
10 changes: 5 additions & 5 deletions tests/integration/targets/k8s_delete/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
that:
- _result.resources | length == 0

# test deletion using select_all=true
# test deletion using delete_all=true
- name: Create deployments
k8s:
namespace: "{{ test_namespace }}"
Expand Down Expand Up @@ -152,11 +152,11 @@
register: _deployment
failed_when: _deployment.resources | length == 0

- name: Trying to delete using select_all=true but missing kind option
- name: Trying to delete using delete_all=true but missing kind option
k8s:
api_version: apps/v1
namespace: "{{ test_namespace }}"
select_all: true
delete_all: true
state: absent
register: _delete
ignore_errors: true
Expand All @@ -167,12 +167,12 @@
- _delete is failed
- _delete.msg == "'kind' option is required to sepcify the resource type."

- name: Trying to delete deployments without name and label_selectors and select_all=true
- name: Trying to delete deployments without name and label_selectors and delete_all=true
k8s:
kind: Deployment
api_version: apps/v1
namespace: "{{ test_namespace }}"
select_all: true
delete_all: true
wait: true
state: absent
register: _delete
Expand Down
70 changes: 0 additions & 70 deletions tests/integration/targets/k8s_patched/files/deployments.yaml

This file was deleted.

Loading

0 comments on commit f9e4e70

Please sign in to comment.