-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbackup.go
365 lines (317 loc) · 10.5 KB
/
backup.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
// Copyright 2022 Camunda Services GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"strconv"
"time"
apps "k8s.io/api/apps/v1"
core "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"github.com/camunda/zeebe-chaos/go-chaos/internal"
"github.com/spf13/cobra"
)
func AddBackupCommand(rootCmd *cobra.Command, flags *Flags) {
var backupCommand = &cobra.Command{
Use: "backup",
Short: "Controls Zeebe backups",
Long: "Can be used to take backups and query their status",
}
var setupBackupCommand = &cobra.Command{
Use: "setup",
Short: "Configures a zeebe cluster's backup settings",
RunE: func(cmd *cobra.Command, args []string) error {
return setupBackup(flags.kubeConfigPath, flags.namespace)
},
}
var takeBackupCommand = &cobra.Command{
Use: "take",
Short: "Trigger a backup",
RunE: func(cmd *cobra.Command, args []string) error {
return takeBackup(flags)
},
}
var waitForBackupCommand = &cobra.Command{
Use: "wait",
Short: "Wait for a backup to complete or fail",
RunE: func(cmd *cobra.Command, args []string) error {
return waitForBackup(flags)
},
}
var restoreBackupCommand = &cobra.Command{
Use: "restore",
Short: "Restore from a given backup id",
RunE: func(cmd *cobra.Command, args []string) error {
return restoreFromBackup(flags)
},
}
rootCmd.AddCommand(backupCommand)
backupCommand.AddCommand(setupBackupCommand)
backupCommand.AddCommand(takeBackupCommand)
takeBackupCommand.Flags().StringVar(&flags.backupId, "backupId", strconv.FormatInt(time.Now().UnixMilli(), 10), "optionally specify the backup id to use, uses the current timestamp by default")
backupCommand.AddCommand(waitForBackupCommand)
waitForBackupCommand.Flags().StringVar(&flags.backupId, "backupId", strconv.FormatInt(time.Now().UnixMilli(), 10), "optionally specify the backup id to use, uses the current timestamp by default")
backupCommand.AddCommand(restoreBackupCommand)
restoreBackupCommand.Flags().StringVar(&flags.backupId, "backupId", strconv.FormatInt(time.Now().UnixMilli(), 10), "optionally specify the backup id to use, uses the current timestamp by default")
}
func setupBackup(kubeConfigPath string, namespace string) error {
k8Client, err := internal.CreateK8Client(kubeConfigPath, namespace)
if err != nil {
panic(err)
}
namespace = k8Client.GetCurrentNamespace()
err = k8Client.PauseReconciliation()
if err != nil {
return err
}
_, err = createBackupSecret(k8Client, namespace)
if err != nil {
return err
}
err = setupStatefulSetForBackups(err, k8Client, namespace)
if err != nil {
return err
}
err = setupGatewayForBackups(err, k8Client, namespace)
return err
}
func setupStatefulSetForBackups(err error, k8Client internal.K8Client, namespace string) error {
sfs, err := k8Client.GetZeebeStatefulSet()
if err != nil {
return err
}
sfsEnvFrom := sfs.Spec.Template.Spec.Containers[0].EnvFrom
sfs.Spec.Template.Spec.Containers[0].EnvFrom = append(sfsEnvFrom, core.EnvFromSource{SecretRef: &core.SecretEnvSource{LocalObjectReference: core.LocalObjectReference{Name: "zeebe-backup-store-s3"}}})
sfsEnv := sfs.Spec.Template.Spec.Containers[0].Env
sfs.Spec.Template.Spec.Containers[0].Env = append(
sfsEnv,
core.EnvVar{Name: "ZEEBE_BROKER_DATA_BACKUP_STORE", Value: "S3"},
core.EnvVar{Name: "ZEEBE_BROKER_EXPERIMENTAL_FEATURES_ENABLEBACKUP", Value: "true"},
core.EnvVar{Name: "MANAGEMENT_ENDPOINTS_WEB_EXPOSURE_INCLUDE", Value: "*"},
core.EnvVar{Name: "MANAGEMENT_ENDPOINTS_BACKUPS_ENABLED", Value: "true"},
)
_, err = k8Client.Clientset.AppsV1().StatefulSets(namespace).Update(context.TODO(), sfs, meta.UpdateOptions{})
return err
}
func setupGatewayForBackups(err error, k8Client internal.K8Client, namespace string) error {
saasGatewayLabels := meta.LabelSelector{
MatchLabels: map[string]string{"app.kubernetes.io/component": "standalone-gateway"},
}
var gatewayDeployments *apps.DeploymentList
gatewayDeployments, err = k8Client.Clientset.AppsV1().Deployments(namespace).List(context.TODO(), meta.ListOptions{LabelSelector: labels.Set(saasGatewayLabels.MatchLabels).String()})
if err != nil {
return err
}
if len(gatewayDeployments.Items) == 0 {
selector := meta.LabelSelector{
MatchLabels: map[string]string{"app.kubernetes.io/component": "zeebe-gateway"},
}
gatewayDeployments, err = k8Client.Clientset.AppsV1().Deployments(namespace).List(
context.TODO(),
meta.ListOptions{LabelSelector: labels.Set(selector.MatchLabels).String()},
)
if err != nil {
return err
}
}
gateway := gatewayDeployments.Items[0]
gateway.Spec.Template.Spec.Containers[0].Env = append(
gateway.Spec.Template.Spec.Containers[0].Env,
core.EnvVar{Name: "ZEEBE_BROKER_EXPERIMENTAL_FEATURES_ENABLEBACKUP", Value: "true"},
core.EnvVar{Name: "MANAGEMENT_ENDPOINTS_WEB_EXPOSURE_INCLUDE", Value: "*"},
core.EnvVar{Name: "MANAGEMENT_ENDPOINTS_BACKUPS_ENABLED", Value: "true"},
)
_, err = k8Client.Clientset.AppsV1().Deployments(namespace).Update(context.TODO(), &gateway, meta.UpdateOptions{})
return err
}
func createBackupSecret(k8Client internal.K8Client, namespace string) (*core.Secret, error) {
return k8Client.Clientset.CoreV1().Secrets(namespace).Create(
context.TODO(),
&core.Secret{
Type: "Opaque",
ObjectMeta: meta.ObjectMeta{Name: "zeebe-backup-store-s3"},
Data: map[string][]byte{
"ZEEBE_BROKER_DATA_BACKUP_S3_BUCKETNAME": []byte(os.Getenv("ZEEBE_BROKER_DATA_BACKUP_S3_BUCKETNAME")),
"ZEEBE_BROKER_DATA_BACKUP_S3_REGION": []byte(os.Getenv("ZEEBE_BROKER_DATA_BACKUP_S3_REGION")),
"ZEEBE_BROKER_DATA_BACKUP_S3_ACCESSKEY": []byte(os.Getenv("ZEEBE_BROKER_DATA_BACKUP_S3_ACCESSKEY")),
"ZEEBE_BROKER_DATA_BACKUP_S3_SECRETKEY": []byte(os.Getenv("ZEEBE_BROKER_DATA_BACKUP_S3_SECRETKEY")),
}},
meta.CreateOptions{},
)
}
func takeBackup(flags *Flags) error {
k8Client, err := createK8ClientWithFlags(flags)
if err != nil {
panic(err)
}
err = k8Client.AwaitReadiness()
if err != nil {
return err
}
port, closePortForward := k8Client.MustGatewayPortForward(0, 9600)
defer closePortForward()
url := fmt.Sprintf("http://localhost:%d/actuator/backup-runtime/%s", port, flags.backupId)
resp, err := http.Post(url, "", nil)
if err != nil {
return err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
if resp.StatusCode < 200 || resp.StatusCode >= 400 {
return errors.New("taking backup failed")
}
return err
}
func waitForBackup(flags *Flags) error {
k8Client, err := createK8ClientWithFlags(flags)
if err != nil {
panic(err)
}
port, closePortForward := k8Client.MustGatewayPortForward(0, 9600)
defer closePortForward()
for {
backup, err := getBackupStatus(port, flags.backupId)
if err != nil {
return err
}
switch backup.Status {
case "COMPLETED":
return nil
case "FAILED":
return errors.New("backup failed")
case "DOES_NOT_EXIST":
return errors.New("backup does not exist")
}
time.Sleep(5 * time.Second)
}
}
func restoreFromBackup(flags *Flags) error {
k8Client, err := createK8ClientWithFlags(flags)
if err != nil {
panic(err)
}
namespace := k8Client.GetCurrentNamespace()
err = k8Client.PauseReconciliation()
if err != nil {
return err
}
initialScale, err := k8Client.ScaleZeebeCluster(0)
if err != nil {
return err
}
sfs, err := k8Client.GetZeebeStatefulSet()
if err != nil {
return err
}
deleteContainer := core.Container{
Name: "delete-data",
Image: "busybox",
ImagePullPolicy: core.PullAlways,
Command: []string{"sh", "-c", "rm -rf /usr/local/zeebe/data/* && ls -lah /usr/local/zeebe/data"},
VolumeMounts: []core.VolumeMount{
{
Name: "data",
ReadOnly: false,
MountPath: "/usr/local/zeebe/data",
},
},
}
restoreContainer := core.Container{
Name: "restore-from-backup",
Image: sfs.Spec.Template.Spec.Containers[0].Image,
ImagePullPolicy: core.PullAlways,
Env: restoreEnvFromSfs(flags, sfs),
EnvFrom: []core.EnvFromSource{{SecretRef: &core.SecretEnvSource{LocalObjectReference: core.LocalObjectReference{Name: "zeebe-backup-store-s3"}}}},
VolumeMounts: []core.VolumeMount{
{
Name: "data",
ReadOnly: false,
MountPath: "/usr/local/zeebe/data",
},
},
}
sfs.Spec.Template.Spec.InitContainers = []core.Container{deleteContainer, restoreContainer}
_, err = k8Client.Clientset.AppsV1().StatefulSets(namespace).Update(context.TODO(), sfs, meta.UpdateOptions{})
if err != nil {
return err
}
// Scale up after adding init containers.
_, err = k8Client.ScaleZeebeCluster(initialScale)
if err != nil {
return err
}
err = k8Client.AwaitReadiness()
if err != nil {
return err
}
err = k8Client.ResumeReconciliation()
if err != nil {
return err
}
return nil
}
func restoreEnvFromSfs(flags *Flags, sfs *apps.StatefulSet) []core.EnvVar {
zeebeEnv := sfs.Spec.Template.Spec.Containers[0].Env
restoreEnv := make([]core.EnvVar, 0)
for _, env := range zeebeEnv {
// Filter out java tool options.
// If we don't, restore app will create a gc.log file in the data folder which prevents restoring because the data
// folder is not empty.
if env.Name != "JAVA_TOOL_OPTIONS" {
restoreEnv = append(restoreEnv, env)
}
}
restoreEnv = append(restoreEnv,
core.EnvVar{
Name: "ZEEBE_RESTORE",
Value: "true",
},
core.EnvVar{
Name: "ZEEBE_RESTORE_FROM_BACKUP_ID",
Value: flags.backupId,
})
return restoreEnv
}
func getBackupStatus(port int, backupId string) (*BackupStatus, error) {
url := fmt.Sprintf("http://localhost:%d/actuator/backup-runtime/%s", port, backupId)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var backup BackupStatus
err = json.Unmarshal(body, &backup)
if err != nil {
return nil, err
}
internal.LogInfo("Found backup %s with status: %s", backupId, backup.Status)
return &backup, nil
}
type BackupStatus struct {
Status string
}