@@ -25,6 +25,7 @@ import (
25
25
"k8s.io/apimachinery/pkg/runtime"
26
26
"k8s.io/apimachinery/pkg/runtime/schema"
27
27
"k8s.io/apimachinery/pkg/types"
28
+ k8sClient "sigs.k8s.io/controller-runtime/pkg/client"
28
29
"sigs.k8s.io/controller-runtime/pkg/controller"
29
30
"sigs.k8s.io/controller-runtime/pkg/handler"
30
31
"sigs.k8s.io/controller-runtime/pkg/manager"
@@ -130,7 +131,6 @@ func (r *ReplicaSetReconciler) Reconcile(request reconcile.Request) (reconcile.R
130
131
return reconcile.Result {}, err
131
132
}
132
133
133
- // TODO: Read current automation config version from config map
134
134
if err := r .ensureAutomationConfig (mdb ); err != nil {
135
135
r .log .Infof ("error creating automation config config map: %s" , err )
136
136
return reconcile.Result {}, err
@@ -265,24 +265,36 @@ func (r ReplicaSetReconciler) ensureAutomationConfig(mdb mdbv1.MongoDB) error {
265
265
if err != nil {
266
266
return err
267
267
}
268
+
268
269
if err := r .client .CreateOrUpdate (& cm ); err != nil {
269
270
return err
270
271
}
271
272
return nil
272
273
}
273
274
274
- func buildAutomationConfig (mdb mdbv1.MongoDB , mdbVersionConfig automationconfig.MongoDbVersionConfig ) automationconfig.AutomationConfig {
275
+ func buildAutomationConfig (mdb mdbv1.MongoDB , mdbVersionConfig automationconfig.MongoDbVersionConfig , client mdbClient. Client ) ( automationconfig.AutomationConfig , error ) {
275
276
domain := getDomain (mdb .ServiceName (), mdb .Namespace , "" )
276
- return automationconfig .NewBuilder ().
277
+
278
+ currentAc , err := getCurrentAutomationConfig (client , mdb )
279
+ if err != nil {
280
+ return automationconfig.AutomationConfig {}, err
281
+ }
282
+ newAc , err := automationconfig .NewBuilder ().
277
283
SetTopology (automationconfig .ReplicaSetTopology ).
278
284
SetName (mdb .Name ).
279
285
SetDomain (domain ).
280
286
SetMembers (mdb .Spec .Members ).
287
+ SetPreviousAutomationConfig (currentAc ).
281
288
SetMongoDBVersion (mdb .Spec .Version ).
282
- SetAutomationConfigVersion (1 ). // TODO: Correctly set the version
283
289
SetFCV (mdb .GetFCV ()).
284
290
AddVersion (mdbVersionConfig ).
285
291
Build ()
292
+
293
+ if err != nil {
294
+ return automationconfig.AutomationConfig {}, err
295
+ }
296
+
297
+ return newAc , nil
286
298
}
287
299
288
300
func readVersionManifestFromDisk () (automationconfig.VersionManifest , error ) {
@@ -318,12 +330,30 @@ func buildService(mdb mdbv1.MongoDB) corev1.Service {
318
330
Build ()
319
331
}
320
332
333
+ func getCurrentAutomationConfig (client mdbClient.Client , mdb mdbv1.MongoDB ) (automationconfig.AutomationConfig , error ) {
334
+ currentCm := corev1.ConfigMap {}
335
+ currentAc := automationconfig.AutomationConfig {}
336
+ if err := client .Get (context .TODO (), types.NamespacedName {Name : mdb .ConfigMapName (), Namespace : mdb .Namespace }, & currentCm ); err != nil {
337
+ // If the AC was not found we don't surface it as an error
338
+ return automationconfig.AutomationConfig {}, k8sClient .IgnoreNotFound (err )
339
+
340
+ }
341
+ if err := json .Unmarshal ([]byte (currentCm .Data [AutomationConfigKey ]), & currentAc ); err != nil {
342
+ return automationconfig.AutomationConfig {}, err
343
+ }
344
+ return currentAc , nil
345
+ }
346
+
321
347
func (r ReplicaSetReconciler ) buildAutomationConfigConfigMap (mdb mdbv1.MongoDB ) (corev1.ConfigMap , error ) {
322
348
manifest , err := r .manifestProvider ()
323
349
if err != nil {
324
350
return corev1.ConfigMap {}, fmt .Errorf ("error reading version manifest from disk: %+v" , err )
325
351
}
326
- ac := buildAutomationConfig (mdb , manifest .BuildsForVersion (mdb .Spec .Version ))
352
+
353
+ ac , err := buildAutomationConfig (mdb , manifest .BuildsForVersion (mdb .Spec .Version ), r .client )
354
+ if err != nil {
355
+ return corev1.ConfigMap {}, err
356
+ }
327
357
acBytes , err := json .Marshal (ac )
328
358
if err != nil {
329
359
return corev1.ConfigMap {}, err
0 commit comments