Skip to content

Commit 851aebd

Browse files
author
Yorick Gruijthuijzen
committed
Added support for copying files to init Containers.
1 parent 1705ced commit 851aebd

File tree

7 files changed

+98
-6
lines changed

7 files changed

+98
-6
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
bugfixes:
3+
- get the `k8s_cp` module to work for initContainers too ((https://github.com/ansible-collections/kubernetes.core/pull/971)[PR:971]).

plugins/module_utils/copy.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def _run_from_pod(self, cmd):
9696
return error, stdout, stderr
9797
except Exception as e:
9898
self.module.fail_json(
99-
msg="Error while running/parsing from pod {1}/{2} command='{0}' : {3}".format(
99+
msg="Error while running/parsing from pod {0}/{1} command='{2}' : {3}".format(
100100
self.namespace, self.name, cmd, to_native(e)
101101
)
102102
)
@@ -435,11 +435,13 @@ def _fail(exc):
435435

436436
try:
437437
result = svc.client.get(resource, name=name, namespace=namespace)
438-
containers = [
439-
c["name"] for c in result.to_dict()["status"]["containerStatuses"]
440-
]
441-
if container and container not in containers:
438+
containers = dict({
439+
c["name"]: c for cl in ['initContainerStatuses','containerStatuses'] for c in result.to_dict()["status"].get(cl, [])
440+
})
441+
if container and container not in containers.keys():
442442
module.fail_json(msg="Pod has no container {0}".format(container))
443-
return containers
443+
if container and container in containers and not bool(containers[container].get('started', False)):
444+
module.fail_json(msg="Pod container {0} is not started".format(container))
445+
return containers.keys()
444446
except Exception as exc:
445447
_fail(exc)

tests/integration/targets/k8s_copy/defaults/main.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,9 @@ pod_with_two_container:
1414

1515
pod_without_executable_find:
1616
name: openjdk-pod
17+
18+
pod_with_initcontainer_and_container:
19+
name: pod-copy-2
20+
container:
21+
- container-20
22+
- container-21

tests/integration/targets/k8s_copy/tasks/main.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,31 @@
1818
wait: yes
1919
template: pods_definition.j2
2020

21+
- name: Create Init Pod
22+
k8s:
23+
namespace: '{{ copy_namespace }}'
24+
template: pods_definition_init.j2
25+
26+
- kubernetes.core.k8s_info:
27+
api_version: v1
28+
kind: Pod
29+
name: '{{ pod_with_initcontainer_and_container.name }}'
30+
namespace: '{{ copy_namespace }}'
31+
register: init_pod_status
32+
until: >-
33+
init_pod_status.resources|length > 0
34+
and 'initContainerStatuses' in init_pod_status.resources.0.status
35+
and init_pod_status.resources.0.status.initContainerStatuses|length > 0
36+
and init_pod_status.resources.0.status.initContainerStatuses.0.started|bool
37+
2138
- include_tasks: test_copy_errors.yml
2239
- include_tasks: test_check_mode.yml
2340
- include_tasks: test_copy_file.yml
2441
- include_tasks: test_multi_container_pod.yml
2542
- include_tasks: test_copy_directory.yml
2643
- include_tasks: test_copy_large_file.yml
2744
- include_tasks: test_copy_item_with_space_in_its_name.yml
45+
- include_tasks: test_init_container_pod.yml
2846

2947
always:
3048

tests/integration/targets/k8s_copy/tasks/test_copy_errors.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,21 @@
6767
that:
6868
- copy_fake_container is failed
6969
- copy_fake_container.msg == "Pod has no container this_is_a_fake_container"
70+
71+
# copy file to not started container in pod should fail
72+
- name: copy file to not started container in pod should fail
73+
k8s_cp:
74+
namespace: '{{ copy_namespace }}'
75+
pod: '{{ pod_with_initcontainer_and_container.name }}'
76+
remote_path: /tmp
77+
local_path: files/simple_file.txt
78+
state: to_pod
79+
container: '{{ pod_with_initcontainer_and_container.container[1] }}'
80+
ignore_errors: true
81+
register: copy_not_started_container
82+
83+
- name: check that error message is as expected
84+
assert:
85+
that:
86+
- copy_not_started_container is failed
87+
- copy_not_started_container.msg == "Pod container {{ pod_with_initcontainer_and_container.container[1] }} is not started"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
- set_fact:
3+
random_content: "{{ lookup('password', '/dev/null chars=ascii_lowercase,digits,punctuation length=128') }}"
4+
5+
- name: Copy content into init container
6+
k8s_cp:
7+
namespace: '{{ copy_namespace }}'
8+
pod: '{{ pod_with_initcontainer_and_container.name }}'
9+
remote_path: /file_from_localhost.txt
10+
content: '{{ random_content }}'
11+
container: '{{ pod_with_initcontainer_and_container.container[0] }}'
12+
state: to_pod
13+
14+
- name: Get the content from copied file
15+
kubernetes.core.k8s_exec:
16+
namespace: '{{ copy_namespace }}'
17+
pod: '{{ pod_with_initcontainer_and_container.name }}'
18+
container: '{{ pod_with_initcontainer_and_container.container[0] }}'
19+
command: cat /file_from_localhost.txt
20+
register: exec_out
21+
22+
- name: check that content is found and the same as generated earlier
23+
assert:
24+
that:
25+
- exec_out.stdout == random_content
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
apiVersion: v1
3+
kind: Pod
4+
metadata:
5+
name: '{{ pod_with_initcontainer_and_container.name }}'
6+
spec:
7+
initContainers:
8+
- name: '{{ pod_with_initcontainer_and_container.container[0] }}'
9+
image: busybox:1.32.0
10+
command:
11+
- /bin/sh
12+
- -c
13+
- while true;do date;sleep 5; done
14+
containers:
15+
- name: '{{ pod_with_initcontainer_and_container.container[1] }}'
16+
image: busybox:1.33.0
17+
command:
18+
- /bin/sh
19+
- -c
20+
- while true;do date;sleep 5; done

0 commit comments

Comments
 (0)