Skip to content

Commit

Permalink
k8s_rollback: new module (ansible-collections#26)
Browse files Browse the repository at this point in the history
This module allows user to rollback deployment and daemonsets.

Signed-off-by: Julien Huon <julien@huon.email>
  • Loading branch information
julienhuon authored Oct 7, 2020
1 parent fc1f4e5 commit 0377a89
Show file tree
Hide file tree
Showing 3 changed files with 442 additions and 0 deletions.
1 change: 1 addition & 0 deletions molecule/default/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
- include_tasks: tasks/log.yml
- include_tasks: tasks/cluster_info.yml
- include_tasks: tasks/access_review.yml
- include_tasks: tasks/rollback.yml

roles:
- helm
Expand Down
217 changes: 217 additions & 0 deletions molecule/default/tasks/rollback.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
---
- block:
- name: Set variables
set_fact:
namespace: "testingrollback"

- name: Create a namespace
k8s:
name: "{{ namespace }}"
kind: Namespace
api_version: v1
apply: no
register: output

- name: show output
debug:
var: output

- name: Create a deployment
k8s:
state: present
wait: yes
inline: &deploy
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deploy
labels:
app: nginx
namespace: "{{ namespace }}"
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.17
ports:
- containerPort: 80
register: output

- name: Show output
debug:
var: output

- name: Crash the existing deployment
k8s:
state: present
wait: yes
definition:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deploy
labels:
app: nginx
namespace: "{{ namespace }}"
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.0.23449928384992872784
ports:
- containerPort: 80
ignore_errors: yes
register: output

- name: Rolling Back the crashed deployment
k8s_rollback:
api_version: apps/v1
kind: Deployment
name: nginx-deploy
namespace: "{{ namespace }}"
when: output.failed
register: output

- name: Show output
debug:
var: output

- name: Create a DaemonSet
k8s:
state: present
wait: yes
definition:
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluentd-elasticsearch
namespace: "{{ namespace }}"
labels:
k8s-app: fluentd-logging
spec:
selector:
matchLabels:
name: fluentd-elasticsearch
template:
metadata:
labels:
name: fluentd-elasticsearch
spec:
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
containers:
- name: fluentd-elasticsearch
image: quay.io/fluentd_elasticsearch/fluentd:v2.5.2
resources:
limits:
memory: 200Mi
requests:
cpu: 100m
memory: 200Mi
volumeMounts:
- name: varlog
mountPath: /var/log
- name: varlibdockercontainers
mountPath: /var/lib/docker/containers
readOnly: true
terminationGracePeriodSeconds: 30
volumes:
- name: varlog
hostPath:
path: /var/log
- name: varlibdockercontainers
hostPath:
path: /var/lib/docker/containers
register: output

- name: Show output
debug:
var: output

- name: Crash the existing DaemonSet
k8s:
state: present
wait: yes
definition:
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluentd-elasticsearch
namespace: "{{ namespace }}"
labels:
k8s-app: fluentd-logging
spec:
selector:
matchLabels:
name: fluentd-elasticsearch
template:
metadata:
labels:
name: fluentd-elasticsearch
spec:
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
containers:
- name: fluentd-elasticsearch
image: quay.io/fluentd_elasticsearch/fluentd:v2734894949
resources:
limits:
memory: 200Mi
requests:
cpu: 100m
memory: 200Mi
volumeMounts:
- name: varlog
mountPath: /var/log
- name: varlibdockercontainers
mountPath: /var/lib/docker/containers
readOnly: true
terminationGracePeriodSeconds: 30
volumes:
- name: varlog
hostPath:
path: /var/log
- name: varlibdockercontainers
hostPath:
path: /var/lib/docker/containers
ignore_errors: yes
register: output

- name: Rolling Back the crashed DaemonSet
k8s_rollback:
api_version: apps/v1
kind: DaemonSet
name: fluentd-elasticsearch
namespace: "{{ namespace }}"
when: output.failed
register: output

- name: Show output
debug:
var: output

always:
- name: Delete {{ namespace }} namespace
k8s:
name: "{{ namespace }}"
kind: Namespace
api_version: v1
state: absent
Loading

0 comments on commit 0377a89

Please sign in to comment.