Skip to content

Commit 6883723

Browse files
CLOUDP-118355: Shared cluster upgrade ability (#533)
Added shared cluster upgrade support
1 parent 033a006 commit 6883723

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

pkg/controller/atlasdeployment/deployment.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func ensureClusterState(ctx *workflow.Context, project *mdbv1.AtlasProject, clus
4646

4747
switch atlasCluster.StateName {
4848
case "IDLE":
49+
4950
return regularClusterIdle(ctx, project, cluster, atlasCluster)
5051
case "CREATING":
5152
return atlasCluster, workflow.InProgress(workflow.ClusterCreating, "cluster is provisioning")
@@ -85,6 +86,15 @@ func regularClusterIdle(ctx *workflow.Context, project *mdbv1.AtlasProject, clus
8586

8687
resultingCluster = cleanupCluster(resultingCluster)
8788

89+
// Handle shared (M0,M2,M5) cluster to non-shared cluster upgrade
90+
scheduled, err := handleSharedClusterUpgrade(ctx, atlasCluster, &resultingCluster)
91+
if err != nil {
92+
return atlasCluster, workflow.Terminate(workflow.Internal, err.Error())
93+
}
94+
if scheduled {
95+
return atlasCluster, workflow.InProgress(workflow.ClusterUpdating, "cluster is upgrading")
96+
}
97+
8898
atlasCluster, _, err = ctx.Client.Clusters.Update(context.Background(), project.Status.ID, cluster.Spec.DeploymentSpec.Name, &resultingCluster)
8999
if err != nil {
90100
return atlasCluster, workflow.Terminate(workflow.ClusterNotUpdatedInAtlas, err.Error())
@@ -241,3 +251,46 @@ func (r *AtlasDeploymentReconciler) ensureConnectionSecrets(ctx *workflow.Contex
241251

242252
return workflow.OK()
243253
}
254+
255+
func handleSharedClusterUpgrade(ctx *workflow.Context, current *mongodbatlas.Cluster, new *mongodbatlas.Cluster) (scheduled bool, _ error) {
256+
baseErr := "can not perform cluster upgrade. ERR: %v"
257+
if !clusterShouldBeUpgraded(current, new) {
258+
ctx.Log.Debug("cluster shouldn't be upgraded")
259+
return false, nil
260+
}
261+
262+
// Remove backingProviderName
263+
new.ProviderSettings.BackingProviderName = ""
264+
ctx.Log.Infof("performing cluster upgrade from %s, to %s",
265+
current.ProviderSettings.InstanceSizeName, new.ProviderSettings.InstanceSizeName)
266+
267+
// TODO: Replace with the go-atlas-client when this method will be added to go-atlas-client
268+
atlasClient := ctx.Client
269+
urlStr := fmt.Sprintf("/api/atlas/v1.0/groups/%s/clusters/tenantUpgrade", current.GroupID)
270+
req, err := atlasClient.NewRequest(context.Background(), http.MethodPost, urlStr, new)
271+
if err != nil {
272+
return false, fmt.Errorf(baseErr, err)
273+
}
274+
275+
_, err = atlasClient.Do(context.Background(), req, &new)
276+
if err != nil {
277+
return false, fmt.Errorf(baseErr, err)
278+
}
279+
280+
return true, nil
281+
}
282+
283+
func clusterShouldBeUpgraded(current *mongodbatlas.Cluster, new *mongodbatlas.Cluster) bool {
284+
if isSharedCluster(current.ProviderSettings.InstanceSizeName) && !isSharedCluster(new.ProviderSettings.InstanceSizeName) {
285+
return true
286+
}
287+
return false
288+
}
289+
290+
func isSharedCluster(instanceSizeName string) bool {
291+
switch strings.ToUpper(instanceSizeName) {
292+
case "M0", "M2", "M5":
293+
return true
294+
}
295+
return false
296+
}

0 commit comments

Comments
 (0)