[backport/2.2] Migrate k8s_log to new refactored code (#336)

This commit is contained in:
Mike Graves
2022-01-17 06:13:58 -05:00
parent 346e303084
commit e2e3f71ecf

View File

@@ -128,12 +128,22 @@ import copy
from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import ( from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import (
AnsibleModule, AnsibleModule,
) )
from ansible.module_utils.six import PY2
from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( from ansible_collections.kubernetes.core.plugins.module_utils.args_common import (
AUTH_ARG_SPEC, AUTH_ARG_SPEC,
NAME_ARG_SPEC, NAME_ARG_SPEC,
) )
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import (
get_api_client,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import (
AnsibleK8SModule,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import (
CoreException,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.service import (
K8sService,
)
def argspec(): def argspec():
@@ -151,32 +161,30 @@ def argspec():
return args return args
def execute_module(module, k8s_ansible_mixin): def execute_module(svc, params):
name = module.params.get("name") name = params.get("name")
namespace = module.params.get("namespace") namespace = params.get("namespace")
label_selector = ",".join(module.params.get("label_selectors", {})) label_selector = ",".join(params.get("label_selectors", {}))
if name and label_selector: if name and label_selector:
module.fail(msg="Only one of name or label_selectors can be provided") raise CoreException("Only one of name or label_selectors can be provided")
resource = k8s_ansible_mixin.find_resource( resource = svc.find_resource(params["kind"], params["api_version"], fail=True)
module.params["kind"], module.params["api_version"], fail=True v1_pods = svc.find_resource("Pod", "v1", fail=True)
)
v1_pods = k8s_ansible_mixin.find_resource("Pod", "v1", fail=True)
if "log" not in resource.subresources: if "log" not in resource.subresources:
if not name: if not name:
module.fail( raise CoreException(
msg="name must be provided for resources that do not support the log subresource" "name must be provided for resources that do not support the log subresource"
) )
instance = resource.get(name=name, namespace=namespace) instance = resource.get(name=name, namespace=namespace)
label_selector = ",".join(extract_selectors(module, instance)) label_selector = ",".join(extract_selectors(instance))
resource = v1_pods resource = v1_pods
if label_selector: if label_selector:
instances = v1_pods.get(namespace=namespace, label_selector=label_selector) instances = v1_pods.get(namespace=namespace, label_selector=label_selector)
if not instances.items: if not instances.items:
module.fail( raise CoreException(
msg="No pods in namespace {0} matched selector {1}".format( "No pods in namespace {0} matched selector {1}".format(
namespace, label_selector namespace, label_selector
) )
) )
@@ -185,12 +193,12 @@ def execute_module(module, k8s_ansible_mixin):
resource = v1_pods resource = v1_pods
kwargs = {} kwargs = {}
if module.params.get("container"): if params.get("container"):
kwargs["query_params"] = dict(container=module.params["container"]) kwargs["query_params"] = {"container": params["container"]}
if module.params.get("since_seconds"): if params.get("since_seconds"):
kwargs.setdefault("query_params", {}).update( kwargs.setdefault("query_params", {}).update(
{"sinceSeconds": module.params["since_seconds"]} {"sinceSeconds": params["since_seconds"]}
) )
if module.params.get("previous"): if module.params.get("previous"):
@@ -198,20 +206,21 @@ def execute_module(module, k8s_ansible_mixin):
{"previous": module.params["previous"]} {"previous": module.params["previous"]}
) )
log = serialize_log( response = resource.log.get(
resource.log.get(name=name, namespace=namespace, serialize=False, **kwargs) name=name, namespace=namespace, serialize=False, **kwargs
) )
log = response.data.decode("utf8")
module.exit_json(changed=False, log=log, log_lines=log.split("\n")) return {"changed": False, "log": log, "log_lines": log.split("\n")}
def extract_selectors(module, instance): def extract_selectors(instance):
# Parses selectors on an object based on the specifications documented here: # Parses selectors on an object based on the specifications documented here:
# https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors # https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors
selectors = [] selectors = []
if not instance.spec.selector: if not instance.spec.selector:
module.fail( raise CoreException(
msg="{0} {1} does not support the log subresource directly, and no Pod selector was found on the object".format( "{0} {1} does not support the log subresource directly, and no Pod selector was found on the object".format(
"/".join(instance.group, instance.apiVersion), instance.kind "/".join(instance.group, instance.apiVersion), instance.kind
) )
) )
@@ -245,8 +254,8 @@ def extract_selectors(module, instance):
) )
) )
else: else:
module.fail( raise CoreException(
msg="The k8s_log module does not support the {0} matchExpression operator".format( "The k8s_log module does not support the {0} matchExpression operator".format(
operator.lower() operator.lower()
) )
) )
@@ -254,22 +263,18 @@ def extract_selectors(module, instance):
return selectors return selectors
def serialize_log(response):
if PY2:
return response.data
return response.data.decode("utf8")
def main(): def main():
module = AnsibleModule(argument_spec=argspec(), supports_check_mode=True) module = AnsibleK8SModule(
from ansible_collections.kubernetes.core.plugins.module_utils.common import ( module_class=AnsibleModule, argument_spec=argspec(), supports_check_mode=True
K8sAnsibleMixin,
get_api_client,
) )
k8s_ansible_mixin = K8sAnsibleMixin(module) try:
k8s_ansible_mixin.client = get_api_client(module=module) client = get_api_client(module=module)
execute_module(module, k8s_ansible_mixin) svc = K8sService(client, module)
result = execute_module(svc, module.params)
module.exit_json(**result)
except CoreException as e:
module.fail_json(msg=e)
if __name__ == "__main__": if __name__ == "__main__":