Skip to content
This repository has been archived by the owner on Nov 20, 2023. It is now read-only.

ROLE: openshift-patch-resource - patch a k8s/openshift resource #386

Merged
merged 3 commits into from
Dec 2, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
89 changes: 89 additions & 0 deletions roles/openshift-patch-resource/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
openshift-patch-resource
========================

Patch a Kubernetes/Openshift resource

Requirements
------------

This role requires the `kubernetes` python module and an active session to your Kubernetes/Openshift cluster.

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") | 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 ||

Example Playbook
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we include some example vars here as well? ... or maybe just point to the tests to see some examples?

----------------

```yaml
---
- 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'"
```

License
-------

Apache 2.0

Author Information
------------------

Red Hat Community of Practice & staff of the Red Hat Open Innovation Labs.
10 changes: 10 additions & 0 deletions roles/openshift-patch-resource/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -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) }}"
2 changes: 2 additions & 0 deletions roles/openshift-patch-resource/tests/inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
localhost ansible_connection=local

1 change: 1 addition & 0 deletions roles/openshift-patch-resource/tests/roles
52 changes: 52 additions & 0 deletions roles/openshift-patch-resource/tests/test.yml
Original file line number Diff line number Diff line change
@@ -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'"