-
Notifications
You must be signed in to change notification settings - Fork 575
/
policyimpl.go
278 lines (231 loc) · 7.99 KB
/
policyimpl.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
270
271
272
273
274
275
276
277
278
package policyservice
/*
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 (
"fmt"
"strings"
"time"
"github.com/crunchydata/postgres-operator/internal/apiserver"
"github.com/crunchydata/postgres-operator/internal/apiserver/labelservice"
"github.com/crunchydata/postgres-operator/internal/config"
"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"
"github.com/crunchydata/postgres-operator/pkg/events"
pgo "github.com/crunchydata/postgres-operator/pkg/generated/clientset/versioned"
log "github.com/sirupsen/logrus"
v1 "k8s.io/api/apps/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// CreatePolicy ...
func CreatePolicy(client pgo.Interface, policyName, policyURL, policyFile, ns, pgouser string) (bool, error) {
log.Debugf("create policy called for %s", policyName)
// Create an instance of our CRD
spec := crv1.PgpolicySpec{}
spec.Namespace = ns
spec.Name = policyName
spec.URL = policyURL
spec.SQL = policyFile
myLabels := make(map[string]string)
myLabels[config.LABEL_PGOUSER] = pgouser
newInstance := &crv1.Pgpolicy{
ObjectMeta: metav1.ObjectMeta{
Name: policyName,
Labels: myLabels,
},
Spec: spec,
}
_, err := client.CrunchydataV1().Pgpolicies(ns).Create(newInstance)
if kerrors.IsAlreadyExists(err) {
log.Debugf("pgpolicy %s was found so we will not create it", policyName)
return true, nil
}
return false, err
}
// ShowPolicy ...
func ShowPolicy(client pgo.Interface, name string, allflags bool, ns string) crv1.PgpolicyList {
policyList := crv1.PgpolicyList{}
if allflags {
//get a list of all policies
list, err := client.CrunchydataV1().Pgpolicies(ns).List(metav1.ListOptions{})
if list != nil && err == nil {
policyList = *list
}
} else {
policy, err := client.CrunchydataV1().Pgpolicies(ns).Get(name, metav1.GetOptions{})
if policy != nil && err == nil {
policyList.Items = []crv1.Pgpolicy{*policy}
}
}
return policyList
}
// DeletePolicy ...
func DeletePolicy(client pgo.Interface, policyName, ns, pgouser string) msgs.DeletePolicyResponse {
resp := msgs.DeletePolicyResponse{}
resp.Status.Code = msgs.Ok
resp.Status.Msg = ""
resp.Results = make([]string, 0)
policyList, err := client.CrunchydataV1().Pgpolicies(ns).List(metav1.ListOptions{})
if err != nil {
resp.Status.Code = msgs.Error
resp.Status.Msg = err.Error()
return resp
}
policyFound := false
log.Debugf("deleting policy %s", policyName)
for _, policy := range policyList.Items {
if policyName == "all" || policyName == policy.Spec.Name {
//update pgpolicy with current pgouser so that
//we can create an event holding the pgouser
//that deleted the policy
policy.ObjectMeta.Labels[config.LABEL_PGOUSER] = pgouser
_, err = client.CrunchydataV1().Pgpolicies(ns).Update(&policy)
//ok, now delete the pgpolicy
policyFound = true
err = client.CrunchydataV1().Pgpolicies(ns).Delete(policy.Spec.Name, &metav1.DeleteOptions{})
if err == nil {
msg := "deleted policy " + policy.Spec.Name
log.Debug(msg)
resp.Results = append(resp.Results, msg)
} else {
resp.Status.Code = msgs.Error
resp.Status.Msg = err.Error()
return resp
}
}
}
if !policyFound {
log.Debugf("policy %s not found", policyName)
resp.Status.Code = msgs.Error
resp.Status.Msg = "policy " + policyName + " not found"
return resp
}
return resp
}
// ApplyPolicy ...
// pgo apply mypolicy --selector=name=mycluster
func ApplyPolicy(request *msgs.ApplyPolicyRequest, ns, pgouser string) msgs.ApplyPolicyResponse {
var err error
resp := msgs.ApplyPolicyResponse{}
resp.Name = make([]string, 0)
resp.Status.Msg = ""
resp.Status.Code = msgs.Ok
//validate policy
err = util.ValidatePolicy(apiserver.Clientset, ns, request.Name)
if err != nil {
resp.Status.Code = msgs.Error
resp.Status.Msg = "policy " + request.Name + " is not found, cancelling request"
return resp
}
//get filtered list of Deployments
selector := request.Selector
log.Debugf("apply policy selector string=[%s]", selector)
//get a list of all clusters
clusterList, err := apiserver.Clientset.
CrunchydataV1().Pgclusters(ns).
List(metav1.ListOptions{LabelSelector: selector})
if err != nil {
resp.Status.Code = msgs.Error
resp.Status.Msg = err.Error()
return resp
}
log.Debugf("apply policy clusters found len is %d", len(clusterList.Items))
// Return an error if any clusters identified for the policy are in standby mode. Standby
// clusters are in read-only mode, and therefore cannot have policies applied 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 allDeployments []v1.Deployment
for _, c := range clusterList.Items {
depSelector := config.LABEL_SERVICE_NAME + "=" + c.Name
deployments, err := apiserver.Clientset.
AppsV1().Deployments(ns).
List(metav1.ListOptions{LabelSelector: depSelector})
if err != nil {
resp.Status.Code = msgs.Error
resp.Status.Msg = err.Error()
return resp
}
if len(deployments.Items) < 1 {
log.Errorf("%s did not have a deployment for some reason", c.Name)
} else {
allDeployments = append(allDeployments, deployments.Items[0])
}
}
if request.DryRun {
for _, d := range allDeployments {
log.Debugf("deployment : %s", d.ObjectMeta.Name)
resp.Name = append(resp.Name, d.ObjectMeta.Name)
}
return resp
}
labels := make(map[string]string)
labels[request.Name] = "pgpolicy"
for _, d := range allDeployments {
if d.ObjectMeta.Labels[config.LABEL_SERVICE_NAME] != d.ObjectMeta.Labels[config.LABEL_PG_CLUSTER] {
log.Debugf("skipping apply policy on deployment %s", d.Name)
continue
//skip non primary deployments
}
log.Debugf("apply policy %s on deployment %s based on selector %s", request.Name, d.ObjectMeta.Name, selector)
cl, err := apiserver.Clientset.
CrunchydataV1().Pgclusters(ns).
Get(d.ObjectMeta.Labels[config.LABEL_SERVICE_NAME], metav1.GetOptions{})
if err != nil {
resp.Status.Code = msgs.Error
resp.Status.Msg = err.Error()
return resp
}
if err := util.ExecPolicy(apiserver.Clientset, apiserver.RESTConfig,
ns, request.Name, d.ObjectMeta.Labels[config.LABEL_SERVICE_NAME], cl.Spec.Port); err != nil {
log.Error(err)
resp.Status.Code = msgs.Error
resp.Status.Msg = err.Error()
return resp
}
err = util.UpdatePolicyLabels(apiserver.Clientset, d.ObjectMeta.Name, ns, labels)
if err != nil {
log.Error(err)
}
//update the pgcluster crd labels with the new policy
err = labelservice.PatchPgcluster(map[string]string{request.Name: config.LABEL_PGPOLICY}, *cl, ns)
if err != nil {
log.Error(err)
}
resp.Name = append(resp.Name, d.ObjectMeta.Name)
//publish event
topics := make([]string, 1)
topics[0] = events.EventTopicPolicy
f := events.EventApplyPolicyFormat{
EventHeader: events.EventHeader{
Namespace: ns,
Username: pgouser,
Topic: topics,
Timestamp: time.Now(),
EventType: events.EventApplyPolicy,
},
Clustername: d.ObjectMeta.Labels[config.LABEL_PG_CLUSTER],
Policyname: request.Name,
}
err = events.Publish(f)
if err != nil {
log.Error(err.Error())
}
}
return resp
}