Skip to content

Commit c38d616

Browse files
author
jmccormick2001
committed
add --replica-count flag and logic for pgo create cluster, update docs
1 parent 2270ed6 commit c38d616

File tree

6 files changed

+87
-16
lines changed

6 files changed

+87
-16
lines changed

apiserver/clusterservice/clusterimpl.go

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,12 @@ func CreateCluster(request *msgs.CreateClusterRequest) msgs.CreateClusterRespons
448448
return resp
449449
}
450450

451+
if request.ReplicaCount < 0 {
452+
resp.Status.Code = msgs.Error
453+
resp.Status.Msg = "invalid replica-count , should be greater than or equal to 0"
454+
return resp
455+
}
456+
451457
errs := validation.IsDNS1035Label(clusterName)
452458
if len(errs) > 0 {
453459
resp.Status.Code = msgs.Error
@@ -740,7 +746,7 @@ func getClusterParams(request *msgs.CreateClusterRequest, name string, userLabel
740746
if request.ContainerResources != "" {
741747
spec.ContainerResources, _ = apiserver.Pgo.GetContainerResource(request.ContainerResources)
742748
} else {
743-
log.Println(apiserver.Pgo.DefaultContainerResources + " is Pgo.DefaultContainerResources")
749+
log.Debugf("Pgo.DefaultContainerResources is %s", apiserver.Pgo.DefaultContainerResources)
744750
defaultContainerResource := apiserver.Pgo.DefaultContainerResources
745751
if defaultContainerResource != "" {
746752
spec.ContainerResources, _ = apiserver.Pgo.GetContainerResource(defaultContainerResource)
@@ -750,19 +756,19 @@ func getClusterParams(request *msgs.CreateClusterRequest, name string, userLabel
750756
if request.StorageConfig != "" {
751757
spec.PrimaryStorage, _ = apiserver.Pgo.GetStorageSpec(request.StorageConfig)
752758
} else {
753-
log.Printf("%v", apiserver.Pgo.PrimaryStorage)
759+
log.Debugf("%v", apiserver.Pgo.PrimaryStorage)
754760
spec.PrimaryStorage, _ = apiserver.Pgo.GetStorageSpec(apiserver.Pgo.PrimaryStorage)
755761
}
756762

757763
if request.ReplicaStorageConfig != "" {
758764
spec.ReplicaStorage, _ = apiserver.Pgo.GetStorageSpec(request.ReplicaStorageConfig)
759765
} else {
760766
spec.ReplicaStorage, _ = apiserver.Pgo.GetStorageSpec(apiserver.Pgo.ReplicaStorage)
761-
log.Printf("%v", apiserver.Pgo.ReplicaStorage)
767+
log.Debugf("%v", apiserver.Pgo.ReplicaStorage)
762768
}
763769

764770
spec.CCPImageTag = apiserver.Pgo.Cluster.CCPImageTag
765-
log.Println(apiserver.Pgo.Cluster.CCPImageTag + " is Pgo.Cluster.CCPImageTag")
771+
log.Debugf("Pgo.Cluster.CCPImageTag %s", apiserver.Pgo.Cluster.CCPImageTag)
766772
if request.CCPImageTag != "" {
767773
spec.CCPImageTag = request.CCPImageTag
768774
log.Debugf("using CCPImageTag from command line %s", request.CCPImageTag)
@@ -777,7 +783,7 @@ func getClusterParams(request *msgs.CreateClusterRequest, name string, userLabel
777783
spec.PrimaryHost = name
778784
if request.Policies == "" {
779785
spec.Policies = apiserver.Pgo.Cluster.Policies
780-
log.Println(apiserver.Pgo.Cluster.Policies + " is Pgo.Cluster.Policies")
786+
log.Debugf("Pgo.Cluster.Policies %s", apiserver.Pgo.Cluster.Policies)
781787
} else {
782788
spec.Policies = request.Policies
783789
}
@@ -788,36 +794,41 @@ func getClusterParams(request *msgs.CreateClusterRequest, name string, userLabel
788794
spec.Database = "userdb"
789795
spec.RootPassword = request.Password
790796
spec.Replicas = "0"
797+
str := apiserver.Pgo.Cluster.Replicas
798+
log.Debugf("%s is the pgo.yaml replicas setting", apiserver.Pgo.Cluster.Replicas)
799+
if str != "" {
800+
spec.Replicas = str
801+
}
802+
log.Debugf("replica count is %d", request.ReplicaCount)
803+
if request.ReplicaCount > 0 {
804+
spec.Replicas = strconv.Itoa(request.ReplicaCount)
805+
log.Debugf("replicas is %s", spec.Replicas)
806+
}
791807
spec.Strategy = "1"
792808
spec.UserLabels = userLabelsMap
793809
spec.UserLabels[util.LABEL_PGO_VERSION] = msgs.PGO_VERSION
794810

795811
//override any values from config file
796-
str := apiserver.Pgo.Cluster.Port
797-
log.Printf("%d", apiserver.Pgo.Cluster.Port)
812+
str = apiserver.Pgo.Cluster.Port
813+
log.Debugf("%d", apiserver.Pgo.Cluster.Port)
798814
if str != "" {
799815
spec.Port = str
800816
}
801817
str = apiserver.Pgo.Cluster.User
802-
log.Println(apiserver.Pgo.Cluster.User + " is Pgo.Cluster.User")
818+
log.Debugf("Pgo.Cluster.User is %s", apiserver.Pgo.Cluster.User)
803819
if str != "" {
804820
spec.User = str
805821
}
806822
str = apiserver.Pgo.Cluster.Database
807-
log.Println(apiserver.Pgo.Cluster.Database + " is Pgo.Cluster.Database")
823+
log.Debugf("Pgo.Cluster.Database is %s", apiserver.Pgo.Cluster.Database)
808824
if str != "" {
809825
spec.Database = str
810826
}
811827
str = apiserver.Pgo.Cluster.Strategy
812-
log.Printf("%d", apiserver.Pgo.Cluster.Strategy)
828+
log.Debugf("%d", apiserver.Pgo.Cluster.Strategy)
813829
if str != "" {
814830
spec.Strategy = str
815831
}
816-
str = apiserver.Pgo.Cluster.Replicas
817-
log.Printf("%d", apiserver.Pgo.Cluster.Replicas)
818-
if str != "" {
819-
spec.Replicas = str
820-
}
821832
//pass along command line flags for a restore
822833
if request.SecretFrom != "" {
823834
spec.SecretFrom = request.SecretFrom

apiservermsgs/clustermsgs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type CreateClusterRequest struct {
3232
Policies string
3333
CCPImageTag string
3434
Series int
35+
ReplicaCount int
3536
ServiceType string
3637
MetricsFlag bool
3738
BadgerFlag bool

hugo/content/getting-started/_index.adoc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ The name of a pgpool secret to use for the pgpool configuration.
168168
|`--policies` |N/A |String |
169169
The policies to apply when creating a cluster, comma separated.
170170

171+
|`--replica-count` |N/A |Int |
172+
The number of replicas to create as part of this cluster. After a cluster is created, you can also add replicas using the scale command.
173+
171174
|`--replica-storage-config` |N/A |String |
172175
The name of a Storage config in pgo.yaml to use for the cluster replica storage.
173176

@@ -193,6 +196,11 @@ Create a single cluster:
193196
pgo create cluster mycluster
194197
....
195198

199+
Create a single cluster with a single replica:
200+
....
201+
pgo create cluster mycluster --replica-count=1
202+
....
203+
196204
===== Complex Creation
197205

198206
Create a series of clusters, specifying it as the xray project, with the xrayapp and

operator/cluster/cluster.go

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ import (
2626
"github.com/crunchydata/postgres-operator/operator/pvc"
2727
"github.com/crunchydata/postgres-operator/util"
2828
kerrors "k8s.io/apimachinery/pkg/api/errors"
29+
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2930
"k8s.io/client-go/kubernetes"
31+
3032
"k8s.io/client-go/rest"
31-
//"strconv"
33+
"strconv"
3234
)
3335

3436
// Strategy ....
@@ -224,6 +226,53 @@ func AddClusterBase(clientset *kubernetes.Clientset, client *rest.RESTClient, cl
224226
AddPgbouncer(clientset, cl, namespace, true)
225227
}
226228

229+
//add replicas if requested
230+
if cl.Spec.Replicas != "" {
231+
replicaCount, err := strconv.Atoi(cl.Spec.Replicas)
232+
if err != nil {
233+
log.Error("error in replicas value " + err.Error())
234+
return
235+
}
236+
//create a CRD for each replica
237+
for i := 0; i < replicaCount; i++ {
238+
spec := crv1.PgreplicaSpec{}
239+
//get the resource config
240+
//get the storage config
241+
spec.ReplicaStorage, _ = operator.Pgo.GetStorageSpec(operator.Pgo.ReplicaStorage)
242+
243+
spec.UserLabels = cl.Spec.UserLabels
244+
labels := make(map[string]string)
245+
labels[util.LABEL_PG_CLUSTER] = cl.Spec.Name
246+
247+
spec.ClusterName = cl.Spec.Name
248+
uniqueName := util.RandStringBytesRmndr(4)
249+
labels[util.LABEL_NAME] = cl.Spec.Name + "-" + uniqueName
250+
spec.Name = labels[util.LABEL_NAME]
251+
newInstance := &crv1.Pgreplica{
252+
ObjectMeta: meta_v1.ObjectMeta{
253+
Name: labels[util.LABEL_NAME],
254+
Labels: labels,
255+
},
256+
Spec: spec,
257+
Status: crv1.PgreplicaStatus{
258+
State: crv1.PgreplicaStateCreated,
259+
Message: "Created, not processed yet",
260+
},
261+
}
262+
result := crv1.Pgreplica{}
263+
264+
err = client.Post().
265+
Resource(crv1.PgreplicaResourcePlural).
266+
Namespace(namespace).
267+
Body(newInstance).
268+
Do().Into(&result)
269+
if err != nil {
270+
log.Error(" in creating Pgreplica instance" + err.Error())
271+
}
272+
273+
}
274+
}
275+
227276
}
228277

229278
// DeleteClusterBase ...

pgo/cmd/cluster.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ func createCluster(args []string) {
169169

170170
r := new(msgs.CreateClusterRequest)
171171
r.Name = args[0]
172+
r.ReplicaCount = ReplicaCount
172173
r.NodeLabel = NodeLabel
173174
r.Password = Password
174175
r.SecretFrom = SecretFrom

pgo/cmd/create.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ func init() {
245245
createClusterCmd.Flags().StringVarP(&PoliciesFlag, "policies", "z", "", "The policies to apply when creating a cluster, comma separated.")
246246
createClusterCmd.Flags().StringVarP(&CCPImageTag, "ccp-image-tag", "c", "", "The CCPImageTag to use for cluster creation. If specified, overrides the pgo.yaml setting.")
247247
createClusterCmd.Flags().IntVarP(&Series, "series", "e", 1, "The number of clusters to create in a series.")
248+
createClusterCmd.Flags().IntVarP(&ReplicaCount, "replica-count", "", 0, "The number of replicas to create as part of the cluster.")
248249
createClusterCmd.Flags().StringVarP(&ContainerResources, "resources-config", "r", "", "The name of a container resource configuration in pgo.yaml that holds CPU and memory requests and limits.")
249250

250251
createIngestCmd.Flags().StringVarP(&IngestConfig, "ingest-config", "i", "", "Defines the path of an ingest configuration file.")

0 commit comments

Comments
 (0)