From 52022bf3d50297bf0d16bc81f51cab104e35789b Mon Sep 17 00:00:00 2001 From: Roo Thorp Date: Fri, 15 Mar 2024 12:18:47 +0000 Subject: [PATCH 1/6] remove readonly fields when exporting tenant deplyment --- .../operator/deployment/deployment.go | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/internal/kubernetes/operator/deployment/deployment.go b/internal/kubernetes/operator/deployment/deployment.go index f714669f48..3901f30af4 100644 --- a/internal/kubernetes/operator/deployment/deployment.go +++ b/internal/kubernetes/operator/deployment/deployment.go @@ -171,9 +171,36 @@ func BuildAtlasAdvancedDeployment(deploymentStore store.OperatorClusterStore, va advancedSpec.ManagedNamespaces = managedNamespaces } + cleanTenantFields(&atlasDeployment.Spec) + return deploymentResult, nil } +func cleanTenantFields(out *akov2.AtlasDeploymentSpec) { + + isTenant := false + if out.DeploymentSpec == nil { + return + } + for _, spec := range out.DeploymentSpec.ReplicationSpecs { + for _, c := range spec.RegionConfigs { + if c.ProviderName == "TENANT" { + isTenant = true + break + } + } + } + + if isTenant { + out.DeploymentSpec.BiConnector = nil + out.DeploymentSpec.EncryptionAtRestProvider = "" + out.DeploymentSpec.DiskSizeGB = nil + out.DeploymentSpec.MongoDBMajorVersion = "" + out.DeploymentSpec.PitEnabled = nil + out.DeploymentSpec.BackupEnabled = nil + } +} + func buildGlobalDeployment(atlasRepSpec []atlasv2.ReplicationSpec, globalDeploymentProvider store.GlobalClusterDescriber, projectID, clusterID string) ([]akov2.CustomZoneMapping, []akov2.ManagedNamespace, error) { globalCluster, err := globalDeploymentProvider.GlobalCluster(projectID, clusterID) if err != nil { From e00e92e1161fe1ee4506256169c14e64bbb19e8c Mon Sep 17 00:00:00 2001 From: Roo Thorp Date: Fri, 15 Mar 2024 12:19:02 +0000 Subject: [PATCH 2/6] add test --- .../operator/deployment/deployment_test.go | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/internal/kubernetes/operator/deployment/deployment_test.go b/internal/kubernetes/operator/deployment/deployment_test.go index 7c20a74971..d423615111 100644 --- a/internal/kubernetes/operator/deployment/deployment_test.go +++ b/internal/kubernetes/operator/deployment/deployment_test.go @@ -520,3 +520,51 @@ func TestBuildServerlessDeployments(t *testing.T) { } }) } + +func TestCleanTenantFields(t *testing.T) { + spec := akov2.AtlasDeploymentSpec{ + DeploymentSpec: &akov2.AdvancedDeploymentSpec{ + BackupEnabled: pointer.Get(true), + BiConnector: &akov2.BiConnectorSpec{ + Enabled: pointer.Get(true), + ReadPreference: "SECONDARY", + }, + EncryptionAtRestProvider: "AWS", + PitEnabled: pointer.Get(true), + MongoDBMajorVersion: "5.1", + ReplicationSpecs: []*akov2.AdvancedReplicationSpec{ + { + RegionConfigs: []*akov2.AdvancedRegionConfig{ + { + ProviderName: "TENANT", + }, + }, + }, + }, + }, + } + + cleanTenantFields(&spec) + + expected := akov2.AtlasDeploymentSpec{ + DeploymentSpec: &akov2.AdvancedDeploymentSpec{ + BackupEnabled: nil, + BiConnector: nil, + EncryptionAtRestProvider: "", + PitEnabled: nil, + MongoDBMajorVersion: "", + ReplicationSpecs: []*akov2.AdvancedReplicationSpec{ + { + RegionConfigs: []*akov2.AdvancedRegionConfig{ + { + ProviderName: "TENANT", + }, + }, + }, + }, + }, + } + if !reflect.DeepEqual(spec, expected) { + t.Fatalf("Cleaning tenant readonly fields mismatch.\r\nexpected: %v\r\ngot: %v\r\n", expected, spec) + } +} From 3a25162ddd7cd4ea2dc6468152327e9902a1d7b2 Mon Sep 17 00:00:00 2001 From: Roo Thorp Date: Fri, 15 Mar 2024 12:26:49 +0000 Subject: [PATCH 3/6] fmt --- internal/kubernetes/operator/deployment/deployment.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/kubernetes/operator/deployment/deployment.go b/internal/kubernetes/operator/deployment/deployment.go index 3901f30af4..03b1cb1739 100644 --- a/internal/kubernetes/operator/deployment/deployment.go +++ b/internal/kubernetes/operator/deployment/deployment.go @@ -177,8 +177,8 @@ func BuildAtlasAdvancedDeployment(deploymentStore store.OperatorClusterStore, va } func cleanTenantFields(out *akov2.AtlasDeploymentSpec) { - isTenant := false + if out.DeploymentSpec == nil { return } From 29f13475a7bc527432239b312c07f221767a5553 Mon Sep 17 00:00:00 2001 From: Roo Thorp Date: Fri, 15 Mar 2024 16:21:14 +0000 Subject: [PATCH 4/6] cleaner function signature --- .../operator/deployment/deployment.go | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/internal/kubernetes/operator/deployment/deployment.go b/internal/kubernetes/operator/deployment/deployment.go index 03b1cb1739..9e9066f08f 100644 --- a/internal/kubernetes/operator/deployment/deployment.go +++ b/internal/kubernetes/operator/deployment/deployment.go @@ -171,34 +171,33 @@ func BuildAtlasAdvancedDeployment(deploymentStore store.OperatorClusterStore, va advancedSpec.ManagedNamespaces = managedNamespaces } - cleanTenantFields(&atlasDeployment.Spec) + if hasTenantRegionConfig(atlasDeployment) { + atlasDeployment.Spec.DeploymentSpec.BiConnector = nil + atlasDeployment.Spec.DeploymentSpec.EncryptionAtRestProvider = "" + atlasDeployment.Spec.DeploymentSpec.DiskSizeGB = nil + atlasDeployment.Spec.DeploymentSpec.MongoDBMajorVersion = "" + atlasDeployment.Spec.DeploymentSpec.PitEnabled = nil + atlasDeployment.Spec.DeploymentSpec.BackupEnabled = nil + } return deploymentResult, nil } -func cleanTenantFields(out *akov2.AtlasDeploymentSpec) { - isTenant := false - - if out.DeploymentSpec == nil { - return +func hasTenantRegionConfig(out *akov2.AtlasDeployment) bool { + if out.Spec.DeploymentSpec == nil { + return false } - for _, spec := range out.DeploymentSpec.ReplicationSpecs { + for _, spec := range out.Spec.DeploymentSpec.ReplicationSpecs { + if spec == nil { + continue + } for _, c := range spec.RegionConfigs { - if c.ProviderName == "TENANT" { - isTenant = true - break + if c != nil && c.ProviderName == "TENANT" { + return true } } } - - if isTenant { - out.DeploymentSpec.BiConnector = nil - out.DeploymentSpec.EncryptionAtRestProvider = "" - out.DeploymentSpec.DiskSizeGB = nil - out.DeploymentSpec.MongoDBMajorVersion = "" - out.DeploymentSpec.PitEnabled = nil - out.DeploymentSpec.BackupEnabled = nil - } + return false } func buildGlobalDeployment(atlasRepSpec []atlasv2.ReplicationSpec, globalDeploymentProvider store.GlobalClusterDescriber, projectID, clusterID string) ([]akov2.CustomZoneMapping, []akov2.ManagedNamespace, error) { From 994c7505a80f3df1c4f0acbb354993786b325061 Mon Sep 17 00:00:00 2001 From: Roo Thorp Date: Fri, 15 Mar 2024 17:56:04 +0000 Subject: [PATCH 5/6] add table tests --- .../operator/deployment/deployment_test.go | 116 +++++++++++++----- 1 file changed, 85 insertions(+), 31 deletions(-) diff --git a/internal/kubernetes/operator/deployment/deployment_test.go b/internal/kubernetes/operator/deployment/deployment_test.go index d423615111..dbeb088a7c 100644 --- a/internal/kubernetes/operator/deployment/deployment_test.go +++ b/internal/kubernetes/operator/deployment/deployment_test.go @@ -522,49 +522,103 @@ func TestBuildServerlessDeployments(t *testing.T) { } func TestCleanTenantFields(t *testing.T) { - spec := akov2.AtlasDeploymentSpec{ - DeploymentSpec: &akov2.AdvancedDeploymentSpec{ - BackupEnabled: pointer.Get(true), - BiConnector: &akov2.BiConnectorSpec{ - Enabled: pointer.Get(true), - ReadPreference: "SECONDARY", + for _, tt := range []struct { + name string + spec akov2.AtlasDeploymentSpec + expect bool + }{ + { + name: "nil deploymentspec", + spec: akov2.AtlasDeploymentSpec{ + DeploymentSpec: nil, }, - EncryptionAtRestProvider: "AWS", - PitEnabled: pointer.Get(true), - MongoDBMajorVersion: "5.1", - ReplicationSpecs: []*akov2.AdvancedReplicationSpec{ - { - RegionConfigs: []*akov2.AdvancedRegionConfig{ + expect: false, + }, + { + name: "nil replicationspec", + spec: akov2.AtlasDeploymentSpec{ + DeploymentSpec: &akov2.AdvancedDeploymentSpec{ + ReplicationSpecs: []*akov2.AdvancedReplicationSpec{ + nil, + }, + }, + }, + expect: false, + }, + { + name: "nil regionconfig", + spec: akov2.AtlasDeploymentSpec{ + DeploymentSpec: &akov2.AdvancedDeploymentSpec{ + ReplicationSpecs: []*akov2.AdvancedReplicationSpec{ { - ProviderName: "TENANT", + RegionConfigs: []*akov2.AdvancedRegionConfig{ + nil, + }, }, }, }, }, + expect: false, }, - } - - cleanTenantFields(&spec) - - expected := akov2.AtlasDeploymentSpec{ - DeploymentSpec: &akov2.AdvancedDeploymentSpec{ - BackupEnabled: nil, - BiConnector: nil, - EncryptionAtRestProvider: "", - PitEnabled: nil, - MongoDBMajorVersion: "", - ReplicationSpecs: []*akov2.AdvancedReplicationSpec{ - { - RegionConfigs: []*akov2.AdvancedRegionConfig{ + { + name: "multiple non-tenant regionconfigs", + spec: akov2.AtlasDeploymentSpec{ + DeploymentSpec: &akov2.AdvancedDeploymentSpec{ + ReplicationSpecs: []*akov2.AdvancedReplicationSpec{ { - ProviderName: "TENANT", + RegionConfigs: []*akov2.AdvancedRegionConfig{ + { + ProviderName: "AWS", + }, + { + ProviderName: "GCP", + }, + { + ProviderName: "AZURE", + }, + { + ProviderName: "AWS", + }, + }, }, }, }, }, + expect: false, }, - } - if !reflect.DeepEqual(spec, expected) { - t.Fatalf("Cleaning tenant readonly fields mismatch.\r\nexpected: %v\r\ngot: %v\r\n", expected, spec) + { + name: "multiple non-tenant regionconfigs and one tenant", + spec: akov2.AtlasDeploymentSpec{ + DeploymentSpec: &akov2.AdvancedDeploymentSpec{ + ReplicationSpecs: []*akov2.AdvancedReplicationSpec{ + { + RegionConfigs: []*akov2.AdvancedRegionConfig{ + { + ProviderName: "AWS", + }, + { + ProviderName: "GCP", + }, + { + ProviderName: "AZURE", + }, + { + ProviderName: "TENANT", + }, + }, + }, + }, + }, + }, + expect: true, + }, + } { + t.Run(tt.name, func(t *testing.T) { + if got := hasTenantRegionConfig(&akov2.AtlasDeployment{ + Spec: tt.spec, + }); got != tt.expect { + t.Errorf("") + } + }) } } From b96c7dd770eff709c7b9948c34ad1a17f6f641b9 Mon Sep 17 00:00:00 2001 From: Roo Thorp Date: Mon, 18 Mar 2024 12:04:21 +0000 Subject: [PATCH 6/6] Add error message on test Co-authored-by: Sergiusz Urbaniak --- internal/kubernetes/operator/deployment/deployment_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/kubernetes/operator/deployment/deployment_test.go b/internal/kubernetes/operator/deployment/deployment_test.go index dbeb088a7c..ff26904c74 100644 --- a/internal/kubernetes/operator/deployment/deployment_test.go +++ b/internal/kubernetes/operator/deployment/deployment_test.go @@ -617,7 +617,7 @@ func TestCleanTenantFields(t *testing.T) { if got := hasTenantRegionConfig(&akov2.AtlasDeployment{ Spec: tt.spec, }); got != tt.expect { - t.Errorf("") + t.Errorf("expect hasTenantRegionConfig to be %t, got %t", tt.expect, got) } }) }