Skip to content

Commit ddd1057

Browse files
author
Jeff McCormick
committed
fix bug related to replica pvc specification for shared and non-shared volume types
1 parent 114cb8b commit ddd1057

File tree

7 files changed

+180
-16
lines changed

7 files changed

+180
-16
lines changed

client/cmd/cluster.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ func listDeployments(name string) {
9494
log.Error("error getting list of deployments" + err.Error())
9595
return
9696
}
97+
9798
for _, d := range deployments.Items {
9899
fmt.Println(TREE_BRANCH + "deployment : " + d.ObjectMeta.Name)
100+
//fmt.Printf("labels : %v\n", d.ObjectMeta)
99101
}
100102

101103
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
{
2+
"kind": "Deployment",
3+
"apiVersion": "extensions/v1beta1",
4+
"metadata": {
5+
"name": "{{.Name}}",
6+
"labels": {
7+
{{.OPERATOR_LABELS}}
8+
}
9+
},
10+
"spec": {
11+
"replicas": {{.REPLICAS}},
12+
"selector": {
13+
"matchLabels": {
14+
"name": "{{.Name}}"
15+
}
16+
},
17+
"template": {
18+
"metadata": {
19+
"labels": {
20+
"name": "{{.Name}}",
21+
"pg-cluster": "{{.ClusterName}}"
22+
}
23+
},
24+
"spec": {
25+
26+
{{.SECURITY_CONTEXT}}
27+
28+
"containers": [{
29+
"name": "database",
30+
"image": "crunchydata/crunchy-postgres:{{.CCP_IMAGE_TAG}}",
31+
"env": [{
32+
"name": "PG_MASTER_PORT",
33+
"value": "{{.Port}}"
34+
}, {
35+
"name": "PG_MASTER_HOST",
36+
"value": "{{.PG_MASTER_HOST}}"
37+
}, {
38+
"name": "PG_MODE",
39+
"value": "slave"
40+
}, {
41+
"name": "PG_DATABASE",
42+
"value": "{{.PG_DATABASE}}"
43+
}, {
44+
"name": "PGHOST",
45+
"value": "/tmp"
46+
}],
47+
"volumeMounts": [
48+
{
49+
"mountPath": "/pgdata",
50+
"name": "pgdata",
51+
"readOnly": false
52+
}, {
53+
"mountPath": "/pguser",
54+
"name": "pguser-volume"
55+
}, {
56+
"mountPath": "/pgmaster",
57+
"name": "pgmaster-volume"
58+
}, {
59+
"mountPath": "/pgroot",
60+
"name": "pgroot-volume"
61+
}
62+
],
63+
64+
"ports": [{
65+
"containerPort": 5432,
66+
"protocol": "TCP"
67+
}],
68+
"resources": {},
69+
"imagePullPolicy": "IfNotPresent"
70+
}],
71+
"volumes": [
72+
{
73+
"name": "pgdata",
74+
"persistentVolumeClaim": {
75+
"claimName": "{{.PVC_NAME}}"
76+
}
77+
}, {
78+
"name": "pguser-volume",
79+
"secret": {
80+
"secretName": "{{.PGUSER_SECRET_NAME}}"
81+
}
82+
}, {
83+
"name": "pgmaster-volume",
84+
"secret": {
85+
"secretName": "{{.PGMASTER_SECRET_NAME}}"
86+
}
87+
}, {
88+
"name": "pgroot-volume",
89+
"secret": {
90+
"secretName": "{{.PGROOT_SECRET_NAME}}"
91+
}
92+
}
93+
],
94+
95+
"restartPolicy": "Always",
96+
"dnsPolicy": "ClusterFirst"
97+
}
98+
},
99+
"strategy": {
100+
"type": "RollingUpdate",
101+
"rollingUpdate": {
102+
"maxUnavailable": 1,
103+
"maxSurge": 1
104+
}
105+
}
106+
}
107+
}

