Issue-ID: RICAPP-169 - Inclusion of InfluxDB helm chart for KPIMON, AD, TS, QP xApps
[ric-plt/ric-dep.git] / helm / 3rdparty / influxdb / templates / backup-cronjob.yaml
1 ################################################################################
2 #   Copyright (c) 2021 HCL Technolgies Limited.                                #
3 #                                                                              #
4 #   Licensed under the Apache License, Version 2.0 (the "License");            #
5 #   you may not use this file except in compliance with the License.           #
6 #   You may obtain a copy of the License at                                    #
7 #                                                                              #
8 #       http://www.apache.org/licenses/LICENSE-2.0                             #
9 #                                                                              #
10 #   Unless required by applicable law or agreed to in writing, software        #
11 #   distributed under the License is distributed on an "AS IS" BASIS,          #
12 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
13 #   See the License for the specific language governing permissions and        #
14 #   limitations under the License.                                             #
15 ################################################################################
16 {{- if .Values.backup.enabled }}
17 apiVersion: batch/v1beta1
18 kind: CronJob
19 metadata:
20   name: {{ include "common.fullname.influxdb" . }}-backup
21   labels:
22     {{- include "common.influxdb.labels" . | nindent 4 }}
23     app.kubernetes.io/component: backup
24   annotations:
25     {{- toYaml .Values.backup.annotations | nindent 4 }}
26 spec:
27   schedule: {{ .Values.backup.schedule | quote }}
28   startingDeadlineSeconds: {{ .Values.backup.startingDeadlineSeconds }}
29   concurrencyPolicy: Forbid
30   jobTemplate:
31     spec:
32       template:
33         metadata:
34           {{- if .Values.backup.podAnnotations }}
35           annotations:
36             {{ toYaml .Values.backup.podAnnotations | nindent 12 }}
37           {{- end }}
38           labels:
39             {{- include "common.influxdb.selectorLabels" . | nindent 12 }}
40         spec:
41           restartPolicy: OnFailure
42           volumes:
43           - name: backup
44           {{- if .Values.backup.persistence.enabled }}
45             persistentVolumeClaim:
46               claimName: {{ include "common.fullname.influxdb" . }}-backup
47           {{- else }}
48             emptyDir: {}
49           {{- end }}
50           {{- if .Values.backup.gcs }}
51           {{- if .Values.backup.gcs.serviceAccountSecret }}
52           - name: google-cloud-key
53             secret:
54               secretName: {{ .Values.backup.gcs.serviceAccountSecret | quote }}
55           {{- end }}
56           {{- end }}
57           {{- if .Values.backup.s3 }}
58           {{- if .Values.backup.s3.credentialsSecret }}
59           - name: aws-credentials-secret
60             secret:
61               secretName: {{ .Values.backup.s3.credentialsSecret | quote }}
62           {{- end }}
63           {{- end }}
64           serviceAccountName: {{ include "common.influxdb.serviceAccountName" . }}
65           initContainers:
66           - name: influxdb-backup
67             image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
68             volumeMounts:
69             - name: backup
70               mountPath: /backup
71             command:
72             - /bin/sh
73             args:
74             - '-c'
75             - |
76               influxd backup \
77                 -host {{ include "common.fullname.influxdb" . }}.{{ .Release.Namespace }}.svc:{{ include "common.serviceport.influxdb.rpc.bind_address" . | default 8088 }} \
78                 -portable /backup/"$(date +%Y%m%d%H%M%S)"
79             resources:
80               {{- toYaml .Values.backup.resources | nindent 14 }}
81           containers:
82           {{- if .Values.backup.gcs }}
83           - name: gsutil-cp
84             image: google/cloud-sdk:alpine
85             command:
86             - /bin/sh
87             args:
88             - '-c'
89             - '-e'
90             - |
91               if [ -n "$KEY_FILE" ]; then
92                 gcloud auth activate-service-account --key-file $KEY_FILE
93               fi
94               gsutil -m cp -r /backup/* "$DST_URL"
95               rm -rf /backup/*
96             volumeMounts:
97             - name: backup
98               mountPath: /backup
99             {{- if .Values.backup.gcs.serviceAccountSecretKey}}
100             - name: google-cloud-key
101               mountPath: /var/secrets/google/
102             {{- end }}
103             env:
104               - name: DST_URL
105                 value: {{ .Values.backup.gcs.destination}}
106               {{- if .Values.backup.gcs.serviceAccountSecretKey}}
107               - name: KEY_FILE
108                 value: /var/secrets/google/{{ .Values.backup.gcs.serviceAccountSecretKey }}
109               {{- end }}
110             resources:
111               {{- toYaml .Values.backup.resources | nindent 14 }}
112           {{- end }}
113           {{- if .Values.backup.azure }}
114           - name: azure-cli
115             image: microsoft/azure-cli
116             command:
117             - /bin/sh
118             args:
119             - '-c'
120             - '-e'
121             - |
122               az storage container create --name "$DST_CONTAINER"
123               az storage blob upload-batch --destination "$DST_CONTAINER" --destination-path "$DST_PATH" --source "$SRC_URL"
124               rm -rf /backup/*
125             volumeMounts:
126             - name: backup
127               mountPath: /backup
128             env:
129               - name: SRC_URL
130                 value: /backup
131               - name: DST_CONTAINER
132                 value: {{ .Values.backup.azure.destination_container }}
133               - name: DST_PATH
134                 value: {{ .Values.backup.azure.destination_path }}
135               - name: AZURE_STORAGE_CONNECTION_STRING
136                 valueFrom:
137                   secretKeyRef:
138                     name: {{ .Values.backup.azure.storageAccountSecret }}
139                     key: connection-string
140             resources:
141               {{- toYaml .Values.backup.resources | nindent 14 }}
142           {{- end }}
143           {{- if .Values.backup.s3 }}
144           - name: aws-cli
145             image: amazon/aws-cli
146             command:
147             - /bin/sh
148             args:
149             - '-c'
150             - '-e'
151             - |
152               aws {{- if .Values.backup.s3.endpointUrl }} --endpoint-url={{ .Values.backup.s3.endpointUrl }} {{- end }} s3 cp --recursive "$SRC_URL" "$DST_URL"
153               rm -rf /backup/*
154             volumeMounts:
155             - name: backup
156               mountPath: /backup
157             {{- if .Values.backup.s3.credentialsSecret}}
158             - name: aws-credentials-secret
159               mountPath: /var/secrets/aws/
160             {{- end }}
161             env:
162               - name: AWS_CONFIG_FILE
163                 value: /var/secrets/aws/credentials
164               - name: SRC_URL
165                 value: /backup
166               - name: DST_URL
167                 value: {{ .Values.backup.s3.destination }}
168             resources:
169               {{- toYaml .Values.backup.resources | nindent 14 }}
170           {{- end }}
171 {{- end }}