@@ -29,7 +29,7 @@ func (r *AtlasDeploymentReconciler) ensureAdvancedDeploymentState(ctx *workflow.
2929 return advancedDeployment , workflow .Terminate (workflow .DeploymentNotCreatedInAtlas , err .Error ())
3030 }
3131
32- advancedDeployment , err = advancedDeploymentSpec .AdvancedDeployment ()
32+ advancedDeployment , err = advancedDeploymentSpec .ToAtlas ()
3333 if err != nil {
3434 return advancedDeployment , workflow .Terminate (workflow .Internal , err .Error ())
3535 }
@@ -58,73 +58,87 @@ func (r *AtlasDeploymentReconciler) ensureAdvancedDeploymentState(ctx *workflow.
5858 }
5959}
6060
61- func advancedDeploymentIdle (ctx * workflow.Context , project * mdbv1.AtlasProject , deployment * mdbv1.AtlasDeployment , advancedDeployment * mongodbatlas.AdvancedCluster ) (* mongodbatlas.AdvancedCluster , workflow.Result ) {
62- resultingDeployment , err := MergedAdvancedDeployment (* advancedDeployment , deployment .Spec )
61+ func advancedDeploymentIdle (ctx * workflow.Context , project * mdbv1.AtlasProject , deployment * mdbv1.AtlasDeployment , atlasDeploymentAsAtlas * mongodbatlas.AdvancedCluster ) (* mongodbatlas.AdvancedCluster , workflow.Result ) {
62+ specDeployment , atlasDeployment , err := MergedAdvancedDeployment (* atlasDeploymentAsAtlas , * deployment .Spec . AdvancedDeploymentSpec )
6363 if err != nil {
64- return advancedDeployment , workflow .Terminate (workflow .Internal , err .Error ())
64+ return atlasDeploymentAsAtlas , workflow .Terminate (workflow .Internal , err .Error ())
6565 }
6666
67- if done := AdvancedDeploymentsEqual (ctx .Log , * advancedDeployment , resultingDeployment ); done {
68- return advancedDeployment , workflow .OK ()
67+ if areEqual := AdvancedDeploymentsEqual (ctx .Log , specDeployment , atlasDeployment ); areEqual {
68+ return atlasDeploymentAsAtlas , workflow .OK ()
6969 }
7070
71- if deployment . Spec . AdvancedDeploymentSpec .Paused != nil {
72- if advancedDeployment .Paused == nil || * advancedDeployment .Paused != * deployment . Spec . AdvancedDeploymentSpec .Paused {
71+ if specDeployment .Paused != nil {
72+ if atlasDeployment .Paused == nil || * atlasDeployment .Paused != * specDeployment .Paused {
7373 // paused is different from Atlas
7474 // we need to first send a special (un)pause request before reconciling everything else
75- resultingDeployment = mongodbatlas. AdvancedCluster {
75+ specDeployment = mdbv1. AdvancedDeploymentSpec {
7676 Paused : deployment .Spec .AdvancedDeploymentSpec .Paused ,
7777 }
7878 } else {
7979 // otherwise, don't send the paused field
80- resultingDeployment .Paused = nil
80+ specDeployment .Paused = nil
8181 }
8282 }
8383
84- resultingDeployment = cleanupAdvancedDeployment (resultingDeployment )
84+ deploymentAsAtlas , err := cleanupTheSpec (specDeployment ).ToAtlas ()
85+ if err != nil {
86+ return atlasDeploymentAsAtlas , workflow .Terminate (workflow .Internal , err .Error ())
87+ }
8588
86- advancedDeployment , _ , err = ctx .Client .AdvancedClusters .Update (context .Background (), project .Status .ID , deployment .Spec .AdvancedDeploymentSpec .Name , & resultingDeployment )
89+ atlasDeploymentAsAtlas , _ , err = ctx .Client .AdvancedClusters .Update (context .Background (), project .Status .ID , deployment .Spec .AdvancedDeploymentSpec .Name , deploymentAsAtlas )
8790 if err != nil {
88- return advancedDeployment , workflow .Terminate (workflow .DeploymentNotUpdatedInAtlas , err .Error ())
91+ return atlasDeploymentAsAtlas , workflow .Terminate (workflow .DeploymentNotUpdatedInAtlas , err .Error ())
8992 }
9093
9194 return nil , workflow .InProgress (workflow .DeploymentUpdating , "deployment is updating" )
9295}
9396
94- func cleanupAdvancedDeployment (deployment mongodbatlas.AdvancedCluster ) mongodbatlas.AdvancedCluster {
95- deployment .ID = ""
97+ func cleanupTheSpec (deployment mdbv1.AdvancedDeploymentSpec ) * mdbv1.AdvancedDeploymentSpec {
9698 deployment .MongoDBVersion = ""
97- deployment .StateName = ""
98- deployment .ConnectionStrings = nil
99- return deployment
99+ return & deployment
100100}
101101
102102// MergedAdvancedDeployment will return the result of merging AtlasDeploymentSpec with Atlas Advanced Deployment
103- func MergedAdvancedDeployment (advancedDeployment mongodbatlas.AdvancedCluster , spec mdbv1.AtlasDeploymentSpec ) (mongodbatlas. AdvancedCluster , error ) {
104- result := mongodbatlas. AdvancedCluster {}
105- if err := compat . JSONCopy ( & result , advancedDeployment ); err != nil {
106- return result , err
103+ func MergedAdvancedDeployment (atlasDeploymentAsAtlas mongodbatlas.AdvancedCluster , specDeployment mdbv1.AdvancedDeploymentSpec ) (mergedDeployment mdbv1. AdvancedDeploymentSpec , atlasDeployment mdbv1. AdvancedDeploymentSpec , err error ) {
104+ atlasDeployment , err = AdvancedDeploymentFromAtlas ( atlasDeploymentAsAtlas )
105+ if err != nil {
106+ return
107107 }
108108
109- if err := compat .JSONCopy (& result , spec .AdvancedDeploymentSpec ); err != nil {
110- return result , err
109+ mergedDeployment = mdbv1.AdvancedDeploymentSpec {}
110+ if err = compat .JSONCopy (& mergedDeployment , atlasDeployment ); err != nil {
111+ return
112+ }
113+
114+ if err = compat .JSONCopy (& mergedDeployment , specDeployment ); err != nil {
115+ return
111116 }
112117
113- for i , replicationSpec := range advancedDeployment .ReplicationSpecs {
118+ for i , replicationSpec := range atlasDeployment .ReplicationSpecs {
114119 for k , v := range replicationSpec .RegionConfigs {
115120 // the response does not return backing provider names in some situations.
116121 // if this is the case, we want to strip these fields so they do not cause a bad comparison.
117122 if v .BackingProviderName == "" {
118- result .ReplicationSpecs [i ].RegionConfigs [k ].BackingProviderName = ""
123+ mergedDeployment .ReplicationSpecs [i ].RegionConfigs [k ].BackingProviderName = ""
119124 }
120125 }
121126 }
127+ return
128+ }
129+
130+ func AdvancedDeploymentFromAtlas (advancedDeployment mongodbatlas.AdvancedCluster ) (mdbv1.AdvancedDeploymentSpec , error ) {
131+ result := mdbv1.AdvancedDeploymentSpec {}
132+ if err := compat .JSONCopy (& result , advancedDeployment ); err != nil {
133+ return result , err
134+ }
135+
122136 return result , nil
123137}
124138
125139// AdvancedDeploymentsEqual compares two Atlas Advanced Deployments
126- func AdvancedDeploymentsEqual (log * zap.SugaredLogger , deploymentAtlas mongodbatlas. AdvancedCluster , deploymentOperator mongodbatlas. AdvancedCluster ) bool {
127- d := cmp .Diff (deploymentAtlas , deploymentOperator , cmpopts .EquateEmpty ())
140+ func AdvancedDeploymentsEqual (log * zap.SugaredLogger , deploymentAtlas mdbv1. AdvancedDeploymentSpec , deploymentOperator mdbv1. AdvancedDeploymentSpec ) bool {
141+ d := cmp .Diff (deploymentOperator , deploymentAtlas , cmpopts .EquateEmpty ())
128142 if d != "" {
129143 log .Debugf ("Deployments are different: %s" , d )
130144 }
0 commit comments