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

feat: add new tail_lines parameter to k8s_log module (#488) #489

Conversation

kurokobo
Copy link
Contributor

SUMMARY

Add new tail_lines parameter to k8s_log module to limit the number of lines to be retrieved from the end of the logs.

Closes #488.

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

kubernetes.core.k8s_log

ADDITIONAL INFORMATION

Thanks for useful collection 😃
This is the first time to send PR to this collection, so please let me know if I'm on the wrong way.

  • The version_added is set to 2.4.0, but I'm not aware of the roadmap for this collection, so I'd like to know this is the right version to specify.
  • Changelog and simple integration test is also added.
  • It seems that the end of log_lines always contains an empty element, so if tail_lines is set to 5, the length of log_lines will be 6, as noted in the comment in the test. I've considered that truncating the trailing empty element, but decided not to for the following reasons.
    • It is inconsistent and unnatural to remove trailing empty elements only when tail_lines is specified.
    • Removing trailing empty elements always with or without tail_lines is a destructive change and should not be done because it would break backward compatibility.

Example tasks in playbook:

  tasks: 
    - name: create a job that has 10 lines of log
      kubernetes.core.k8s:
        state: present
        wait: yes
        wait_timeout: "{{ k8s_wait_timeout | default(omit) }}"
        wait_condition:
          type: Complete
          status: 'True'
        definition:
          apiVersion: batch/v1
          kind: Job
          metadata:
            name: multiline-log
            namespace: test
          spec:
            template:
              spec:
                containers:
                  - name: busybox
                    image: busybox
                    command: ['sh']
                    args: ['-c', 'for i in $(seq 0 9); do echo $i; done']
                restartPolicy: Never
            backoffLimit: 4

    - name: retrieve all logs from the job
      kubernetes.core.k8s_log:
        api_version: batch/v1
        kind: Job
        namespace: test
        name: multiline-log
      register: full_log

    - name: retrieve last 5 lines of log from the job
      kubernetes.core.k8s_log:
        api_version: batch/v1
        kind: Job
        namespace: test
        name: multiline-log
        tail_lines: 5
      register: tailed_log

    - ansible.builtin.debug:
        var: full_log.log_lines

    - ansible.builtin.debug:
        var: tailed_log.log_lines

Example output:

TASK [create a job that has 10 lines of log] *****************************************************************************************
ok: [localhost]

TASK [retrieve all logs from the job] ************************************************************************************************
ok: [localhost]

TASK [retrieve last 5 lines of log from the job] *************************************************************************************
ok: [localhost]

TASK [ansible.builtin.debug] *********************************************************************************************************
ok: [localhost] => 
  full_log.log_lines:
  - '0'
  - '1'
  - '2'
  - '3'
  - '4'
  - '5'
  - '6'
  - '7'
  - '8'
  - '9'
  - ''

TASK [ansible.builtin.debug] *********************************************************************************************************
ok: [localhost] => 
  tailed_log.log_lines:
  - '5'
  - '6'
  - '7'
  - '8'
  - '9'
  - ''

PLAY RECAP ***************************************************************************************************************************
localhost                  : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

@softwarefactory-project-zuul
Copy link

Build succeeded.

✔️ build-ansible-collection SUCCESS in 3m 11s
ansible-test-sanity-docker-devel FAILURE in 10m 04s (non-voting)
ansible-test-sanity-docker-milestone FAILURE in 9m 44s (non-voting)
✔️ ansible-test-sanity-docker-stable-2.9 SUCCESS in 11m 00s
✔️ ansible-test-sanity-docker-stable-2.11 SUCCESS in 9m 47s
✔️ ansible-test-sanity-docker-stable-2.12 SUCCESS in 9m 44s
✔️ ansible-test-units-kubernetes-core-python38 SUCCESS in 6m 35s
✔️ ansible-test-splitter SUCCESS in 2m 27s
✔️ integration-kubernetes.core-devel-1 SUCCESS in 7m 37s (non-voting)
⚠️ integration-kubernetes.core-devel-2 SKIPPED (non-voting)
⚠️ integration-kubernetes.core-devel-3 SKIPPED (non-voting)
✔️ integration-kubernetes.core-milestone-1 SUCCESS in 8m 09s
⚠️ integration-kubernetes.core-milestone-2 SKIPPED
⚠️ integration-kubernetes.core-milestone-3 SKIPPED
✔️ integration-kubernetes.core-2.9-1 SUCCESS in 7m 26s
⚠️ integration-kubernetes.core-2.9-2 SKIPPED
⚠️ integration-kubernetes.core-2.9-3 SKIPPED
✔️ integration-kubernetes.core-2.10-1 SUCCESS in 7m 14s
⚠️ integration-kubernetes.core-2.10-2 SKIPPED
⚠️ integration-kubernetes.core-2.10-3 SKIPPED
✔️ integration-kubernetes.core-with-turbo-1 SUCCESS in 8m 23s
⚠️ integration-kubernetes.core-with-turbo-2 SKIPPED
⚠️ integration-kubernetes.core-with-turbo-3 SKIPPED
✔️ ansible-tox-linters SUCCESS in 4m 43s
✔️ ansible-galaxy-importer SUCCESS in 3m 57s

@kurokobo
Copy link
Contributor Author

The cause of the two FAILUREs both appear to be kubernetes.core.kubectl connection plugin and appear to totally be unrelated to the changes made in my PR 🤔

@abikouo
Copy link
Contributor

abikouo commented Jul 18, 2022

Thanks, @kurokobo for contributing to this collection.
The code and tests look good to me.

Copy link
Member

@gravesm gravesm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. The failures are for known issues in development versions of ansible, so nothing to worry about.

@softwarefactory-project-zuul
Copy link

Build succeeded (gate pipeline).

✔️ build-ansible-collection SUCCESS in 3m 42s
ansible-test-sanity-docker-devel RETRY_LIMIT in 5m 33s (non-voting)
ansible-test-sanity-docker-milestone FAILURE in 8m 39s (non-voting)
✔️ ansible-test-sanity-docker-stable-2.9 SUCCESS in 10m 14s
✔️ ansible-test-sanity-docker-stable-2.11 SUCCESS in 10m 22s
✔️ ansible-test-sanity-docker-stable-2.12 SUCCESS in 9m 40s
✔️ ansible-test-units-kubernetes-core-python38 SUCCESS in 5m 50s
✔️ ansible-test-splitter SUCCESS in 2m 44s
✔️ integration-kubernetes.core-devel-1 SUCCESS in 9m 40s (non-voting)
⚠️ integration-kubernetes.core-devel-2 SKIPPED (non-voting)
⚠️ integration-kubernetes.core-devel-3 SKIPPED (non-voting)
✔️ integration-kubernetes.core-milestone-1 SUCCESS in 9m 39s
⚠️ integration-kubernetes.core-milestone-2 SKIPPED
⚠️ integration-kubernetes.core-milestone-3 SKIPPED
✔️ integration-kubernetes.core-2.9-1 SUCCESS in 8m 36s
⚠️ integration-kubernetes.core-2.9-2 SKIPPED
⚠️ integration-kubernetes.core-2.9-3 SKIPPED
✔️ integration-kubernetes.core-2.10-1 SUCCESS in 9m 21s
⚠️ integration-kubernetes.core-2.10-2 SKIPPED
⚠️ integration-kubernetes.core-2.10-3 SKIPPED
✔️ integration-kubernetes.core-with-turbo-1 SUCCESS in 9m 17s
⚠️ integration-kubernetes.core-with-turbo-2 SKIPPED
⚠️ integration-kubernetes.core-with-turbo-3 SKIPPED
✔️ ansible-tox-linters SUCCESS in 5m 08s
✔️ ansible-galaxy-importer SUCCESS in 3m 56s

@softwarefactory-project-zuul softwarefactory-project-zuul bot merged commit 58cbbf6 into ansible-collections:main Jul 25, 2022
@kurokobo kurokobo deleted the k8s_log/tail_lines branch July 26, 2022 00:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

k8s_log: add parameter to limit the number of log lines to be retrieved
3 participants