@@ -21,6 +21,7 @@ import (
2121 "errors"
2222 "fmt"
2323 "strings"
24+ "time"
2425
2526 "go.uber.org/zap"
2627
@@ -58,6 +59,12 @@ import (
5859 "github.com/mongodb/mongodb-atlas-kubernetes/pkg/util/kube"
5960)
6061
62+ const (
63+ instancePhaseChangedInAtlas = "InstancePhaseChangedInAtlas"
64+ instancePhaseChangedInAtlasMsg = "db instance phase has changed in Atlas"
65+ updateAnnotationKey = "atlas.mongodb.com/updated-at"
66+ )
67+
6168// MongoDBAtlasInstanceReconciler reconciles a MongoDBAtlasInstance object
6269type MongoDBAtlasInstanceReconciler struct {
6370 Client client.Client
@@ -227,11 +234,17 @@ func (r *MongoDBAtlasInstanceReconciler) reconcileAtlasDeployment(cx context.Con
227234 return ctrl.Result {}, err
228235 }
229236
230- result := setInstanceStatusWithDeploymentInfo (atlasClient , inst , atlasDeployment , instData .ProjectName )
237+ stateChangedInAtlas , result := setInstanceStatusWithDeploymentInfo (atlasClient , inst , atlasDeployment , instData .ProjectName )
231238 if ! result .IsOk () {
239+ if stateChangedInAtlas {
240+ // Update an annotation in the atlas deployment resource to trigger its reconciliation
241+ log .Infof ("Trigger AtlasDeployment reconciliation. Reason: %v" , result .Message ())
242+ _ = r .annotateAtlasDeployment (cx , atlasDeployment )
243+ }
232244 log .Infof ("Error setting instance status: %v" , result .Message ())
233245 return ctrl.Result {}, errors .New (result .Message ())
234246 }
247+
235248 return ctrl.Result {}, nil
236249}
237250
@@ -358,6 +371,16 @@ func (r *MongoDBAtlasInstanceReconciler) getAtlasProjectForCreation(instance *db
358371 }, nil
359372}
360373
374+ func (r * MongoDBAtlasInstanceReconciler ) annotateAtlasDeployment (cx context.Context , atlasDeployment * v1.AtlasDeployment ) error {
375+ annotations := atlasDeployment .GetAnnotations ()
376+ if annotations == nil {
377+ annotations = make (map [string ]string )
378+ }
379+ annotations [updateAnnotationKey ] = time .Now ().Format (time .RFC3339 )
380+ atlasDeployment .SetAnnotations (annotations )
381+ return r .Client .Update (cx , atlasDeployment , & client.UpdateOptions {})
382+ }
383+
361384// getAtlasDeploymentSpec returns the spec for the desired cluster
362385func getAtlasDeploymentSpec (atlasProject * v1.AtlasProject , data * InstanceData ) * v1.AtlasDeploymentSpec {
363386 var providerSettingsSpec * v1.ProviderSettingsSpec
@@ -461,7 +484,7 @@ func instanceMutateFn(atlasProject *v1.AtlasProject, atlasDeployment *v1.AtlasDe
461484 }
462485}
463486
464- func setInstanceStatusWithDeploymentInfo (atlasClient * mongodbatlas.Client , inst * dbaas.MongoDBAtlasInstance , atlasDeployment * v1.AtlasDeployment , project string ) workflow.Result {
487+ func setInstanceStatusWithDeploymentInfo (atlasClient * mongodbatlas.Client , inst * dbaas.MongoDBAtlasInstance , atlasDeployment * v1.AtlasDeployment , project string ) ( bool , workflow.Result ) {
465488 instInfo , result := atlasinventory .GetClusterInfo (atlasClient , project , inst .Spec .Name )
466489 if result .IsOk () {
467490 // Stores the phase info in inst.Status.Phase and remove from instInfo.InstanceInf map
@@ -479,7 +502,12 @@ func setInstanceStatusWithDeploymentInfo(atlasClient *mongodbatlas.Client, inst
479502 if cond .Type == status .DeploymentReadyType {
480503 statusFound = true
481504 if cond .Status == corev1 .ConditionTrue {
482- dbaas .SetInstanceCondition (inst , dbaasv1alpha1 .DBaaSInstanceProviderSyncType , metav1 .ConditionStatus (cond .Status ), "Ready" , cond .Message )
505+ if inst .Status .Phase == dbaasv1alpha1 .InstancePhaseReady {
506+ dbaas .SetInstanceCondition (inst , dbaasv1alpha1 .DBaaSInstanceProviderSyncType , metav1 .ConditionStatus (cond .Status ), "Ready" , cond .Message )
507+ return false , result
508+ }
509+ dbaas .SetInstanceCondition (inst , dbaasv1alpha1 .DBaaSInstanceProviderSyncType , metav1 .ConditionFalse , instancePhaseChangedInAtlas , instancePhaseChangedInAtlasMsg )
510+ return true , result
483511 } else {
484512 if strings .Contains (cond .Message , FreeClusterFailed ) {
485513 inst .Status .Phase = dbaasv1alpha1 .InstancePhaseFailed
@@ -491,6 +519,5 @@ func setInstanceStatusWithDeploymentInfo(atlasClient *mongodbatlas.Client, inst
491519 if ! statusFound {
492520 dbaas .SetInstanceCondition (inst , dbaasv1alpha1 .DBaaSInstanceProviderSyncType , metav1 .ConditionFalse , string (dbaasv1alpha1 .InstancePhasePending ), "Waiting for cluster creation to start" )
493521 }
494-
495- return result
522+ return false , result
496523}
0 commit comments