From 542339b7974034d8114cb94136058b87e568ca1b Mon Sep 17 00:00:00 2001 From: Petter Abrahamsson Date: Mon, 2 Dec 2019 13:34:29 -0500 Subject: [PATCH 1/3] ROLE: openshift-patch-resource - patch a k8s/openshift resource --- roles/openshift-patch-resource/README.md | 41 +++++++++++++++ roles/openshift-patch-resource/tasks/main.yml | 10 ++++ .../openshift-patch-resource/tests/inventory | 2 + roles/openshift-patch-resource/tests/roles | 1 + roles/openshift-patch-resource/tests/test.yml | 52 +++++++++++++++++++ 5 files changed, 106 insertions(+) create mode 100644 roles/openshift-patch-resource/README.md create mode 100644 roles/openshift-patch-resource/tasks/main.yml create mode 100644 roles/openshift-patch-resource/tests/inventory create mode 120000 roles/openshift-patch-resource/tests/roles create mode 100644 roles/openshift-patch-resource/tests/test.yml diff --git a/roles/openshift-patch-resource/README.md b/roles/openshift-patch-resource/README.md new file mode 100644 index 00000000..8f59745b --- /dev/null +++ b/roles/openshift-patch-resource/README.md @@ -0,0 +1,41 @@ +openshift-patch-resource +======================== + +Patch a Kubernetes/Openshift resource + +Requirements +------------ + +This role requires the `kubernetes` python module + +Role Variables +-------------- + +| Variable | Description | Required | Defaults | +| :------- | :---------- | :------- | :------- | +| api_version | The api_version of the resource | yes || +| definition | The patch definition | yes || +| kind | The kind of the resource | yes || +| merge_type | A list of merge types ("strategic-merge, merge, json") | no | strategic-merge | +| name | The name of the resource | yes || +| namespace | The namespace of the resource | no || + +Example Playbook +---------------- + +```yaml +--- +- hosts: localhost + roles: + - openshift-patch-resource +``` + +License +------- + +Apache 2.0 + +Author Information +------------------ + +Red Hat Community of Practice & staff of the Red Hat Open Innovation Labs. diff --git a/roles/openshift-patch-resource/tasks/main.yml b/roles/openshift-patch-resource/tasks/main.yml new file mode 100644 index 00000000..80fc652c --- /dev/null +++ b/roles/openshift-patch-resource/tasks/main.yml @@ -0,0 +1,10 @@ +--- +# tasks file for openshift-patch-resource +- name: Patch {{ api_version }} {{ kind }}/{{ name }} + k8s: + api_version: "{{ api_version }}" + kind: "{{ kind }}" + merge_type: "{{ merge_type| default(omit) }}" + name: "{{ name }}" + namespace: "{{ namespace | default(omit) }}" + definition: "{{ definition | default(omit) }}" diff --git a/roles/openshift-patch-resource/tests/inventory b/roles/openshift-patch-resource/tests/inventory new file mode 100644 index 00000000..49d4fe2b --- /dev/null +++ b/roles/openshift-patch-resource/tests/inventory @@ -0,0 +1,2 @@ +localhost ansible_connection=local + diff --git a/roles/openshift-patch-resource/tests/roles b/roles/openshift-patch-resource/tests/roles new file mode 120000 index 00000000..20c4c58c --- /dev/null +++ b/roles/openshift-patch-resource/tests/roles @@ -0,0 +1 @@ +../../../roles \ No newline at end of file diff --git a/roles/openshift-patch-resource/tests/test.yml b/roles/openshift-patch-resource/tests/test.yml new file mode 100644 index 00000000..a7aca47e --- /dev/null +++ b/roles/openshift-patch-resource/tests/test.yml @@ -0,0 +1,52 @@ +--- +- hosts: localhost + tasks: + - name: Patch annotations on default project (strategic-merge strategy) + include_role: + name: openshift-patch-resource + vars: + api_version: v1 + definition: + metadata: + labels: + patch-role: patched + kind: Namespace + merge_type: strategic-merge + name: default + - name: Get annotated namespace + k8s_info: + kind: Namespace + label_selectors: + - patch-role=patched + register: r_namespace_label + - name: Assert namespace was labelled + assert: + that: + - (r_namespace_label.resources | length) == 1 + - "'patched' in r_namespace_label.resources[0].metadata.labels['patch-role']" + fail_msg: "Expected one namespace with the label 'patch-role: patched'" + success_msg: "There's one namespace with the label 'patch-role: patched'" + - name: Remove patch-role label (merge strategy) + include_role: + name: openshift-patch-resource + vars: + api_version: v1 + definition: + metadata: + labels: + patch-role: null + kind: Namespace + merge_type: merge + name: default + - name: Get annotated namespace + k8s_info: + kind: Namespace + label_selectors: + - patch-role=patched + register: r_namespace + - name: Assert namespace label was removed + assert: + that: + - (r_namespace.resources | length) == 0 + fail_msg: "Expected zero namespace with the label 'patch-role: patched'" + success_msg: "There's no namespace with the label 'patch-role: patched'" From d65811b5c7b9607b005ddc03cbf8695f2c5fc2f2 Mon Sep 17 00:00:00 2001 From: Petter Abrahamsson Date: Mon, 2 Dec 2019 13:41:02 -0500 Subject: [PATCH 2/3] Update README --- roles/openshift-patch-resource/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/roles/openshift-patch-resource/README.md b/roles/openshift-patch-resource/README.md index 8f59745b..a2cdaf23 100644 --- a/roles/openshift-patch-resource/README.md +++ b/roles/openshift-patch-resource/README.md @@ -16,7 +16,7 @@ Role Variables | api_version | The api_version of the resource | yes || | definition | The patch definition | yes || | kind | The kind of the resource | yes || -| merge_type | A list of merge types ("strategic-merge, merge, json") | no | strategic-merge | +| merge_type | A list of merge types ("strategic-merge, merge") | no | strategic-merge | | name | The name of the resource | yes || | namespace | The namespace of the resource | no || From d0805aa79fe84d84f6170b3a88f2ca24b2593614 Mon Sep 17 00:00:00 2001 From: Petter Abrahamsson Date: Mon, 2 Dec 2019 14:01:20 -0500 Subject: [PATCH 3/3] Updated README based on PR feedback --- roles/openshift-patch-resource/README.md | 56 ++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/roles/openshift-patch-resource/README.md b/roles/openshift-patch-resource/README.md index a2cdaf23..76d3bfa8 100644 --- a/roles/openshift-patch-resource/README.md +++ b/roles/openshift-patch-resource/README.md @@ -6,7 +6,7 @@ Patch a Kubernetes/Openshift resource Requirements ------------ -This role requires the `kubernetes` python module +This role requires the `kubernetes` python module and an active session to your Kubernetes/Openshift cluster. Role Variables -------------- @@ -16,7 +16,7 @@ Role Variables | api_version | The api_version of the resource | yes || | definition | The patch definition | yes || | kind | The kind of the resource | yes || -| merge_type | A list of merge types ("strategic-merge, merge") | no | strategic-merge | +| merge_type | A list of merge types ("strategic-merge, merge") | no | [strategic-merge](https://docs.ansible.com/ansible/latest/modules/k8s_module.html) | | name | The name of the resource | yes || | namespace | The namespace of the resource | no || @@ -26,8 +26,56 @@ Example Playbook ```yaml --- - hosts: localhost - roles: - - openshift-patch-resource + tasks: + - name: Patch annotations on default project (strategic-merge strategy) + include_role: + name: openshift-patch-resource + vars: + api_version: v1 + definition: + metadata: + labels: + patch-role: patched + kind: Namespace + merge_type: strategic-merge + name: default + - name: Get annotated namespace + k8s_info: + kind: Namespace + label_selectors: + - patch-role=patched + register: r_namespace_label + - name: Assert namespace was labelled + assert: + that: + - (r_namespace_label.resources | length) == 1 + - "'patched' in r_namespace_label.resources[0].metadata.labels['patch-role']" + fail_msg: "Expected one namespace with the label 'patch-role: patched'" + success_msg: "There's one namespace with the label 'patch-role: patched'" + - name: Remove patch-role label (merge strategy) + include_role: + name: openshift-patch-resource + vars: + api_version: v1 + definition: + metadata: + labels: + patch-role: null + kind: Namespace + merge_type: merge + name: default + - name: Get annotated namespace + k8s_info: + kind: Namespace + label_selectors: + - patch-role=patched + register: r_namespace + - name: Assert namespace label was removed + assert: + that: + - (r_namespace.resources | length) == 0 + fail_msg: "Expected zero namespace with the label 'patch-role: patched'" + success_msg: "There's no namespace with the label 'patch-role: patched'" ``` License