forked from CrunchyData/postgres-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restore.go
127 lines (106 loc) · 4.35 KB
/
restore.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
package pgdump
/*
Copyright 2019 - 2022 Crunchy Data Solutions, Inc.
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.
*/
import (
"bytes"
"encoding/json"
"fmt"
"os"
"github.com/crunchydata/postgres-operator/internal/config"
"github.com/crunchydata/postgres-operator/internal/kubeapi"
"github.com/crunchydata/postgres-operator/internal/operator"
"github.com/crunchydata/postgres-operator/internal/operator/pvc"
"github.com/crunchydata/postgres-operator/internal/util"
crv1 "github.com/crunchydata/postgres-operator/pkg/apis/crunchydata.com/v1"
log "github.com/sirupsen/logrus"
v1batch "k8s.io/api/batch/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type restorejobTemplateFields struct {
JobName string
TaskName string
ClusterName string
SecurityContext string
FromClusterPVCName string
PgRestoreHost string
PgRestoreDB string
PgRestoreUserSecret string
PgPrimaryPort string
PGRestoreOpts string
PITRTarget string
CCPImagePrefix string
CCPImageTag string
PgPort string
NodeSelector string
}
// Restore ...
func Restore(namespace string, clientset kubeapi.Interface, task *crv1.Pgtask) {
log.Infof(" PgDump Restore not implemented %s, %s", namespace, task.Name)
clusterName := task.Spec.Parameters[config.LABEL_PGRESTORE_FROM_CLUSTER]
fromPvcName := task.Spec.Parameters[config.LABEL_PGRESTORE_FROM_PVC]
if !(len(fromPvcName) > 0) || !pvc.Exists(clientset, fromPvcName, namespace) {
log.Errorf("pgrestore: could not find source pvc required for restore: %s", fromPvcName)
return
}
cluster, err := clientset.CrunchydataV1().Pgclusters(namespace).Get(clusterName, metav1.GetOptions{})
if err != nil {
log.Errorf("pgrestore: could not find a pgcluster in Restore Workflow for %s", clusterName)
return
}
//use the storage config from the primary PostgreSQL cluster
storage := cluster.Spec.PrimaryStorage
taskName := task.Name
jobFields := restorejobTemplateFields{
JobName: fmt.Sprintf("pgrestore-%s-%s", task.Spec.Parameters[config.LABEL_PGRESTORE_FROM_CLUSTER],
util.RandStringBytesRmndr(4)),
TaskName: taskName,
ClusterName: clusterName,
SecurityContext: operator.GetPodSecurityContext(storage.GetSupplementalGroups()),
FromClusterPVCName: fromPvcName,
PgRestoreHost: task.Spec.Parameters[config.LABEL_PGRESTORE_HOST],
PgRestoreDB: task.Spec.Parameters[config.LABEL_PGRESTORE_DB],
PgRestoreUserSecret: task.Spec.Parameters[config.LABEL_PGRESTORE_USER],
PgPrimaryPort: operator.Pgo.Cluster.Port,
PGRestoreOpts: task.Spec.Parameters[config.LABEL_PGRESTORE_OPTS],
PITRTarget: task.Spec.Parameters[config.LABEL_PGRESTORE_PITR_TARGET],
CCPImagePrefix: util.GetValueOrDefault(cluster.Spec.CCPImagePrefix, operator.Pgo.Cluster.CCPImagePrefix),
CCPImageTag: operator.Pgo.Cluster.CCPImageTag,
NodeSelector: operator.GetAffinity(task.Spec.Parameters["NodeLabelKey"], task.Spec.Parameters["NodeLabelValue"], "In"),
}
var doc2 bytes.Buffer
err = config.PgRestoreJobTemplate.Execute(&doc2, jobFields)
if err != nil {
log.Error(err.Error())
log.Error("restore workflow: error executing job template")
return
}
if operator.CRUNCHY_DEBUG {
config.PgRestoreJobTemplate.Execute(os.Stdout, jobFields)
}
newjob := v1batch.Job{}
err = json.Unmarshal(doc2.Bytes(), &newjob)
if err != nil {
log.Error("restore workflow: error unmarshalling json into Job " + err.Error())
return
}
// set the container image to an override value, if one exists
operator.SetContainerImageOverride(config.CONTAINER_IMAGE_CRUNCHY_PGRESTORE,
&newjob.Spec.Template.Spec.Containers[0])
j, err := clientset.BatchV1().Jobs(namespace).Create(&newjob)
if err != nil {
log.Error(err)
log.Error("restore workflow: error in creating restore job")
return
}
log.Debugf("pgrestore job %s created", j.Name)
}