@@ -100,7 +100,14 @@ func (r *ReplicaDbCountSpecEnforcer) Enforce() error {
100
100
r .resetInSpecManualFailover ()
101
101
}
102
102
103
- if r .shouldMoreReplicaBeDeployed () {
103
+ if r .isReplicaOperationInProgress () {
104
+ return nil
105
+ }
106
+
107
+ // Check if the number of deployed replicas == spec if not then deploy one
108
+ nbreNewReplicaToDeploy := r .getExpectedNbreReplicasToDeploy () - r .getNbreDeployedReplicas ()
109
+
110
+ if nbreNewReplicaToDeploy > 0 {
104
111
105
112
if r .isAutomaticFailoverDisabled () &&
106
113
! isManualFailoverRequested &&
@@ -110,15 +117,44 @@ func (r *ReplicaDbCountSpecEnforcer) Enforce() error {
110
117
return nil
111
118
}
112
119
113
- return r .deployReplicaStatefulSets ()
120
+ return r .deployReplicaStatefulSet ()
121
+
122
+ } else if nbreNewReplicaToDeploy < 0 {
123
+ replicaToUndeploy := r .getReplicaToUndeploy ()
124
+ return r .undeployReplicaStatefulSets (replicaToUndeploy )
114
125
115
- } else if r .shouldLessReplicaBeDeployed () {
116
- return r .undeployReplicaStatefulSets ()
126
+ } else if nbreNewReplicaToDeploy == 0 {
127
+ for _ , replicaStatefulSet := range r .getDeployedReplicas () {
128
+ if ! replicaStatefulSet .IsReady {
129
+ return r .undeployReplicaStatefulSets (replicaStatefulSet )
130
+ }
131
+ }
117
132
}
118
133
119
134
return nil
120
135
}
121
136
137
+ func (r * ReplicaDbCountSpecEnforcer ) isReplicaOperationInProgress () bool {
138
+ return r .blockingOperation .GetActiveOperation ().OperationId == operation .OperationIdReplicaDbCountSpecEnforcement
139
+ }
140
+
141
+ func (r * ReplicaDbCountSpecEnforcer ) getDeployedReplicas () []statefulset.StatefulSetWrapper {
142
+ return r .resourcesStates .StatefulSets .Replicas .All .GetAllSortedByInstanceIndex ()
143
+ }
144
+
145
+ func (r * ReplicaDbCountSpecEnforcer ) getNbreDeployedReplicas () int32 {
146
+ return r .resourcesStates .StatefulSets .Replicas .NbreDeployed
147
+ }
148
+
149
+ func (r * ReplicaDbCountSpecEnforcer ) getExpectedNbreReplicasToDeploy () int32 {
150
+ expectedNbreToDeploy := r .resourcesStates .StatefulSets .SpecExpectedNbreToDeploy
151
+
152
+ if expectedNbreToDeploy <= 1 {
153
+ return 0
154
+ }
155
+ return expectedNbreToDeploy - 1
156
+ }
157
+
122
158
func (r * ReplicaDbCountSpecEnforcer ) hasLastAttemptTimedOut () bool {
123
159
return r .blockingOperation .HasActiveOperationIdTimedOut (operation .OperationIdReplicaDbCountSpecEnforcement )
124
160
}
@@ -179,10 +215,6 @@ func (r *ReplicaDbCountSpecEnforcer) resetInSpecManualFailover() error {
179
215
return r .kubegresContext .Client .Update (r .kubegresContext .Ctx , r .kubegresContext .Kubegres )
180
216
}
181
217
182
- func (r * ReplicaDbCountSpecEnforcer ) shouldMoreReplicaBeDeployed () bool {
183
- return r .resourcesStates .StatefulSets .ShouldMoreReplicaBeDeployed ()
184
- }
185
-
186
218
func (r * ReplicaDbCountSpecEnforcer ) isPrimaryDbReady () bool {
187
219
return r .resourcesStates .StatefulSets .Primary .IsReady
188
220
}
@@ -207,23 +239,6 @@ func (r *ReplicaDbCountSpecEnforcer) isReplicaDbUndeployed(operation postgresV1.
207
239
return err != nil
208
240
}
209
241
210
- func (r * ReplicaDbCountSpecEnforcer ) deployReplicaStatefulSets () error {
211
-
212
- if ! r .resourcesStates .StatefulSets .ShouldMoreReplicaBeDeployed () {
213
- return nil
214
- }
215
-
216
- numberOfReplicasToDeploy := r .resourcesStates .StatefulSets .GetNbreReplicaToDeploy ()
217
- r .kubegresContext .Log .Info ("We are going to deploy " + strconv .Itoa (int (numberOfReplicasToDeploy )) + " Replica statefulSet(s)" )
218
-
219
- var index int32
220
- for index = 0 ; index < numberOfReplicasToDeploy ; index ++ {
221
- return r .deployReplicaStatefulSet ()
222
- }
223
-
224
- return nil
225
- }
226
-
227
242
func (r * ReplicaDbCountSpecEnforcer ) deployReplicaStatefulSet () error {
228
243
229
244
instanceIndex := r .kubegresContext .Status .GetLastCreatedInstanceIndex () + 1
@@ -268,27 +283,11 @@ func (r *ReplicaDbCountSpecEnforcer) activateBlockingOperationForUndeployment(st
268
283
statefulSetInstanceIndex )
269
284
}
270
285
271
- func (r * ReplicaDbCountSpecEnforcer ) shouldLessReplicaBeDeployed () bool {
272
- return r .resourcesStates .StatefulSets .ShouldLessReplicaBeDeployed ()
273
- }
274
-
275
- func (r * ReplicaDbCountSpecEnforcer ) undeployReplicaStatefulSets () error {
276
-
277
- if ! r .resourcesStates .StatefulSets .ShouldLessReplicaBeDeployed () {
278
- return nil
279
- }
280
-
281
- nbreReplicasToUndeploy := r .resourcesStates .StatefulSets .GetNbreReplicaToUndeploy ()
282
-
283
- if nbreReplicasToUndeploy == 0 {
284
- return nil
285
- }
286
+ func (r * ReplicaDbCountSpecEnforcer ) undeployReplicaStatefulSets (replicaToUndeploy statefulset.StatefulSetWrapper ) error {
286
287
287
- replicasToUndeploy := r .getReplicasReverseSortedByInstanceIndex ()
288
- if len (replicasToUndeploy ) == 0 {
288
+ if replicaToUndeploy .StatefulSet .Name == "" {
289
289
return nil
290
290
}
291
- replicaToUndeploy := replicasToUndeploy [0 ]
292
291
293
292
r .kubegresContext .Log .Info ("We are going to undeploy a Replica statefulSet." , "InstanceIndex" , replicaToUndeploy .InstanceIndex )
294
293
@@ -309,6 +308,17 @@ func (r *ReplicaDbCountSpecEnforcer) undeployReplicaStatefulSets() error {
309
308
return nil
310
309
}
311
310
311
+ func (r * ReplicaDbCountSpecEnforcer ) getReplicaToUndeploy () statefulset.StatefulSetWrapper {
312
+
313
+ replicasToUndeploy := r .getReplicasReverseSortedByInstanceIndex ()
314
+
315
+ if len (replicasToUndeploy ) == 0 {
316
+ return statefulset.StatefulSetWrapper {}
317
+ }
318
+
319
+ return replicasToUndeploy [0 ]
320
+ }
321
+
312
322
func (r * ReplicaDbCountSpecEnforcer ) getReplicasReverseSortedByInstanceIndex () []statefulset.StatefulSetWrapper {
313
323
return r .resourcesStates .StatefulSets .Replicas .All .GetAllReverseSortedByInstanceIndex ()
314
324
}
0 commit comments