/
loadimpl.go
265 lines (223 loc) · 7.58 KB
/
loadimpl.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
package loadservice
/*
Copyright 2017 - 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 (
"bytes"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
crv1 "github.com/crunchydata/postgres-operator/apis/crunchydata.com/v1"
"github.com/crunchydata/postgres-operator/apiserver"
"github.com/crunchydata/postgres-operator/apiserver/policyservice"
msgs "github.com/crunchydata/postgres-operator/apiservermsgs"
"github.com/crunchydata/postgres-operator/config"
"github.com/crunchydata/postgres-operator/events"
"github.com/crunchydata/postgres-operator/kubeapi"
"github.com/crunchydata/postgres-operator/operator"
log "github.com/sirupsen/logrus"
v1batch "k8s.io/api/batch/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/rand"
)
type loadJobTemplateFields struct {
Name string
PGOImagePrefix string
PGOImageTag string
DbHost string
DbDatabase string
DbUser string
DbPass string
DbPort string
TableToLoad string
FilePath string
FileType string
PVCName string
SecurityContext string
PGUserSecret string
}
// LoadConfig ...
//var LoadConfig string
// Load ...
// pgo load --policies=jsonload --selector=name=mycluster --load-config=./sample-load-config.json
func Load(request *msgs.LoadRequest, ns, pgouser string) msgs.LoadResponse {
var err error
resp := msgs.LoadResponse{}
resp.Status.Code = msgs.Ok
resp.Results = make([]string, 0)
resp.Status.Msg = ""
LoadConfigTemplate := loadJobTemplateFields{}
var LoadCfg LoadConfig
_, err = LoadCfg.getConf(bytes.NewBufferString(request.LoadConfig))
if err != nil {
resp.Status.Code = msgs.Error
resp.Status.Msg = err.Error()
return resp
}
err = LoadCfg.validate()
if err != nil {
resp.Status.Code = msgs.Error
resp.Status.Msg = err.Error()
return resp
}
supplementalGroups := []int64{}
supplementalGroup, _ := strconv.Atoi(LoadCfg.SupplementalGroup)
if supplementalGroup > 0 {
supplementalGroups = append(supplementalGroups, int64(supplementalGroup))
}
LoadConfigTemplate.PGOImagePrefix = LoadCfg.PGOImagePrefix
LoadConfigTemplate.PGOImageTag = LoadCfg.PGOImageTag
LoadConfigTemplate.DbDatabase = LoadCfg.DbDatabase
LoadConfigTemplate.DbUser = LoadCfg.DbUser
LoadConfigTemplate.DbPort = LoadCfg.DbPort
LoadConfigTemplate.TableToLoad = LoadCfg.TableToLoad
LoadConfigTemplate.FilePath = LoadCfg.FilePath
LoadConfigTemplate.FileType = LoadCfg.FileType
LoadConfigTemplate.PVCName = LoadCfg.PVCName
LoadConfigTemplate.SecurityContext = operator.GetPodSecurityContext(supplementalGroups)
clusterList := crv1.PgclusterList{}
if len(request.Args) == 0 && request.Selector == "" {
resp.Status.Code = msgs.Error
resp.Status.Msg = "args or --selector required"
return resp
}
if request.Selector != "" {
_, err := labels.Parse(request.Selector)
if err != nil {
log.Error("could not parse selector flag")
resp.Status.Code = msgs.Error
resp.Status.Msg = err.Error()
return resp
}
//get the clusters list
err = kubeapi.GetpgclustersBySelector(apiserver.RESTClient,
&clusterList, request.Selector, ns)
if err != nil {
resp.Status.Code = msgs.Error
resp.Status.Msg = err.Error()
return resp
}
if len(clusterList.Items) == 0 {
log.Debug("no clusters found")
}
} else {
for i := 0; i < len(request.Args); i++ {
cl := crv1.Pgcluster{}
found, err := kubeapi.Getpgcluster(apiserver.RESTClient, &cl, request.Args[i], ns)
if !found {
resp.Status.Code = msgs.Error
resp.Status.Msg = err.Error()
return resp
}
// check if the current cluster is not upgraded to the deployed
// Operator version. If not, do not allow the command to complete
if cl.Annotations[config.ANNOTATION_IS_UPGRADED] == config.ANNOTATIONS_FALSE {
resp.Status.Code = msgs.Error
resp.Status.Msg = cl.Name + msgs.UpgradeError
return resp
}
clusterList.Items = append(clusterList.Items, cl)
}
}
var policies []string
if request.Policies != "" {
policies = strings.Split(request.Policies, ",")
}
log.Debugf("policies to apply before loading are %v len=%d", request.Policies, len(policies))
// Return an error if any clusters identified for the load are in standby mode. Standby
// clusters are in read-only mode as they replicate from a remote primary, and therefore
// cannot have data loaded into to them until standby mode has been disabled.
if hasStandby, standbyClusters := apiserver.PGClusterListHasStandby(clusterList); hasStandby {
resp.Status.Code = msgs.Error
resp.Status.Msg = fmt.Sprintf("Request rejected, unable to load clusters "+
"%s: %s.", strings.Join(standbyClusters, ","), apiserver.ErrStandbyNotAllowed.Error())
return resp
}
var jobName string
for _, c := range clusterList.Items {
for _, p := range policies {
log.Debugf("applying policy %s to %s", p, c.Name)
//apply policies to this cluster
applyReq := msgs.ApplyPolicyRequest{}
applyReq.Name = p
applyReq.Namespace = ns
applyReq.DryRun = false
applyReq.Selector = "name=" + c.Name
applyResp := policyservice.ApplyPolicy(&applyReq, ns, pgouser)
if applyResp.Status.Code != msgs.Ok {
log.Error("error in applying policy " + applyResp.Status.Msg)
resp.Status.Code = msgs.Error
resp.Status.Msg = err.Error()
return resp
}
}
//create the load job for this cluster
log.Debugf("creating load job for %s", c.Name)
jobName, err = createJob(c.Name, &LoadConfigTemplate, ns)
if err != nil {
resp.Status.Code = msgs.Error
resp.Status.Msg = err.Error()
return resp
}
//publish event for Load
topics := make([]string, 1)
topics[0] = events.EventTopicLoad
f := events.EventLoadFormat{
EventHeader: events.EventHeader{
Namespace: ns,
Username: pgouser,
Topic: topics,
Timestamp: time.Now(),
EventType: events.EventLoad,
},
Clustername: c.Name,
Loadconfig: LoadCfg.TableToLoad,
}
err = events.Publish(f)
if err != nil {
log.Error(err.Error())
}
resp.Results = append(resp.Results, "created Job "+jobName)
}
log.Debugf("on return load results is %v", resp.Results)
return resp
}
func createJob(clusterName string, template *loadJobTemplateFields, ns string) (string, error) {
var err error
// generate the name for the load job. Substituted out the legacy entropy
// method to use the one that Kubernetes provides
template.Name = "pgo-load-" + clusterName + "-" + rand.String(3)
template.DbHost = clusterName
template.PGUserSecret = clusterName + crv1.RootSecretSuffix
var doc2 bytes.Buffer
err = config.LoadTemplate.Execute(&doc2, template)
if err != nil {
log.Error(err.Error())
return "", err
}
jobDocString := doc2.String()
log.Debug(jobDocString)
newjob := v1batch.Job{}
err = json.Unmarshal(doc2.Bytes(), &newjob)
if err != nil {
log.Error("error unmarshalling json into Job " + err.Error())
return "", err
}
// set the container image to an override value, if one exists
operator.SetContainerImageOverride(config.CONTAINER_IMAGE_PGO_LOAD,
&newjob.Spec.Template.Spec.Containers[0])
var jobName string
jobName, err = kubeapi.CreateJob(apiserver.Clientset, &newjob, ns)
return jobName, err
}