-
Notifications
You must be signed in to change notification settings - Fork 586
/
restore.go
269 lines (234 loc) · 9.66 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
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
package backrest
/*
Copyright 2018 - 2020 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 (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/crunchydata/postgres-operator/internal/config"
"github.com/crunchydata/postgres-operator/internal/kubeapi"
crv1 "github.com/crunchydata/postgres-operator/pkg/apis/crunchydata.com/v1"
"github.com/crunchydata/postgres-operator/pkg/events"
pgo "github.com/crunchydata/postgres-operator/pkg/generated/clientset/versioned"
log "github.com/sirupsen/logrus"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
)
// restoreTargetRegex defines a regex to detect if a restore target has been specified
// for pgBackRest using the '--target' option
var restoreTargetRegex = regexp.MustCompile("--target(=| +)")
type BackrestRestoreJobTemplateFields struct {
JobName string
ClusterName string
WorkflowID string
ToClusterPVCName string
SecurityContext string
PGOImagePrefix string
PGOImageTag string
CommandOpts string
PITRTarget string
PgbackrestStanza string
PgbackrestDBPath string
PgbackrestRepo1Path string
PgbackrestRepo1Host string
PgbackrestS3EnvVars string
NodeSelector string
Tablespaces string
TablespaceVolumes string
TablespaceVolumeMounts string
}
// UpdatePGClusterSpecForRestore updates the spec for pgcluster resource provided as need to
// perform a restore
func UpdatePGClusterSpecForRestore(clientset kubeapi.Interface, cluster *crv1.Pgcluster,
task *crv1.Pgtask) {
cluster.Spec.PGDataSource.RestoreFrom = cluster.GetName()
restoreOpts := task.Spec.Parameters[config.LABEL_BACKREST_RESTORE_OPTS]
// set the proper target for the restore job
pitrTarget := task.Spec.Parameters[config.LABEL_BACKREST_PITR_TARGET]
if pitrTarget != "" && !restoreTargetRegex.MatchString(restoreOpts) {
restoreOpts = fmt.Sprintf("%s --target=%s", restoreOpts, strconv.Quote(pitrTarget))
}
// set the proper backrest storage type for the restore job
storageType := task.Spec.Parameters[config.LABEL_BACKREST_STORAGE_TYPE]
if storageType != "" && !strings.Contains(restoreOpts, "--repo-type") {
restoreOpts = fmt.Sprintf("%s --repo-type=%s", restoreOpts, storageType)
}
cluster.Spec.PGDataSource.RestoreOpts = restoreOpts
// set the proper node affinity for the restore job
cluster.Spec.UserLabels[config.LABEL_NODE_LABEL_KEY] =
task.Spec.Parameters[config.LABEL_NODE_LABEL_KEY]
cluster.Spec.UserLabels[config.LABEL_NODE_LABEL_VALUE] =
task.Spec.Parameters[config.LABEL_NODE_LABEL_VALUE]
return
}
// PrepareClusterForRestore prepares a PostgreSQL cluster for a restore. This includes deleting
// variousresources (Deployments, Jobs, PVCs & pgtasks) while also patching various custome
// resources (pgreplicas) as needed to perform a restore.
func PrepareClusterForRestore(clientset kubeapi.Interface, cluster *crv1.Pgcluster,
task *crv1.Pgtask) (*crv1.Pgcluster, error) {
var err error
var patchedCluster *crv1.Pgcluster
namespace := cluster.Namespace
clusterName := cluster.Name
log.Debugf("restore workflow: started for cluster %s", clusterName)
// prepare the pgcluster CR for restore
clusterPatch := fmt.Sprintf(`{"metadata":{"annotations":{"%s":"","%s":"%s"},`+
`"labels":{"%s":"%s"}},"spec":{"status":""},"status":{"message":"%s","state":"%s"}}`,
config.ANNOTATION_BACKREST_RESTORE, config.ANNOTATION_CURRENT_PRIMARY, clusterName,
config.LABEL_DEPLOYMENT_NAME, clusterName, "Cluster is being restored",
crv1.PgclusterStateRestore)
if patchedCluster, err = clientset.CrunchydataV1().Pgclusters(namespace).Patch(clusterName,
types.MergePatchType, []byte(clusterPatch)); err != nil {
log.Errorf("pgtask Controller: " + err.Error())
return nil, err
}
log.Debugf("restore workflow: patched pgcluster %s for restore", clusterName)
// find all pgreplica CR's
replicas, err := clientset.CrunchydataV1().Pgreplicas(namespace).List(metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", config.LABEL_PG_CLUSTER, clusterName),
})
if err != nil {
return nil, err
}
// prepare pgreplica CR's for restore
replicaPatch := fmt.Sprintf(`{"metadata":{"annotations":{"%s":null}},"spec":{"status":""},`+
`"status":{"message":"%s","state":"%s"}}`, config.ANNOTATION_PGHA_BOOTSTRAP_REPLICA,
"Cluster is being restored", crv1.PgclusterStateRestore)
for _, r := range replicas.Items {
if _, err := clientset.CrunchydataV1().Pgreplicas(namespace).Patch(r.GetName(),
types.MergePatchType, []byte(replicaPatch)); err != nil {
return nil, err
}
}
log.Debugf("restore workflow: patched replicas in cluster %s for restore", clusterName)
// find all current pg deployments
pgInstances, err := clientset.AppsV1().Deployments(namespace).List(metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s,%s", config.LABEL_PG_CLUSTER, clusterName,
config.LABEL_PG_DATABASE),
})
if err != nil {
return nil, err
}
// delete all the primary and replica deployments
if err := clientset.AppsV1().Deployments(namespace).DeleteCollection(&metav1.DeleteOptions{},
metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s,%s", config.LABEL_PG_CLUSTER, clusterName,
config.LABEL_PG_DATABASE),
}); err != nil {
return nil, err
}
log.Debugf("restore workflow: deleted primary and replicas %v", pgInstances)
// delete all existing jobs
deletePropagation := metav1.DeletePropagationBackground
if err := clientset.BatchV1().Jobs(namespace).DeleteCollection(
&metav1.DeleteOptions{PropagationPolicy: &deletePropagation},
metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", config.LABEL_PG_CLUSTER, clusterName),
}); err != nil {
return nil, err
}
log.Debugf("restore workflow: deleted all existing jobs for cluster %s", clusterName)
// delete all PostgreSQL PVCs (the primary and all replica PVCs)
for _, deployment := range pgInstances.Items {
err := clientset.
CoreV1().PersistentVolumeClaims(namespace).
Delete(deployment.GetName(), &metav1.DeleteOptions{})
if err != nil && !kerrors.IsNotFound(err) {
return nil, err
}
log.Debugf("restore workflow: deleted primary or replica PVC %s", deployment.GetName())
}
// Wait for all PG PVCs to be removed. If unable to verify that all PVCs have been
// removed, then the restore cannot proceed the function returns.
if err := wait.Poll(time.Second/2, time.Minute*3, func() (bool, error) {
notFound := true
for _, deployment := range pgInstances.Items {
if _, err := clientset.CoreV1().PersistentVolumeClaims(namespace).
Get(deployment.GetName(), metav1.GetOptions{}); err == nil {
notFound = false
}
}
return notFound, nil
}); err != nil {
return nil, err
}
log.Debugf("restore workflow: finished waiting for PVCs for cluster %s to be removed",
clusterName)
// Delete the DCS and leader ConfigMaps. These will be recreated during the restore.
configMaps := []string{fmt.Sprintf("%s-config", clusterName),
fmt.Sprintf("%s-leader", clusterName)}
for _, c := range configMaps {
if err := clientset.CoreV1().ConfigMaps(namespace).Delete(c,
&metav1.DeleteOptions{}); err != nil && !kerrors.IsNotFound(err) {
return nil, err
}
}
log.Debugf("restore workflow: deleted 'config' and 'leader' ConfigMaps for cluster %s",
clusterName)
initPatch := `{"data":{"init":"true"}}`
if _, err := clientset.CoreV1().ConfigMaps(namespace).Patch(fmt.Sprintf("%s-pgha-config",
clusterName), types.MergePatchType,
[]byte(initPatch)); err != nil {
return nil, err
}
log.Debugf("restore workflow: set 'init' flag to 'true' for cluster %s",
clusterName)
return patchedCluster, nil
}
// UpdateWorkflow is responsible for updating the workflow for a restore
func UpdateWorkflow(clientset pgo.Interface, workflowID, namespace, status string) error {
//update workflow
log.Debugf("restore workflow: update workflow %s", workflowID)
selector := crv1.PgtaskWorkflowID + "=" + workflowID
taskList, err := clientset.CrunchydataV1().Pgtasks(namespace).List(metav1.ListOptions{LabelSelector: selector})
if err != nil {
log.Errorf("restore workflow error: could not get workflow %s", workflowID)
return err
}
if len(taskList.Items) != 1 {
log.Errorf("restore workflow error: workflow %s not found", workflowID)
return errors.New("restore workflow error: workflow not found")
}
task := taskList.Items[0]
task.Spec.Parameters[status] = time.Now().Format(time.RFC3339)
_, err = clientset.CrunchydataV1().Pgtasks(namespace).Update(&task)
if err != nil {
log.Errorf("restore workflow error: could not update workflow %s to status %s", workflowID, status)
return err
}
return err
}
// PublishRestore is responsible for publishing the 'RestoreCluster' event for a restore
func PublishRestore(id, clusterName, username, namespace string) {
topics := make([]string, 1)
topics[0] = events.EventTopicCluster
f := events.EventRestoreClusterFormat{
EventHeader: events.EventHeader{
Namespace: namespace,
Username: username,
Topic: topics,
Timestamp: time.Now(),
EventType: events.EventRestoreCluster,
},
Clustername: clusterName,
}
err := events.Publish(f)
if err != nil {
log.Error(err.Error())
}
}