Skip to content

Conversation

yorick1989
Copy link

@yorick1989 yorick1989 commented Jul 30, 2025

SUMMARY

Was going trough the list with issues and found 958; which seemed a quick fix.

What I fixed with with this PR:

  • Added support for copying files to init containers.
  • Fixed the format message when an exec is failing for a pod (the order was wrong).
  • Added a check if the container that you try to run copy for is started.
ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

copy.py module

ADDITIONAL INFORMATION

Some testing.

Verify that the pod does not exist:

kubectl -n default get pod/yorick

Output:

Error from server (NotFound): pods "yorick" not found

Run the playbook to create the file, create the deployment, wait for the init container to be ready, copy the created file to the init container, cat the copied file (using kubernetes.core.k8s_exec) that is now in the init container and try to copy the created file to the (not started) container (which fails - to see the new error message for it):

cat << EOF | ansible-playbook /dev/stdin
- hosts: localhost
  gather_facts: False
  tasks:

  - ansible.builtin.copy:
      content: |
        Hi there
      dest: /tmp/yorick.txt

  - name: Deploy pod with initContainer with an unlimited while loop
    kubernetes.core.k8s:
      kubeconfig: "~/.kube/config"
      definition:
        apiVersion: v1
        kind: Pod
        metadata:
          name: "yorick"
          namespace: "default"
        spec:
          initContainers:
            - name: "yorick-init"
              image: busybox:latest
              command: ["/bin/sh"]
              args:
                - "-c"
                - |
                  echo "Init container started, waiting for file..."
                  # Wait for the file to be copied
                  while :;do
                    echo "Waiting for file"
                    sleep 5
                  done
                  echo "File received! Init container completing..."
          containers:
            - name: "yorick-container"
              image: busybox:latest
              command: ["/bin/sh"]
              args:
                - "-c"
                - |
                  # Keep container running for testing
                  sleep 300

  - kubernetes.core.k8s_info:
      kubeconfig: "~/.kube/config"
      api_version: v1
      kind: Pod
      name: "yorick"
      namespace: "default"
    register: pod_status
    until: >-
      pod_status.resources|length > 0
      and 'initContainerStatuses' in pod_status.resources.0.status
      and pod_status.resources.0.status.initContainerStatuses|length > 0
      and pod_status.resources.0.status.initContainerStatuses.0.started|bool

  - name: Copy /tmp/yorick.txt to the yorick-init init container
    kubernetes.core.k8s_cp:
      kubeconfig: "~/.kube/config"
      namespace: default
      pod: yorick
      remote_path: /tmp/yorick.txt
      local_path: /tmp/yorick.txt
      container: yorick-init

  - name: Execute a command
    kubernetes.core.k8s_exec:
      kubeconfig: "~/.kube/config"
      namespace: default
      pod: yorick
      container: yorick-init
      command: cat /tmp/yorick.txt
    register: exec_out

  - ansible.builtin.debug:
      var: exec_out.stdout

  - name: Try to copy /tmp/yorick.txt to the yorick-container container
    kubernetes.core.k8s_cp:
      kubeconfig: "~/.kube/config"
      namespace: default
      pod: yorick
      remote_path: /tmp/yorick.txt
      local_path: /tmp/yorick.txt
      container: yorick-container
EOF

Output:

PLAY [localhost] ********************************************************************************************************************************************************************

TASK [ansible.builtin.copy] *********************************************************************************************************************************************************
Thursday 31 July 2025  02:01:21 +0200 (0:00:00.016)       0:00:00.016 *********
ok: [localhost]

TASK [Deploy pod with initContainer with an unlimited while loop] *******************************************************************************************************************
Thursday 31 July 2025  02:01:21 +0200 (0:00:00.788)       0:00:00.804 *********
changed: [localhost]

TASK [kubernetes.core.k8s_info] *****************************************************************************************************************************************************
Thursday 31 July 2025  02:01:25 +0200 (0:00:03.963)       0:00:04.768 *********
FAILED - RETRYING: [localhost]: kubernetes.core.k8s_info (3 retries left).
ok: [localhost]

