Skip to content

Commit

Permalink
Add *RoleBinding rescue capabilities
Browse files Browse the repository at this point in the history
If changes happen to the ClusterRoleBinding or RoleBinding then
generally the system is not going to allow you to patch the object. Adds
block/rescue logic to remove the existing ClusterRoleBinding or
RoleBinding before creating it when patching the object fails.
  • Loading branch information
leifmadsen committed Sep 20, 2023
1 parent 0ed228b commit 5d0210d
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 63 deletions.
107 changes: 75 additions & 32 deletions roles/servicetelemetry/tasks/component_alertmanager.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,42 @@
verbs:
- create

- name: Create ClusterRoleBinding/alertmanager-stf
k8s:
definition:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: alertmanager-stf
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: alertmanager-stf
subjects:
- kind: ServiceAccount
name: alertmanager-stf
namespace: '{{ ansible_operator_meta.namespace }}'
- name: Setup ClusterRoleBinding for Alertmanager
block:
- name: Define ClusterRoleBinding/alertmanager-stf
set_fact:
def_alertmanager_stf_crb: |
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: alertmanager-stf
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: alertmanager-stf
subjects:
- kind: ServiceAccount
name: alertmanager-stf
namespace: '{{ ansible_operator_meta.namespace }}'
- name: Create ClusterRoleBinding/alertmanager-stf
k8s:
definition:
"{{ def_alertmanager_stf_crb }}"
rescue:
- name: Remove ClusterRoleBinding/alertmanager-stf when fail to update
k8s:
state: absent
definition:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: alertmanager-stf

- name: Create ClusterRoleBinding/alertmanager-stf
k8s:
definition:
"{{ def_alertmanager_stf_crb }}"

- name: Create Role/alertmanager-stf
k8s:
Expand All @@ -132,23 +153,45 @@
verbs:
- use

- name: Create RoleBinding/alertmanager-stf
k8s:
definition:
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: alertmanager-stf
namespace: '{{ ansible_operator_meta.namespace }}'
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: alertmanager-stf
namespace: '{{ ansible_operator_meta.namespace }}'
subjects:
- kind: ServiceAccount
name: alertmanager-stf
namespace: '{{ ansible_operator_meta.namespace }}'
- name: Setup RoleBinding for Alertmanager
block:
- name: Define RoleBinding/alertmanager-stf
set_fact:
def_alertmanager_stf_rb: |
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: alertmanager-stf
namespace: '{{ ansible_operator_meta.namespace }}'
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: alertmanager-stf
namespace: '{{ ansible_operator_meta.namespace }}'
subjects:
- kind: ServiceAccount
name: alertmanager-stf
namespace: '{{ ansible_operator_meta.namespace }}'
- name: Create RoleBinding/alertmanager-stf
k8s:
definition:
"{{ def_alertmanager_stf_rb }}"
rescue:
- name: Remove RoleBinding/alertmanager-stf when fail to update
k8s:
state: absent
definition:
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: alertmanager-stf
namespace: '{{ ansible_operator_meta.namespace }}'

- name: Create RoleBinding/alertmanager-stf
k8s:
definition:
"{{ def_alertmanager_stf_rb }}"

- name: Set default alertmanager service template
set_fact:
Expand Down
105 changes: 74 additions & 31 deletions roles/servicetelemetry/tasks/component_prometheus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,42 @@
verbs:
- get

- name: Create ClusterRoleBinding/prometheus-stf
k8s:
definition:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: prometheus-stf
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: prometheus-stf
subjects:
- kind: ServiceAccount
name: prometheus-stf
namespace: '{{ ansible_operator_meta.namespace }}'
- name: Setup ClusterRoleBinding for Prometheus
block:
- name: Define ClusterRoleBinding/prometheus-stf
set_fact:
def_prometheus_stf_crb: |
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: prometheus-stf
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: prometheus-stf
subjects:
- kind: ServiceAccount
name: prometheus-stf
namespace: '{{ ansible_operator_meta.namespace }}'
- name: Create ClusterRoleBinding/prometheus-stf
k8s:
definition:
"{{ def_prometheus_stf_crb }}"
rescue:
- name: Remove ClusterRoleBinding/prometheus-stf when fail to update
k8s:
state: absent
definition:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: prometheus-stf

- name: Create ClusterRoleBinding/prometheus-stf
k8s:
definition:
"{{ def_prometheus_stf_crb }}"

- name: Create Role/prometheus-stf for Prometheus operations
k8s:
Expand Down Expand Up @@ -103,22 +124,44 @@
verbs:
- use

- name: Create RoleBinding/prometheus-stf
k8s:
definition:
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: prometheus-stf
namespace: '{{ ansible_operator_meta.namespace }}'
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: prometheus-stf
subjects:
- kind: ServiceAccount
name: prometheus-stf
namespace: '{{ ansible_operator_meta.namespace }}'
- name: Setup RoleBinding for Prometheus
block:
- name: Define RoleBinding/prometheus-stf
set_fact:
def_prometheus_stf_rb: |
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: prometheus-stf
namespace: '{{ ansible_operator_meta.namespace }}'
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: prometheus-stf
subjects:
- kind: ServiceAccount
name: prometheus-stf
namespace: '{{ ansible_operator_meta.namespace }}'
- name: Create RoleBinding/prometheus-stf
k8s:
definition:
"{{ def_prometheus_stf_rb }}"
rescue:
- name: Remove RoleBinding/prometheus-stf on failure to update
k8s:
state: absent
definition:
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: prometheus-stf
namespace: '{{ ansible_operator_meta.namespace }}'

- name: Create RoleBinding/prometheus-stf
k8s:
definition:
"{{ def_prometheus_stf_rb }}"

- name: Remove old ClusterRoleBinding for prometheus-k8s using CMO roleRef
k8s:
Expand Down

0 comments on commit 5d0210d

Please sign in to comment.