[k8s] allow user to pass list of resources in to definition parameter (#42377)

* allow user to pass list of resources in to definition parameter

* Add new validator for list|dict|string

* use string_types instead of string

* state/force information is lost after the first item in the list

* Add tests

* Appease ansibot
This commit is contained in:
Fabian von Feilitzsch 2018-07-09 09:33:16 -04:00 committed by James Cammarata
commit e9c7b513a1
4 changed files with 125 additions and 5 deletions

View file

@ -2,6 +2,10 @@
pip:
name: openshift
# TODO: This is the only way I could get the kubeconfig, I don't know why. Running the lookup outside of debug seems to return an empty string
- debug: msg={{ lookup('env', 'K8S_AUTH_KUBECONFIG') }}
register: kubeconfig
# Kubernetes resources
- name: Create a namespace
k8s:
@ -9,6 +13,8 @@
kind: namespace
register: output
- debug: msg={{ lookup("k8s", kind="Namespace", api_version="v1", resource_name='testing', kubeconfig=kubeconfig.msg) }}
- name: show output
debug:
var: output
@ -188,3 +194,103 @@
- name: DC creation should be idempotent
assert:
that: not output.changed
### Type tests
- name: Create a namespace from a string
k8s:
definition: |+
---
kind: Namespace
apiVersion: v1
metadata:
name: testing1
- name: Namespace should exist
assert:
that: '{{ lookup("k8s", kind="Namespace", api_version="v1", resource_name="testing1", kubeconfig=kubeconfig.msg).status.phase == "Active" }}'
- name: Create resources from a multidocument yaml string
k8s:
definition: |+
---
kind: Namespace
apiVersion: v1
metadata:
name: testing2
---
kind: Namespace
apiVersion: v1
metadata:
name: testing3
- name: Resources should exist
assert:
that: lookup("k8s", kind="Namespace", api_version="v1", resource_name=item, kubeconfig=kubeconfig.msg).status.phase == "Active"
loop:
- testing2
- testing3
- name: Delete resources from a multidocument yaml string
k8s:
state: absent
definition: |+
---
kind: Namespace
apiVersion: v1
metadata:
name: testing2
---
kind: Namespace
apiVersion: v1
metadata:
name: testing3
- name: Resources should not exist
assert:
that: not ns or ns.status.phase == "Terminating"
loop:
- testing2
- testing3
vars:
ns: '{{ lookup("k8s", kind="Namespace", api_version="v1", resource_name=item, kubeconfig=kubeconfig.msg) }}'
- name: Create resources from a list
k8s:
definition:
- kind: Namespace
apiVersion: v1
metadata:
name: testing4
- kind: Namespace
apiVersion: v1
metadata:
name: testing5
- name: Resources should exist
assert:
that: lookup("k8s", kind="Namespace", api_version="v1", resource_name=item, kubeconfig=kubeconfig.msg).status.phase == "Active"
loop:
- testing4
- testing5
- name: Delete resources from a list
k8s:
state: absent
definition:
- kind: Namespace
apiVersion: v1
metadata:
name: testing4
- kind: Namespace
apiVersion: v1
metadata:
name: testing5
- name: Resources should not exist
assert:
that: not ns or ns.status.phase == "Terminating"
loop:
- testing4
- testing5
vars:
ns: '{{ lookup("k8s", kind="Namespace", api_version="v1", resource_name=item, kubeconfig=kubeconfig.msg) }}'