-
Notifications
You must be signed in to change notification settings - Fork 0
/
restart-workloads-on-secret-change.yaml
89 lines (87 loc) · 2.96 KB
/
restart-workloads-on-secret-change.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
---
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: restart-workloads-on-secret-change
annotations:
policies.kyverno.io/title: Restart workloads On Secret Change
policies.kyverno.io/subject: Deployment, Statefulset, Daemonset
policies.kyverno.io/description: >-
If Secrets are mounted in ways which do not naturally allow updates to
be live refreshed it may be necessary to modify a Deployment. This policy
watches a Secret and if it changes will write an annotation
to one or more target Deployments, Statefulsets or Daemonsets thus triggering a new rollout and thereby
refreshing the referred Secret.
spec:
mutateExistingOnPolicyUpdate: false
rules:
- name: update-secret
match:
any:
- resources:
kinds:
- Secret
names:
- "*"
annotations:
restart-workloads: "true"
preconditions:
all:
- key: "{{"{{ request.operation || 'BACKGROUND' }}"}}"
operator: Equals
value: UPDATE
mutate:
targets:
- apiVersion: apps/v1
kind: Deployment
namespace: "{{"{{ request.object.metadata.namespace }}"}}"
preconditions:
all:
- key: "{{"{{ target.metadata.name }}"}}"
operator: AnyIn
value: "{{"{{"}} request.object.metadata.annotations.\"restart-deployments\" | split(@, ','){{"}}"}}"
- apiVersion: apps/v1
kind: StatefulSet
namespace: "{{"{{ request.object.metadata.namespace }}"}}"
preconditions:
all:
- key: "{{"{{ target.metadata.name }}"}}"
operator: AnyIn
value: "{{"{{"}} request.object.metadata.annotations.\"restart-statefulsets\" | split(@, ','){{"}}"}}"
- apiVersion: apps/v1
kind: DaemonSet
namespace: "{{"{{ request.object.metadata.namespace }}"}}"
preconditions:
all:
- key: "{{"{{ target.metadata.name }}"}}"
operator: AnyIn
value: "{{"{{"}} request.object.metadata.annotations.\"restart-daemonsets\" | split(@, ','){{"}}"}}"
patchStrategicMerge:
spec:
template:
metadata:
annotations:
ops.corp.com/triggerrestart: "{{"{{request.object.metadata.resourceVersion}}"}}"
---
# ClusterRole granting permissions for Kyverno background controller to update deployments and statefulsets
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
app.kubernetes.io/component: background-controller
app.kubernetes.io/instance: kyverno
app.kubernetes.io/part-of: kyverno
name: kyverno:generate-workloads
rules:
- apiGroups:
- apps
resources:
- deployments
- statefulsets
- daemonsets
verbs:
- get
- list
- patch
- update
- watch