@@ -25,6 +25,7 @@ import (
2525 "k8s.io/apimachinery/pkg/runtime"
2626 "k8s.io/apimachinery/pkg/runtime/schema"
2727 "k8s.io/apimachinery/pkg/types"
28+ k8sClient "sigs.k8s.io/controller-runtime/pkg/client"
2829 "sigs.k8s.io/controller-runtime/pkg/controller"
2930 "sigs.k8s.io/controller-runtime/pkg/handler"
3031 "sigs.k8s.io/controller-runtime/pkg/manager"
@@ -130,7 +131,6 @@ func (r *ReplicaSetReconciler) Reconcile(request reconcile.Request) (reconcile.R
130131 return reconcile.Result {}, err
131132 }
132133
133- // TODO: Read current automation config version from config map
134134 if err := r .ensureAutomationConfig (mdb ); err != nil {
135135 r .log .Infof ("error creating automation config config map: %s" , err )
136136 return reconcile.Result {}, err
@@ -265,24 +265,36 @@ func (r ReplicaSetReconciler) ensureAutomationConfig(mdb mdbv1.MongoDB) error {
265265 if err != nil {
266266 return err
267267 }
268+
268269 if err := r .client .CreateOrUpdate (& cm ); err != nil {
269270 return err
270271 }
271272 return nil
272273}
273274
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 ) {
275276 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 ().
277283 SetTopology (automationconfig .ReplicaSetTopology ).
278284 SetName (mdb .Name ).
279285 SetDomain (domain ).
280286 SetMembers (mdb .Spec .Members ).
287+ SetPreviousAutomationConfig (currentAc ).
281288 SetMongoDBVersion (mdb .Spec .Version ).
282- SetAutomationConfigVersion (1 ). // TODO: Correctly set the version
283289 SetFCV (mdb .GetFCV ()).
284290 AddVersion (mdbVersionConfig ).
285291 Build ()
292+
293+ if err != nil {
294+ return automationconfig.AutomationConfig {}, err
295+ }
296+
297+ return newAc , nil
286298}
287299
288300func readVersionManifestFromDisk () (automationconfig.VersionManifest , error ) {
@@ -318,12 +330,30 @@ func buildService(mdb mdbv1.MongoDB) corev1.Service {
318330 Build ()
319331}
320332
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+
321347func (r ReplicaSetReconciler ) buildAutomationConfigConfigMap (mdb mdbv1.MongoDB ) (corev1.ConfigMap , error ) {
322348 manifest , err := r .manifestProvider ()
323349 if err != nil {
324350 return corev1.ConfigMap {}, fmt .Errorf ("error reading version manifest from disk: %+v" , err )
325351 }
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+ }
327357 acBytes , err := json .Marshal (ac )
328358 if err != nil {
329359 return corev1.ConfigMap {}, err
0 commit comments