-
Notifications
You must be signed in to change notification settings - Fork 123
/
bindata.go
1885 lines (1684 loc) · 57 KB
/
bindata.go
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Code generated for package etcd_assets by go-bindata DO NOT EDIT. (@generated)
// sources:
// bindata/etcd/backups-cr.yaml
// bindata/etcd/backups-crb.yaml
// bindata/etcd/backups-sa.yaml
// bindata/etcd/cluster-backup-cronjob.yaml
// bindata/etcd/cluster-backup-job.yaml
// bindata/etcd/cluster-backup-pod.yaml
// bindata/etcd/cluster-backup.sh
// bindata/etcd/cluster-restore.sh
// bindata/etcd/cm.yaml
// bindata/etcd/etcd-common-tools
// bindata/etcd/minimal-sm.yaml
// bindata/etcd/ns.yaml
// bindata/etcd/pod-cm.yaml
// bindata/etcd/pod.yaml
// bindata/etcd/restore-pod-cm.yaml
// bindata/etcd/restore-pod.yaml
// bindata/etcd/sa.yaml
// bindata/etcd/scripts-cm.yaml
// bindata/etcd/sm.yaml
// bindata/etcd/svc.yaml
package etcd_assets
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
)
type asset struct {
bytes []byte
info os.FileInfo
}
type bindataFileInfo struct {
name string
size int64
mode os.FileMode
modTime time.Time
}
// Name return file name
func (fi bindataFileInfo) Name() string {
return fi.name
}
// Size return file size
func (fi bindataFileInfo) Size() int64 {
return fi.size
}
// Mode return file mode
func (fi bindataFileInfo) Mode() os.FileMode {
return fi.mode
}
// Mode return file modify time
func (fi bindataFileInfo) ModTime() time.Time {
return fi.modTime
}
// IsDir return file whether a directory
func (fi bindataFileInfo) IsDir() bool {
return fi.mode&os.ModeDir != 0
}
// Sys return file is sys mode
func (fi bindataFileInfo) Sys() interface{} {
return nil
}
var _etcdBackupsCrYaml = []byte(`kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: system:openshift:operator:etcd-backup-role
rules:
- apiGroups:
- "operator.openshift.io"
resources:
- "etcdbackups"
- "etcdbackups/status"
- "etcdbackups/finalizers"
verbs:
- "create"
- "update"
- "delete"
- apiGroups:
- "batch"
resources:
- "jobs/finalizers"
verbs:
- "update"
`)
func etcdBackupsCrYamlBytes() ([]byte, error) {
return _etcdBackupsCrYaml, nil
}
func etcdBackupsCrYaml() (*asset, error) {
bytes, err := etcdBackupsCrYamlBytes()
if err != nil {
return nil, err
}
info := bindataFileInfo{name: "etcd/backups-cr.yaml", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
var _etcdBackupsCrbYaml = []byte(`apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: system:openshift:operator:etcd-backup-crb
annotations:
include.release.openshift.io/self-managed-high-availability: "true"
include.release.openshift.io/single-node-developer: "true"
roleRef:
kind: ClusterRole
name: system:openshift:operator:etcd-backup-role
subjects:
- kind: ServiceAccount
namespace: openshift-etcd
name: etcd-backup-sa
`)
func etcdBackupsCrbYamlBytes() ([]byte, error) {
return _etcdBackupsCrbYaml, nil
}
func etcdBackupsCrbYaml() (*asset, error) {
bytes, err := etcdBackupsCrbYamlBytes()
if err != nil {
return nil, err
}
info := bindataFileInfo{name: "etcd/backups-crb.yaml", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
var _etcdBackupsSaYaml = []byte(`apiVersion: v1
kind: ServiceAccount
metadata:
namespace: openshift-etcd
name: etcd-backup-sa
`)
func etcdBackupsSaYamlBytes() ([]byte, error) {
return _etcdBackupsSaYaml, nil
}
func etcdBackupsSaYaml() (*asset, error) {
bytes, err := etcdBackupsSaYamlBytes()
if err != nil {
return nil, err
}
info := bindataFileInfo{name: "etcd/backups-sa.yaml", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
var _etcdClusterBackupCronjobYaml = []byte(`apiVersion: batch/v1
kind: CronJob
metadata:
name: templated
namespace: openshift-etcd
labels:
app: cluster-backup-cronjob
backup-name: templated
spec:
schedule: "templated"
concurrencyPolicy: "Forbid"
failedJobsHistoryLimit: 10
successfulJobsHistoryLimit: 5
jobTemplate:
metadata:
labels:
app: cluster-backup-cronjob
spec:
template:
metadata:
labels:
app: cluster-backup-cronjob
spec:
initContainers:
- name: retention
imagePullPolicy: IfNotPresent
terminationMessagePolicy: FallbackToLogsOnError
# since we can expect hostPath mounts, we need to run as privileged to access them
securityContext:
privileged: true
command: [ "cluster-etcd-operator" ]
args: [ "templated" ]
volumeMounts:
- mountPath: /etc/kubernetes/cluster-backup
name: etc-kubernetes-cluster-backup
containers:
- name: cluster-backup
imagePullPolicy: IfNotPresent
terminationMessagePolicy: FallbackToLogsOnError
command: [ "cluster-etcd-operator" ]
args: [ "templated" ]
env:
- name: MY_JOB_NAME
valueFrom:
fieldRef:
fieldPath: metadata.labels['batch.kubernetes.io/job-name']
- name: MY_JOB_UID
valueFrom:
fieldRef:
fieldPath: metadata.labels['batch.kubernetes.io/controller-uid']
serviceAccountName: etcd-backup-sa
nodeSelector:
node-role.kubernetes.io/master: ""
tolerations:
- operator: "Exists"
restartPolicy: OnFailure
volumes:
- name: etc-kubernetes-cluster-backup
persistentVolumeClaim:
claimName: templated
`)
func etcdClusterBackupCronjobYamlBytes() ([]byte, error) {
return _etcdClusterBackupCronjobYaml, nil
}
func etcdClusterBackupCronjobYaml() (*asset, error) {
bytes, err := etcdClusterBackupCronjobYamlBytes()
if err != nil {
return nil, err
}
info := bindataFileInfo{name: "etcd/cluster-backup-cronjob.yaml", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
var _etcdClusterBackupJobYaml = []byte(`apiVersion: batch/v1
kind: Job
metadata:
name: cluster-backup-job
namespace: openshift-etcd
labels:
app: cluster-backup-job
backup-name: templated
spec:
template:
spec:
initContainers:
- name: verify-storage
imagePullPolicy: IfNotPresent
terminationMessagePolicy: FallbackToLogsOnError
command: [ "cluster-etcd-operator", "verify", "backup-storage" ]
securityContext:
privileged: true
resources:
requests:
memory: 50Mi
cpu: 5m
volumeMounts:
- mountPath: /etc/kubernetes/cluster-backup
name: etc-kubernetes-cluster-backup
- mountPath: /var/run/secrets/etcd-client
name: etcd-client
- mountPath: /var/run/configmaps/etcd-ca
name: etcd-ca
containers:
- name: cluster-backup
imagePullPolicy: IfNotPresent
terminationMessagePolicy: FallbackToLogsOnError
command:
- /bin/sh
- -c
- |
#!/bin/sh
set -exuo pipefail
/usr/local/bin/cluster-backup.sh --force ${CLUSTER_BACKUP_PATH}
resources:
requests:
memory: 80Mi
cpu: 10m
securityContext:
privileged: true
volumeMounts:
- mountPath: /usr/local/bin
name: usr-local-bin
- mountPath: /etc/kubernetes/static-pod-resources
name: resources-dir
- mountPath: /etc/kubernetes/static-pod-certs
name: cert-dir
- mountPath: /etc/kubernetes/manifests
name: static-pod-dir
- mountPath: /etc/kubernetes/cluster-backup
name: etc-kubernetes-cluster-backup
priorityClassName: system-node-critical
nodeSelector:
node-role.kubernetes.io/master: ""
restartPolicy: Never
hostNetwork: true
tolerations:
- operator: "Exists"
volumes:
- hostPath:
path: /usr/local/bin
name: usr-local-bin
- hostPath:
path: /etc/kubernetes/manifests
name: static-pod-dir
- hostPath:
path: /etc/kubernetes/static-pod-resources
name: resources-dir
- hostPath:
path: /etc/kubernetes/static-pod-resources/etcd-certs
name: cert-dir
- name: etcd-client
secret:
secretName: etcd-client
- name: etcd-ca
configMap:
name: etcd-ca-bundle
- name: etc-kubernetes-cluster-backup
persistentVolumeClaim:
claimName: templated
`)
func etcdClusterBackupJobYamlBytes() ([]byte, error) {
return _etcdClusterBackupJobYaml, nil
}
func etcdClusterBackupJobYaml() (*asset, error) {
bytes, err := etcdClusterBackupJobYamlBytes()
if err != nil {
return nil, err
}
info := bindataFileInfo{name: "etcd/cluster-backup-job.yaml", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
var _etcdClusterBackupPodYaml = []byte(`apiVersion: v1
kind: Pod
metadata:
name: cluster-backup
namespace: openshift-etcd
labels:
app: cluster-backup
spec:
initContainers:
- name: verify-storage
imagePullPolicy: IfNotPresent
terminationMessagePolicy: FallbackToLogsOnError
command: [ "cluster-etcd-operator", "verify", "backup-storage" ]
securityContext:
privileged: true
resources:
requests:
memory: 50Mi
cpu: 5m
volumeMounts:
- mountPath: /etc/kubernetes/cluster-backup
name: etc-kubernetes-cluster-backup
- mountPath: /var/run/secrets/etcd-client
name: etcd-client
- mountPath: /var/run/configmaps/etcd-ca
name: etcd-ca
containers:
- name: cluster-backup
imagePullPolicy: IfNotPresent
terminationMessagePolicy: FallbackToLogsOnError
command:
- /bin/sh
- -c
- |
#!/bin/sh
set -exuo pipefail
if [ -n "${DELETE_BACKUP_DIR}" ]; then
echo "removing all backups in ${DELETE_BACKUP_DIR}"
rm -rf "${DELETE_BACKUP_DIR}/*"
fi
/usr/local/bin/cluster-backup.sh --force ${CLUSTER_BACKUP_PATH}
resources:
requests:
memory: 80Mi
cpu: 10m
securityContext:
privileged: true
volumeMounts:
- mountPath: /usr/local/bin
name: usr-local-bin
- mountPath: /etc/kubernetes/static-pod-resources
name: resources-dir
- mountPath: /etc/kubernetes/static-pod-certs
name: cert-dir
- mountPath: /etc/kubernetes/manifests
name: static-pod-dir
- mountPath: /etc/kubernetes/cluster-backup
name: etc-kubernetes-cluster-backup
priorityClassName: system-node-critical
nodeSelector:
node-role.kubernetes.io/master: ""
restartPolicy: Never
hostNetwork: true
tolerations:
- operator: "Exists"
volumes:
- hostPath:
path: /usr/local/bin
name: usr-local-bin
- hostPath:
path: /etc/kubernetes/cluster-backup
name: etc-kubernetes-cluster-backup
- hostPath:
path: /etc/kubernetes/manifests
name: static-pod-dir
- hostPath:
path: /etc/kubernetes/static-pod-resources
name: resources-dir
- hostPath:
path: /etc/kubernetes/static-pod-resources/etcd-certs
name: cert-dir
- name: etcd-client
secret:
secretName: etcd-client
- name: etcd-ca
configMap:
name: etcd-ca-bundle
`)
func etcdClusterBackupPodYamlBytes() ([]byte, error) {
return _etcdClusterBackupPodYaml, nil
}
func etcdClusterBackupPodYaml() (*asset, error) {
bytes, err := etcdClusterBackupPodYamlBytes()
if err != nil {
return nil, err
}
info := bindataFileInfo{name: "etcd/cluster-backup-pod.yaml", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
var _etcdClusterBackupSh = []byte(`#!/usr/bin/env bash
### Created by cluster-etcd-operator. DO NOT edit.
set -o errexit
set -o pipefail
set -o errtrace
# example
# cluster-backup.sh $path-to-snapshot
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
function usage {
echo 'Path to backup dir required: ./cluster-backup.sh [--force] <path-to-backup-dir>'
exit 1
}
IS_DIRTY=""
if [ "$1" == "--force" ]; then
IS_DIRTY="__POSSIBLY_DIRTY__"
shift
fi
# If the first argument is missing, or it is an existing file, then print usage and exit
if [ -z "$1" ] || [ -f "$1" ]; then
usage
fi
if [ ! -d "$1" ]; then
mkdir -p "$1"
fi
function check_if_operator_is_progressing {
local operator="$1"
if [ ! -f "${KUBECONFIG}" ]; then
echo "Valid kubeconfig is not found in kube-apiserver-certs. Exiting!"
exit 1
fi
progressing=$(oc get co "${operator}" -o jsonpath='{.status.conditions[?(@.type=="Progressing")].status}') || true
if [ "$progressing" == "" ]; then
echo "Could not find the status of the $operator. Check if the API server is running. Pass the --force flag to skip checks."
exit 1
elif [ "$progressing" != "False" ]; then
echo "Currently the $operator operator is progressing. A reliable backup requires that a rollout is not in progress. Aborting!"
exit 1
fi
}
# backup latest static pod resources
function backup_latest_kube_static_resources {
local backup_tar_file="$1"
local backup_resource_list=("kube-apiserver" "kube-controller-manager" "kube-scheduler" "etcd")
local latest_resource_dirs=()
for resource in "${backup_resource_list[@]}"; do
if [ ! -f "/etc/kubernetes/manifests/${resource}-pod.yaml" ]; then
echo "error finding manifests for the ${resource} pod. please check if it is running."
exit 1
fi
local latest_resource
latest_resource=$(grep -o -m 1 "/etc/kubernetes/static-pod-resources/${resource}-pod-[0-9]*" "/etc/kubernetes/manifests/${resource}-pod.yaml") || true
if [ -z "${latest_resource}" ]; then
echo "error finding static-pod-resources for the ${resource} pod. please check if it is running."
exit 1
fi
if [ "${IS_DIRTY}" == "" ]; then
check_if_operator_is_progressing "${resource}"
fi
echo "found latest ${resource}: ${latest_resource}"
latest_resource_dirs+=("${latest_resource#${CONFIG_FILE_DIR}/}")
done
# tar latest resources with the path relative to CONFIG_FILE_DIR
tar -cpzf "$backup_tar_file" -C "${CONFIG_FILE_DIR}" "${latest_resource_dirs[@]}"
chmod 600 "$backup_tar_file"
}
function source_required_dependency {
local src_path="$1"
if [ ! -f "${src_path}" ]; then
echo "required dependencies not found, please ensure this script is run on a node with a functional etcd static pod"
exit 1
fi
# shellcheck disable=SC1090
source "${src_path}"
}
BACKUP_DIR="$1"
DATESTRING=$(date "+%F_%H%M%S")
BACKUP_TAR_FILE=${BACKUP_DIR}/static_kuberesources_${DATESTRING}${IS_DIRTY}.tar.gz
SNAPSHOT_FILE="${BACKUP_DIR}/snapshot_${DATESTRING}${IS_DIRTY}.db"
trap 'rm -f ${BACKUP_TAR_FILE} ${SNAPSHOT_FILE}' ERR
source_required_dependency /etc/kubernetes/static-pod-resources/etcd-certs/configmaps/etcd-scripts/etcd.env
source_required_dependency /etc/kubernetes/static-pod-resources/etcd-certs/configmaps/etcd-scripts/etcd-common-tools
# replacing the value of variables sourced form etcd.env to use the local node folders if the script is not running into the cluster-backup pod
if [ ! -f "${ETCDCTL_CACERT}" ]; then
echo "Certificate ${ETCDCTL_CACERT} is missing. Checking in different directory"
export ETCDCTL_CACERT=$(echo ${ETCDCTL_CACERT} | sed -e "s|static-pod-certs|static-pod-resources/etcd-certs|")
export ETCDCTL_CERT=$(echo ${ETCDCTL_CERT} | sed -e "s|static-pod-certs|static-pod-resources/etcd-certs|")
export ETCDCTL_KEY=$(echo ${ETCDCTL_KEY} | sed -e "s|static-pod-certs|static-pod-resources/etcd-certs|")
if [ ! -f "${ETCDCTL_CACERT}" ]; then
echo "Certificate ${ETCDCTL_CACERT} is also missing in the second directory. Exiting!"
exit 1
else
echo "Certificate ${ETCDCTL_CACERT} found!"
fi
fi
backup_latest_kube_static_resources "${BACKUP_TAR_FILE}"
# Download etcdctl and get the etcd snapshot
dl_etcdctl
# snapshot save will continue to stay in etcdctl
ETCDCTL_ENDPOINTS="https://${NODE_NODE_ENVVAR_NAME_IP}:2379" etcdctl snapshot save "${SNAPSHOT_FILE}"
# Check the integrity of the snapshot
check_snapshot_status "${SNAPSHOT_FILE}"
snapshot_failed=$?
# If check_snapshot_status returned 1 it failed, so exit with code 1
if [[ $snapshot_failed -eq 1 ]]; then
echo "snapshot failed with exit code ${snapshot_failed}"
exit 1
fi
echo "snapshot db and kube resources are successfully saved to ${BACKUP_DIR}"
`)
func etcdClusterBackupShBytes() ([]byte, error) {
return _etcdClusterBackupSh, nil
}
func etcdClusterBackupSh() (*asset, error) {
bytes, err := etcdClusterBackupShBytes()
if err != nil {
return nil, err
}
info := bindataFileInfo{name: "etcd/cluster-backup.sh", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
var _etcdClusterRestoreSh = []byte(`#!/usr/bin/env bash
### Created by cluster-etcd-operator. DO NOT edit.
set -o errexit
set -o pipefail
set -o errtrace
# example
# ./cluster-restore.sh $path-to-backup
# There are several customization switches based on env variables:
# ETCD_RESTORE_SKIP_MOVE_CP_STATIC_PODS - when set this script will not move the other (non-etcd) static pod yamls
# ETCD_ETCDCTL_RESTORE - when set this script will use ` + "`" + `etcdctl snapshot restore` + "`" + ` instead of a restore pod yaml
# ETCD_ETCDCTL_RESTORE_ENABLE_BUMP - when set this script will spawn the restore pod with a large enough revision bump
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
function source_required_dependency {
local src_path="$1"
if [ ! -f "${src_path}" ]; then
echo "required dependencies not found, please ensure this script is run on a node with a functional etcd static pod"
exit 1
fi
# shellcheck disable=SC1090
source "${src_path}"
}
source_required_dependency /etc/kubernetes/static-pod-resources/etcd-certs/configmaps/etcd-scripts/etcd.env
source_required_dependency /etc/kubernetes/static-pod-resources/etcd-certs/configmaps/etcd-scripts/etcd-common-tools
function usage() {
echo 'Path to the directory containing backup files is required: ./cluster-restore.sh <path-to-backup>'
echo 'The backup directory is expected to be contain two files:'
echo ' 1. etcd snapshot'
echo ' 2. A copy of the Static POD resources at the time of backup'
exit 1
}
# If the argument is not passed, or if it is not a directory, print usage and exit.
if [ "$1" == "" ] || [ ! -d "$1" ]; then
usage
fi
function restore_static_pods() {
local backup_file="$1"
shift
local static_pods=("$@")
for pod_file_name in "${static_pods[@]}"; do
backup_pod_path=$(tar -tvf "${backup_file}" "*${pod_file_name}" | awk '{ print $6 }') || true
if [ -z "${backup_pod_path}" ]; then
echo "${pod_file_name} does not exist in ${backup_file}"
exit 1
fi
echo "starting ${pod_file_name}"
tar -xvf "${backup_file}" --strip-components=2 -C "${MANIFEST_DIR}"/ "${backup_pod_path}"
done
}
function wait_for_containers_to_stop() {
local containers=("$@")
for container_name in "${containers[@]}"; do
echo "Waiting for container ${container_name} to stop"
while [[ -n $(crictl ps --label io.kubernetes.container.name="${container_name}" -q) ]]; do
echo -n "."
sleep 1
done
echo "complete"
done
}
function mv_static_pods() {
local containers=("$@")
# Move manifests and stop static pods
if [ ! -d "$MANIFEST_STOPPED_DIR" ]; then
mkdir -p "$MANIFEST_STOPPED_DIR"
fi
for POD_FILE_NAME in "${containers[@]}"; do
echo "...stopping ${POD_FILE_NAME}"
[ ! -f "${MANIFEST_DIR}/${POD_FILE_NAME}" ] && continue
mv "${MANIFEST_DIR}/${POD_FILE_NAME}" "${MANIFEST_STOPPED_DIR}"
done
}
BACKUP_DIR="$1"
# shellcheck disable=SC2012
BACKUP_FILE=$(ls -vd "${BACKUP_DIR}"/static_kuberesources*.tar.gz | tail -1) || true
# shellcheck disable=SC2012
SNAPSHOT_FILE=$(ls -vd "${BACKUP_DIR}"/snapshot*.db | tail -1) || true
ETCD_STATIC_POD_LIST=("etcd-pod.yaml")
AUX_STATIC_POD_LIST=("kube-apiserver-pod.yaml" "kube-controller-manager-pod.yaml" "kube-scheduler-pod.yaml")
ETCD_STATIC_POD_CONTAINERS=("etcd" "etcdctl" "etcd-metrics" "etcd-readyz")
AUX_STATIC_POD_CONTAINERS=("kube-controller-manager" "kube-apiserver" "kube-scheduler")
if [ ! -f "${SNAPSHOT_FILE}" ]; then
echo "etcd snapshot ${SNAPSHOT_FILE} does not exist"
exit 1
fi
# Download etcdctl and check the snapshot status
dl_etcdctl
check_snapshot_status "${SNAPSHOT_FILE}"
ETCD_CLIENT="${ETCD_ETCDCTL_BIN+etcdctl}"
if [ -n "${ETCD_ETCDUTL_BIN}" ]; then
ETCD_CLIENT="${ETCD_ETCDUTL_BIN}"
fi
# Move static pod manifests out of MANIFEST_DIR, if required
if [ -z "${ETCD_RESTORE_SKIP_MOVE_CP_STATIC_PODS}" ]; then
mv_static_pods "${AUX_STATIC_POD_LIST[@]}"
wait_for_containers_to_stop "${AUX_STATIC_POD_CONTAINERS[@]}"
fi
# always move etcd pod and wait for all containers to exit
mv_static_pods "${ETCD_STATIC_POD_LIST[@]}"
wait_for_containers_to_stop "${ETCD_STATIC_POD_CONTAINERS[@]}"
if [ ! -d "${ETCD_DATA_DIR_BACKUP}" ]; then
mkdir -p "${ETCD_DATA_DIR_BACKUP}"
fi
# backup old data-dir
if [ -d "${ETCD_DATA_DIR}/member" ]; then
if [ -d "${ETCD_DATA_DIR_BACKUP}/member" ]; then
echo "removing previous backup ${ETCD_DATA_DIR_BACKUP}/member"
rm -rf "${ETCD_DATA_DIR_BACKUP}"/member
fi
echo "Moving etcd data-dir ${ETCD_DATA_DIR}/member to ${ETCD_DATA_DIR_BACKUP}"
mv "${ETCD_DATA_DIR}"/member "${ETCD_DATA_DIR_BACKUP}"/
fi
if [ -z "${ETCD_ETCDCTL_RESTORE}" ]; then
# Restore static pod resources
tar -C "${CONFIG_FILE_DIR}" -xzf "${BACKUP_FILE}" static-pod-resources
# Copy snapshot to backupdir
cp -p "${SNAPSHOT_FILE}" "${ETCD_DATA_DIR_BACKUP}"/snapshot.db
echo "starting restore-etcd static pod"
# ideally this can be solved with jq and a real env var, but we don't have it available at this point
if [ -n "${ETCD_ETCDCTL_RESTORE_ENABLE_BUMP}" ]; then
sed "s/export ETCD_ETCDCTL_RESTORE_ENABLE_BUMP=\"false\"/export ETCD_ETCDCTL_RESTORE_ENABLE_BUMP=\"true\"/" "${RESTORE_ETCD_POD_YAML}" > "${MANIFEST_DIR}/etcd-pod.yaml"
else
cp -p "${RESTORE_ETCD_POD_YAML}" "${MANIFEST_DIR}/etcd-pod.yaml"
fi
else
echo "removing etcd data dir..."
rm -rf "${ETCD_DATA_DIR}"
mkdir -p "${ETCD_DATA_DIR}"
echo "starting snapshot restore through etcdctl..."
if ! ${ETCD_CLIENT} snapshot restore "${SNAPSHOT_FILE}" --data-dir="${ETCD_DATA_DIR}"; then
echo "Snapshot restore failed. Aborting!"
exit 1
fi
# start the original etcd static pod again through the new snapshot
echo "restoring old etcd pod to start etcd again"
mv "${MANIFEST_STOPPED_DIR}/etcd-pod.yaml" "${MANIFEST_DIR}/etcd-pod.yaml"
fi
# start remaining static pods
if [ -z "${ETCD_RESTORE_SKIP_MOVE_CP_STATIC_PODS}" ]; then
restore_static_pods "${BACKUP_FILE}" "${AUX_STATIC_POD_LIST[@]}"
fi
`)
func etcdClusterRestoreShBytes() ([]byte, error) {
return _etcdClusterRestoreSh, nil
}
func etcdClusterRestoreSh() (*asset, error) {
bytes, err := etcdClusterRestoreShBytes()
if err != nil {
return nil, err
}
info := bindataFileInfo{name: "etcd/cluster-restore.sh", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
var _etcdCmYaml = []byte(`apiVersion: v1
kind: ConfigMap
metadata:
namespace: openshift-etcd
name: config
data:
config.yaml:
`)
func etcdCmYamlBytes() ([]byte, error) {
return _etcdCmYaml, nil
}
func etcdCmYaml() (*asset, error) {
bytes, err := etcdCmYamlBytes()
if err != nil {
return nil, err
}
info := bindataFileInfo{name: "etcd/cm.yaml", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
var _etcdEtcdCommonTools = []byte(`# Common environment variables
ASSET_DIR="/home/core/assets"
CONFIG_FILE_DIR="/etc/kubernetes"
MANIFEST_DIR="${CONFIG_FILE_DIR}/manifests"
ETCD_DATA_DIR="/var/lib/etcd"
ETCD_DATA_DIR_BACKUP="/var/lib/etcd-backup"
MANIFEST_STOPPED_DIR="${ASSET_DIR}/manifests-stopped"
RESTORE_ETCD_POD_YAML="${CONFIG_FILE_DIR}/static-pod-resources/etcd-certs/configmaps/restore-etcd-pod/pod.yaml"
ETCDCTL_BIN_DIR="${CONFIG_FILE_DIR}/static-pod-resources/bin"
PATH=${PATH}:${ETCDCTL_BIN_DIR}
export KUBECONFIG="/etc/kubernetes/static-pod-resources/kube-apiserver-certs/secrets/node-kubeconfigs/localhost.kubeconfig"
export ETCD_ETCDCTL_BIN="etcdctl"
# download etcdctl from download release image
function dl_etcdctl {
# Avoid caching the binary when podman exists, the etcd image is always available locally and we need a way to update etcdctl.
# When we're running from an etcd image there's no podman and we can continue without a download.
if ([ -n "$(command -v podman)" ]); then
local etcdimg=${ETCD_IMAGE}
local etcdctr=$(podman create --authfile=/var/lib/kubelet/config.json ${etcdimg})
local etcdmnt=$(podman mount "${etcdctr}")
[ ! -d ${ETCDCTL_BIN_DIR} ] && mkdir -p ${ETCDCTL_BIN_DIR}
cp ${etcdmnt}/bin/etcdctl ${ETCDCTL_BIN_DIR}/
if [ -f "${etcdmnt}/bin/etcdutl" ]; then
cp ${etcdmnt}/bin/etcdutl ${ETCDCTL_BIN_DIR}/
export ETCD_ETCDUTL_BIN=etcdutl
fi
umount "${etcdmnt}"
podman rm "${etcdctr}"
etcdctl version
return
fi
if ([ -x "$(command -v etcdctl)" ]); then
echo "etcdctl is already installed"
if [ -x "$(command -v etcdutl)" ]; then
echo "etcdutl is already installed"
export ETCD_ETCDUTL_BIN=etcdutl
fi
return
fi
echo "Could neither pull etcdctl nor find it locally in cache. Aborting!"
exit 1
}
function check_snapshot_status() {
local snap_file="$1"
ETCD_CLIENT="${ETCD_ETCDCTL_BIN}"
if [ -n "${ETCD_ETCDUTL_BIN}" ]; then
ETCD_CLIENT="${ETCD_ETCDUTL_BIN}"
fi
if ! ${ETCD_CLIENT} snapshot status "${snap_file}" -w json; then
echo "Backup integrity verification failed. Backup appears corrupted. Aborting!"
return 1
fi
}
`)
func etcdEtcdCommonToolsBytes() ([]byte, error) {
return _etcdEtcdCommonTools, nil
}
func etcdEtcdCommonTools() (*asset, error) {
bytes, err := etcdEtcdCommonToolsBytes()
if err != nil {
return nil, err
}
info := bindataFileInfo{name: "etcd/etcd-common-tools", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
var _etcdMinimalSmYaml = []byte(`apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: etcd-minimal
namespace: openshift-etcd-operator
labels:
app.kubernetes.io/name: etcd
k8s-app: etcd
monitoring.openshift.io/collection-profile: minimal
spec:
endpoints:
- interval: 30s
metricRelabelings:
- action: keep
regex: (etcd_disk_backend_commit_duration_seconds_bucket|etcd_disk_wal_fsync_duration_seconds_bucket|etcd_mvcc_db_total_size_in_bytes|etcd_mvcc_db_total_size_in_use_in_bytes|etcd_network_peer_round_trip_time_seconds_bucket|etcd_network_peer_sent_failures_total|etcd_server_has_leader|etcd_server_is_leader|etcd_server_proposals_failed_total|etcd_server_quota_backend_bytes|grpc_server_handled_total|grpc_server_handling_seconds_bucket|grpc_server_started_total|process_start_time_seconds)
sourceLabels:
- __name__
port: etcd-metrics
scheme: https
tlsConfig:
ca:
configMap:
name: etcd-metric-serving-ca
key: ca-bundle.crt
cert:
secret:
name: etcd-metric-client
key: tls.crt
keySecret:
name: etcd-metric-client
key: tls.key
jobLabel: k8s-app
namespaceSelector:
matchNames:
- openshift-etcd
selector:
matchLabels:
k8s-app: etcd
`)
func etcdMinimalSmYamlBytes() ([]byte, error) {
return _etcdMinimalSmYaml, nil
}
func etcdMinimalSmYaml() (*asset, error) {
bytes, err := etcdMinimalSmYamlBytes()
if err != nil {
return nil, err
}
info := bindataFileInfo{name: "etcd/minimal-sm.yaml", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
var _etcdNsYaml = []byte(`apiVersion: v1
kind: Namespace
metadata:
annotations:
openshift.io/node-selector: ""
workload.openshift.io/allowed: "management"
name: openshift-etcd
labels:
openshift.io/run-level: "0"
pod-security.kubernetes.io/enforce: privileged
pod-security.kubernetes.io/audit: privileged
pod-security.kubernetes.io/warn: privileged
`)
func etcdNsYamlBytes() ([]byte, error) {
return _etcdNsYaml, nil
}
func etcdNsYaml() (*asset, error) {
bytes, err := etcdNsYamlBytes()
if err != nil {
return nil, err
}
info := bindataFileInfo{name: "etcd/ns.yaml", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
var _etcdPodCmYaml = []byte(`apiVersion: v1
kind: ConfigMap
metadata:
namespace: openshift-etcd
name: etcd-pod
data:
pod.yaml:
forceRedeploymentReason:
version: