|
| 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