-
Notifications
You must be signed in to change notification settings - Fork 586
/
cloneimpl.go
231 lines (192 loc) · 7.64 KB
/
cloneimpl.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
package cloneservice
/*
Copyright 2019 - 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"
"io/ioutil"
"time"
"github.com/crunchydata/postgres-operator/internal/apiserver"
"github.com/crunchydata/postgres-operator/internal/config"
"github.com/crunchydata/postgres-operator/internal/kubeapi"
"github.com/crunchydata/postgres-operator/internal/util"
crv1 "github.com/crunchydata/postgres-operator/pkg/apis/crunchydata.com/v1"
msgs "github.com/crunchydata/postgres-operator/pkg/apiservermsgs"
log "github.com/sirupsen/logrus"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// Clone allows a user to clone a cluster into a new deployment
func Clone(request *msgs.CloneRequest, namespace, pgouser string) msgs.CloneResponse {
log.Debugf("clone called with ")
// set up the response here
response := msgs.CloneResponse{
Status: msgs.Status{
Code: msgs.Ok,
Msg: "",
},
}
log.Debug("Getting pgcluster")
// get the information about the current pgcluster by name, to ensure it
// exists
sourcePgcluster := crv1.Pgcluster{}
_, err := kubeapi.Getpgcluster(apiserver.RESTClient, &sourcePgcluster,
request.SourceClusterName, namespace)
// if there is an error getting the pgcluster, abort here
if err != nil {
response.Status.Code = msgs.Error
response.Status.Msg = fmt.Sprintf("Could not get cluster: %s", err)
return response
}
// validate the parameters of the request that do not require setting
// additional information, so we can avoid additional API lookups
if err := validateCloneRequest(request, sourcePgcluster); err != nil {
response.Status.Code = msgs.Error
response.Status.Msg = err.Error()
return response
}
// check if the current cluster is not upgraded to the deployed
// Operator version. If not, do not allow the command to complete
if sourcePgcluster.Annotations[config.ANNOTATION_IS_UPGRADED] == config.ANNOTATIONS_FALSE {
response.Status.Code = msgs.Error
response.Status.Msg = sourcePgcluster.Name + msgs.UpgradeError
return response
}
// now, let's ensure the target pgCluster does *not* exist
targetPgcluster := crv1.Pgcluster{}
targetPgclusterExists, _ := kubeapi.Getpgcluster(apiserver.RESTClient,
&targetPgcluster, request.TargetClusterName, namespace)
if targetPgclusterExists {
response.Status.Code = msgs.Error
response.Status.Msg = fmt.Sprintf("Could not clone cluster: %s already exists",
request.TargetClusterName)
return response
}
// finally, let's make sure there is not already a task in progress for
// making the clone
selector := fmt.Sprintf("%s=true,pg-cluster=%s", config.LABEL_PGO_CLONE, request.TargetClusterName)
taskList := crv1.PgtaskList{}
if err := kubeapi.GetpgtasksBySelector(apiserver.RESTClient, &taskList, selector, namespace); err != nil {
log.Error(err)
response.Status.Code = msgs.Error
response.Status.Msg = fmt.Sprintf("Could not clone cluster: could not validate %s", err.Error())
return response
}
// iterate through the list of tasks and see if there are any pending
for _, task := range taskList.Items {
if task.Spec.Status != crv1.CompletedStatus {
response.Status.Code = msgs.Error
response.Status.Msg = fmt.Sprintf("Could not clone cluster: there exists an ongoing clone task: [%s]. If you believe this is an error, try deleting this pgtask CRD.", task.Spec.Name)
return response
}
}
// create the workflow task to track how this is progressing
uid := util.RandStringBytesRmndr(4)
workflowID, err := createWorkflowTask(request.TargetClusterName, uid, namespace)
if err != nil {
response.Status.Code = msgs.Error
response.Status.Msg = fmt.Errorf("could not create clone workflow task: %s", err).Error()
return response
}
// alright, begin the create the proper clone task!
cloneTask := util.CloneTask{
BackrestPVCSize: request.BackrestPVCSize,
BackrestStorageSource: request.BackrestStorageSource,
EnableMetrics: request.EnableMetrics,
PGOUser: pgouser,
PVCSize: request.PVCSize,
SourceClusterName: request.SourceClusterName,
TargetClusterName: request.TargetClusterName,
TaskStepLabel: config.LABEL_PGO_CLONE_STEP_1,
TaskType: crv1.PgtaskCloneStep1,
Timestamp: time.Now(),
WorkflowID: workflowID,
}
task := cloneTask.Create()
// create the Pgtask CRD for the clone task
err = kubeapi.Createpgtask(apiserver.RESTClient, task, namespace)
if err != nil {
response.Status.Code = msgs.Error
response.Status.Msg = fmt.Sprintf("Could not create clone task: %s", err)
return response
}
response.TargetClusterName = request.TargetClusterName
response.WorkflowID = workflowID
return response
}
// createWorkflowTask creates the workflow task that is tracked as we attempt
// to clone the cluster
func createWorkflowTask(targetClusterName, uid, namespace string) (string, error) {
// set a random ID for this workflow task
u, err := ioutil.ReadFile("/proc/sys/kernel/random/uuid")
if err != nil {
return "", err
}
id := string(u[:len(u)-1])
// set up the workflow task
taskName := fmt.Sprintf("%s-%s-%s", targetClusterName, uid, crv1.PgtaskWorkflowCloneType)
task := &crv1.Pgtask{
ObjectMeta: meta_v1.ObjectMeta{
Name: taskName,
Labels: map[string]string{
config.LABEL_PG_CLUSTER: targetClusterName,
crv1.PgtaskWorkflowID: id,
},
},
Spec: crv1.PgtaskSpec{
Namespace: namespace,
Name: taskName,
TaskType: crv1.PgtaskWorkflow,
Parameters: map[string]string{
crv1.PgtaskWorkflowSubmittedStatus: time.Now().Format(time.RFC3339),
config.LABEL_PG_CLUSTER: targetClusterName,
crv1.PgtaskWorkflowID: id,
},
},
}
// create the workflow task
err = kubeapi.Createpgtask(apiserver.RESTClient, task, namespace)
if err != nil {
return "", err
}
// return successfully after creating the task
return id, nil
}
// validateCloneRequest validates the input from the create clone request
// that does not set any additional information
func validateCloneRequest(request *msgs.CloneRequest, cluster crv1.Pgcluster) error {
// ensure the cluster name for the source of the clone is set
if request.SourceClusterName == "" {
return errors.New("the source cluster name must be set")
}
// ensure the cluster name for the target of the clone (the new cluster) is
// set
if request.TargetClusterName == "" {
return errors.New("the target cluster name must be set")
}
// if any of the the PVCSizes are set to a customized value, ensure that they
// are recognizable by Kubernetes
// first, the primary/replica PVC size
if err := apiserver.ValidateQuantity(request.PVCSize); err != nil {
return fmt.Errorf(apiserver.ErrMessagePVCSize, request.PVCSize, err.Error())
}
// next, the pgBackRest repo PVC size
if err := apiserver.ValidateQuantity(request.BackrestPVCSize); err != nil {
return fmt.Errorf(apiserver.ErrMessagePVCSize, request.BackrestPVCSize, err.Error())
}
// clone is a form of restore, so validate using ValidateBackrestStorageTypeOnBackupRestore
if err := util.ValidateBackrestStorageTypeOnBackupRestore(request.BackrestStorageSource,
cluster.Spec.UserLabels[config.LABEL_BACKREST_STORAGE_TYPE], true); err != nil {
return err
}
return nil
}