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

k8s loop flattening for template argument #49

Merged
merged 20 commits into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ changelogs/.plugin-cache.yaml
# Temporary test files.
tests/output
tests/integration/cloud-config-*
.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
minor_changes:
- k8s - Handle list of definition for option `template` (https://github.com/ansible-collections/kubernetes.core/pull/49).
- k8s - `continue_on_error` option added (whether to continue on creation/deletion errors) (https://github.com/ansible-collections/kubernetes.core/pull/49).
127 changes: 116 additions & 11 deletions molecule/default/tasks/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
matchLabels:
app: "{{ k8s_pod_name }}"
vars:
k8s_pod_name: pod
k8s_pod_name_one: pod
k8s_pod_namespace: "{{ template_namespace }}"
register: r
ignore_errors: yes
Expand All @@ -57,7 +57,7 @@
src: "../templates/pod_template_one.j2"
template: "pod_template_one.j2"
vars:
k8s_pod_name: pod
k8s_pod_name_one: pod
k8s_pod_namespace: "{{ template_namespace }}"
register: r
ignore_errors: yes
Expand All @@ -73,7 +73,7 @@
template: "pod_template_one.j2"
wait: yes
vars:
k8s_pod_name: pod-1
k8s_pod_name_one: pod-1
k8s_pod_namespace: "{{ template_namespace }}"
register: r

Expand All @@ -88,7 +88,7 @@
- default
wait: yes
vars:
k8s_pod_name: pod-2
k8s_pod_name_one: pod-2
k8s_pod_namespace: "{{ template_namespace }}"
register: r
ignore_errors: True
Expand All @@ -105,7 +105,7 @@
path: "pod_template_one.j2"
wait: yes
vars:
k8s_pod_name: pod-3
k8s_pod_name_one: pod-3
k8s_pod_namespace: "{{ template_namespace }}"
register: r

Expand All @@ -122,7 +122,7 @@
variable_end_string: ']]'
wait: yes
vars:
k8s_pod_name: pod-4
k8s_pod_name_two: pod-4
k8s_pod_namespace: "[[ template_namespace ]]"
ansible_python_interpreter: "[[ ansible_playbook_python ]]"
register: r
Expand All @@ -138,8 +138,8 @@
path: "pod_template_three.j2"
wait: yes
vars:
k8s_pod_name_one: pod-5
k8s_pod_name_two: pod-6
k8s_pod_name_three_one: pod-5
k8s_pod_name_three_two: pod-6
k8s_pod_namespace: "{{ template_namespace }}"
register: r

Expand All @@ -148,20 +148,125 @@
that:
- r is successful

- name: Create pods using list of template
kubernetes.core.k8s:
template:
- pod_template_one.j2
- path: "pod_template_two.j2"
variable_start_string: '[['
variable_end_string: ']]'
- path: "pod_template_three.j2"
wait: yes
vars:
k8s_pod_name_one: pod-7
k8s_pod_name_two: pod-8
k8s_pod_name_three_one: pod-9
k8s_pod_name_three_two: pod-10
k8s_pod_namespace: "{{ template_namespace }}"
register: r

- name: Assert that pod creation succeeded using template
assert:
that:
- r is successful

# continue_on_error
- name: define variable for test
set_fact:
k8s_pod_name_one: pod-11
k8s_pod_bad_name: pod-12
k8s_pod_namespace: "{{ template_namespace }}"
k8s_pod_bad_namespace: "dummy-namespace-012345"

- name: delete pod if it exists
kubernetes.core.k8s:
template: pod_template_one.j2
wait: true
state: absent

- name: create pod on bad namespace ( continue_on_error set to default(false) )
kubernetes.core.k8s:
template:
- pod_with_bad_namespace.j2
- pod_template_one.j2
register: resource
ignore_errors: true

- name: validate that creation failed
assert:
that:
- resource is failed
- '"Failed to create object" in resource.msg'

- name: assert pod has not been created
kubernetes.core.k8s_info:
kind: "{{ item.kind }}"
namespace: "{{ item.namespace }}"
name: "{{ item.name }}"
with_items:
- kind: pod
namespace: "{{ k8s_pod_bad_namespace }}"
name: "{{ k8s_pod_bad_name }}"
- kind: pod
namespace: "{{ k8s_pod_name_one }}"
name: "{{ k8s_pod_namespace }}"
register: resource

- name: check that resources creation failed
assert:
that:
- '{{ resource.results[0].resources | length == 0 }}'
- '{{ resource.results[1].resources | length == 0 }}'

- name: create pod without namespace (continue_on_error = true)
kubernetes.core.k8s:
template:
- pod_with_bad_namespace.j2
- pod_template_one.j2
continue_on_error: true
wait: true
register: resource
ignore_errors: true

- name: validate that creation succeeded
assert:
that:
- resource is successful

- name: assert pod has not been created
kubernetes.core.k8s_info:
kind: "{{ item.kind }}"
namespace: "{{ item.namespace }}"
name: "{{ item.name }}"
with_items:
- kind: pod
namespace: "{{ k8s_pod_bad_namespace }}"
name: "{{ k8s_pod_bad_name }}"
- kind: pod
namespace: "{{ k8s_pod_name_one }}"
name: "{{ k8s_pod_namespace }}"
register: resource

- name: check that resources creation failed
assert:
that:
- '{{ resource.results[0].resources | length == 0 }}'
- '{{ resource.results[1].resources | length > 0 }}'

- name: Remove Pod (Cleanup)
k8s:
kubernetes.core.k8s:
api_version: v1
kind: Pod
name: "pod-{{ item }}"
namespace: "{{ template_namespace }}"
state: absent
wait: yes
ignore_errors: yes
loop: "{{ range(1, 7) | list }}"
loop: "{{ range(1, 12) | list }}"

always:
- name: Remove namespace (Cleanup)
k8s:
kubernetes.core.k8s:
kind: Namespace
name: "{{ template_namespace }}"
state: absent
6 changes: 3 additions & 3 deletions molecule/default/templates/pod_template_one.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ apiVersion: v1
kind: Pod
metadata:
labels:
app: "{{ k8s_pod_name }}"
name: '{{ k8s_pod_name }}'
app: "{{ k8s_pod_name_one }}"
name: '{{ k8s_pod_name_one }}'
namespace: '{{ k8s_pod_namespace }}'
spec:
containers:
Expand All @@ -13,4 +13,4 @@ spec:
- while true; do echo $(date); sleep 10; done
image: python:3.7-alpine
imagePullPolicy: Always
name: '{{ k8s_pod_name }}'
name: '{{ k8s_pod_name_one }}'
12 changes: 6 additions & 6 deletions molecule/default/templates/pod_template_three.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ apiVersion: v1
kind: Pod
metadata:
labels:
app: "{{ k8s_pod_name_one }}"
name: '{{ k8s_pod_name_one }}'
app: "{{ k8s_pod_name_three_one }}"
name: '{{ k8s_pod_name_three_one }}'
namespace: '{{ k8s_pod_namespace }}'
spec:
containers:
Expand All @@ -14,15 +14,15 @@ spec:
- while true; do echo $(date); sleep 10; done
image: python:3.7-alpine
imagePullPolicy: Always
name: '{{ k8s_pod_name_one }}'
name: '{{ k8s_pod_name_three_one }}'

---
apiVersion: v1
kind: Pod
metadata:
labels:
app: "{{ k8s_pod_name_two }}"
name: '{{ k8s_pod_name_two }}'
app: "{{ k8s_pod_name_three_two }}"
name: '{{ k8s_pod_name_three_two }}'
namespace: '{{ k8s_pod_namespace }}'
spec:
containers:
Expand All @@ -32,4 +32,4 @@ spec:
- while true; do echo $(date); sleep 10; done
image: python:3.7-alpine
imagePullPolicy: Always
name: '{{ k8s_pod_name_two }}'
name: '{{ k8s_pod_name_three_two }}'
6 changes: 3 additions & 3 deletions molecule/default/templates/pod_template_two.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ apiVersion: v1
kind: Pod
metadata:
labels:
app: '[[ k8s_pod_name ]]'
name: '[[ k8s_pod_name ]]'
app: '[[ k8s_pod_name_two ]]'
name: '[[ k8s_pod_name_two ]]'
namespace: '[[ k8s_pod_namespace ]]'
spec:
containers:
Expand All @@ -13,4 +13,4 @@ spec:
- while true; do echo $(date); sleep 10; done
image: python:3.7-alpine
imagePullPolicy: Always
name: '[[ k8s_pod_name ]]'
name: '[[ k8s_pod_name_two ]]'
16 changes: 16 additions & 0 deletions molecule/default/templates/pod_with_bad_namespace.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: v1
kind: Pod
metadata:
labels:
app: "{{ k8s_pod_bad_name }}"
name: '{{ k8s_pod_bad_name }}'
namespace: '{{ k8s_pod_bad_namespace }}'
spec:
containers:
- args:
- /bin/sh
- -c
- while true; do echo $(date); sleep 10; done
image: python:3.7-alpine
imagePullPolicy: Always
name: '{{ k8s_pod_bad_name }}'
Loading