fed2d1884145fd931560c998594eb6e4db624888
[ric-plt/ric-dep.git] / helm / 3rdparty / influxdb / templates / backup-cronjob.yaml
1 {{- if .Values.backup.enabled }}
2 apiVersion: batch/v1beta1
3 kind: CronJob
4 metadata:
5   name: {{ include "influxdb.fullname" . }}-backup
6   labels:
7     {{- include "influxdb.labels" . | nindent 4 }}
8     app.kubernetes.io/component: backup
9   annotations:
10     {{- toYaml .Values.backup.annotations | nindent 4 }}
11 spec:
12   schedule: {{ .Values.backup.schedule | quote }}
13   startingDeadlineSeconds: {{ .Values.backup.startingDeadlineSeconds }}
14   concurrencyPolicy: Forbid
15   jobTemplate:
16     spec:
17       template:
18         metadata:
19           {{- if .Values.backup.podAnnotations }}
20           annotations:
21             {{ toYaml .Values.backup.podAnnotations | nindent 12 }}
22           {{- end }}
23           labels:
24             {{- include "influxdb.selectorLabels" . | nindent 12 }}
25         spec:
26           restartPolicy: OnFailure
27           volumes:
28           - name: backup
29           {{- if .Values.backup.persistence.enabled }}
30             persistentVolumeClaim:
31               claimName: {{ include "influxdb.fullname" . }}-backup
32           {{- else }}
33             emptyDir: {}
34           {{- end }}
35           {{- if .Values.backup.gcs }}
36           {{- if .Values.backup.gcs.serviceAccountSecret }}
37           - name: google-cloud-key
38             secret:
39               secretName: {{ .Values.backup.gcs.serviceAccountSecret | quote }}
40           {{- end }}
41           {{- end }}
42           {{- if .Values.backup.s3 }}
43           {{- if .Values.backup.s3.credentialsSecret }}
44           - name: aws-credentials-secret
45             secret:
46               secretName: {{ .Values.backup.s3.credentialsSecret | quote }}
47           {{- end }}
48           {{- end }}
49           serviceAccountName: {{ include "influxdb.serviceAccountName" . }}
50           initContainers:
51           - name: influxdb-backup
52             image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
53             volumeMounts:
54             - name: backup
55               mountPath: /backup
56             command:
57             - /bin/sh
58             args:
59             - '-c'
60             - |
61               influxd backup \
62                 -host {{ include "influxdb.fullname" . }}.{{ .Release.Namespace }}.svc:{{ .Values.config.rpc.bind_address | default 8088 }} \
63                 -portable /backup/"$(date +%Y%m%d%H%M%S)"
64             resources:
65               {{- toYaml .Values.backup.resources | nindent 14 }}
66           containers:
67           {{- if .Values.backup.gcs }}
68           - name: gsutil-cp
69             image: google/cloud-sdk:alpine
70             command:
71             - /bin/sh
72             args:
73             - '-c'
74             - '-e'
75             - |
76               if [ -n "$KEY_FILE" ]; then
77                 gcloud auth activate-service-account --key-file $KEY_FILE
78               fi
79               gsutil -m cp -r /backup/* "$DST_URL"
80               rm -rf /backup/*
81             volumeMounts:
82             - name: backup
83               mountPath: /backup
84             {{- if .Values.backup.gcs.serviceAccountSecretKey}}
85             - name: google-cloud-key
86               mountPath: /var/secrets/google/
87             {{- end }}
88             env:
89               - name: DST_URL
90                 value: {{ .Values.backup.gcs.destination}}
91               {{- if .Values.backup.gcs.serviceAccountSecretKey}}
92               - name: KEY_FILE
93                 value: /var/secrets/google/{{ .Values.backup.gcs.serviceAccountSecretKey }}
94               {{- end }}
95             resources:
96               {{- toYaml .Values.backup.resources | nindent 14 }}
97           {{- end }}
98           {{- if .Values.backup.azure }}
99           - name: azure-cli
100             image: microsoft/azure-cli
101             command:
102             - /bin/sh
103             args:
104             - '-c'
105             - '-e'
106             - |
107               az storage container create --name "$DST_CONTAINER"
108               az storage blob upload-batch --destination "$DST_CONTAINER" --destination-path "$DST_PATH" --source "$SRC_URL"
109               rm -rf /backup/*
110             volumeMounts:
111             - name: backup
112               mountPath: /backup
113             env:
114               - name: SRC_URL
115                 value: /backup
116               - name: DST_CONTAINER
117                 value: {{ .Values.backup.azure.destination_container }}
118               - name: DST_PATH
119                 value: {{ .Values.backup.azure.destination_path }}
120               - name: AZURE_STORAGE_CONNECTION_STRING
121                 valueFrom:
122                   secretKeyRef:
123                     name: {{ .Values.backup.azure.storageAccountSecret }}
124                     key: connection-string
125             resources:
126               {{- toYaml .Values.backup.resources | nindent 14 }}
127           {{- end }}
128           {{- if .Values.backup.s3 }}
129           - name: aws-cli
130             image: amazon/aws-cli
131             command:
132             - /bin/sh
133             args:
134             - '-c'
135             - '-e'
136             - |
137               aws {{- if .Values.backup.s3.endpointUrl }} --endpoint-url={{ .Values.backup.s3.endpointUrl }} {{- end }} s3 cp --recursive "$SRC_URL" "$DST_URL"
138               rm -rf /backup/*
139             volumeMounts:
140             - name: backup
141               mountPath: /backup
142             {{- if .Values.backup.s3.credentialsSecret}}
143             - name: aws-credentials-secret
144               mountPath: /var/secrets/aws/
145             {{- end }}
146             env:
147               - name: AWS_CONFIG_FILE
148                 value: /var/secrets/aws/credentials
149               - name: SRC_URL
150                 value: /backup
151               - name: DST_URL
152                 value: {{ .Values.backup.s3.destination }}
153             resources:
154               {{- toYaml .Values.backup.resources | nindent 14 }}
155           {{- end }}
156 {{- end }}