Skip to content

Commit 0772b62

Browse files
author
Jeff McCormick
committed
add policy apply logic for when a new cluster goes Ready
1 parent a76427c commit 0772b62

File tree

5 files changed

+176
-8
lines changed

5 files changed

+176
-8
lines changed

apis/cr/v1/task.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const PgtaskDeleteBackups = "delete-backups"
2626
const PgtaskDeleteData = "delete-data"
2727
const PgtaskFailover = "failover"
2828
const PgtaskAutoFailover = "autofailover"
29+
const PgtaskAddPolicies = "addpolicies"
2930

3031
// PgtaskSpec ...
3132
type PgtaskSpec struct {

apiserver/clusterservice/clusterimpl.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ func CreateCluster(request *msgs.CreateClusterRequest) msgs.CreateClusterRespons
540540

541541
// Create an instance of our CRD
542542
newInstance := getClusterParams(request, clusterName, userLabelsMap)
543-
validateConfigPolicies(request.Policies)
543+
validateConfigPolicies(clusterName, request.Policies)
544544

545545
t := time.Now()
546546
newInstance.Spec.PswLastUpdate = t.Format(time.RFC3339)
@@ -558,18 +558,19 @@ func CreateCluster(request *msgs.CreateClusterRequest) msgs.CreateClusterRespons
558558

559559
}
560560

561-
func validateConfigPolicies(PoliciesFlag string) error {
561+
func validateConfigPolicies(clusterName, PoliciesFlag string) error {
562562
var err error
563563
var configPolicies string
564+
564565
if PoliciesFlag == "" {
565-
log.Println(apiserver.Pgo.Cluster.Policies + " is Pgo.Cluster.Policies")
566+
log.Debug(apiserver.Pgo.Cluster.Policies + " is Pgo.Cluster.Policies")
566567
configPolicies = apiserver.Pgo.Cluster.Policies
567568
} else {
568569
configPolicies = PoliciesFlag
569570
}
571+
570572
if configPolicies == "" {
571-
log.Debug("no policies are specified")
572-
err = errors.New("no policies are specified")
573+
log.Debug("no policies are specified in either pgo.yaml or from user")
573574
return err
574575
}
575576

@@ -590,8 +591,31 @@ func validateConfigPolicies(PoliciesFlag string) error {
590591
log.Error("error getting pgpolicy " + v + err.Error())
591592
return err
592593
}
594+
//create a pgtask to add the policy after the db is ready
595+
}
596+
597+
spec := crv1.PgtaskSpec{}
598+
spec.StorageSpec = crv1.PgStorageSpec{}
599+
spec.TaskType = crv1.PgtaskAddPolicies
600+
spec.Status = "requested"
601+
spec.Parameters = make(map[string]string)
602+
for _, v := range policies {
603+
spec.Parameters[v] = v
604+
}
605+
spec.Name = clusterName + "-policies"
606+
labels := make(map[string]string)
607+
labels[util.LABEL_PG_CLUSTER] = clusterName
608+
609+
newInstance := &crv1.Pgtask{
610+
ObjectMeta: meta_v1.ObjectMeta{
611+
Name: spec.Name,
612+
Labels: labels,
613+
},
614+
Spec: spec,
593615
}
594616

617+
kubeapi.Createpgtask(apiserver.RESTClient, newInstance, apiserver.Namespace)
618+
595619
return err
596620
}
597621

conf/apiserver/pgo.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ Cluster:
1010
Replicas: 0
1111
ArchiveMode: false
1212
ArchiveTimeout: 60
13-
PrimaryStorage: storage3
14-
BackupStorage: storage3
15-
ReplicaStorage: storage3
13+
PrimaryStorage: storage1
14+
BackupStorage: storage1
15+
ReplicaStorage: storage1
1616
Storage:
1717
storage1:
1818
AccessMode: ReadWriteMany

controller/podcontroller.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"context"
2020
log "github.com/Sirupsen/logrus"
2121
clusteroperator "github.com/crunchydata/postgres-operator/operator/cluster"
22+
taskoperator "github.com/crunchydata/postgres-operator/operator/task"
2223
"github.com/crunchydata/postgres-operator/util"
2324
apiv1 "k8s.io/api/core/v1"
2425
"k8s.io/apimachinery/pkg/fields"
@@ -115,4 +116,17 @@ func (c *PodController) checkReadyStatus(oldpod, newpod *apiv1.Pod) {
115116
}
116117
}
117118

119+
//handle applying policies after a database is made Ready
120+
if newpod.ObjectMeta.Labels[util.LABEL_PRIMARY] == "true" {
121+
for _, v := range newpod.Status.ContainerStatuses {
122+
if v.Name == "database" {
123+
//see if there are pgtasks for adding a policy
124+
if v.Ready {
125+
log.Debug(newpod.ObjectMeta.Labels[util.LABEL_PG_CLUSTER] + " went to Ready, apply policies...")
126+
taskoperator.ApplyPolicies(newpod.ObjectMeta.Labels[util.LABEL_PG_CLUSTER], c.PodClientset, c.PodClient)
127+
}
128+
}
129+
}
130+
}
131+
118132
}