conf/postgres-operator/cluster/1/cluster-replica-deployment-1.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@
7171
"volumes": [
7272
{
7373
"name": "pgdata",
74-
"persistentVolumeClaim": {
75-
"claimName": "{{.PVC_NAME}}"
76-
}
74+
"emptyDir": {}
7775
}, {
7876
"name": "pguser-volume",
7977
"secret": {

docs/strategies.asciidoc

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,24 @@ image::operator-diagram-cluster.png?raw=true[]
6969
The default cluster strategy creates the following:
7070

7171
* deployment running a Postgres *master* container with replica count of 1
72-
* deployment running a Postgres *replica* container with replica count of 2
72+
* deployment running a Postgres *replica* container with replica count of 0
7373
* service mapped to the *master* Postgres database
7474
* service mapped to the *replica* Postgres database
75-
* PVC for the *master* will be created if not specified in configuration
76-
75+
* PVC for the *master* will be created if not specified in configuration, this
76+
assumes you are using a non-shared volume technology (e.g. Amazon EBS),
77+
if the CLUSTER.PVC_NAME value is set in your configuration then a
78+
shared volume technology is assumed (e.g. HostPath or NFS), if a PVC
79+
is created for the master, the naming convention is *clustername-pvc*
80+
where clustername is the name of your cluster.
81+
82+
Replica(s) will be created based upon either a shared or non-shared
83+
volume type. The selection of the Replica configuration template
84+
is based on the cluster's PVC_NAME value, if this is not set, a non-shared
85+
volume is assumed and the non-shared configuration template is used.
86+
87+
Currently, the example non-shared replica configuration template
88+
uses emptyDir volume types to simulate a dynamic volume type. If you
89+
are wanting replica(s) to persist to a persistent volume type and
90+
not emptyDir, you will need to modify the *cluster-replica-deployment-1.json*
91+
configuration file to specify your volume type specification.
7792

examples/operator/remove-tpr.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash -x
2+
# Copyright 2016 Crunchy Data Solutions, Inc.
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
16+
17+
source $DIR/setup.sh
18+
19+
$CO_CMD delete thirdpartyresources pg-backup.crunchydata.com \
20+
pg-clone.crunchydata.com \
21+
pg-cluster.crunchydata.com \
22+
pg-policy.crunchydata.com \
23+
pg-upgrade.crunchydata.com
24+

operator/cluster/cluster.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434
)
3535

3636
type ClusterStrategy interface {
37-
AddCluster(*kubernetes.Clientset, *rest.RESTClient, *tpr.PgCluster, string) error
37+
AddCluster(*kubernetes.Clientset, *rest.RESTClient, *tpr.PgCluster, string, string) error
3838
DeleteCluster(*kubernetes.Clientset, *rest.RESTClient, *tpr.PgCluster, string) error
3939

4040
MinorUpgrade(*kubernetes.Clientset, *rest.RESTClient, *tpr.PgCluster, *tpr.PgUpgrade, string) error
@@ -134,16 +134,18 @@ func addCluster(clientset *kubernetes.Clientset, client *rest.RESTClient, cl *tp
134134
}
135135

136136
//create the PVC for the master if required
137+
var pvcName = cl.Spec.PVC_NAME
138+
137139
if cl.Spec.PVC_NAME == "" {
138-
cl.Spec.PVC_NAME = cl.Spec.Name + "-pvc"
140+
pvcName = cl.Spec.Name + "-pvc"
139141
log.Debug("PVC_NAME=%s PVC_SIZE=%s PVC_ACCESS_MODE=%s\n",
140-
cl.Spec.PVC_NAME, cl.Spec.PVC_ACCESS_MODE, cl.Spec.PVC_SIZE)
141-
err = pvc.Create(clientset, cl.Spec.PVC_NAME, cl.Spec.PVC_ACCESS_MODE, cl.Spec.PVC_SIZE, namespace)
142+
pvcName, cl.Spec.PVC_ACCESS_MODE, cl.Spec.PVC_SIZE)
143+
err = pvc.Create(clientset, pvcName, cl.Spec.PVC_ACCESS_MODE, cl.Spec.PVC_SIZE, namespace)
142144
if err != nil {
143145
log.Error("error in pvc create " + err.Error())
144146
return
145147
}
146-
log.Info("created PVC =" + cl.Spec.PVC_NAME + " in namespace " + namespace)
148+
log.Info("created PVC =" + pvcName + " in namespace " + namespace)
147149
}
148150
log.Debug("creating PgCluster object strategy is [" + cl.Spec.STRATEGY + "]")
149151

@@ -179,7 +181,7 @@ func addCluster(clientset *kubernetes.Clientset, client *rest.RESTClient, cl *tp
179181

180182
setFullVersion(client, cl, namespace)
181183

182-
strategy.AddCluster(clientset, client, cl, namespace)
184+
strategy.AddCluster(clientset, client, cl, namespace, pvcName)
183185

184186
err = util.Patch(client, "/spec/status", tpr.UPGRADE_COMPLETED_STATUS, "pgclusters", cl.Spec.Name, namespace)
185187
if err != nil {

operator/cluster/cluster_strategy_1.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,18 @@ type ClusterStrategy1 struct{}
4141

4242
var DeploymentTemplate1 *template.Template
4343
var ReplicaDeploymentTemplate1 *template.Template
44+
var ReplicaDeploymentTemplate1Shared *template.Template
4445
var ServiceTemplate1 *template.Template
4546

4647
func init() {
4748

4849
ServiceTemplate1 = util.LoadTemplate("/operator-conf/cluster-service-1.json")
4950
ReplicaDeploymentTemplate1 = util.LoadTemplate("/operator-conf/cluster-replica-deployment-1.json")
51+
ReplicaDeploymentTemplate1Shared = util.LoadTemplate("/operator-conf/cluster-replica-deployment-1-shared.json")
5052
DeploymentTemplate1 = util.LoadTemplate("/operator-conf/cluster-deployment-1.json")
5153
}
5254

53-
func (r ClusterStrategy1) AddCluster(clientset *kubernetes.Clientset, client *rest.RESTClient, cl *tpr.PgCluster, namespace string) error {
55+
func (r ClusterStrategy1) AddCluster(clientset *kubernetes.Clientset, client *rest.RESTClient, cl *tpr.PgCluster, namespace string, masterPvcName string) error {
5456
var serviceDoc, replicaServiceDoc, masterDoc, replicaDoc bytes.Buffer
5557
var err error
5658
var replicaServiceResult, serviceResult *v1.Service
@@ -124,7 +126,7 @@ func (r ClusterStrategy1) AddCluster(clientset *kubernetes.Clientset, client *re
124126
ClusterName: cl.Spec.Name,
125127
Port: cl.Spec.Port,
126128
CCP_IMAGE_TAG: cl.Spec.CCP_IMAGE_TAG,
127-
PVC_NAME: cl.Spec.PVC_NAME,
129+
PVC_NAME: masterPvcName,
128130
OPERATOR_LABELS: util.GetLabels(cl.Spec.Name, cl.Spec.ClusterName, false, false),
129131
BACKUP_PVC_NAME: util.CreateBackupPVCSnippet(cl.Spec.BACKUP_PVC_NAME),
130132
BACKUP_PATH: cl.Spec.BACKUP_PATH,
@@ -179,7 +181,14 @@ func (r ClusterStrategy1) AddCluster(clientset *kubernetes.Clientset, client *re
179181
PGUSER_SECRET_NAME: cl.Spec.PGUSER_SECRET_NAME,
180182
}
181183

182-
err = ReplicaDeploymentTemplate1.Execute(&replicaDoc, replicaDeploymentFields)
184+
if cl.Spec.PVC_NAME == "" {
185+
//if no PVC_NAME then assume a non-shared volume type
186+
log.Debug("using the dynamic replica template ")
187+
err = ReplicaDeploymentTemplate1.Execute(&replicaDoc, replicaDeploymentFields)
188+
} else {
189+
log.Debug("using the shared replica template ")
190+
err = ReplicaDeploymentTemplate1Shared.Execute(&replicaDoc, replicaDeploymentFields)
191+
}
183192
if err != nil {
184193
log.Error(err.Error())
185194
return err
@@ -367,7 +376,14 @@ func (r ClusterStrategy1) PrepareClone(clientset *kubernetes.Clientset, tprclien
367376
PGUSER_SECRET_NAME: cl.Spec.PGUSER_SECRET_NAME,
368377
}
369378

370-
err = ReplicaDeploymentTemplate1.Execute(&replicaDoc, replicaDeploymentFields)
379+
if cl.Spec.PVC_NAME == "" {
380+
//if PVC_NAME is blank, assume a non-shared volume type
381+
log.Debug("using the dynamic replica template ")
382+
err = ReplicaDeploymentTemplate1.Execute(&replicaDoc, replicaDeploymentFields)
383+
} else {
384+
log.Debug("using the shared replica template ")
385+
err = ReplicaDeploymentTemplate1Shared.Execute(&replicaDoc, replicaDeploymentFields)
386+
}
371387
if err != nil {
372388
log.Error("error in clone rep dep tem exec " + err.Error())
373389
return err

0 commit comments

Comments
 (0)