Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions pkg/controller/atlasdeployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func ensureClusterState(ctx *workflow.Context, project *mdbv1.AtlasProject, clus

switch atlasCluster.StateName {
case "IDLE":

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This empty line seems strange, not critical thought =)

return regularClusterIdle(ctx, project, cluster, atlasCluster)
case "CREATING":
return atlasCluster, workflow.InProgress(workflow.ClusterCreating, "cluster is provisioning")
Expand Down Expand Up @@ -85,6 +86,15 @@ func regularClusterIdle(ctx *workflow.Context, project *mdbv1.AtlasProject, clus

resultingCluster = cleanupCluster(resultingCluster)

// Handle shared (M0,M2,M5) cluster to non-shared cluster upgrade
scheduled, err := handleSharedClusterUpgrade(ctx, atlasCluster, &resultingCluster)
if err != nil {
return atlasCluster, workflow.Terminate(workflow.Internal, err.Error())
}
if scheduled {
return atlasCluster, workflow.InProgress(workflow.ClusterUpdating, "cluster is upgrading")
}

atlasCluster, _, err = ctx.Client.Clusters.Update(context.Background(), project.Status.ID, cluster.Spec.DeploymentSpec.Name, &resultingCluster)
if err != nil {
return atlasCluster, workflow.Terminate(workflow.ClusterNotUpdatedInAtlas, err.Error())
Expand Down Expand Up @@ -241,3 +251,46 @@ func (r *AtlasDeploymentReconciler) ensureConnectionSecrets(ctx *workflow.Contex

return workflow.OK()
}

func handleSharedClusterUpgrade(ctx *workflow.Context, current *mongodbatlas.Cluster, new *mongodbatlas.Cluster) (scheduled bool, _ error) {
baseErr := "can not perform cluster upgrade. ERR: %v"
if !clusterShouldBeUpgraded(current, new) {
ctx.Log.Debug("cluster shouldn't be upgraded")
return false, nil
}

// Remove backingProviderName
new.ProviderSettings.BackingProviderName = ""
ctx.Log.Infof("performing cluster upgrade from %s, to %s",
current.ProviderSettings.InstanceSizeName, new.ProviderSettings.InstanceSizeName)

// TODO: Replace with the go-atlas-client when this method will be added to go-atlas-client
atlasClient := ctx.Client
urlStr := fmt.Sprintf("/api/atlas/v1.0/groups/%s/clusters/tenantUpgrade", current.GroupID)
req, err := atlasClient.NewRequest(context.Background(), http.MethodPost, urlStr, new)
if err != nil {
return false, fmt.Errorf(baseErr, err)
}

_, err = atlasClient.Do(context.Background(), req, &new)
if err != nil {
return false, fmt.Errorf(baseErr, err)
}

return true, nil
}

func clusterShouldBeUpgraded(current *mongodbatlas.Cluster, new *mongodbatlas.Cluster) bool {
if isSharedCluster(current.ProviderSettings.InstanceSizeName) && !isSharedCluster(new.ProviderSettings.InstanceSizeName) {
Copy link
Contributor

@fabritsius fabritsius May 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just return this expression 😅

return true
}
return false
}

func isSharedCluster(instanceSizeName string) bool {
switch strings.ToUpper(instanceSizeName) {
case "M0", "M2", "M5":
return true
}
return false
}