k8s_cp: add support for check_mode, fix doc issue, remove dependency with 'find' when state=from_pod (#512)

k8s_cp: add support for check_mode, fix doc issue, remove dependency with 'find' when state=from_pod

Depends-On: ansible/ansible-zuul-jobs#1635
Depends-On: ansible/ansible-zuul-jobs#1636
Depends-On: #518
Depends-On: #520
SUMMARY

add support for check_mode, closes #380
fix doc issue, closes #485
Remove dependency with 'find' executable when state=from_pod, closes #486

ISSUE TYPE


Bugfix Pull Request
Docs Pull Request
Feature Pull Request

Reviewed-by: Gonéri Le Bouder <goneri@lebouder.net>
Reviewed-by: Mike Graves <mgraves@redhat.com>
Reviewed-by: Bikouo Aubin <None>
This commit is contained in:
Bikouo Aubin
2022-12-09 17:00:14 +01:00
committed by GitHub
parent 646eb18806
commit 979b492233
8 changed files with 424 additions and 186 deletions

View File

@@ -11,3 +11,6 @@ pod_with_two_container:
container:
- container-10
- container-11
pod_without_executable_find:
name: openjdk-pod

View File

@@ -19,6 +19,7 @@
template: pods_definition.j2
- include_tasks: test_copy_errors.yml
- include_tasks: test_check_mode.yml
- include_tasks: test_copy_file.yml
- include_tasks: test_multi_container_pod.yml
- include_tasks: test_copy_directory.yml

View File

@@ -0,0 +1,142 @@
---
- name: Create temporary directory for testing
tempfile:
state: directory
suffix: ansible-k8s-copy
register: tmpdir
- block:
# setup
- name: Create local files for testing
copy:
content: "{{ item.content }}"
dest: "{{ local_dir_path }}/{{ item.dest }}"
with_items: "{{ test_files }}"
- name: Create directory into Pod
k8s_exec:
namespace: '{{ copy_namespace }}'
pod: '{{ pod_with_one_container.name }}'
command: "mkdir {{ pod_dir_path }}"
- name: Create files into Pod
k8s_cp:
namespace: '{{ copy_namespace }}'
pod: '{{ pod_with_one_container.name }}'
remote_path: "{{ pod_dir_path }}/{{ item.dest }}"
content: "{{ item.content }}"
state: to_pod
with_items: "{{ test_files }}"
# Test copy into Pod using check_mode=true
- name: Copy text file into remote pod
k8s_cp:
namespace: '{{ copy_namespace }}'
pod: '{{ pod_with_one_container.name }}'
remote_path: "{{ pod_dir_path }}/ansible.txt"
local_path: "{{ local_dir_path }}/{{ test_files[0].dest }}"
state: to_pod
check_mode: true
register: copy_file
- name: Ensure task is changed
assert:
that:
- copy_file is changed
- name: Ensure file does not exists into Pod
k8s_exec:
namespace: '{{ copy_namespace }}'
pod: '{{ pod_with_one_container.name }}'
command: "test -e {{ pod_dir_path }}/ansible.txt"
register: test_file
failed_when: test_file.return_code == 0
- name: Copy directory into Pod
k8s_cp:
namespace: '{{ copy_namespace }}'
pod: '{{ pod_with_one_container.name }}'
remote_path: "{{ pod_dir_path }}/mydir"
local_path: "{{ local_dir_path }}"
state: to_pod
check_mode: true
register: copy_dir
- name: Ensure task is changed
assert:
that:
- copy_dir is changed
- name: Ensure file does not exists into Pod
k8s_exec:
namespace: '{{ copy_namespace }}'
pod: '{{ pod_with_one_container.name }}'
command: "test -e {{ pod_dir_path }}/mydir"
register: test_dir
failed_when: test_dir.return_code == 0
# Test copy from pod using check_mode=true
- name: Copy file from Pod into local file system
k8s_cp:
namespace: '{{ copy_namespace }}'
pod: '{{ pod_with_one_container.name }}'
remote_path: "{{ pod_dir_path }}/{{ test_files[0].dest }}"
local_path: "{{ local_dir_path }}/ansible.txt"
state: from_pod
check_mode: true
register: copy_file
- name: Ensure task is changed
assert:
that:
- copy_file is changed
- name: Ensure file does not exists into local file system
stat:
path: "{{ local_dir_path }}/ansible.txt"
register: testfile
failed_when: testfile.stat.exists
- name: Copy directory from Pod into local file system
k8s_cp:
namespace: '{{ copy_namespace }}'
pod: '{{ pod_with_one_container.name }}'
remote_path: "{{ pod_dir_path }}"
local_path: "{{ local_dir_path }}/mydir"
state: from_pod
check_mode: true
register: _dir
- name: Ensure task is changed
assert:
that:
- _dir is changed
- name: Ensure directory does not exist into local file system
stat:
path: "{{ local_dir_path }}/mydir"
register: testdir
failed_when: testdir.stat.exists
vars:
local_dir_path: "{{ tmpdir.path }}"
pod_dir_path: "/tmp/test_check_mode"
test_files:
- content: "collection = kubernetes.core"
dest: collection.txt
- content: "modules = k8s_cp and k8s_exec"
dest: modules.txt
always:
- name: Delete temporary directory
file:
state: absent
path: "{{ local_dir_path }}"
ignore_errors: true
- name: Delete temporary directory created into Pod
k8s_exec:
namespace: '{{ copy_namespace }}'
pod: '{{ pod_with_one_container.name }}'
command: 'rm -r {{ pod_dir_path }}'
ignore_errors: true

View File

@@ -60,6 +60,59 @@
remote_path: /tmp/data
local_path: /tmp/data
# Test copy from Pod where the executable 'find' is missing
- name: Ensure 'find' is missing from Pod
k8s_exec:
namespace: '{{ copy_namespace }}'
pod: '{{ pod_without_executable_find.name }}'
command: 'find'
ignore_errors: true
register: _result
- name: Validate that 'find' executable is missing from Pod
assert:
that:
- _result is failed
fail_msg: "Pod contains 'find' executable, therefore we cannot run the next tasks."
- name: Copy files into container
k8s_cp:
namespace: "{{ copy_namespace }}"
pod: '{{ pod_without_executable_find.name }}'
remote_path: '{{ item.path }}'
content: '{{ item.content }}'
state: to_pod
with_items:
- path: /ansible/root.txt
content: this file is located at the root directory
- path: /ansible/.hidden_root.txt
content: this hidden file is located at the root directory
- path: /ansible/.sudir/root.txt
content: this file is located at the root of the sub directory
- path: /ansible/.sudir/.hidden_root.txt
content: this hidden file is located at the root of the sub directory
- name: Delete existing directory
file:
path: /tmp/openjdk-files
state: absent
ignore_errors: true
- name: copy directory from Pod into local filesystem (new directory to create)
k8s_cp:
namespace: '{{ copy_namespace }}'
pod: '{{ pod_without_executable_find.name }}'
remote_path: /ansible
local_path: /tmp/openjdk-files
state: from_pod
- name: Compare directories
kubectl_file_compare:
namespace: '{{ copy_namespace }}'
pod: '{{ pod_without_executable_find.name }}'
remote_path: /ansible
local_path: /tmp/openjdk-files
always:
- name: Remove directories created into remote Pod
k8s_exec:
@@ -79,3 +132,4 @@
with_items:
- /tmp/data
- /tmp/test
- /tmp/openjdk-files

View File

@@ -30,4 +30,16 @@ spec:
- /bin/sh
- -c
- while true;do date;sleep 5; done
---
apiVersion: v1
kind: Pod
metadata:
name: '{{ pod_without_executable_find.name }}'
spec:
containers:
- name: openjdk17
image: openjdk:17
command:
- /bin/sh
- -c
- while true;do date;sleep 5; done