operator/task/applypolicies.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package task
2+
3+
/*
4+
Copyright 2017-2018 Crunchy Data Solutions, Inc.
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
import (
19+
"encoding/json"
20+
log "github.com/Sirupsen/logrus"
21+
crv1 "github.com/crunchydata/postgres-operator/apis/cr/v1"
22+
"github.com/crunchydata/postgres-operator/kubeapi"
23+
"github.com/crunchydata/postgres-operator/operator"
24+
"github.com/crunchydata/postgres-operator/operator/cluster"
25+
"github.com/crunchydata/postgres-operator/util"
26+
jsonpatch "github.com/evanphx/json-patch"
27+
"k8s.io/apimachinery/pkg/types"
28+
"k8s.io/client-go/kubernetes"
29+
"k8s.io/client-go/rest"
30+
"strings"
31+
)
32+
33+
// RemoveBackups ...
34+
func ApplyPolicies(clusterName string, Clientset *kubernetes.Clientset, RESTClient *rest.RESTClient) {
35+
36+
taskName := clusterName + "-policies"
37+
task := crv1.Pgtask{}
38+
task.Spec = crv1.PgtaskSpec{}
39+
task.Spec.Name = taskName
40+
41+
found, err := kubeapi.Getpgtask(RESTClient, &task, taskName, operator.NAMESPACE)
42+
if found && err == nil {
43+
//apply those policies
44+
for k, _ := range task.Spec.Parameters {
45+
log.Debug("applying policy %s to %s", k, clusterName)
46+
applyPolicy(Clientset, RESTClient, k, clusterName)
47+
}
48+
//delete the pgtask to not redo this again
49+
kubeapi.Deletepgtask(RESTClient, taskName, operator.NAMESPACE)
50+
}
51+
}
52+
53+
func applyPolicy(clientset *kubernetes.Clientset, restclient *rest.RESTClient, policyName, clusterName string) {
54+
err := util.ExecPolicy(clientset, restclient, operator.NAMESPACE, policyName, clusterName)
55+
if err != nil {
56+
log.Error(err)
57+
return
58+
}
59+
60+
labels := make(map[string]string)
61+
labels[policyName] = "pgpolicy"
62+
63+
//look up the cluster CRD to get the strategy
64+
cl := crv1.Pgcluster{}
65+
_, err = kubeapi.Getpgcluster(restclient, &cl, clusterName, operator.NAMESPACE)
66+
if err != nil {
67+
log.Error(err)
68+
return
69+
70+
}
71+
72+
var strategyMap map[string]cluster.Strategy
73+
strategyMap = make(map[string]cluster.Strategy)
74+
strategyMap["1"] = cluster.Strategy1{}
75+
76+
strategy, ok := strategyMap[cl.Spec.Strategy]
77+
if !ok {
78+
log.Error("invalid Strategy requested for cluster creation" + cl.Spec.Strategy)
79+
return
80+
}
81+
82+
err = strategy.UpdatePolicyLabels(clientset, clusterName, operator.NAMESPACE, labels)
83+
if err != nil {
84+
log.Error(err)
85+
}
86+
87+
//update the pgcluster crd labels with the new policy
88+
err = PatchPgcluster(restclient, policyName+"=pgpolicy", cl)
89+
if err != nil {
90+
log.Error(err)
91+
}
92+
93+
}
94+
95+
func PatchPgcluster(restclient *rest.RESTClient, newLabel string, oldCRD crv1.Pgcluster) error {
96+
97+
fields := strings.Split(newLabel, "=")
98+
labelKey := fields[0]
99+
labelValue := fields[1]
100+
oldData, err := json.Marshal(oldCRD)
101+
if err != nil {
102+
return err
103+
}
104+
if oldCRD.ObjectMeta.Labels == nil {
105+
oldCRD.ObjectMeta.Labels = make(map[string]string)
106+
}
107+
oldCRD.ObjectMeta.Labels[labelKey] = labelValue
108+
var newData, patchBytes []byte
109+
newData, err = json.Marshal(oldCRD)
110+
if err != nil {
111+
return err
112+
}
113+
patchBytes, err = jsonpatch.CreateMergePatch(oldData, newData)
114+
if err != nil {
115+
return err
116+
}
117+
118+
log.Debug(string(patchBytes))
119+
_, err6 := restclient.Patch(types.MergePatchType).
120+
Namespace(operator.NAMESPACE).
121+
Resource(crv1.PgclusterResourcePlural).
122+
Name(oldCRD.Spec.Name).
123+
Body(patchBytes).
124+
Do().
125+
Get()
126+
127+
return err6
128+
129+
}

0 commit comments

Comments
 (0)