Ensure k8s apply works with check mode (#60572)

* 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 #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 <felix@fontein.de>
(cherry picked from commit a684bb9f5b)
pull/62572/head
Will Thames 5 years ago committed by Toshio Kuratomi
parent 1bb6d3c21a
commit 5a99d473d3

@ -0,0 +1,2 @@
bugfixes:
- k8s - ensure that apply works with check mode. Bumps minimum openshift version for apply to 0.9.2.

@ -53,6 +53,13 @@ except ImportError:
K8S_CONFIG_HASH_IMP_ERR = traceback.format_exc() K8S_CONFIG_HASH_IMP_ERR = traceback.format_exc()
HAS_K8S_CONFIG_HASH = False 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): class KubernetesRawModule(KubernetesAnsibleModule):
@ -117,11 +124,11 @@ class KubernetesRawModule(KubernetesAnsibleModule):
if LooseVersion(self.openshift_version) < LooseVersion("0.6.2"): if LooseVersion(self.openshift_version) < LooseVersion("0.6.2"):
self.fail_json(msg=missing_required_lib("openshift >= 0.6.2", reason="for merge_type")) self.fail_json(msg=missing_required_lib("openshift >= 0.6.2", reason="for merge_type"))
if self.params.get('apply') is not None: if self.params.get('apply') is not None:
if LooseVersion(self.openshift_version) < LooseVersion("0.9.0"): if not HAS_K8S_APPLY:
self.fail_json(msg=missing_required_lib("openshift >= 0.9.0", reason="for apply")) self.fail_json(msg=missing_required_lib("openshift >= 0.9.2", reason="for apply"))
self.apply = self.params['apply'] self.apply = self.params['apply']
else: else:
self.apply = LooseVersion(self.openshift_version) >= LooseVersion("0.9.0") self.apply = HAS_K8S_APPLY
if resource_definition: if resource_definition:
if isinstance(resource_definition, string_types): if isinstance(resource_definition, string_types):
@ -288,7 +295,7 @@ class KubernetesRawModule(KubernetesAnsibleModule):
else: else:
if self.apply: if self.apply:
if self.check_mode: if self.check_mode:
k8s_obj = definition ignored, k8s_obj = apply_object(resource, definition)
else: else:
try: try:
k8s_obj = resource.apply(definition, namespace=namespace).to_dict() k8s_obj = resource.apply(definition, namespace=namespace).to_dict()

@ -55,6 +55,27 @@
that: that:
- k8s_configmap_2 is not changed - 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 - name: add same configmap again but using name and namespace args
k8s: k8s:
name: "apply-configmap" name: "apply-configmap"

@ -12,7 +12,7 @@
- pip: - pip:
name: name:
- 'openshift>=0.9.0' - openshift>=0.9.2
- coverage - coverage
virtualenv: "{{ virtualenv }}" virtualenv: "{{ virtualenv }}"
virtualenv_command: "{{ virtualenv_command }}" virtualenv_command: "{{ virtualenv_command }}"
@ -25,13 +25,14 @@
- file: - file:
path: "{{ virtualenv }}" path: "{{ virtualenv }}"
state: absent state: absent
no_log: yes
# Test validate with kubernetes-validate # Test validate with kubernetes-validate
- pip: - pip:
name: name:
- kubernetes-validate==1.12.0 - kubernetes-validate==1.12.0
- 'openshift>=0.9.0' - openshift>=0.9.2
- coverage - coverage
virtualenv: "{{ virtualenv }}" virtualenv: "{{ virtualenv }}"
virtualenv_command: "{{ virtualenv_command }}" virtualenv_command: "{{ virtualenv_command }}"
@ -45,6 +46,7 @@
- file: - file:
path: "{{ virtualenv }}" path: "{{ virtualenv }}"
state: absent state: absent
no_log: yes
# Test graceful failure for older versions of openshift # Test graceful failure for older versions of openshift
@ -66,12 +68,13 @@
- file: - file:
path: "{{ virtualenv }}" path: "{{ virtualenv }}"
state: absent state: absent
no_log: yes
# Run full test suite # Run full test suite
- pip: - pip:
name: name:
- 'openshift>=0.9.0' - openshift>=0.9.2
- coverage - coverage
virtualenv: "{{ virtualenv }}" virtualenv: "{{ virtualenv }}"
virtualenv_command: "{{ virtualenv_command }}" virtualenv_command: "{{ virtualenv_command }}"
@ -86,3 +89,4 @@
- file: - file:
path: "{{ virtualenv }}" path: "{{ virtualenv }}"
state: absent state: absent
no_log: yes

@ -66,4 +66,4 @@
assert: assert:
that: that:
- k8s_apply is failed - 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)')"

Loading…
Cancel
Save