Skip to content

Commit f550d8f

Browse files
author
Jeff McCormick
committed
fix deployment labeling using pgo label command
1 parent ccebe3c commit f550d8f

File tree

4 files changed

+71
-5
lines changed

4 files changed

+71
-5
lines changed

client/cmd/label.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
log "github.com/Sirupsen/logrus"
2121
"k8s.io/apimachinery/pkg/labels"
2222
"k8s.io/client-go/rest"
23+
"os"
2324
//"github.com/crunchydata/postgres-operator/operator/util"
2425
"encoding/json"
2526
"github.com/crunchydata/postgres-operator/tpr"
@@ -38,6 +39,7 @@ import (
3839
)
3940

4041
var LabelCmdLabel string
42+
var LabelMap map[string]string
4143
var DeleteLabel bool
4244

4345
var labelCmd = &cobra.Command{
@@ -60,6 +62,7 @@ pgo label --label=environment=prod --selector=status=final --dry-run
6062
if LabelCmdLabel == "" {
6163
log.Error(`You must specify the label to apply.`)
6264
} else {
65+
validateLabel()
6366
labelClusters(args)
6467
}
6568
},
@@ -156,13 +159,12 @@ func addLabels(items []tpr.PgCluster) {
156159
return
157160
}
158161

159-
newLabels := make(map[string]string)
160162
for _, d := range deployments.Items {
161163
//update Deployment with the label
162164
//fmt.Println(TREE_BRANCH + "deployment : " + d.ObjectMeta.Name)
163165
if DryRun {
164166
} else {
165-
err := updateLabels(&d, Clientset, items[i].Spec.Name, Namespace, newLabels)
167+
err := updateLabels(&d, Clientset, items[i].Spec.Name, Namespace, LabelMap)
166168
if err != nil {
167169
log.Error(err.Error())
168170
}
@@ -176,6 +178,8 @@ func updateLabels(deployment *v1beta1.Deployment, clientset *kubernetes.Clientse
176178

177179
var err error
178180

181+
log.Debugf("%v is the labels to apply\n", newLabels)
182+
179183
var patchBytes, newData, origData []byte
180184
origData, err = json.Marshal(deployment)
181185
if err != nil {
@@ -254,3 +258,17 @@ func PatchPgCluster(tprclient *rest.RESTClient, newLabel string, oldTpr tpr.PgCl
254258
return err6
255259

256260
}
261+
262+
func validateLabel() {
263+
//TODO use the k8s label parser for this validation
264+
LabelMap = make(map[string]string)
265+
userValues := strings.Split(LabelCmdLabel, ",")
266+
for _, v := range userValues {
267+
pair := strings.Split(v, "=")
268+
if len(pair) != 2 {
269+
log.Error("label format incorrect, requires name=value")
270+
os.Exit(2)
271+
}
272+
LabelMap[pair[0]] = pair[1]
273+
}
274+
}

operator/cluster/cluster_strategy_1.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ func (r ClusterStrategy1) AddCluster(clientset *kubernetes.Clientset, client *re
125125
log.Info("master Deployment " + cl.Spec.Name + " in namespace " + namespace + " already existed so not creating it ")
126126
}
127127

128+
err = util.PatchClusterTPR(client, masterLabels, cl, namespace)
129+
if err != nil {
130+
log.Error("could not patch master tpr with labels")
131+
return err
132+
}
133+
128134
newReplicas, err := strconv.Atoi(cl.Spec.REPLICAS)
129135
if err != nil {
130136
log.Error("could not convert REPLICAS config setting")

operator/cluster/upgrade_strategy_1.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ func (r ClusterStrategy1) MinorUpgrade(clientset *kubernetes.Clientset, tprclien
6868

6969
//create the master deployment
7070

71+
masterLabels := getMasterLabels(cl.Spec.Name, cl.Spec.ClusterName, false, false, cl.Spec.UserLabels)
72+
7173
deploymentFields := DeploymentTemplateFields{
7274
Name: cl.Spec.Name,
7375
ClusterName: cl.Spec.Name,
7476
Port: cl.Spec.Port,
7577
CCP_IMAGE_TAG: upgrade.Spec.CCP_IMAGE_TAG,
7678
PVC_NAME: util.CreatePVCSnippet(cl.Spec.MasterStorage.StorageType, cl.Spec.MasterStorage.PvcName),
77-
OPERATOR_LABELS: util.GetLabels(cl.Spec.Name, cl.Spec.ClusterName, false, false),
79+
OPERATOR_LABELS: util.GetLabelsFromMap(masterLabels),
7880
BACKUP_PVC_NAME: util.CreateBackupPVCSnippet(cl.Spec.BACKUP_PVC_NAME),
7981
BACKUP_PATH: cl.Spec.BACKUP_PATH,
8082
PGDATA_PATH_OVERRIDE: cl.Spec.Name,
@@ -187,14 +189,16 @@ func (r ClusterStrategy1) MajorUpgradeFinalize(clientset *kubernetes.Clientset,
187189

188190
log.Info("major cluster upgrade finalize using Strategy 1 in namespace " + namespace)
189191

192+
masterLabels := getMasterLabels(cl.Spec.Name, cl.Spec.ClusterName, false, false, cl.Spec.UserLabels)
193+
190194
//start the master deployment
191195
deploymentFields := DeploymentTemplateFields{
192196
Name: cl.Spec.Name,
193197
ClusterName: cl.Spec.Name,
194198
Port: cl.Spec.Port,
195199
CCP_IMAGE_TAG: upgrade.Spec.CCP_IMAGE_TAG,
196200
PVC_NAME: util.CreatePVCSnippet(cl.Spec.MasterStorage.StorageType, upgrade.Spec.NEW_PVC_NAME),
197-
OPERATOR_LABELS: util.GetLabels(cl.Spec.Name, cl.Spec.ClusterName, false, false),
201+
OPERATOR_LABELS: util.GetLabelsFromMap(masterLabels),
198202
BACKUP_PVC_NAME: util.CreateBackupPVCSnippet(upgrade.Spec.BACKUP_PVC_NAME),
199203
PGDATA_PATH_OVERRIDE: upgrade.Spec.NEW_DATABASE_NAME,
200204
PG_DATABASE: cl.Spec.PG_DATABASE,

operator/util/util.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ import (
2424
"strconv"
2525
"text/template"
2626

27+
"github.com/crunchydata/postgres-operator/tpr"
28+
jsonpatch "github.com/evanphx/json-patch"
29+
2730
"k8s.io/apimachinery/pkg/types"
2831
"k8s.io/client-go/kubernetes"
29-
//extensionsv1beta1 "k8s.io/client-go/kubernetes/typed/extensions/v1beta1"
3032
"k8s.io/client-go/rest"
3133
)
3234

@@ -220,3 +222,39 @@ func GetLabelsFromMap(labels map[string]string) string {
220222
}
221223
return output
222224
}
225+
226+
func PatchClusterTPR(tprclient *rest.RESTClient, labelMap map[string]string, oldTpr *tpr.PgCluster, namespace string) error {
227+
228+
oldData, err := json.Marshal(oldTpr)
229+
if err != nil {
230+
return err
231+
}
232+
if oldTpr.Metadata.Labels == nil {
233+
oldTpr.Metadata.Labels = make(map[string]string)
234+
}
235+
for k, v := range labelMap {
236+
oldTpr.Metadata.Labels[k] = v
237+
}
238+
var newData, patchBytes []byte
239+
newData, err = json.Marshal(oldTpr)
240+
if err != nil {
241+
return err
242+
}
243+
patchBytes, err = jsonpatch.CreateMergePatch(oldData, newData)
244+
if err != nil {
245+
return err
246+
}
247+
248+
log.Debug(string(patchBytes))
249+
250+
_, err6 := tprclient.Patch(types.MergePatchType).
251+
Namespace(namespace).
252+
Resource(tpr.CLUSTER_RESOURCE).
253+
Name(oldTpr.Spec.Name).
254+
Body(patchBytes).
255+
Do().
256+
Get()
257+
258+
return err6
259+
260+
}

0 commit comments

Comments
 (0)