-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrestore_playbook.yaml
406 lines (371 loc) · 14.7 KB
/
restore_playbook.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
---
- hosts: localhost
tasks:
- debug: msg="Running Etcd Restore Ansible Operator Playbook"
- name: set fact restore_s3
set_fact:
restore_s3: true
etcd_secure_client: "absent"
etcd_secure_peer: "absent"
- name: lookup for status
set_fact:
cr: "{{ q('k8s',
api_version='etcd.database.coreos.com/v1beta2',
kind='EtcdRestore',
namespace=meta.namespace,
resource_name=meta.name)
}}"
- meta: end_play
when: cr[0].get("status", {}).get("phase", "") == "Complete"
- debug: msg="phase is Complete hence first run"
- name: Update etcdrestore cr status to Restoring
k8s_status:
api_version: "etcd.database.coreos.com/v1beta2"
kind: "EtcdRestore"
name: "{{ meta.name }}"
namespace: "{{ meta.namespace }}"
status:
phase: Restoring
reason: "Restoring EtcdCluster {{ meta.namespace }}/{{ meta.name }}"
- name: Set fail=true if bucket name is undefined
set_fact:
fail_reconciliation: true
fail_reason: "Bucket name is undefined"
when: s_3.path is undefined
- name: Set fail=true if aws secret is undefined
set_fact:
fail_reconciliation: true
fail_reason: "AWS Secret is undefined"
when: s_3.aws_secret is undefined
- name: Get EtcdCluster cr and save the spec
set_fact:
cluster_spec: "{{ q('k8s',
api_version='etcd.database.coreos.com/v1beta2',
kind='EtcdCluster',
namespace=meta.namespace,
resource_name=meta.name)
}}"
- name: Set fail=true if EtcdCluster CR not found
set_fact:
fail_reconciliation: true
fail_reason: "EtcdCluster CR {{ meta.namespace }}/{{ meta.name }} not Found"
when: cluster_spec|length == 0
- name: Determine if secure client
set_fact:
etcd_secure_client: "present"
when: (cluster_spec[0].spec.TLS is not undefined and cluster_spec[0].spec.TLS.static is not undefined and cluster_spec[0].spec.TLS.static.operatorSecret is not undefined and cluster_spec[0].spec.TLS.static.operatorSecret|length > 0 )
- name: Determine if secure peer
set_fact:
etcd_secure_peer: "present"
when: ( cluster_spec[0].spec.TLS is not undefined and cluster_spec[0].spec.TLS.static is not undefined and cluster_spec[0].spec.TLS.static.member is not undefined and cluster_spec[0].spec.TLS.static.member.peerSecret|length > 0)
# - name: call create certs on secure client
# vars:
# tls_secret_name: "{{ _tls.static.operator_secret }}"
# include_tasks: create_certs.yaml
# when: etcd_secure_client == "present"
- block:
- name: Set status when EtcdCluster cr not found
k8s_status:
api_version: "etcd.database.coreos.com/v1beta2"
kind: "EtcdRestore"
name: "{{ meta.name }}"
namespace: "{{ meta.namespace }}"
status:
phase: Failed
reason: "{{ fail_reason }}"
- debug: msg="{{ fail_reason }}"
- meta: end_play
when: fail_reconciliation is defined
- name: Pause EtcdCluster cr
k8s:
state: present
definition:
apiVersion: "etcd.database.coreos.com/v1beta2"
kind: "EtcdCluster"
metadata:
name: "{{ meta.name }}"
namespace: "{{ meta.namespace }}"
spec:
pause: true
#The EtcdCluster Operator can be in middle of a blocking reconciliation call when paused hence might not pause immediately
- name: Wait for EtcdCluster Operator to relinquish control
k8s_facts:
kind: EtcdCluster
api_version: etcd.database.coreos.com/v1beta2
namespace: "{{ meta.namespace }}"
name: "{{ meta.name }}"
register: etcd_cr
until: etcd_cr.get("resources", []) and etcd_cr.resources[0].get("status", {}).get("controlPaused", "") == true
retries: 30
delay: 5
- name: Delete EtcdCluster StatefulSet
k8s:
state: absent
definition:
apiVersion: "apps/v1"
kind: "StatefulSet"
metadata:
name: "{{ meta.name }}"
namespace: "{{ meta.namespace }}"
- name: Delete EtcdCluster Restore Pods
k8s:
state: absent
api_version: "v1"
kind: "Pod"
namespace: "{{ meta.namespace }}"
name: "{{ meta.name }}-{{ item|string }}"
with_sequence: start=0 end={{cluster_spec[0].spec.size|int - 1}}
- name: Wait while all pods terminate
vars:
pods: "{{ q('k8s', api_version='v1', kind='Pod', namespace=meta.namespace,
label_selector='etcd_cluster='+ meta.name + ',app=etcd') }}"
set_fact:
c_pods: "{{ pods }}"
until: pods|length == 0
retries: 50
delay: 10
- name: Wait while all restore pods terminate
vars:
pods: "{{ q('k8s', api_version='v1', kind='Pod', namespace=meta.namespace,
label_selector='etcd_cluster='+ meta.name + ',app=etcd') }}"
set_fact:
c_pods: "{{ pods }}"
until: pods|length == 0
retries: 50
delay: 10
- name: set variables for etcd restore
set_fact:
fetch_args:
- "/tmp/ansible/get_file.yaml"
- "-e"
- "s_3_path={{ s_3.path }}"
- "-e"
- "s_3_aws_secret={{ s_3.aws_secret }}"
- "-e"
- "storage_type={{ backup_storage_type }}"
- "-e"
- "etcd_namespace={{ meta.namespace }}"
- "-e"
- "file_path=/var/run/etcd"
initial_cluster: []
etcd_token: "{{ meta.name }}"
image: "{{ 'quay.io/coreos/etcd:v' + cluster_spec[0].spec.version | string }}"
etcd_volume_mounts:
- mountPath: "/var/run/etcd"
name: "data"
- block:
- name: generate initial cluster variable
set_fact:
initial_cluster: "{{ initial_cluster + [meta.name + '-' + (item|string) + '=http://' + meta.name + '-' + (item|string) + '.' + meta.name + '.' + meta.namespace + '.svc.cluster.local:2380'] }}"
# initial_cluster: "{{ initial_cluster + [meta.name + '-' + (item|string) + '=http://' + meta.name + '-' + (item|string) + '.' + meta.name + '.' + meta.namespace + '.svc.cluster.local:2380'] }}"
with_sequence: start=0 end={{cluster_spec[0].spec.size|int - 1}}
- name: Create Restore Pods
vars:
etcd_volumes:
- name: data
persistentVolumeClaim:
claimName: "data-{{ meta.name }}-{{ item|string }}"
pod_name: "{{ meta.name }}-{{ item|string }}"
etcd_peer_url: "http://{{ meta.name }}-{{ item|string }}.{{meta.name}}.{{meta.namespace}}.svc.cluster.local:2380"
init_containers:
- name: "fetch-backup"
image: "docker.io/alaypatel07/etcd-ansible-restore"
command: ["ansible-playbook"]
args: "{{ fetch_args }}"
imagePullPolicy: Always
volumeMounts: "{{ etcd_volume_mounts }}"
- name: "delete-datadir"
image: "{{ image }}"
command:
- "/bin/sh"
args:
- "-ec"
- "if [ -d '/var/run/etcd/default.etcd' ]; then rm -rf /var/run/etcd/default.etcd; fi && if [ -f '/var/run/etcd/member_id' ]; then rm -f /var/run/etcd/member_id; fi"
volumeMounts: "{{ etcd_volume_mounts }}"
k8s:
state: present
definition:
apiVersion: "v1"
kind: Pod
metadata:
name: "{{ pod_name }}"
namespace: "{{ meta.namespace }}"
labels:
etcd_cluster: "{{ meta.name }}"
app: "etcd"
spec:
serviceAccountName: "etcd-ansible-operator"
initContainers: "{{ init_containers }}"
hostname: "{{ pod_name }}"
subdomain: "{{ meta.name }}"
containers:
- name: "restore-datadir"
image: "{{ image }}"
command:
- "/bin/sh"
args:
- "-ec"
- "while true; do
echo 'Waiting for {{ pod_name }} to come up';
ping -W 1 -c 1 {{ pod_name }}.{{ meta.name }}.{{ meta.namespace }}.svc.cluster.local > /dev/null && break;
sleep 1s;
done &&
ETCDCTL_API=3 etcdctl snapshot restore /var/run/etcd/latest.backup
--name {{ pod_name }}
--initial-cluster {{ initial_cluster | join(',') }}
--initial-cluster-token {{ etcd_token }}
--initial-advertise-peer-urls {{ etcd_peer_url }}
--data-dir /var/run/etcd/default.etcd 2>/dev/termination-log"
volumeMounts: "{{ etcd_volume_mounts }}"
- name: restore-check
image: "{{ image }}"
command:
- "/bin/sh"
args:
- "-ec"
- "while [ ! -d '/var/run/etcd/default.etcd' ]; do sleep 1; done &&
exit 0;"
volumeMounts: "{{ etcd_volume_mounts }}"
restartPolicy: OnFailure
volumes: "{{ etcd_volumes }}"
with_sequence: start=0 end={{cluster_spec[0].spec.size|int - 1}}
when: etcd_secure_peer == "absent"
- block:
- name: generate initial cluster variable
set_fact:
initial_cluster: "{{ initial_cluster + [meta.name + '-' + (item|string) + '=https://' + meta.name + '-' + (item|string) + '.' + meta.name + '.' + meta.namespace + '.svc.cluster.local:2380'] }}"
with_sequence: start=0 end={{cluster_spec[0].spec.size|int - 1}}
- name: Create Restore Pods
vars:
etcd_volumes:
- name: data
persistentVolumeClaim:
claimName: "data-{{ meta.name }}-{{ item|string }}"
pod_name: "{{ meta.name }}-{{ item|string }}"
etcd_peer_url: "https://{{ meta.name }}-{{ item|string }}.{{meta.name}}.{{meta.namespace}}.svc.cluster.local:2380"
init_containers:
- name: "fetch-backup"
image: "docker.io/alaypatel07/etcd-ansible-restore"
command: ["ansible-playbook"]
args: "{{ fetch_args }}"
imagePullPolicy: Always
volumeMounts: "{{ etcd_volume_mounts }}"
- name: "delete-datadir"
image: "{{ image }}"
command:
- "/bin/sh"
args:
- "-ec"
- "if [ -d '/var/run/etcd/default.etcd' ]; then rm -rf /var/run/etcd/default.etcd; fi && if [ -f '/var/run/etcd/member_id' ]; then rm -f /var/run/etcd/member_id; fi"
volumeMounts: "{{ etcd_volume_mounts }}"
k8s:
state: present
definition:
apiVersion: "v1"
kind: Pod
metadata:
name: "{{ pod_name }}"
namespace: "{{ meta.namespace }}"
labels:
etcd_cluster: "{{ meta.name }}"
app: "etcd"
spec:
serviceAccountName: "etcd-ansible-operator"
initContainers: "{{ init_containers }}"
hostname: "{{ pod_name }}"
subdomain: "{{ meta.name }}"
containers:
- name: "restore-datadir"
image: "{{ image }}"
command:
- "/bin/sh"
args:
- "-ec"
- "while true; do
echo 'Waiting for {{ pod_name }} to come up';
ping -W 1 -c 1 {{ pod_name }}.{{ meta.name }}.{{ meta.namespace }}.svc.cluster.local > /dev/null && break;
sleep 1s;
done &&
ETCDCTL_API=3 etcdctl snapshot restore /var/run/etcd/latest.backup
--name {{ pod_name }}
--initial-cluster {{ initial_cluster | join(',') }}
--initial-cluster-token {{ etcd_token }}
--initial-advertise-peer-urls {{ etcd_peer_url }}
--data-dir /var/run/etcd/default.etcd 2>/dev/termination-log"
volumeMounts: "{{ etcd_volume_mounts }}"
- name: restore-check
image: "{{ image }}"
command:
- "/bin/sh"
args:
- "-ec"
- "while [ ! -d '/var/run/etcd/default.etcd' ]; do sleep 1; done &&
exit 0;"
volumeMounts: "{{ etcd_volume_mounts }}"
restartPolicy: OnFailure
volumes: "{{ etcd_volumes }}"
with_sequence: start=0 end={{cluster_spec[0].spec.size|int - 1}}
when: etcd_secure_peer == "present" and etcd_secure_client == "present"
- name: Wait while 3 pods are running
vars:
pods: "{{ q('k8s', api_version='v1', kind='Pod', namespace=meta.namespace,
label_selector='etcd_cluster='+ meta.name + ',app=etcd') }}"
set_fact:
c_pods: "{{ pods }}"
until: pods|length == cluster_spec[0].spec.size and pods[0].status.phase == "Succeeded"
retries: 30
delay: 10
- name: Delete Restore Pods
vars:
pod_name: "{{ meta.name }}-{{ item|string }}"
k8s:
state: absent
definition:
apiVersion: "v1"
kind: Pod
metadata:
name: "{{ pod_name }}"
namespace: "{{ meta.namespace }}"
with_sequence: start=0 end={{cluster_spec[0].spec.size|int - 1}}
- name: Unpause EtcdCluster
k8s:
state: present
definition:
apiVersion: "etcd.database.coreos.com/v1beta2"
kind: "EtcdCluster"
metadata:
name: "{{ meta.name }}"
namespace: "{{ meta.namespace }}"
spec:
pause: false
- name: Wait for cluster to be available
k8s_facts:
kind: EtcdCluster
api_version: etcd.database.coreos.com/v1beta2
namespace: "{{ meta.namespace }}"
name: "{{ meta.name }}"
register: sts
until: sts.get("resources", []) and sts.resources[0].get("status", {}).get("phase", "") == "Available"
retries: 30
delay: 10
ignore_errors: yes
- name: Update etcdrestore cr status to Failed
k8s_status:
api_version: "etcd.database.coreos.com/v1beta2"
kind: "EtcdRestore"
name: "{{ meta.name }}"
namespace: "{{ meta.namespace }}"
status:
phase: Failed
reason: "EtcdCluster is not Available"
when: sts.get("resources", []) and sts.resources[0].get("status", {}).get("phase", "") != "Available"
- name: Update etcdrestore cr status to Complete
k8s_status:
api_version: "etcd.database.coreos.com/v1beta2"
kind: "EtcdRestore"
name: "{{ meta.name }}"
namespace: "{{ meta.namespace }}"
status:
phase: Complete
reason: "EtcdCluster Restored successfully"
when: sts.get("resources", []) and sts.resources[0].get("status", {}).get("phase", "") == "Available"