Skip to content

Commit b5c1e1f

Browse files
author
Jeff McCormick
committed
fix auto replica creation for new clusters
1 parent e9c671c commit b5c1e1f

File tree

2 files changed

+56
-42
lines changed

2 files changed

+56
-42
lines changed

operator/cluster/cluster.go

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -300,23 +300,35 @@ func AddUpgrade(clientset *kubernetes.Clientset, client *rest.RESTClient, upgrad
300300

301301
func updateCluster(clientset *kubernetes.Clientset, client *rest.RESTClient, cl *tpr.PgCluster, oldcluster *tpr.PgCluster, namespace string) {
302302

303+
log.Debug("updateCluster on pgcluster called..something changed")
304+
303305
if oldcluster.Spec.REPLICAS != cl.Spec.REPLICAS {
304306
log.Debug("detected change to REPLICAS for " + cl.Spec.Name + " from " + oldcluster.Spec.REPLICAS + " to " + cl.Spec.REPLICAS)
305-
ScaleReplicas(clientset, client, cl, oldcluster, namespace)
307+
oldCount, err := strconv.Atoi(oldcluster.Spec.REPLICAS)
308+
if err != nil {
309+
log.Error(err)
310+
return
311+
}
312+
newCount, err := strconv.Atoi(cl.Spec.REPLICAS)
313+
if err != nil {
314+
log.Error(err)
315+
return
316+
}
317+
if oldCount > newCount {
318+
log.Error("scale down is not implemented yet")
319+
return
320+
}
321+
newReps := newCount - oldCount
322+
if newReps > 0 {
323+
ScaleReplicas(clientset, cl, newReps, namespace)
324+
} else {
325+
log.Error("scale to the same number does nothing")
326+
}
306327
}
307328

308329
}
309330

310-
func ScaleReplicas(clientset *kubernetes.Clientset, client *rest.RESTClient, cl *tpr.PgCluster, oldcluster *tpr.PgCluster, namespace string) {
311-
312-
oldCount, err := strconv.Atoi(oldcluster.Spec.REPLICAS)
313-
if err != nil {
314-
log.Error(err)
315-
}
316-
newCount, err := strconv.Atoi(cl.Spec.REPLICAS)
317-
if err != nil {
318-
log.Error(err)
319-
}
331+
func ScaleReplicas(clientset *kubernetes.Clientset, cl *tpr.PgCluster, newReplicas int, namespace string) {
320332

321333
//get the strategy to use
322334
if cl.Spec.STRATEGY == "" {
@@ -332,39 +344,33 @@ func ScaleReplicas(clientset *kubernetes.Clientset, client *rest.RESTClient, cl
332344
return
333345
}
334346

335-
if oldCount > newCount {
336-
log.Debug("scale down not implemented yet")
337-
} else {
338-
//scale up
339-
log.Debug("scale up called ")
340-
newReplicas := newCount - oldCount
341-
342-
for i := 0; i < newReplicas; i++ {
343-
//generate a unique name suffix
344-
uniqueName := RandStringBytesRmndr(4)
345-
depName := cl.Spec.Name + "-replica-" + uniqueName
346-
347-
//create a PVC
348-
pvcName, err := createPVC(clientset, depName, &cl.Spec.ReplicaStorage, namespace)
349-
if err != nil {
350-
log.Error(err)
351-
return
352-
}
353-
//create a Deployment and its service
354-
serviceName := depName + "-replica"
355-
replicaServiceFields := ServiceTemplateFields{
356-
Name: serviceName,
357-
ClusterName: cl.Spec.Name,
358-
Port: cl.Spec.Port,
359-
}
347+
log.Debug("scale up called ")
360348

361-
err = CreateService(clientset, &replicaServiceFields, namespace)
362-
if err != nil {
363-
log.Error(err)
364-
return
365-
}
366-
strategy.CreateReplica(serviceName, clientset, cl, depName, pvcName, namespace, false)
349+
for i := 0; i < newReplicas; i++ {
350+
//generate a unique name suffix
351+
uniqueName := RandStringBytesRmndr(4)
352+
depName := cl.Spec.Name + "-replica-" + uniqueName
353+
354+
//create a PVC
355+
pvcName, err := createPVC(clientset, depName, &cl.Spec.ReplicaStorage, namespace)
356+
if err != nil {
357+
log.Error(err)
358+
return
359+
}
360+
//create a Deployment and its service
361+
serviceName := depName + "-replica"
362+
replicaServiceFields := ServiceTemplateFields{
363+
Name: serviceName,
364+
ClusterName: cl.Spec.Name,
365+
Port: cl.Spec.Port,
366+
}
367+
368+
err = CreateService(clientset, &replicaServiceFields, namespace)
369+
if err != nil {
370+
log.Error(err)
371+
return
367372
}
373+
strategy.CreateReplica(serviceName, clientset, cl, depName, pvcName, namespace, false)
368374
}
369375
}
370376

operator/cluster/cluster_strategy_1.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
kerrors "k8s.io/client-go/pkg/api/errors"
3131
"k8s.io/client-go/pkg/api/meta"
3232
"k8s.io/client-go/pkg/api/v1"
33+
"strconv"
3334

3435
"k8s.io/client-go/pkg/apis/extensions/v1beta1"
3536
"k8s.io/client-go/rest"
@@ -118,6 +119,13 @@ func (r ClusterStrategy1) AddCluster(clientset *kubernetes.Clientset, client *re
118119
log.Info("master Deployment " + cl.Spec.Name + " in namespace " + namespace + " already existed so not creating it ")
119120
}
120121

122+
newReplicas, err := strconv.Atoi(cl.Spec.REPLICAS)
123+
if err != nil {
124+
log.Error("could not convert REPLICAS config setting")
125+
} else {
126+
ScaleReplicas(clientset, cl, newReplicas, namespace)
127+
}
128+
121129
return err
122130

123131
}

0 commit comments

Comments
 (0)