Skip to content

Commit

Permalink
Ensure k8s apply works with check mode (ansible#60572)
Browse files Browse the repository at this point in the history
* Ensure k8s apply works with check mode

Update the new predicted object with fields from the previous object
before applying in check mode

Don't log output of `file` with `state: absent` on huge virtualenvs!

Fixes ansible#60510

* Use openshift client fix to improve apply for check mode

Use new apply_object method to get a better approximation
of the expected object in check mode.

Requires released upgrade to openshift

* Add changelog fragment for k8s apply check mode fix

* Update changelogs/fragments/60510-k8s-apply-check-mode.yml

Co-Authored-By: Felix Fontein <[email protected]>
  • Loading branch information
willthames and felixfontein committed Sep 19, 2019
1 parent bebb11b commit a684bb9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/60510-k8s-apply-check-mode.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- k8s - ensure that apply works with check mode. Bumps minimum openshift version for apply to 0.9.2.
15 changes: 11 additions & 4 deletions lib/ansible/module_utils/k8s/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@
K8S_CONFIG_HASH_IMP_ERR = traceback.format_exc()
HAS_K8S_CONFIG_HASH = False

HAS_K8S_APPLY = None
try:
from openshift.dynamic.apply import apply_object
HAS_K8S_APPLY = True
except ImportError:
HAS_K8S_APPLY = False


class KubernetesRawModule(KubernetesAnsibleModule):

Expand Down Expand Up @@ -118,11 +125,11 @@ def __init__(self, k8s_kind=None, *args, **kwargs):
if LooseVersion(self.openshift_version) < LooseVersion("0.6.2"):
self.fail_json(msg=missing_required_lib("openshift >= 0.6.2", reason="for merge_type"))
if self.params.get('apply') is not None:
if LooseVersion(self.openshift_version) < LooseVersion("0.9.0"):
self.fail_json(msg=missing_required_lib("openshift >= 0.9.0", reason="for apply"))
if not HAS_K8S_APPLY:
self.fail_json(msg=missing_required_lib("openshift >= 0.9.2", reason="for apply"))
self.apply = self.params['apply']
else:
self.apply = LooseVersion(self.openshift_version) >= LooseVersion("0.9.0")
self.apply = HAS_K8S_APPLY

if resource_definition:
if isinstance(resource_definition, string_types):
Expand Down Expand Up @@ -293,7 +300,7 @@ def perform_action(self, resource, definition):
else:
if self.apply:
if self.check_mode:
k8s_obj = definition
ignored, k8s_obj = apply_object(resource, definition)
else:
try:
k8s_obj = resource.apply(definition, namespace=namespace).to_dict()
Expand Down
21 changes: 21 additions & 0 deletions test/integration/targets/k8s/tasks/apply.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,27 @@
that:
- k8s_configmap_2 is not changed

- name: add same configmap again with check mode on
k8s:
definition:
kind: ConfigMap
apiVersion: v1
metadata:
name: "apply-configmap"
namespace: "{{ apply_namespace }}"
data:
one: "1"
two: "2"
three: "3"
apply: yes
check_mode: yes
register: k8s_configmap_check

- name: check nothing changed
assert:
that:
- k8s_configmap_check is not changed

- name: add same configmap again but using name and namespace args
k8s:
name: "apply-configmap"
Expand Down
10 changes: 7 additions & 3 deletions test/integration/targets/k8s/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

- pip:
name:
- 'openshift>=0.9.0'
- openshift>=0.9.2
- coverage
virtualenv: "{{ virtualenv }}"
virtualenv_command: "{{ virtualenv_command }}"
Expand All @@ -25,13 +25,14 @@
- file:
path: "{{ virtualenv }}"
state: absent
no_log: yes

# Test validate with kubernetes-validate

- pip:
name:
- kubernetes-validate==1.12.0
- 'openshift>=0.9.0'
- openshift>=0.9.2
- coverage
virtualenv: "{{ virtualenv }}"
virtualenv_command: "{{ virtualenv_command }}"
Expand All @@ -45,6 +46,7 @@
- file:
path: "{{ virtualenv }}"
state: absent
no_log: yes

# Test graceful failure for older versions of openshift

Expand All @@ -66,12 +68,13 @@
- file:
path: "{{ virtualenv }}"
state: absent
no_log: yes

# Run full test suite

- pip:
name:
- 'openshift>=0.9.0'
- openshift>=0.9.2
- coverage
virtualenv: "{{ virtualenv }}"
virtualenv_command: "{{ virtualenv_command }}"
Expand All @@ -86,3 +89,4 @@
- file:
path: "{{ virtualenv }}"
state: absent
no_log: yes
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@
assert:
that:
- k8s_apply is failed
- "k8s_apply.msg.startswith('Failed to import the required Python library (openshift >= 0.9.0)')"
- "k8s_apply.msg.startswith('Failed to import the required Python library (openshift >= 0.9.2)')"

0 comments on commit a684bb9

Please sign in to comment.