feat(crdb): use crdb native backup and s3 backup added (#1915)

* fix(zitadelctl): implement takedown command

* fix(zitadelctl): correct destroy flow

* fix(zitadelctl): correct backup commands to read crds beforehand

* fix: add of destroyfile

* fix: clean for userlist

* fix: change backup and restore to crdb native

* fix: timeout for delete pvc for cockroachdb

* fix: corrected unit tests

* fix: add ignored file for scale

* fix: correct handling of gitops in backup command

* feat: add s3 backup kind

* fix: backuplist for s3 and timeout for pv deletion

* fix(database): fix nil pointer with binary version

* fix(database): cleanup of errors which cam with merging of the s3 logic

* fix: correct unit tests

* fix: cleanup monitor output

Co-authored-by: Elio Bischof <[email protected]>

* fix: backup imagepullpolixy to ifnotpresent

Co-authored-by: Elio Bischof <[email protected]>
This commit is contained in:
Stefan Benz
2021-10-13 14:34:03 +02:00
committed by GitHub
co-authored by Elio Bischof
parent 591a460450
commit 425a8b5fd5
67 changed files with 3867 additions and 626 deletions
@@ -22,6 +22,7 @@ const (
internalSecretName = "client-certs"
rootSecretName = "cockroachdb.client.root"
timeout = 45 * time.Minute
saJsonBase64Env = "SAJSON"
)
func AdaptFunc(
@@ -29,7 +30,6 @@ func AdaptFunc(
backupName string,
namespace string,
componentLabels *labels.Component,
databases []string,
bucketName string,
timestamp string,
nodeselector map[string]string,
@@ -37,6 +37,8 @@ func AdaptFunc(
checkDBReady operator.EnsureFunc,
secretName string,
secretKey string,
dbURL string,
dbPort int32,
image string,
) (
queryFunc operator.QueryFunc,
@@ -47,9 +49,12 @@ func AdaptFunc(
jobName := jobPrefix + backupName + jobSuffix
command := getCommand(
timestamp,
databases,
bucketName,
backupName,
certPath,
secretPath,
dbURL,
dbPort,
)
jobdef := getJob(
@@ -60,7 +65,8 @@ func AdaptFunc(
secretName,
secretKey,
command,
image)
image,
)
destroyJ, err := job.AdaptFuncToDestroy(jobName, namespace)
if err != nil {
@@ -19,7 +19,6 @@ func TestBackup_Adapt1(t *testing.T) {
monitor := mntr.Monitor{}
namespace := "testNs"
databases := []string{"testDb"}
nodeselector := map[string]string{"test": "test"}
tolerations := []corev1.Toleration{
{Key: "testKey", Operator: "testOp"}}
@@ -29,6 +28,8 @@ func TestBackup_Adapt1(t *testing.T) {
image := "testImage2"
secretKey := "testKey"
secretName := "testSecretName"
dbURL := "testDB"
dbPort := int32(80)
jobName := GetJobName(backupName)
componentLabels := labels.MustForComponent(labels.MustForAPI(labels.MustForOperator("testProd", "testOp", "testVersion"), "testKind", "testVersion"), "testComponent")
nameLabels := labels.MustForName(componentLabels, jobName)
@@ -46,9 +47,12 @@ func TestBackup_Adapt1(t *testing.T) {
secretKey,
getCommand(
timestamp,
databases,
bucketName,
backupName,
certPath,
secretPath,
dbURL,
dbPort,
),
image,
)
@@ -61,7 +65,6 @@ func TestBackup_Adapt1(t *testing.T) {
backupName,
namespace,
componentLabels,
databases,
bucketName,
timestamp,
nodeselector,
@@ -69,6 +72,8 @@ func TestBackup_Adapt1(t *testing.T) {
checkDBReady,
secretName,
secretKey,
dbURL,
dbPort,
image,
)
@@ -84,7 +89,6 @@ func TestBackup_Adapt2(t *testing.T) {
monitor := mntr.Monitor{}
namespace := "testNs2"
databases := []string{"testDb1", "testDb2"}
nodeselector := map[string]string{"test2": "test2"}
tolerations := []corev1.Toleration{
{Key: "testKey2", Operator: "testOp2"}}
@@ -94,6 +98,8 @@ func TestBackup_Adapt2(t *testing.T) {
image := "testImage2"
secretKey := "testKey2"
secretName := "testSecretName2"
dbURL := "testDB"
dbPort := int32(80)
jobName := GetJobName(backupName)
componentLabels := labels.MustForComponent(labels.MustForAPI(labels.MustForOperator("testProd2", "testOp2", "testVersion2"), "testKind2", "testVersion2"), "testComponent2")
nameLabels := labels.MustForName(componentLabels, jobName)
@@ -111,9 +117,12 @@ func TestBackup_Adapt2(t *testing.T) {
secretKey,
getCommand(
timestamp,
databases,
bucketName,
backupName,
certPath,
secretPath,
dbURL,
dbPort,
),
image,
)
@@ -126,7 +135,6 @@ func TestBackup_Adapt2(t *testing.T) {
backupName,
namespace,
componentLabels,
databases,
bucketName,
timestamp,
nodeselector,
@@ -134,6 +142,8 @@ func TestBackup_Adapt2(t *testing.T) {
checkDBReady,
secretName,
secretKey,
dbURL,
dbPort,
image,
)
@@ -1,28 +1,36 @@
package restore
import "strings"
import (
"strconv"
"strings"
)
func getCommand(
timestamp string,
databases []string,
bucketName string,
backupName string,
certsFolder string,
serviceAccountPath string,
dbURL string,
dbPort int32,
) string {
backupCommands := make([]string, 0)
for _, database := range databases {
backupCommands = append(backupCommands,
strings.Join([]string{
"/scripts/restore.sh",
bucketName,
backupName,
timestamp,
database,
secretPath,
certPath,
}, " "))
}
backupCommands = append(backupCommands, "export "+saJsonBase64Env+"=$(cat "+serviceAccountPath+" | base64 | tr -d '\n' )")
backupCommands = append(backupCommands,
strings.Join([]string{
"cockroach",
"sql",
"--certs-dir=" + certsFolder,
"--host=" + dbURL,
"--port=" + strconv.Itoa(int(dbPort)),
"-e",
"\"RESTORE FROM \\\"gs://" + bucketName + "/" + backupName + "/" + timestamp + "?AUTH=specified&CREDENTIALS=${" + saJsonBase64Env + "}\\\";\"",
}, " ",
),
)
return strings.Join(backupCommands, " && ")
}
@@ -6,45 +6,42 @@ import (
)
func TestBackup_Command1(t *testing.T) {
timestamp := ""
databases := []string{}
timestamp := "test1"
bucketName := "testBucket"
backupName := "testBackup"
dbURL := "testDB"
dbPort := int32(80)
cmd := getCommand(timestamp, databases, bucketName, backupName)
equals := ""
cmd := getCommand(
timestamp,
bucketName,
backupName,
certPath,
secretPath,
dbURL,
dbPort,
)
equals := "export SAJSON=$(cat /secrets/sa.json | base64 | tr -d '\n' ) && cockroach sql --certs-dir=/cockroach/cockroach-certs --host=testDB --port=80 -e \"RESTORE FROM \\\"gs://testBucket/testBackup/test1?AUTH=specified&CREDENTIALS=${SAJSON}\\\";\""
assert.Equal(t, equals, cmd)
}
func TestBackup_Command2(t *testing.T) {
timestamp := ""
databases := []string{"testDb"}
timestamp := "test2"
bucketName := "testBucket"
backupName := "testBackup"
dbURL := "testDB2"
dbPort := int32(81)
cmd := getCommand(timestamp, databases, bucketName, backupName)
equals := "/scripts/restore.sh testBucket testBackup testDb /secrets/sa.json /cockroach/cockroach-certs"
assert.Equal(t, equals, cmd)
}
func TestBackup_Command3(t *testing.T) {
timestamp := "test"
databases := []string{"testDb"}
bucketName := "testBucket"
backupName := "testBackup"
cmd := getCommand(timestamp, databases, bucketName, backupName)
equals := "/scripts/restore.sh testBucket testBackup test testDb /secrets/sa.json /cockroach/cockroach-certs"
assert.Equal(t, equals, cmd)
}
func TestBackup_Command4(t *testing.T) {
timestamp := ""
databases := []string{}
bucketName := "test"
backupName := "test"
cmd := getCommand(timestamp, databases, bucketName, backupName)
equals := ""
cmd := getCommand(
timestamp,
bucketName,
backupName,
certPath,
secretPath,
dbURL,
dbPort,
)
equals := "export SAJSON=$(cat /secrets/sa.json | base64 | tr -d '\n' ) && cockroach sql --certs-dir=/cockroach/cockroach-certs --host=testDB2 --port=81 -e \"RESTORE FROM \\\"gs://testBucket/testBackup/test2?AUTH=specified&CREDENTIALS=${SAJSON}\\\";\""
assert.Equal(t, equals, cmd)
}
@@ -46,7 +46,7 @@ func getJob(
SubPath: secretKey,
MountPath: secretPath,
}},
ImagePullPolicy: corev1.PullAlways,
ImagePullPolicy: corev1.PullIfNotPresent,
}},
Volumes: []corev1.Volume{{
Name: internalSecretName,
@@ -61,7 +61,7 @@ func TestBackup_Job1(t *testing.T) {
SubPath: secretKey,
MountPath: secretPath,
}},
ImagePullPolicy: corev1.PullAlways,
ImagePullPolicy: corev1.PullIfNotPresent,
}},
Volumes: []corev1.Volume{{
Name: internalSecretName,
@@ -137,7 +137,7 @@ func TestBackup_Job2(t *testing.T) {
SubPath: secretKey,
MountPath: secretPath,
}},
ImagePullPolicy: corev1.PullAlways,
ImagePullPolicy: corev1.PullIfNotPresent,
}},
Volumes: []corev1.Volume{{
Name: internalSecretName,