TASK [Copy /tmp/yorick.txt to the yorick-init init container] ***********************************************************************************************************************
Thursday 31 July 2025  02:01:32 +0200 (0:00:06.598)       0:00:11.366 *********
changed: [localhost]

TASK [Execute a command] ************************************************************************************************************************************************************
Thursday 31 July 2025  02:01:39 +0200 (0:00:07.017)       0:00:18.383 *********
changed: [localhost]

TASK [ansible.builtin.debug] ********************************************************************************************************************************************************
Thursday 31 July 2025  02:01:40 +0200 (0:00:00.644)       0:00:19.028 *********
ok: [localhost] => {
    "exec_out.stdout": "Hi there\n"
}

TASK [Try to copy /tmp/yorick.txt to the yorick-container container] ****************************************************************************************************************
Thursday 31 July 2025  02:01:40 +0200 (0:00:00.021)       0:00:19.050 *********
fatal: [localhost]: FAILED! => {
    "changed": false
}

MSG:

Pod container yorick-container is not started

PLAY RECAP **************************************************************************************************************************************************************************
localhost                  : ok=6    changed=3    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Playbook run took 0 days, 0 hours, 0 minutes, 21 seconds

Copy link

@yorick1989 yorick1989 marked this pull request as ready for review July 30, 2025 23:18
@yorick1989 yorick1989 force-pushed the bugfix/cp_initcontainers branch from 8b04927 to b240b03 Compare July 30, 2025 23:35
Copy link

@yorick1989 yorick1989 force-pushed the bugfix/cp_initcontainers branch from b240b03 to 1af0ee6 Compare July 31, 2025 00:03
Copy link

@yurnov
Copy link
Contributor

yurnov commented Jul 31, 2025

Hi @yorick1989,

Thanks for your PR, could you please add changelog fragment, you can find documentation here

And it will be nice, if you would update the integration test to ensure that support of init containers is not broken

@yorick1989 yorick1989 force-pushed the bugfix/cp_initcontainers branch 2 times, most recently from 4795133 to 851aebd Compare July 31, 2025 12:00
Copy link

@yorick1989
Copy link
Author

yorick1989 commented Jul 31, 2025

Hi @yurnov ,

I've updated the PR. Hopefully, this is as expected.

By the way, I had to make the following changes to get the K8S_AUTH_KUBECONFIG environment variable to work when testing using a virtual Python environment:

$ cat playbook.yaml
---
- connection: local
  gather_facts: false
  hosts: localhost
  environment:
    K8S_AUTH_KUBECONFIG: "{{ lookup('ansible.builtin.env', 'K8S_AUTH_KUBECONFIG', default='~/.kube/config') }}"
  roles:
  - k8s_copy

Do you have any idea why I had to do this? And is there a better way to get it to work in a virtual Python environment?

Thanks!

@yorick1989 yorick1989 force-pushed the bugfix/cp_initcontainers branch from 851aebd to 7db5b96 Compare July 31, 2025 15:36
@yorick1989
Copy link
Author

I saw the error during the sanity test. I've patched the errors that came up during this sanity test.

Copy link

@spatterIight
Copy link

Just wanted to say that I ran this on my cluster and it worked for me! Thanks

TASK [Display k8s_cp result (expected to fail)] *****************************************************************************************
ok: [node-01] => 
    msg: |-
        k8s_cp Result: {'changed': True, 'result': '/tmp/test-file.txt successfully copied into remote Pod into /tmp/test-file-remote.txt', 'failed': False}

@yorick1989 yorick1989 force-pushed the bugfix/cp_initcontainers branch from 7db5b96 to 310f1fc Compare August 4, 2025 23:09
Copy link

@yorick1989 yorick1989 force-pushed the bugfix/cp_initcontainers branch from 310f1fc to bf1293c Compare August 5, 2025 21:15
@yorick1989
Copy link
Author

yorick1989 commented Aug 5, 2025

These test errors are keep coming up. =) I ran tox -e linters -vv --skip-missing-interpreters=false manually and fixed all the errors. Hopefully, this was the last fix. Check for the changes: https://github.com/ansible-collections/kubernetes.core/compare/310f1fc9df8c3e0bcc3a1c2662a312901a3a0084..bf1293cd79969f86786c75f8ade83d95239f28d8

Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants