@@ -21,6 +21,7 @@ import (
21
21
"errors"
22
22
"fmt"
23
23
"strings"
24
+ "time"
24
25
25
26
"go.uber.org/zap"
26
27
@@ -58,6 +59,12 @@ import (
58
59
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/util/kube"
59
60
)
60
61
62
+ const (
63
+ instancePhaseChangedInAtlas = "InstancePhaseChangedInAtlas"
64
+ instancePhaseChangedInAtlasMsg = "db instance phase has changed in Atlas"
65
+ updateAnnotationKey = "atlas.mongodb.com/updated-at"
66
+ )
67
+
61
68
// MongoDBAtlasInstanceReconciler reconciles a MongoDBAtlasInstance object
62
69
type MongoDBAtlasInstanceReconciler struct {
63
70
Client client.Client
@@ -227,11 +234,17 @@ func (r *MongoDBAtlasInstanceReconciler) reconcileAtlasDeployment(cx context.Con
227
234
return ctrl.Result {}, err
228
235
}
229
236
230
- result := setInstanceStatusWithDeploymentInfo (atlasClient , inst , atlasDeployment , instData .ProjectName )
237
+ stateChangedInAtlas , result := setInstanceStatusWithDeploymentInfo (atlasClient , inst , atlasDeployment , instData .ProjectName )
231
238
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
+ }
232
244
log .Infof ("Error setting instance status: %v" , result .Message ())
233
245
return ctrl.Result {}, errors .New (result .Message ())
234
246
}
247
+
235
248
return ctrl.Result {}, nil
236
249
}
237
250
@@ -358,6 +371,16 @@ func (r *MongoDBAtlasInstanceReconciler) getAtlasProjectForCreation(instance *db
358
371
}, nil
359
372
}
360
373
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
+
361
384
// getAtlasDeploymentSpec returns the spec for the desired cluster
362
385
func getAtlasDeploymentSpec (atlasProject * v1.AtlasProject , data * InstanceData ) * v1.AtlasDeploymentSpec {
363
386
var providerSettingsSpec * v1.ProviderSettingsSpec
@@ -461,7 +484,7 @@ func instanceMutateFn(atlasProject *v1.AtlasProject, atlasDeployment *v1.AtlasDe
461
484
}
462
485
}
463
486
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 ) {
465
488
instInfo , result := atlasinventory .GetClusterInfo (atlasClient , project , inst .Spec .Name )
466
489
if result .IsOk () {
467
490
// Stores the phase info in inst.Status.Phase and remove from instInfo.InstanceInf map
@@ -479,7 +502,12 @@ func setInstanceStatusWithDeploymentInfo(atlasClient *mongodbatlas.Client, inst
479
502
if cond .Type == status .DeploymentReadyType {
480
503
statusFound = true
481
504
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
483
511
} else {
484
512
if strings .Contains (cond .Message , FreeClusterFailed ) {
485
513
inst .Status .Phase = dbaasv1alpha1 .InstancePhaseFailed
@@ -491,6 +519,5 @@ func setInstanceStatusWithDeploymentInfo(atlasClient *mongodbatlas.Client, inst
491
519
if ! statusFound {
492
520
dbaas .SetInstanceCondition (inst , dbaasv1alpha1 .DBaaSInstanceProviderSyncType , metav1 .ConditionFalse , string (dbaasv1alpha1 .InstancePhasePending ), "Waiting for cluster creation to start" )
493
521
}
494
-
495
- return result
522
+ return false , result
496
523
}
0 commit comments