From 05b8dcbcb55d319b41a0db79fa52be6a2dac1c2a Mon Sep 17 00:00:00 2001 From: Igor Karpukhin Date: Mon, 16 May 2022 13:01:10 +0200 Subject: [PATCH 1/3] Added a cluster upgrade --- pkg/controller/atlasdeployment/deployment.go | 23 ++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pkg/controller/atlasdeployment/deployment.go b/pkg/controller/atlasdeployment/deployment.go index 073fbfa7a3..5ca4107e4e 100644 --- a/pkg/controller/atlasdeployment/deployment.go +++ b/pkg/controller/atlasdeployment/deployment.go @@ -46,6 +46,7 @@ func ensureClusterState(ctx *workflow.Context, project *mdbv1.AtlasProject, clus switch atlasCluster.StateName { case "IDLE": + return regularClusterIdle(ctx, project, cluster, atlasCluster) case "CREATING": return atlasCluster, workflow.InProgress(workflow.ClusterCreating, "cluster is provisioning") @@ -241,3 +242,25 @@ func (r *AtlasDeploymentReconciler) ensureConnectionSecrets(ctx *workflow.Contex return workflow.OK() } + +func (r *AtlasClusterReconciler) handleClusterVersionUpgrade(current *mdbv1.ClusterSpec, new *mdbv1.ClusterSpec) error { + shouldBeUpdated, err := r.clusterMustBeUpgraded(current, new) + if err != nil { + return err + } + + if !shouldBeUpdated { + r.Log.Debug("cluster shouldn't be upgraded") + return nil + } + + r.Log.Infof("performing cluster upgrade from %s, to %s", + current.ProviderSettings.InstanceSizeName, new.ProviderSettings.InstanceSizeName) + return nil +} + +// If instance size name changed from M +func (r *AtlasClusterReconciler) clusterMustBeUpgraded(current *mdbv1.ClusterSpec, new *mdbv1.ClusterSpec) (bool, error) { + //if current.MongoDBMajorVersion + return false, nil +} From 6fc55fe2fc17d29c2f26cb15b8b9493c4c9099ff Mon Sep 17 00:00:00 2001 From: Igor Karpukhin Date: Tue, 17 May 2022 17:55:43 +0200 Subject: [PATCH 2/3] Added shared cluster upgrade support --- pkg/controller/atlasdeployment/deployment.go | 64 ++++++++++++++++---- 1 file changed, 51 insertions(+), 13 deletions(-) diff --git a/pkg/controller/atlasdeployment/deployment.go b/pkg/controller/atlasdeployment/deployment.go index 5ca4107e4e..fe8da23512 100644 --- a/pkg/controller/atlasdeployment/deployment.go +++ b/pkg/controller/atlasdeployment/deployment.go @@ -2,6 +2,7 @@ package atlasdeployment import ( "context" + "encoding/json" "fmt" "net/http" "strings" @@ -86,6 +87,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()) @@ -243,24 +253,52 @@ func (r *AtlasDeploymentReconciler) ensureConnectionSecrets(ctx *workflow.Contex return workflow.OK() } -func (r *AtlasClusterReconciler) handleClusterVersionUpgrade(current *mdbv1.ClusterSpec, new *mdbv1.ClusterSpec) error { - shouldBeUpdated, err := r.clusterMustBeUpgraded(current, new) +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 err + return false, fmt.Errorf(baseErr, err) } - if !shouldBeUpdated { - r.Log.Debug("cluster shouldn't be upgraded") - return nil + dta, err := json.MarshalIndent(new, "", " ") + if err != nil { + panic(err) } - r.Log.Infof("performing cluster upgrade from %s, to %s", - current.ProviderSettings.InstanceSizeName, new.ProviderSettings.InstanceSizeName) - return nil + fmt.Println("DEBUG UPGRADE", string(dta)) + + _, err = atlasClient.Do(context.Background(), req, &new) + if err != nil { + return false, fmt.Errorf(baseErr, err) + } + + return true, nil } -// If instance size name changed from M -func (r *AtlasClusterReconciler) clusterMustBeUpgraded(current *mdbv1.ClusterSpec, new *mdbv1.ClusterSpec) (bool, error) { - //if current.MongoDBMajorVersion - return false, nil +func clusterShouldBeUpgraded(current *mongodbatlas.Cluster, new *mongodbatlas.Cluster) bool { + if isSharedCluster(current.ProviderSettings.InstanceSizeName) && !isSharedCluster(new.ProviderSettings.InstanceSizeName) { + return true + } + return false +} + +func isSharedCluster(instanceSizeName string) bool { + switch strings.ToUpper(instanceSizeName) { + case "M0", "M2", "M5": + return true + } + return false } From d9895e716468a7bc57be30bf88b9572a17041736 Mon Sep 17 00:00:00 2001 From: Igor Karpukhin Date: Tue, 17 May 2022 18:33:27 +0200 Subject: [PATCH 3/3] Removed debug statement --- pkg/controller/atlasdeployment/deployment.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/pkg/controller/atlasdeployment/deployment.go b/pkg/controller/atlasdeployment/deployment.go index fe8da23512..17a52a3073 100644 --- a/pkg/controller/atlasdeployment/deployment.go +++ b/pkg/controller/atlasdeployment/deployment.go @@ -2,7 +2,6 @@ package atlasdeployment import ( "context" - "encoding/json" "fmt" "net/http" "strings" @@ -273,13 +272,6 @@ func handleSharedClusterUpgrade(ctx *workflow.Context, current *mongodbatlas.Clu return false, fmt.Errorf(baseErr, err) } - dta, err := json.MarshalIndent(new, "", " ") - if err != nil { - panic(err) - } - - fmt.Println("DEBUG UPGRADE", string(dta)) - _, err = atlasClient.Do(context.Background(), req, &new) if err != nil { return false, fmt.Errorf(baseErr, err)