mirror of
https://github.com/ansible-collections/kubernetes.core.git
synced 2026-05-07 13:32:37 +00:00
Enable black formatting test (#259)
Enable black formatting test SUMMARY Signed-off-by: Abhijeet Kasurde akasurde@redhat.com ISSUE TYPE Bugfix Pull Request COMPONENT NAME plugins/action/k8s_info.py plugins/connection/kubectl.py plugins/doc_fragments/helm_common_options.py plugins/doc_fragments/k8s_auth_options.py plugins/doc_fragments/k8s_delete_options.py plugins/doc_fragments/k8s_name_options.py plugins/doc_fragments/k8s_resource_options.py plugins/doc_fragments/k8s_scale_options.py plugins/doc_fragments/k8s_state_options.py plugins/doc_fragments/k8s_wait_options.py plugins/filter/k8s.py plugins/inventory/k8s.py plugins/lookup/k8s.py plugins/lookup/kustomize.py plugins/module_utils/ansiblemodule.py plugins/module_utils/apply.py plugins/module_utils/args_common.py plugins/module_utils/client/discovery.py plugins/module_utils/client/resource.py plugins/module_utils/common.py plugins/module_utils/exceptions.py plugins/module_utils/hashes.py plugins/module_utils/helm.py plugins/module_utils/k8sdynamicclient.py plugins/module_utils/selector.py plugins/modules/helm.py plugins/modules/helm_info.py plugins/modules/helm_plugin.py plugins/modules/helm_plugin_info.py plugins/modules/helm_repository.py plugins/modules/helm_template.py plugins/modules/k8s.py plugins/modules/k8s_cluster_info.py plugins/modules/k8s_cp.py plugins/modules/k8s_drain.py plugins/modules/k8s_exec.py plugins/modules/k8s_info.py plugins/modules/k8s_json_patch.py plugins/modules/k8s_log.py plugins/modules/k8s_rollback.py plugins/modules/k8s_scale.py plugins/modules/k8s_service.py tests/integration/targets/kubernetes/library/test_tempfile.py tests/unit/module_utils/test_apply.py tests/unit/module_utils/test_common.py tests/unit/module_utils/test_discoverer.py tests/unit/module_utils/test_hashes.py tests/unit/module_utils/test_marshal.py tests/unit/module_utils/test_selector.py tox.ini Reviewed-by: None <None> Reviewed-by: Mike Graves <mgraves@redhat.com> Reviewed-by: None <None>
This commit is contained in:
@@ -9,7 +9,7 @@ from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
|
||||
DOCUMENTATION = r'''
|
||||
DOCUMENTATION = r"""
|
||||
module: k8s_log
|
||||
|
||||
short_description: Fetch logs from Kubernetes resources
|
||||
@@ -65,9 +65,9 @@ requirements:
|
||||
- "python >= 3.6"
|
||||
- "kubernetes >= 12.0.0"
|
||||
- "PyYAML >= 3.11"
|
||||
'''
|
||||
"""
|
||||
|
||||
EXAMPLES = r'''
|
||||
EXAMPLES = r"""
|
||||
- name: Get a log from a Pod
|
||||
kubernetes.core.k8s_log:
|
||||
name: example-1
|
||||
@@ -100,9 +100,9 @@ EXAMPLES = r'''
|
||||
namespace: testing
|
||||
name: example
|
||||
register: log
|
||||
'''
|
||||
"""
|
||||
|
||||
RETURN = r'''
|
||||
RETURN = r"""
|
||||
log:
|
||||
type: str
|
||||
description:
|
||||
@@ -113,15 +113,20 @@ log_lines:
|
||||
description:
|
||||
- The log of the object, split on newlines
|
||||
returned: success
|
||||
'''
|
||||
"""
|
||||
|
||||
|
||||
import copy
|
||||
|
||||
from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import AnsibleModule
|
||||
from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import (
|
||||
AnsibleModule,
|
||||
)
|
||||
from ansible.module_utils.six import PY2
|
||||
|
||||
from ansible_collections.kubernetes.core.plugins.module_utils.args_common import (AUTH_ARG_SPEC, NAME_ARG_SPEC)
|
||||
from ansible_collections.kubernetes.core.plugins.module_utils.args_common import (
|
||||
AUTH_ARG_SPEC,
|
||||
NAME_ARG_SPEC,
|
||||
)
|
||||
|
||||
|
||||
def argspec():
|
||||
@@ -129,55 +134,62 @@ def argspec():
|
||||
args.update(NAME_ARG_SPEC)
|
||||
args.update(
|
||||
dict(
|
||||
kind=dict(type='str', default='Pod'),
|
||||
kind=dict(type="str", default="Pod"),
|
||||
container=dict(),
|
||||
since_seconds=dict(),
|
||||
label_selectors=dict(type='list', elements='str', default=[]),
|
||||
label_selectors=dict(type="list", elements="str", default=[]),
|
||||
)
|
||||
)
|
||||
return args
|
||||
|
||||
|
||||
def execute_module(module, k8s_ansible_mixin):
|
||||
name = module.params.get('name')
|
||||
namespace = module.params.get('namespace')
|
||||
label_selector = ','.join(module.params.get('label_selectors', {}))
|
||||
name = module.params.get("name")
|
||||
namespace = module.params.get("namespace")
|
||||
label_selector = ",".join(module.params.get("label_selectors", {}))
|
||||
if name and label_selector:
|
||||
module.fail(msg='Only one of name or label_selectors can be provided')
|
||||
module.fail(msg="Only one of name or label_selectors can be provided")
|
||||
|
||||
resource = k8s_ansible_mixin.find_resource(module.params['kind'], module.params['api_version'], fail=True)
|
||||
v1_pods = k8s_ansible_mixin.find_resource('Pod', 'v1', fail=True)
|
||||
resource = k8s_ansible_mixin.find_resource(
|
||||
module.params["kind"], module.params["api_version"], 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:
|
||||
module.fail(msg='name must be provided for resources that do not support the log subresource')
|
||||
module.fail(
|
||||
msg="name must be provided for resources that do not support the log subresource"
|
||||
)
|
||||
instance = resource.get(name=name, namespace=namespace)
|
||||
label_selector = ','.join(extract_selectors(module, instance))
|
||||
label_selector = ",".join(extract_selectors(module, instance))
|
||||
resource = v1_pods
|
||||
|
||||
if label_selector:
|
||||
instances = v1_pods.get(namespace=namespace, label_selector=label_selector)
|
||||
if not instances.items:
|
||||
module.fail(msg='No pods in namespace {0} matched selector {1}'.format(namespace, label_selector))
|
||||
module.fail(
|
||||
msg="No pods in namespace {0} matched selector {1}".format(
|
||||
namespace, label_selector
|
||||
)
|
||||
)
|
||||
# This matches the behavior of kubectl when logging pods via a selector
|
||||
name = instances.items[0].metadata.name
|
||||
resource = v1_pods
|
||||
|
||||
kwargs = {}
|
||||
if module.params.get('container'):
|
||||
kwargs['query_params'] = dict(container=module.params['container'])
|
||||
if module.params.get("container"):
|
||||
kwargs["query_params"] = dict(container=module.params["container"])
|
||||
|
||||
if module.params.get('since_seconds'):
|
||||
kwargs.setdefault('query_params', {}).update({'sinceSeconds': module.params['since_seconds']})
|
||||
if module.params.get("since_seconds"):
|
||||
kwargs.setdefault("query_params", {}).update(
|
||||
{"sinceSeconds": module.params["since_seconds"]}
|
||||
)
|
||||
|
||||
log = serialize_log(resource.log.get(
|
||||
name=name,
|
||||
namespace=namespace,
|
||||
serialize=False,
|
||||
**kwargs
|
||||
))
|
||||
log = serialize_log(
|
||||
resource.log.get(name=name, namespace=namespace, serialize=False, **kwargs)
|
||||
)
|
||||
|
||||
module.exit_json(changed=False, log=log, log_lines=log.split('\n'))
|
||||
module.exit_json(changed=False, log=log, log_lines=log.split("\n"))
|
||||
|
||||
|
||||
def extract_selectors(module, instance):
|
||||
@@ -185,35 +197,46 @@ def extract_selectors(module, instance):
|
||||
# https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors
|
||||
selectors = []
|
||||
if not instance.spec.selector:
|
||||
module.fail(msg='{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))
|
||||
module.fail(
|
||||
msg="{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
|
||||
)
|
||||
)
|
||||
|
||||
if not (instance.spec.selector.matchLabels or instance.spec.selector.matchExpressions):
|
||||
if not (
|
||||
instance.spec.selector.matchLabels or instance.spec.selector.matchExpressions
|
||||
):
|
||||
# A few resources (like DeploymentConfigs) just use a simple key:value style instead of supporting expressions
|
||||
for k, v in dict(instance.spec.selector).items():
|
||||
selectors.append('{0}={1}'.format(k, v))
|
||||
selectors.append("{0}={1}".format(k, v))
|
||||
return selectors
|
||||
|
||||
if instance.spec.selector.matchLabels:
|
||||
for k, v in dict(instance.spec.selector.matchLabels).items():
|
||||
selectors.append('{0}={1}'.format(k, v))
|
||||
selectors.append("{0}={1}".format(k, v))
|
||||
|
||||
if instance.spec.selector.matchExpressions:
|
||||
for expression in instance.spec.selector.matchExpressions:
|
||||
operator = expression.operator
|
||||
|
||||
if operator == 'Exists':
|
||||
if operator == "Exists":
|
||||
selectors.append(expression.key)
|
||||
elif operator == 'DoesNotExist':
|
||||
selectors.append('!{0}'.format(expression.key))
|
||||
elif operator in ['In', 'NotIn']:
|
||||
selectors.append('{key} {operator} {values}'.format(
|
||||
key=expression.key,
|
||||
operator=operator.lower(),
|
||||
values='({0})'.format(', '.join(expression.values))
|
||||
))
|
||||
elif operator == "DoesNotExist":
|
||||
selectors.append("!{0}".format(expression.key))
|
||||
elif operator in ["In", "NotIn"]:
|
||||
selectors.append(
|
||||
"{key} {operator} {values}".format(
|
||||
key=expression.key,
|
||||
operator=operator.lower(),
|
||||
values="({0})".format(", ".join(expression.values)),
|
||||
)
|
||||
)
|
||||
else:
|
||||
module.fail(msg='The k8s_log module does not support the {0} matchExpression operator'.format(operator.lower()))
|
||||
module.fail(
|
||||
msg="The k8s_log module does not support the {0} matchExpression operator".format(
|
||||
operator.lower()
|
||||
)
|
||||
)
|
||||
|
||||
return selectors
|
||||
|
||||
@@ -221,18 +244,20 @@ def extract_selectors(module, instance):
|
||||
def serialize_log(response):
|
||||
if PY2:
|
||||
return response.data
|
||||
return response.data.decode('utf8')
|
||||
return response.data.decode("utf8")
|
||||
|
||||
|
||||
def main():
|
||||
module = AnsibleModule(argument_spec=argspec(), supports_check_mode=True)
|
||||
from ansible_collections.kubernetes.core.plugins.module_utils.common import (
|
||||
K8sAnsibleMixin, get_api_client)
|
||||
K8sAnsibleMixin,
|
||||
get_api_client,
|
||||
)
|
||||
|
||||
k8s_ansible_mixin = K8sAnsibleMixin(module)
|
||||
k8s_ansible_mixin.client = get_api_client(module=module)
|
||||
execute_module(module, k8s_ansible_mixin)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user