Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

k8s support diff mode #146

Merged
merged 13 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from 8 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
2 changes: 2 additions & 0 deletions changelogs/fragments/146-k8s-add-support-diff-mode.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- k8s - Add support for diff mode, diff is returned only when it is explicitly requested (https://github.com/ansible-collections/kubernetes.core/pull/146).
abikouo marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 8 additions & 0 deletions molecule/default/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@
tags:
- always

- name: Include diff.yml
include_tasks:
file: tasks/diff.yml
apply:
tags: [ diff, k8s ]
tags:
- always

roles:
- role: helm
tags:
Expand Down
153 changes: 153 additions & 0 deletions molecule/default/tasks/diff.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
---
- set_fact:
diff_namespace: "diff"
diff_configmap: "diff-configmap"

- block:
- name: Ensure namespace
k8s:
kind: Namespace
name: '{{ diff_namespace }}'

# Using option 'apply' set to 'yes'
- name: Create Pod using apply and diff set to yes
k8s:
namespace: '{{ diff_namespace }}'
apply: yes
template: "pod_diff.j2"
diff: yes
vars:
pod_name: "pod-apply"
pod_image: "busybox:1.32.0"
register: result

- name: check that result has diff attribute
assert:
that:
- result is changed
- result.diff is defined

- name: Update pod definition using apply and diff set to no
k8s:
namespace: '{{ diff_namespace }}'
apply: yes
template: "pod_diff.j2"
diff: no
vars:
pod_name: "pod-apply"
pod_image: "busybox:1.33.0"
register: result

- name: check that output has no diff attribute
assert:
that:
- result is changed
- result.diff is not defined

# Using option 'state=patched'
- name: Create Pod using state=present and diff set to yes
k8s:
namespace: '{{ diff_namespace }}'
state: present
template: "pod_diff.j2"
vars:
pod_name: "pod-patch"
pod_image: "busybox:1.32.0"
register: result

- name: Update pod definition using state=patched
k8s:
namespace: '{{ diff_namespace }}'
state: patched
template: "pod_diff.j2"
diff: no
vars:
pod_name: "pod-patch"
pod_image: "busybox:1.33.0"
pod_label: "patching"
register: result

- name: check that output has no diff attribute
assert:
that:
- result is changed
- result.diff is not defined

- name: Update pod definition using state=patched and diff=yes
k8s:
namespace: '{{ diff_namespace }}'
state: patched
template: "pod_diff.j2"
diff: yes
vars:
pod_name: "pod-patch"
pod_image: "busybox:1.33.0"
pod_label: "running"
register: result

- name: check that output has no diff attribute
assert:
that:
- result is changed
- result.diff is defined

# check diff mode using force=yes
- name: Create a ConfigMap
k8s:
kind: ConfigMap
name: '{{ diff_configmap }}'
namespace: '{{ diff_namespace }}'
definition:
data:
key: "initial value"
diff: yes
register: result

- name: check that output has no diff attribute
assert:
that:
- result is changed
- result.diff is not defined

- name: Update ConfigMap using force and diff=no
k8s:
kind: ConfigMap
name: '{{ diff_configmap }}'
namespace: '{{ diff_namespace }}'
force: yes
definition:
data:
key: "update value with diff=no"
diff: no
register: result

- name: check that output has no diff attribute
assert:
that:
- result is changed
- result.diff is not defined

- name: Update ConfigMap using force and diff=yes
k8s:
kind: ConfigMap
name: '{{ diff_configmap }}'
namespace: '{{ diff_namespace }}'
force: yes
definition:
data:
key: "update value with diff=yes"
diff: yes
register: result

- name: check that output has diff attribute
assert:
that:
- result is changed
- result.diff is defined

always:
- name: Ensure namespace is deleted
k8s:
state: absent
kind: Namespace
name: '{{ diff_namespace }}'
14 changes: 14 additions & 0 deletions molecule/default/templates/pod_diff.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Pod
metadata:
name: {{ pod_name }}
labels:
ansible: {{ pod_label | default('demo') }}
spec:
containers:
- name: c0
image: {{ pod_image }}
command:
- /bin/sh
- -c
- while true;do date;sleep 5; done
9 changes: 6 additions & 3 deletions plugins/module_utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,8 @@ def build_error_msg(kind, name, msg):
existing = {}
match, diffs = self.diff_objects(existing, result['result'])
result['changed'] = not match
result['diff'] = diffs
if self.module._diff:
result['diff'] = diffs
result['method'] = 'apply'
if not success:
msg = "Resource apply timed out"
Expand Down Expand Up @@ -785,7 +786,8 @@ def build_error_msg(kind, name, msg):
match, diffs = self.diff_objects(existing.to_dict(), result['result'])
result['changed'] = not match
result['method'] = 'replace'
result['diff'] = diffs
if self.module._diff:
result['diff'] = diffs
if not success:
msg = "Resource replacement timed out"
if continue_on_error:
Expand Down Expand Up @@ -819,7 +821,8 @@ def build_error_msg(kind, name, msg):
match, diffs = self.diff_objects(existing.to_dict(), result['result'])
result['changed'] = not match
result['method'] = 'patch'
result['diff'] = diffs
if self.module._diff:
result['diff'] = diffs

if not success:
msg = "Resource update timed out"
Expand Down