Skip to content

Commit 2ea5c41

Browse files
author
Jeff McCormick
committed
tweeks for strategies code
1 parent ec6193a commit 2ea5c41

File tree

5 files changed

+67
-22
lines changed

5 files changed

+67
-22
lines changed

client/cmd/cluster.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ func getClusterParams(name string) *tpr.PgCluster {
168168
REPLICAS: "2",
169169
FS_GROUP: "",
170170
SUPPLEMENTAL_GROUPS: "",
171+
STRATEGY: "1",
171172
}
172173

173174
//override any values from config file
@@ -215,10 +216,6 @@ func getClusterParams(name string) *tpr.PgCluster {
215216
if str != "" {
216217
spec.PG_ROOT_PASSWORD = str
217218
}
218-
str = viper.GetString("CLUSTER.REPLICAS")
219-
if str != "" {
220-
spec.REPLICAS = str
221-
}
222219
str = viper.GetString("DB.FSGROUP")
223220
if str != "" {
224221
spec.FS_GROUP = str
@@ -227,6 +224,14 @@ func getClusterParams(name string) *tpr.PgCluster {
227224
if str != "" {
228225
spec.SUPPLEMENTAL_GROUPS = str
229226
}
227+
str = viper.GetString("CLUSTER.STRATEGY")
228+
if str != "" {
229+
spec.STRATEGY = str
230+
}
231+
str = viper.GetString("CLUSTER.REPLICAS")
232+
if str != "" {
233+
spec.REPLICAS = str
234+
}
230235

231236
//override from command line
232237

client/cmd/database.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ func getDatabaseParams(name string) *tpr.PgDatabase {
121121
BACKUP_PATH: "",
122122
FS_GROUP: "",
123123
SUPPLEMENTAL_GROUPS: "",
124+
STRATEGY: "1",
124125
}
125126

126127
//override any values from config file
@@ -176,6 +177,10 @@ func getDatabaseParams(name string) *tpr.PgDatabase {
176177
if str != "" {
177178
spec.SUPPLEMENTAL_GROUPS = str
178179
}
180+
str = viper.GetString("DB.STRATEGY")
181+
if str != "" {
182+
spec.STRATEGY = str
183+
}
179184

180185
//pass along command line flags for a restore
181186
spec.BACKUP_PATH = BackupPath

examples/sample.pgo.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ DB:
1313
PVC_ACCESS_MODE: ReadWriteMany
1414
FSGROUP: 26
1515
SUPPLEMENTALGROUPS: 65534
16+
STRATEGY: 1
1617
PGO:
1718
LSPVC_TEMPLATE: /home/jeffmc/.pgo.lspvc-template.json
1819
CO_IMAGE_TAG: centos7-1.0.0
1920
DEBUG: false
2021
CLUSTER:
2122
REPLICAS: 2
22-
STRATEGY: default
23+
STRATEGY: 1

operator/cluster/cluster.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,14 @@ type DeploymentTemplateFields struct {
6363

6464
const REPLICA_SUFFIX = "-replica"
6565

66-
var strategy1 ClusterStrategy
66+
var strategyMap map[string]ClusterStrategy
67+
68+
//var strategy1 ClusterStrategy
6769

6870
func init() {
69-
strategy1 = ClusterStrategy1{}
71+
//strategy1 = ClusterStrategy1{}
72+
strategyMap = make(map[string]ClusterStrategy)
73+
strategyMap["1"] = ClusterStrategy1{}
7074
}
7175

7276
func Process(clientset *kubernetes.Clientset, client *rest.RESTClient, stopchan chan struct{}) {
@@ -118,7 +122,6 @@ func Process(clientset *kubernetes.Clientset, client *rest.RESTClient, stopchan
118122

119123
func addCluster(clientset *kubernetes.Clientset, client *rest.RESTClient, db *tpr.PgCluster) {
120124
var err error
121-
var strategy ClusterStrategy
122125

123126
//create the PVC for the master if required
124127
if db.Spec.PVC_NAME == "" {
@@ -132,26 +135,38 @@ func addCluster(clientset *kubernetes.Clientset, client *rest.RESTClient, db *tp
132135
}
133136
log.Info("created PVC =" + db.Spec.PVC_NAME)
134137
}
135-
log.Debug("creating PgCluster object " + db.Spec.STRATEGY)
138+
log.Debug("creating PgCluster object strategy is [" + db.Spec.STRATEGY + "]")
139+
140+
if db.Spec.STRATEGY == "" {
141+
db.Spec.STRATEGY = "1"
142+
log.Info("using default strategy")
143+
}
136144

137-
if db.Spec.STRATEGY == "1" || db.Spec.STRATEGY == "" {
138-
strategy = strategy1
145+
strategy, ok := strategyMap[db.Spec.STRATEGY]
146+
if ok {
147+
log.Info("strategy found")
139148
} else {
140149
log.Error("invalid STRATEGY requested for cluster creation" + db.Spec.STRATEGY)
150+
return
141151
}
152+
142153
strategy.AddCluster(clientset, client, db)
143154

144155
}
145156

146157
func deleteCluster(clientset *kubernetes.Clientset, client *rest.RESTClient, db *tpr.PgCluster) {
147-
var strategy ClusterStrategy
148158

149159
log.Debug("deleteCluster called with strategy " + db.Spec.STRATEGY)
150160

151-
if db.Spec.STRATEGY == "1" || db.Spec.STRATEGY == "" {
152-
strategy = strategy1
161+
if db.Spec.STRATEGY == "" {
162+
db.Spec.STRATEGY = "1"
163+
}
164+
165+
strategy, ok := strategyMap[db.Spec.STRATEGY]
166+
if ok {
167+
log.Info("strategy found")
153168
} else {
154-
log.Error("invalid STRATEGY requested cluster deletion " + db.Spec.STRATEGY)
169+
log.Error("invalid STRATEGY requested for cluster creation" + db.Spec.STRATEGY)
155170
return
156171
}
157172
strategy.DeleteCluster(clientset, client, db)

operator/database/database.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ type DatabaseStrategy interface {
5656
DeleteDatabase(*kubernetes.Clientset, *rest.RESTClient, *tpr.PgDatabase) error
5757
}
5858

59-
var strategy1 DatabaseStrategy
59+
var strategyMap map[string]DatabaseStrategy
6060

6161
func init() {
62-
strategy1 = DatabaseStrategy1{}
62+
strategyMap = make(map[string]DatabaseStrategy)
63+
strategyMap["1"] = DatabaseStrategy1{}
64+
6365
}
6466

6567
func Process(clientset *kubernetes.Clientset, client *rest.RESTClient, stopchan chan struct{}) {
@@ -127,23 +129,40 @@ func addDatabase(clientset *kubernetes.Clientset, client *rest.RESTClient, db *t
127129

128130
log.Debug("creating PgDatabase object " + db.Spec.STRATEGY)
129131

130-
if db.Spec.STRATEGY == "1" || db.Spec.STRATEGY == "" {
131-
strategy = strategy1
132+
if db.Spec.STRATEGY == "" {
133+
db.Spec.STRATEGY = "1"
134+
log.Info("using default strategy")
135+
}
136+
137+
strategy, ok := strategyMap[db.Spec.STRATEGY]
138+
if ok {
139+
log.Info("strategy found")
140+
132141
} else {
133142
log.Error("invalid STRATEGY requested for Database creation" + db.Spec.STRATEGY)
134143
return
135144
}
145+
136146
strategy.AddDatabase(clientset, client, db)
137147

138148
}
139149

140150
func deleteDatabase(clientset *kubernetes.Clientset, client *rest.RESTClient, db *tpr.PgDatabase) {
141151
log.Debug("deleting PgDatabase object with strategy " + db.Spec.STRATEGY)
142152

143-
if db.Spec.STRATEGY == "1" || db.Spec.STRATEGY == "" {
144-
strategy1.DeleteDatabase(clientset, client, db)
153+
if db.Spec.STRATEGY == "" {
154+
db.Spec.STRATEGY = "1"
155+
log.Info("using default strategy")
156+
}
157+
158+
strategy, ok := strategyMap[db.Spec.STRATEGY]
159+
if ok {
160+
log.Info("strategy found")
145161
} else {
146-
log.Error("invalid STRATEGY requested Database deletion " + db.Spec.STRATEGY)
162+
log.Error("invalid STRATEGY requested for database creation" + db.Spec.STRATEGY)
163+
return
147164
}
148165

166+
strategy.DeleteDatabase(clientset, client, db)
167+
149168
}

0 commit comments

Comments
 (0)