From ac2e83580aa18d10aebb4f16da2081996c9f5b48 Mon Sep 17 00:00:00 2001 From: Pavel Vasilev Date: Tue, 16 Mar 2021 12:25:50 +0300 Subject: [PATCH 01/17] Ensure secrets on cluster update --- .../atlascluster/atlascluster_controller.go | 7 ++-- pkg/controller/atlascluster/cluster.go | 36 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/pkg/controller/atlascluster/atlascluster_controller.go b/pkg/controller/atlascluster/atlascluster_controller.go index 2699a449fa..1baf5b6cca 100644 --- a/pkg/controller/atlascluster/atlascluster_controller.go +++ b/pkg/controller/atlascluster/atlascluster_controller.go @@ -57,8 +57,6 @@ type AtlasClusterReconciler struct { // +kubebuilder:rbac:groups=atlas.mongodb.com,namespace=default,resources=atlasclusters/status,verbs=get;update;patch func (r *AtlasClusterReconciler) Reconcile(context context.Context, req ctrl.Request) (ctrl.Result, error) { - // TODO use the context passed - _ = context log := r.Log.With("atlascluster", req.NamespacedName) cluster := &mdbv1.AtlasCluster{} @@ -103,6 +101,11 @@ func (r *AtlasClusterReconciler) Reconcile(context context.Context, req ctrl.Req return result.ReconcileResult(), nil } + if csResult := ensureConnectionSecrets(context, ctx, r.Client, project, c); !csResult.IsOk() { + ctx.SetConditionFromResult(status.ClusterReadyType, csResult) + return csResult.ReconcileResult(), nil + } + ctx. SetConditionTrue(status.ClusterReadyType). EnsureStatusOption(status.AtlasClusterMongoDBVersionOption(c.MongoDBVersion)). diff --git a/pkg/controller/atlascluster/cluster.go b/pkg/controller/atlascluster/cluster.go index 5d0ec914e5..08e5debf02 100644 --- a/pkg/controller/atlascluster/cluster.go +++ b/pkg/controller/atlascluster/cluster.go @@ -9,8 +9,10 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "go.mongodb.org/atlas/mongodbatlas" "go.uber.org/zap" + "sigs.k8s.io/controller-runtime/pkg/client" mdbv1 "github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1" + "github.com/mongodb/mongodb-atlas-kubernetes/pkg/controller/connectionsecret" "github.com/mongodb/mongodb-atlas-kubernetes/pkg/controller/workflow" "github.com/mongodb/mongodb-atlas-kubernetes/pkg/util/compat" ) @@ -130,3 +132,37 @@ func clustersEqual(log *zap.SugaredLogger, clusterA mongodbatlas.Cluster, cluste return d == "" } + +func ensureConnectionSecrets(ctx context.Context, wctx *workflow.Context, k8sClient client.Client, project *mdbv1.AtlasProject, cluster *mongodbatlas.Cluster) workflow.Result { + dbUsers, _, err := wctx.Client.DatabaseUsers.List(ctx, project.ID(), &mongodbatlas.ListOptions{}) + if err != nil { + return workflow.Terminate(workflow.Internal, err.Error()) + } + +out: + for _, dbUser := range dbUsers { + for _, scope := range dbUser.Scopes { + if scope.Type == string(mdbv1.ClusterScopeType) && scope.Name == cluster.Name { + break + } + + continue out + } + + data := connectionsecret.ConnectionData{ + DBUserName: dbUser.Username, + ConnURL: cluster.ConnectionStrings.Standard, + SrvConnURL: cluster.ConnectionStrings.StandardSrv, + Password: dbUser.Password, + } + + var secretName string + if secretName, err = connectionsecret.Ensure(k8sClient, project.Namespace, project.Spec.Name, project.ID(), cluster.Name, data); err != nil { + return workflow.Terminate(workflow.DatabaseUserConnectionSecretsNotCreated, err.Error()) + } + + wctx.Log.Debugw("Ensured connection Secret up-to-date", "name", secretName) + } + + return workflow.OK() +} From aa8175a0513e7c99e8b7096868085eae8b5b1a37 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 23 Mar 2021 13:29:53 +0300 Subject: [PATCH 02/17] Rework secret reconciliation from Cluster --- .../atlascluster/atlascluster_controller.go | 2 +- pkg/controller/atlascluster/cluster.go | 34 ++++++++----------- .../atlasdatabaseuser/connectionsecrets.go | 2 +- .../atlasdatabaseuser/databaseuser.go | 2 +- test/int/cluster_test.go | 34 +++++++++++++++++++ test/int/dbuser_test.go | 29 ++++++++-------- 6 files changed, 65 insertions(+), 38 deletions(-) diff --git a/pkg/controller/atlascluster/atlascluster_controller.go b/pkg/controller/atlascluster/atlascluster_controller.go index 1baf5b6cca..2e9613aad6 100644 --- a/pkg/controller/atlascluster/atlascluster_controller.go +++ b/pkg/controller/atlascluster/atlascluster_controller.go @@ -101,7 +101,7 @@ func (r *AtlasClusterReconciler) Reconcile(context context.Context, req ctrl.Req return result.ReconcileResult(), nil } - if csResult := ensureConnectionSecrets(context, ctx, r.Client, project, c); !csResult.IsOk() { + if csResult := ensureConnectionSecrets(ctx, r.Client, project, c); !csResult.IsOk() { ctx.SetConditionFromResult(status.ClusterReadyType, csResult) return csResult.ReconcileResult(), nil } diff --git a/pkg/controller/atlascluster/cluster.go b/pkg/controller/atlascluster/cluster.go index 08e5debf02..d192a60273 100644 --- a/pkg/controller/atlascluster/cluster.go +++ b/pkg/controller/atlascluster/cluster.go @@ -12,7 +12,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" mdbv1 "github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1" - "github.com/mongodb/mongodb-atlas-kubernetes/pkg/controller/connectionsecret" + "github.com/mongodb/mongodb-atlas-kubernetes/pkg/controller/atlasdatabaseuser" "github.com/mongodb/mongodb-atlas-kubernetes/pkg/controller/workflow" "github.com/mongodb/mongodb-atlas-kubernetes/pkg/util/compat" ) @@ -133,35 +133,29 @@ func clustersEqual(log *zap.SugaredLogger, clusterA mongodbatlas.Cluster, cluste return d == "" } -func ensureConnectionSecrets(ctx context.Context, wctx *workflow.Context, k8sClient client.Client, project *mdbv1.AtlasProject, cluster *mongodbatlas.Cluster) workflow.Result { - dbUsers, _, err := wctx.Client.DatabaseUsers.List(ctx, project.ID(), &mongodbatlas.ListOptions{}) - if err != nil { - return workflow.Terminate(workflow.Internal, err.Error()) - } +func ensureConnectionSecrets(wctx *workflow.Context, k8sClient client.Client, project *mdbv1.AtlasProject, cluster *mongodbatlas.Cluster) workflow.Result { + databaseUsers := mdbv1.AtlasDatabaseUserList{} + + for _, dbUser := range databaseUsers.Items { + found := false -out: - for _, dbUser := range dbUsers { - for _, scope := range dbUser.Scopes { - if scope.Type == string(mdbv1.ClusterScopeType) && scope.Name == cluster.Name { + for _, scope := range dbUser.GetScopes(mdbv1.ClusterScopeType) { + if scope == cluster.Name { + found = true break } - continue out + continue } - data := connectionsecret.ConnectionData{ - DBUserName: dbUser.Username, - ConnURL: cluster.ConnectionStrings.Standard, - SrvConnURL: cluster.ConnectionStrings.StandardSrv, - Password: dbUser.Password, + if !found { + continue } - var secretName string - if secretName, err = connectionsecret.Ensure(k8sClient, project.Namespace, project.Spec.Name, project.ID(), cluster.Name, data); err != nil { + err := atlasdatabaseuser.CreateOrUpdateConnectionSecrets(wctx, k8sClient, *project, dbUser) + if err != nil { return workflow.Terminate(workflow.DatabaseUserConnectionSecretsNotCreated, err.Error()) } - - wctx.Log.Debugw("Ensured connection Secret up-to-date", "name", secretName) } return workflow.OK() diff --git a/pkg/controller/atlasdatabaseuser/connectionsecrets.go b/pkg/controller/atlasdatabaseuser/connectionsecrets.go index 5ecbcecc98..93ec68adc9 100644 --- a/pkg/controller/atlasdatabaseuser/connectionsecrets.go +++ b/pkg/controller/atlasdatabaseuser/connectionsecrets.go @@ -13,7 +13,7 @@ import ( "github.com/mongodb/mongodb-atlas-kubernetes/pkg/util/stringutil" ) -func createOrUpdateConnectionSecrets(ctx *workflow.Context, k8sClient client.Client, project mdbv1.AtlasProject, dbUser mdbv1.AtlasDatabaseUser) error { +func CreateOrUpdateConnectionSecrets(ctx *workflow.Context, k8sClient client.Client, project mdbv1.AtlasProject, dbUser mdbv1.AtlasDatabaseUser) error { clusters, _, err := ctx.Client.Clusters.List(context.Background(), project.ID(), &mongodbatlas.ListOptions{}) if err != nil { // TODO CLOUDP-84205 ignore the 404 exception in case no clusters exist by this time diff --git a/pkg/controller/atlasdatabaseuser/databaseuser.go b/pkg/controller/atlasdatabaseuser/databaseuser.go index 620f3f74cf..602b0c7210 100644 --- a/pkg/controller/atlasdatabaseuser/databaseuser.go +++ b/pkg/controller/atlasdatabaseuser/databaseuser.go @@ -55,7 +55,7 @@ func (r *AtlasDatabaseUserReconciler) ensureDatabaseUser(ctx *workflow.Context, return result } - if err = createOrUpdateConnectionSecrets(ctx, r.Client, project, dbUser); err != nil { + if err = CreateOrUpdateConnectionSecrets(ctx, r.Client, project, dbUser); err != nil { return workflow.Terminate(workflow.DatabaseUserConnectionSecretsNotCreated, err.Error()) } diff --git a/test/int/cluster_test.go b/test/int/cluster_test.go index 9c727f1a45..93049e0fd4 100644 --- a/test/int/cluster_test.go +++ b/test/int/cluster_test.go @@ -392,6 +392,40 @@ var _ = Describe("AtlasCluster", func() { }) }) }) + + Describe("Create DBUser before cluster & check secrets", func() { + createdDBUser := mdbv1.DefaultDBUser(namespace.Name, "test-db-user", createdProject.Name).WithPasswordSecret(UserPasswordSecret) + By(fmt.Sprintf("Creating the Database User %s", kube.ObjectKeyFromObject(createdDBUser)), func() { + Expect(k8sClient.Create(context.Background(), createdDBUser)).ToNot(HaveOccurred()) + + Eventually(testutil.WaitFor(k8sClient, createdDBUser, status.TrueCondition(status.ReadyType)), + 20, interval).Should(BeTrue()) + + checkUserInAtlas(createdProject.ID(), *createdDBUser) + }) + + createdCluster = mdbv1.DefaultGCPCluster(namespace.Name, createdProject.Name) + By(fmt.Sprintf("Creating the Cluster %s", kube.ObjectKeyFromObject(createdCluster)), func() { + Expect(k8sClient.Create(context.Background(), createdCluster)).ToNot(HaveOccurred()) + + Eventually(testutil.WaitFor(k8sClient, createdCluster, status.TrueCondition(status.ReadyType), validateClusterCreatingFunc()), + 1800, interval).Should(BeTrue()) + + doCommonChecks() + checkAtlasState() + }) + + By("Checking connection Secrets", func() { + Eventually(tryConnect(createdProject.ID(), *createdCluster, *createdDBUser), 90, interval).Should(Succeed()) + validateSecret(k8sClient, *createdProject, *createdCluster, *createdDBUser) + checkNumberOfConnectionSecrets(k8sClient, *createdProject, 2) + + expectedSecretsInStatus := map[string]string{ + "test-cluster-gcp": kube.NormalizeIdentifier(fmt.Sprintf("%s-%s-%s", createdProject.Spec.Name, createdCluster.Spec.Name, createdDBUser.Spec.Username)), + } + Expect(createdDBUser.Status.ConnectionSecrets).To(Equal(expectedSecretsInStatus)) + }) + }) }) func validateClusterCreatingFunc() func(a mdbv1.AtlasCustomResource) { diff --git a/test/int/dbuser_test.go b/test/int/dbuser_test.go index df19c06d59..5fde3c38bc 100644 --- a/test/int/dbuser_test.go +++ b/test/int/dbuser_test.go @@ -123,17 +123,6 @@ var _ = Describe("AtlasDatabaseUser", func() { removeControllersAndNamespace() }) - checkUserInAtlas := func(user mdbv1.AtlasDatabaseUser) { - By("Verifying Database User state in Atlas", func() { - atlasDBUser, _, err := atlasClient.DatabaseUsers.Get(context.Background(), user.Spec.DatabaseName, createdProject.ID(), user.Spec.Username) - Expect(err).ToNot(HaveOccurred()) - operatorDBUser, err := user.ToAtlas(k8sClient) - Expect(err).ToNot(HaveOccurred()) - - Expect(*atlasDBUser).To(Equal(normalize(*operatorDBUser, createdProject.ID()))) - }) - } - connSecretname := func(suffix string) string { return kube.NormalizeIdentifier(createdProject.Spec.Name) + suffix } @@ -148,7 +137,7 @@ var _ = Describe("AtlasDatabaseUser", func() { Eventually(testutil.WaitFor(k8sClient, createdDBUser, status.TrueCondition(status.ReadyType)), 20, interval).Should(BeTrue()) - checkUserInAtlas(*createdDBUser) + checkUserInAtlas(createdProject.ID(), *createdDBUser) // TODO CLOUDP-83026 and CLOUDP-83098 remove Eventually in favor of Expect Eventually(tryConnect(createdProject.ID(), *createdClusterGCP, *createdDBUser), 90, interval).Should(Succeed()) @@ -184,7 +173,7 @@ var _ = Describe("AtlasDatabaseUser", func() { Eventually(testutil.WaitFor(k8sClient, createdDBUser, status.TrueCondition(status.ReadyType)), 20, interval).Should(BeTrue()) - checkUserInAtlas(*createdDBUser) + checkUserInAtlas(createdProject.ID(), *createdDBUser) By("Checking connection Secrets", func() { validateSecret(k8sClient, *createdProject, *createdClusterGCP, *createdDBUser) @@ -217,7 +206,7 @@ var _ = Describe("AtlasDatabaseUser", func() { Eventually(testutil.WaitFor(k8sClient, secondDBUser, status.TrueCondition(status.ReadyType)), 20, interval).Should(BeTrue()) - checkUserInAtlas(*secondDBUser) + checkUserInAtlas(createdProject.ID(), *secondDBUser) By("Checking connection Secrets", func() { validateSecret(k8sClient, *createdProject, *createdClusterGCP, *createdDBUser) validateSecret(k8sClient, *createdProject, *createdClusterAWS, *createdDBUser) @@ -257,7 +246,6 @@ var _ = Describe("AtlasDatabaseUser", func() { checkNumberOfConnectionSecrets(k8sClient, *createdProject, 0) }) - }) }) }) @@ -430,3 +418,14 @@ func checkSecretsDontExist(namespace string, secretNames []string) func() bool { return nonExisting == len(secretNames) } } + +func checkUserInAtlas(projectID string, user mdbv1.AtlasDatabaseUser) { + By("Verifying Database User state in Atlas", func() { + atlasDBUser, _, err := atlasClient.DatabaseUsers.Get(context.Background(), user.Spec.DatabaseName, projectID, user.Spec.Username) + Expect(err).ToNot(HaveOccurred()) + operatorDBUser, err := user.ToAtlas(k8sClient) + Expect(err).ToNot(HaveOccurred()) + + Expect(*atlasDBUser).To(Equal(normalize(*operatorDBUser, projectID))) + }) +} From bcd962020f1b0ac1179bee7bafe6c86b2b9bbdc9 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 23 Mar 2021 13:37:54 +0300 Subject: [PATCH 03/17] Fix tests --- test/int/cluster_test.go | 48 +++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/test/int/cluster_test.go b/test/int/cluster_test.go index 93049e0fd4..73e82cc987 100644 --- a/test/int/cluster_test.go +++ b/test/int/cluster_test.go @@ -394,36 +394,38 @@ var _ = Describe("AtlasCluster", func() { }) Describe("Create DBUser before cluster & check secrets", func() { - createdDBUser := mdbv1.DefaultDBUser(namespace.Name, "test-db-user", createdProject.Name).WithPasswordSecret(UserPasswordSecret) - By(fmt.Sprintf("Creating the Database User %s", kube.ObjectKeyFromObject(createdDBUser)), func() { - Expect(k8sClient.Create(context.Background(), createdDBUser)).ToNot(HaveOccurred()) + It("Should Succeed", func() { + createdDBUser := mdbv1.DefaultDBUser(namespace.Name, "test-db-user", createdProject.Name).WithPasswordSecret(UserPasswordSecret) + By(fmt.Sprintf("Creating the Database User %s", kube.ObjectKeyFromObject(createdDBUser)), func() { + Expect(k8sClient.Create(context.Background(), createdDBUser)).ToNot(HaveOccurred()) - Eventually(testutil.WaitFor(k8sClient, createdDBUser, status.TrueCondition(status.ReadyType)), - 20, interval).Should(BeTrue()) + Eventually(testutil.WaitFor(k8sClient, createdDBUser, status.TrueCondition(status.ReadyType)), + 20, interval).Should(BeTrue()) - checkUserInAtlas(createdProject.ID(), *createdDBUser) - }) + checkUserInAtlas(createdProject.ID(), *createdDBUser) + }) - createdCluster = mdbv1.DefaultGCPCluster(namespace.Name, createdProject.Name) - By(fmt.Sprintf("Creating the Cluster %s", kube.ObjectKeyFromObject(createdCluster)), func() { - Expect(k8sClient.Create(context.Background(), createdCluster)).ToNot(HaveOccurred()) + createdCluster = mdbv1.DefaultGCPCluster(namespace.Name, createdProject.Name) + By(fmt.Sprintf("Creating the Cluster %s", kube.ObjectKeyFromObject(createdCluster)), func() { + Expect(k8sClient.Create(context.Background(), createdCluster)).ToNot(HaveOccurred()) - Eventually(testutil.WaitFor(k8sClient, createdCluster, status.TrueCondition(status.ReadyType), validateClusterCreatingFunc()), - 1800, interval).Should(BeTrue()) + Eventually(testutil.WaitFor(k8sClient, createdCluster, status.TrueCondition(status.ReadyType), validateClusterCreatingFunc()), + 1800, interval).Should(BeTrue()) - doCommonChecks() - checkAtlasState() - }) + doCommonChecks() + checkAtlasState() + }) - By("Checking connection Secrets", func() { - Eventually(tryConnect(createdProject.ID(), *createdCluster, *createdDBUser), 90, interval).Should(Succeed()) - validateSecret(k8sClient, *createdProject, *createdCluster, *createdDBUser) - checkNumberOfConnectionSecrets(k8sClient, *createdProject, 2) + By("Checking connection Secrets", func() { + Eventually(tryConnect(createdProject.ID(), *createdCluster, *createdDBUser), 90, interval).Should(Succeed()) + validateSecret(k8sClient, *createdProject, *createdCluster, *createdDBUser) + checkNumberOfConnectionSecrets(k8sClient, *createdProject, 2) - expectedSecretsInStatus := map[string]string{ - "test-cluster-gcp": kube.NormalizeIdentifier(fmt.Sprintf("%s-%s-%s", createdProject.Spec.Name, createdCluster.Spec.Name, createdDBUser.Spec.Username)), - } - Expect(createdDBUser.Status.ConnectionSecrets).To(Equal(expectedSecretsInStatus)) + expectedSecretsInStatus := map[string]string{ + "test-cluster-gcp": kube.NormalizeIdentifier(fmt.Sprintf("%s-%s-%s", createdProject.Spec.Name, createdCluster.Spec.Name, createdDBUser.Spec.Username)), + } + Expect(createdDBUser.Status.ConnectionSecrets).To(Equal(expectedSecretsInStatus)) + }) }) }) }) From 232ac356594caa81ba915d59090fb0fac0da4725 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 23 Mar 2021 16:21:27 +0300 Subject: [PATCH 04/17] Fix secret not being created --- test/int/cluster_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/int/cluster_test.go b/test/int/cluster_test.go index 73e82cc987..719ae0d4f9 100644 --- a/test/int/cluster_test.go +++ b/test/int/cluster_test.go @@ -395,6 +395,11 @@ var _ = Describe("AtlasCluster", func() { Describe("Create DBUser before cluster & check secrets", func() { It("Should Succeed", func() { + By(fmt.Sprintf("Creating password Secret %s", UserPasswordSecret), func() { + passwordSecret := buildPasswordSecret(UserPasswordSecret, DBUserPassword) + Expect(k8sClient.Create(context.Background(), &passwordSecret)).To(Succeed()) + }) + createdDBUser := mdbv1.DefaultDBUser(namespace.Name, "test-db-user", createdProject.Name).WithPasswordSecret(UserPasswordSecret) By(fmt.Sprintf("Creating the Database User %s", kube.ObjectKeyFromObject(createdDBUser)), func() { Expect(k8sClient.Create(context.Background(), createdDBUser)).ToNot(HaveOccurred()) From 81141b3448acf7bed2ce1a729b38af52dee5a566 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 24 Mar 2021 15:33:39 +0300 Subject: [PATCH 05/17] Add WaitFor for DBUser --- test/int/cluster_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/int/cluster_test.go b/test/int/cluster_test.go index 719ae0d4f9..810f43cfb8 100644 --- a/test/int/cluster_test.go +++ b/test/int/cluster_test.go @@ -422,6 +422,9 @@ var _ = Describe("AtlasCluster", func() { }) By("Checking connection Secrets", func() { + Eventually(testutil.WaitFor(k8sClient, createdDBUser, status.TrueCondition(status.ReadyType), validateDatabaseUserWaitingForCluster()), + 1800, interval).Should(BeTrue()) + Eventually(tryConnect(createdProject.ID(), *createdCluster, *createdDBUser), 90, interval).Should(Succeed()) validateSecret(k8sClient, *createdProject, *createdCluster, *createdDBUser) checkNumberOfConnectionSecrets(k8sClient, *createdProject, 2) From 7f01f274e230101c2b2ce2fe3397e917258fdfe8 Mon Sep 17 00:00:00 2001 From: Pavel Vasilev Date: Mon, 29 Mar 2021 20:36:52 +0300 Subject: [PATCH 06/17] Add IP accesslists to project --- pkg/api/v1/status/zz_generated.deepcopy.go | 7 +++++++ test/int/cluster_test.go | 6 ++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/api/v1/status/zz_generated.deepcopy.go b/pkg/api/v1/status/zz_generated.deepcopy.go index 5370515171..978cd23f05 100644 --- a/pkg/api/v1/status/zz_generated.deepcopy.go +++ b/pkg/api/v1/status/zz_generated.deepcopy.go @@ -41,6 +41,13 @@ func (in *AtlasClusterStatus) DeepCopy() *AtlasClusterStatus { func (in *AtlasDatabaseUserStatus) DeepCopyInto(out *AtlasDatabaseUserStatus) { *out = *in in.Common.DeepCopyInto(&out.Common) + if in.ConnectionSecrets != nil { + in, out := &in.ConnectionSecrets, &out.ConnectionSecrets + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AtlasDatabaseUserStatus. diff --git a/test/int/cluster_test.go b/test/int/cluster_test.go index 810f43cfb8..55acca99d5 100644 --- a/test/int/cluster_test.go +++ b/test/int/cluster_test.go @@ -13,6 +13,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" mdbv1 "github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1" + "github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1/project" "github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1/status" "github.com/mongodb/mongodb-atlas-kubernetes/pkg/controller/workflow" "github.com/mongodb/mongodb-atlas-kubernetes/pkg/util/kube" @@ -46,7 +47,8 @@ var _ = Describe("AtlasCluster", func() { By(fmt.Sprintf("Creating the Secret %s", kube.ObjectKeyFromObject(&connectionSecret))) Expect(k8sClient.Create(context.Background(), &connectionSecret)).To(Succeed()) - createdProject = mdbv1.DefaultProject(namespace.Name, connectionSecret.Name) + createdProject = mdbv1.DefaultProject(namespace.Name, connectionSecret.Name).WithIPAccessList(project.NewIPAccessList().WithIP("0.0.0.0/0")) + By("Creating the project " + createdProject.Name) Expect(k8sClient.Create(context.Background(), createdProject)).To(Succeed()) Eventually(testutil.WaitFor(k8sClient, createdProject, status.TrueCondition(status.ReadyType)), @@ -393,7 +395,7 @@ var _ = Describe("AtlasCluster", func() { }) }) - Describe("Create DBUser before cluster & check secrets", func() { + FDescribe("Create DBUser before cluster & check secrets", func() { It("Should Succeed", func() { By(fmt.Sprintf("Creating password Secret %s", UserPasswordSecret), func() { passwordSecret := buildPasswordSecret(UserPasswordSecret, DBUserPassword) From 59c9e11bd140ff65a1ecfee5c4cf9f5911869318 Mon Sep 17 00:00:00 2001 From: Pavel Vasilev Date: Tue, 30 Mar 2021 11:06:36 +0300 Subject: [PATCH 07/17] Remove FDescribe --- test/int/cluster_test.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/int/cluster_test.go b/test/int/cluster_test.go index 55acca99d5..b7677cd5be 100644 --- a/test/int/cluster_test.go +++ b/test/int/cluster_test.go @@ -395,7 +395,7 @@ var _ = Describe("AtlasCluster", func() { }) }) - FDescribe("Create DBUser before cluster & check secrets", func() { + Describe("Create DBUser before cluster & check secrets", func() { It("Should Succeed", func() { By(fmt.Sprintf("Creating password Secret %s", UserPasswordSecret), func() { passwordSecret := buildPasswordSecret(UserPasswordSecret, DBUserPassword) @@ -424,9 +424,6 @@ var _ = Describe("AtlasCluster", func() { }) By("Checking connection Secrets", func() { - Eventually(testutil.WaitFor(k8sClient, createdDBUser, status.TrueCondition(status.ReadyType), validateDatabaseUserWaitingForCluster()), - 1800, interval).Should(BeTrue()) - Eventually(tryConnect(createdProject.ID(), *createdCluster, *createdDBUser), 90, interval).Should(Succeed()) validateSecret(k8sClient, *createdProject, *createdCluster, *createdDBUser) checkNumberOfConnectionSecrets(k8sClient, *createdProject, 2) From f542cecf12ed00d2d3c3115e09da49c13b8a61f7 Mon Sep 17 00:00:00 2001 From: Pavel Vasilev Date: Tue, 30 Mar 2021 12:37:36 +0300 Subject: [PATCH 08/17] Replace Eventually with Expect --- test/int/cluster_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/int/cluster_test.go b/test/int/cluster_test.go index b7677cd5be..20ecaea36d 100644 --- a/test/int/cluster_test.go +++ b/test/int/cluster_test.go @@ -424,7 +424,7 @@ var _ = Describe("AtlasCluster", func() { }) By("Checking connection Secrets", func() { - Eventually(tryConnect(createdProject.ID(), *createdCluster, *createdDBUser), 90, interval).Should(Succeed()) + Expect(tryConnect(createdProject.ID(), *createdCluster, *createdDBUser), 90, interval).To(Succeed()) validateSecret(k8sClient, *createdProject, *createdCluster, *createdDBUser) checkNumberOfConnectionSecrets(k8sClient, *createdProject, 2) From f61d31f66b44c610b419b54dd8b68a1605219888 Mon Sep 17 00:00:00 2001 From: Pavel Vasilev Date: Tue, 30 Mar 2021 13:36:10 +0300 Subject: [PATCH 09/17] Check test order after merges --- test/int/cluster_test.go | 60 ++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/test/int/cluster_test.go b/test/int/cluster_test.go index 0810c578da..55cf6f2423 100644 --- a/test/int/cluster_test.go +++ b/test/int/cluster_test.go @@ -495,45 +495,45 @@ var _ = Describe("AtlasCluster", func() { checkUserInAtlas(createdProject.ID(), *createdDBUser) }) }) + }) - Describe("Create cluster, user, delete cluster and check secrets are removed", func() { - It("Should Succeed", func() { - createdCluster = mdbv1.DefaultGCPCluster(namespace.Name, createdProject.Name) - By(fmt.Sprintf("Creating the Cluster %s", kube.ObjectKeyFromObject(createdCluster)), func() { - Expect(k8sClient.Create(context.Background(), createdCluster)).ToNot(HaveOccurred()) - - Eventually(testutil.WaitFor(k8sClient, createdCluster, status.TrueCondition(status.ReadyType), validateClusterCreatingFunc()), - 1800, interval).Should(BeTrue()) + Describe("Create cluster, user, delete cluster and check secrets are removed", func() { + It("Should Succeed", func() { + createdCluster = mdbv1.DefaultGCPCluster(namespace.Name, createdProject.Name) + By(fmt.Sprintf("Creating the Cluster %s", kube.ObjectKeyFromObject(createdCluster)), func() { + Expect(k8sClient.Create(context.Background(), createdCluster)).ToNot(HaveOccurred()) - doCommonStatusChecks() - checkAtlasState() - }) + Eventually(testutil.WaitFor(k8sClient, createdCluster, status.TrueCondition(status.ReadyType), validateClusterCreatingFunc()), + 1800, interval).Should(BeTrue()) - passwordSecret := buildPasswordSecret(UserPasswordSecret, DBUserPassword) - Expect(k8sClient.Create(context.Background(), &passwordSecret)).To(Succeed()) + doCommonStatusChecks() + checkAtlasState() + }) - createdDBUser := mdbv1.DefaultDBUser(namespace.Name, "test-db-user", createdProject.Name).WithPasswordSecret(UserPasswordSecret) - By(fmt.Sprintf("Creating the Database User %s", kube.ObjectKeyFromObject(createdDBUser)), func() { - Expect(k8sClient.Create(context.Background(), createdDBUser)).ToNot(HaveOccurred()) + passwordSecret := buildPasswordSecret(UserPasswordSecret, DBUserPassword) + Expect(k8sClient.Create(context.Background(), &passwordSecret)).To(Succeed()) - Eventually(testutil.WaitFor(k8sClient, createdDBUser, status.TrueCondition(status.ReadyType)), - 80, interval).Should(BeTrue()) - }) + createdDBUser := mdbv1.DefaultDBUser(namespace.Name, "test-db-user", createdProject.Name).WithPasswordSecret(UserPasswordSecret) + By(fmt.Sprintf("Creating the Database User %s", kube.ObjectKeyFromObject(createdDBUser)), func() { + Expect(k8sClient.Create(context.Background(), createdDBUser)).ToNot(HaveOccurred()) - By("Removing Atlas Cluster "+createdCluster.Name, func() { - Expect(k8sClient.Delete(context.Background(), createdCluster)).To(Succeed()) - Eventually(checkAtlasClusterRemoved(createdProject.Status.ID, createdCluster.Spec.Name), 600, interval).Should(BeTrue()) - }) + Eventually(testutil.WaitFor(k8sClient, createdDBUser, status.TrueCondition(status.ReadyType)), + 80, interval).Should(BeTrue()) + }) - By("Checking that Secrets got removed", func() { - secretNames := []string{kube.NormalizeIdentifier(fmt.Sprintf("%s-%s-%s", createdProject.Spec.Name, createdCluster.Spec.Name, createdDBUser.Spec.Username))} - Eventually(checkSecretsDontExist(namespace.Name, secretNames), 50, interval).Should(BeTrue()) - checkNumberOfConnectionSecrets(k8sClient, *createdProject, 0) - }) + By("Removing Atlas Cluster "+createdCluster.Name, func() { + Expect(k8sClient.Delete(context.Background(), createdCluster)).To(Succeed()) + Eventually(checkAtlasClusterRemoved(createdProject.Status.ID, createdCluster.Spec.Name), 600, interval).Should(BeTrue()) + }) - // prevent cleanup from failing due to cluster already deleted - createdCluster = nil + By("Checking that Secrets got removed", func() { + secretNames := []string{kube.NormalizeIdentifier(fmt.Sprintf("%s-%s-%s", createdProject.Spec.Name, createdCluster.Spec.Name, createdDBUser.Spec.Username))} + Eventually(checkSecretsDontExist(namespace.Name, secretNames), 50, interval).Should(BeTrue()) + checkNumberOfConnectionSecrets(k8sClient, *createdProject, 0) }) + + // prevent cleanup from failing due to cluster already deleted + createdCluster = nil }) }) }) From 70c0c198386a9139250491a21d9a5ddc059384b6 Mon Sep 17 00:00:00 2001 From: Pavel Vasilev Date: Tue, 30 Mar 2021 15:32:13 +0300 Subject: [PATCH 10/17] Fix missing test case body after merge --- test/int/cluster_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/int/cluster_test.go b/test/int/cluster_test.go index 55cf6f2423..edca1e9fff 100644 --- a/test/int/cluster_test.go +++ b/test/int/cluster_test.go @@ -494,6 +494,23 @@ var _ = Describe("AtlasCluster", func() { checkUserInAtlas(createdProject.ID(), *createdDBUser) }) + + createdCluster = mdbv1.DefaultGCPCluster(namespace.Name, createdProject.Name) + By(fmt.Sprintf("Creating the Cluster %s", kube.ObjectKeyFromObject(createdCluster)), func() { + Expect(k8sClient.Create(context.Background(), createdCluster)).ToNot(HaveOccurred()) + + Eventually(testutil.WaitFor(k8sClient, createdCluster, status.TrueCondition(status.ReadyType), validateClusterCreatingFunc()), + 1800, interval).Should(BeTrue()) + + doCommonStatusChecks() + checkAtlasState() + }) + + By("Checking connection Secrets", func() { + Expect(tryConnect(createdProject.ID(), *createdCluster, *createdDBUser), 90, interval).To(Succeed()) + validateSecret(k8sClient, *createdProject, *createdCluster, *createdDBUser) + checkNumberOfConnectionSecrets(k8sClient, *createdProject, 1) + }) }) }) From 3f299fc02495f52c8167f64d33e64700a5e3e5c2 Mon Sep 17 00:00:00 2001 From: Pavel Vasilev Date: Tue, 30 Mar 2021 16:35:30 +0300 Subject: [PATCH 11/17] Fix arguments --- test/int/cluster_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/int/cluster_test.go b/test/int/cluster_test.go index edca1e9fff..39a42218d4 100644 --- a/test/int/cluster_test.go +++ b/test/int/cluster_test.go @@ -507,7 +507,7 @@ var _ = Describe("AtlasCluster", func() { }) By("Checking connection Secrets", func() { - Expect(tryConnect(createdProject.ID(), *createdCluster, *createdDBUser), 90, interval).To(Succeed()) + Expect(tryConnect(createdProject.ID(), *createdCluster, *createdDBUser)).To(Succeed()) validateSecret(k8sClient, *createdProject, *createdCluster, *createdDBUser) checkNumberOfConnectionSecrets(k8sClient, *createdProject, 1) }) From ad2c3379e3369c3b6b6754fbb32fc01d59813908 Mon Sep 17 00:00:00 2001 From: Pavel Vasilev Date: Tue, 30 Mar 2021 18:12:57 +0300 Subject: [PATCH 12/17] Fix users not populated in ensureConnectionSecrets --- pkg/controller/atlascluster/cluster.go | 4 ++++ test/int/cluster_test.go | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/controller/atlascluster/cluster.go b/pkg/controller/atlascluster/cluster.go index 9c58c5fe1b..0a180dda30 100644 --- a/pkg/controller/atlascluster/cluster.go +++ b/pkg/controller/atlascluster/cluster.go @@ -144,6 +144,10 @@ func ClustersEqual(log *zap.SugaredLogger, clusterAtlas mongodbatlas.Cluster, cl func ensureConnectionSecrets(wctx *workflow.Context, k8sClient client.Client, project *mdbv1.AtlasProject, cluster *mongodbatlas.Cluster) workflow.Result { databaseUsers := mdbv1.AtlasDatabaseUserList{} + err := k8sClient.List(context.TODO(), &databaseUsers, client.InNamespace(project.Namespace)) + if err != nil { + return workflow.Terminate(workflow.Internal, err.Error()) + } for _, dbUser := range databaseUsers.Items { found := false diff --git a/test/int/cluster_test.go b/test/int/cluster_test.go index 39a42218d4..15782349fa 100644 --- a/test/int/cluster_test.go +++ b/test/int/cluster_test.go @@ -508,8 +508,8 @@ var _ = Describe("AtlasCluster", func() { By("Checking connection Secrets", func() { Expect(tryConnect(createdProject.ID(), *createdCluster, *createdDBUser)).To(Succeed()) - validateSecret(k8sClient, *createdProject, *createdCluster, *createdDBUser) checkNumberOfConnectionSecrets(k8sClient, *createdProject, 1) + validateSecret(k8sClient, *createdProject, *createdCluster, *createdDBUser) }) }) }) From d48ffd6a77ee7dc2b05d9866d8fb704eac3bc85b Mon Sep 17 00:00:00 2001 From: Pavel Vasilev Date: Tue, 30 Mar 2021 18:52:44 +0300 Subject: [PATCH 13/17] Fixes after review --- .../atlascluster/atlascluster_controller.go | 2 +- pkg/controller/atlascluster/cluster.go | 35 ++++++++++++++----- pkg/controller/workflow/reason.go | 9 ++--- test/int/cluster_test.go | 10 ++++++ 4 files changed, 42 insertions(+), 14 deletions(-) diff --git a/pkg/controller/atlascluster/atlascluster_controller.go b/pkg/controller/atlascluster/atlascluster_controller.go index db29ff6323..e4565176b9 100644 --- a/pkg/controller/atlascluster/atlascluster_controller.go +++ b/pkg/controller/atlascluster/atlascluster_controller.go @@ -102,7 +102,7 @@ func (r *AtlasClusterReconciler) Reconcile(context context.Context, req ctrl.Req return result.ReconcileResult(), nil } - if csResult := ensureConnectionSecrets(ctx, r.Client, project, c); !csResult.IsOk() { + if csResult := ensureConnectionSecrets(r.Client, project, c); !csResult.IsOk() { ctx.SetConditionFromResult(status.ClusterReadyType, csResult) return csResult.ReconcileResult(), nil } diff --git a/pkg/controller/atlascluster/cluster.go b/pkg/controller/atlascluster/cluster.go index 0a180dda30..26e9977c10 100644 --- a/pkg/controller/atlascluster/cluster.go +++ b/pkg/controller/atlascluster/cluster.go @@ -9,12 +9,15 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "go.mongodb.org/atlas/mongodbatlas" "go.uber.org/zap" + v1 "k8s.io/api/core/v1" "sigs.k8s.io/controller-runtime/pkg/client" mdbv1 "github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1" - "github.com/mongodb/mongodb-atlas-kubernetes/pkg/controller/atlasdatabaseuser" + "github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1/status" + "github.com/mongodb/mongodb-atlas-kubernetes/pkg/controller/connectionsecret" "github.com/mongodb/mongodb-atlas-kubernetes/pkg/controller/workflow" "github.com/mongodb/mongodb-atlas-kubernetes/pkg/util/compat" + "github.com/mongodb/mongodb-atlas-kubernetes/pkg/util/stringutil" ) func (r *AtlasClusterReconciler) ensureClusterState(ctx *workflow.Context, project *mdbv1.AtlasProject, cluster *mdbv1.AtlasCluster) (atlasCluster *mongodbatlas.Cluster, _ workflow.Result) { @@ -142,7 +145,7 @@ func ClustersEqual(log *zap.SugaredLogger, clusterAtlas mongodbatlas.Cluster, cl return d == "" } -func ensureConnectionSecrets(wctx *workflow.Context, k8sClient client.Client, project *mdbv1.AtlasProject, cluster *mongodbatlas.Cluster) workflow.Result { +func ensureConnectionSecrets(k8sClient client.Client, project *mdbv1.AtlasProject, cluster *mongodbatlas.Cluster) workflow.Result { databaseUsers := mdbv1.AtlasDatabaseUserList{} err := k8sClient.List(context.TODO(), &databaseUsers, client.InNamespace(project.Namespace)) if err != nil { @@ -151,23 +154,37 @@ func ensureConnectionSecrets(wctx *workflow.Context, k8sClient client.Client, pr for _, dbUser := range databaseUsers.Items { found := false - - for _, scope := range dbUser.GetScopes(mdbv1.ClusterScopeType) { - if scope == cluster.Name { + for _, c := range dbUser.Status.Conditions { + if c.Type == status.ReadyType && c.Status == v1.ConditionTrue { found = true break } + } + if !found { continue } - if !found { + scopes := dbUser.GetScopes(mdbv1.ClusterScopeType) + if len(scopes) != 0 && !stringutil.Contains(scopes, cluster.Name) { continue } - result := atlasdatabaseuser.CreateOrUpdateConnectionSecrets(wctx, k8sClient, *project, dbUser) - if !result.IsOk() { - return result + password, err := dbUser.ReadPassword(k8sClient) + if err != nil { + return workflow.Terminate(workflow.ClusterConnectionSecretsNotCreated, err.Error()) + } + + data := connectionsecret.ConnectionData{ + DBUserName: dbUser.Spec.Username, + ConnURL: cluster.ConnectionStrings.Standard, + SrvConnURL: cluster.ConnectionStrings.StandardSrv, + Password: password, + } + + _, err = connectionsecret.Ensure(k8sClient, project.Namespace, project.Spec.Name, project.ID(), cluster.Name, data) + if err != nil { + return workflow.Terminate(workflow.ClusterConnectionSecretsNotCreated, err.Error()) } } diff --git a/pkg/controller/workflow/reason.go b/pkg/controller/workflow/reason.go index a55469cb24..79ac888045 100644 --- a/pkg/controller/workflow/reason.go +++ b/pkg/controller/workflow/reason.go @@ -19,10 +19,11 @@ const ( // Atlas Cluster reasons const ( - ClusterNotCreatedInAtlas ConditionReason = "ClusterNotCreatedInAtlas" - ClusterNotUpdatedInAtlas ConditionReason = "ClusterNotUpdatedInAtlas" - ClusterCreating ConditionReason = "ClusterCreating" - ClusterUpdating ConditionReason = "ClusterUpdating" + ClusterNotCreatedInAtlas ConditionReason = "ClusterNotCreatedInAtlas" + ClusterNotUpdatedInAtlas ConditionReason = "ClusterNotUpdatedInAtlas" + ClusterCreating ConditionReason = "ClusterCreating" + ClusterUpdating ConditionReason = "ClusterUpdating" + ClusterConnectionSecretsNotCreated ConditionReason = "ClusterConnectionSecretsNotCreated" ) // Atlas Database User reasons diff --git a/test/int/cluster_test.go b/test/int/cluster_test.go index 15782349fa..02235d2c3e 100644 --- a/test/int/cluster_test.go +++ b/test/int/cluster_test.go @@ -495,6 +495,16 @@ var _ = Describe("AtlasCluster", func() { checkUserInAtlas(createdProject.ID(), *createdDBUser) }) + createdDBUserFakeScope := mdbv1.DefaultDBUser(namespace.Name, "test-db-user", createdProject.Name). + WithPasswordSecret(UserPasswordSecret). + WithScope(mdbv1.ClusterScopeType, "fake-cluster") + By(fmt.Sprintf("Creating the Database User %s", kube.ObjectKeyFromObject(createdDBUserFakeScope)), func() { + Expect(k8sClient.Create(context.Background(), createdDBUserFakeScope)).ToNot(HaveOccurred()) + + Eventually(testutil.WaitFor(k8sClient, createdDBUserFakeScope, status.FalseCondition(status.DatabaseUserReadyType).WithReason(string(workflow.DatabaseUserInvalidSpec))), + 20, interval).Should(BeTrue()) + }) + createdCluster = mdbv1.DefaultGCPCluster(namespace.Name, createdProject.Name) By(fmt.Sprintf("Creating the Cluster %s", kube.ObjectKeyFromObject(createdCluster)), func() { Expect(k8sClient.Create(context.Background(), createdCluster)).ToNot(HaveOccurred()) From 3659b847319ffb9a03eda034dbec7c3ef2ee2565 Mon Sep 17 00:00:00 2001 From: antonlisovenko Date: Wed, 31 Mar 2021 09:31:57 +0100 Subject: [PATCH 14/17] fix test --- test/int/cluster_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/int/cluster_test.go b/test/int/cluster_test.go index 02235d2c3e..d732c41079 100644 --- a/test/int/cluster_test.go +++ b/test/int/cluster_test.go @@ -495,7 +495,7 @@ var _ = Describe("AtlasCluster", func() { checkUserInAtlas(createdProject.ID(), *createdDBUser) }) - createdDBUserFakeScope := mdbv1.DefaultDBUser(namespace.Name, "test-db-user", createdProject.Name). + createdDBUserFakeScope := mdbv1.DefaultDBUser(namespace.Name, "test-db-user-fake-scope", createdProject.Name). WithPasswordSecret(UserPasswordSecret). WithScope(mdbv1.ClusterScopeType, "fake-cluster") By(fmt.Sprintf("Creating the Database User %s", kube.ObjectKeyFromObject(createdDBUserFakeScope)), func() { From a6d9c5a1b2fd0b6065bb0a9edec920bd4e353cd0 Mon Sep 17 00:00:00 2001 From: antonlisovenko Date: Wed, 31 Mar 2021 11:32:44 +0100 Subject: [PATCH 15/17] minor changes --- pkg/controller/atlascluster/atlascluster_controller.go | 2 +- pkg/controller/atlascluster/cluster.go | 3 ++- test/int/cluster_test.go | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/controller/atlascluster/atlascluster_controller.go b/pkg/controller/atlascluster/atlascluster_controller.go index e4565176b9..db29ff6323 100644 --- a/pkg/controller/atlascluster/atlascluster_controller.go +++ b/pkg/controller/atlascluster/atlascluster_controller.go @@ -102,7 +102,7 @@ func (r *AtlasClusterReconciler) Reconcile(context context.Context, req ctrl.Req return result.ReconcileResult(), nil } - if csResult := ensureConnectionSecrets(r.Client, project, c); !csResult.IsOk() { + if csResult := ensureConnectionSecrets(ctx, r.Client, project, c); !csResult.IsOk() { ctx.SetConditionFromResult(status.ClusterReadyType, csResult) return csResult.ReconcileResult(), nil } diff --git a/pkg/controller/atlascluster/cluster.go b/pkg/controller/atlascluster/cluster.go index 26e9977c10..66f6fa29b8 100644 --- a/pkg/controller/atlascluster/cluster.go +++ b/pkg/controller/atlascluster/cluster.go @@ -145,7 +145,7 @@ func ClustersEqual(log *zap.SugaredLogger, clusterAtlas mongodbatlas.Cluster, cl return d == "" } -func ensureConnectionSecrets(k8sClient client.Client, project *mdbv1.AtlasProject, cluster *mongodbatlas.Cluster) workflow.Result { +func ensureConnectionSecrets(ctx *workflow.Context, k8sClient client.Client, project *mdbv1.AtlasProject, cluster *mongodbatlas.Cluster) workflow.Result { databaseUsers := mdbv1.AtlasDatabaseUserList{} err := k8sClient.List(context.TODO(), &databaseUsers, client.InNamespace(project.Namespace)) if err != nil { @@ -162,6 +162,7 @@ func ensureConnectionSecrets(k8sClient client.Client, project *mdbv1.AtlasProjec } if !found { + ctx.Log.Debugw("AtlasDatabaseUser not ready - not creating connection secret", "user.name", dbUser.Name) continue } diff --git a/test/int/cluster_test.go b/test/int/cluster_test.go index d732c41079..7884108cad 100644 --- a/test/int/cluster_test.go +++ b/test/int/cluster_test.go @@ -504,6 +504,7 @@ var _ = Describe("AtlasCluster", func() { Eventually(testutil.WaitFor(k8sClient, createdDBUserFakeScope, status.FalseCondition(status.DatabaseUserReadyType).WithReason(string(workflow.DatabaseUserInvalidSpec))), 20, interval).Should(BeTrue()) }) + checkNumberOfConnectionSecrets(k8sClient, *createdProject, 0) createdCluster = mdbv1.DefaultGCPCluster(namespace.Name, createdProject.Name) By(fmt.Sprintf("Creating the Cluster %s", kube.ObjectKeyFromObject(createdCluster)), func() { From 22f575cb672f2acefb03744248001a81013ba860 Mon Sep 17 00:00:00 2001 From: antonlisovenko Date: Wed, 31 Mar 2021 12:43:37 +0100 Subject: [PATCH 16/17] e2e verbose --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 22ba1911a2..047c1e912a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -151,7 +151,7 @@ jobs: go version go get github.com/onsi/ginkgo/ginkgo && \ go get github.com/onsi/gomega/... - ginkgo --focus "${TEST_NAME}" -nodes=3 test/e2e/ + ginkgo --focus "${TEST_NAME}" -v -nodes=3 test/e2e/ - name: Upload operator logs if: ${{ failure() }} From ae16708e0a4e629e44944aa1cf436818494e38bd Mon Sep 17 00:00:00 2001 From: antonlisovenko Date: Wed, 31 Mar 2021 13:43:09 +0100 Subject: [PATCH 17/17] increase timeouts --- test/int/cluster_test.go | 17 +++++++++-------- test/int/dbuser_test.go | 12 ++++++------ 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/test/int/cluster_test.go b/test/int/cluster_test.go index b98693a4c6..5bb5d45c06 100644 --- a/test/int/cluster_test.go +++ b/test/int/cluster_test.go @@ -25,7 +25,8 @@ import ( const ( // Set this to true if you are debugging cluster creation. // This may not help much if there was the update though... - ClusterDevMode = false + ClusterDevMode = false + ClusterUpdateTimeout = 3600 ) var _ = Describe("AtlasCluster", func() { @@ -208,7 +209,7 @@ var _ = Describe("AtlasCluster", func() { Expect(k8sClient.Create(context.Background(), expectedCluster)).ToNot(HaveOccurred()) Eventually(testutil.WaitFor(k8sClient, createdCluster, status.TrueCondition(status.ReadyType), validateClusterCreatingFunc()), - 1800, interval).Should(BeTrue()) + ClusterUpdateTimeout, interval).Should(BeTrue()) doCommonStatusChecks() checkAtlasState() @@ -287,7 +288,7 @@ var _ = Describe("AtlasCluster", func() { Expect(k8sClient.Create(context.Background(), createdCluster)).To(Succeed()) Eventually(testutil.WaitFor(k8sClient, createdCluster, status.TrueCondition(status.ReadyType), validateClusterCreatingFunc()), - 1800, interval).Should(BeTrue()) + ClusterUpdateTimeout, interval).Should(BeTrue()) doCommonStatusChecks() @@ -304,10 +305,10 @@ var _ = Describe("AtlasCluster", func() { createdCluster.Spec.ProviderSettings.AutoScaling.Compute.MaxInstanceSize = "M30" - performUpdate(80 * time.Minute) + performUpdate(ClusterUpdateTimeout * time.Minute) Eventually(testutil.WaitFor(k8sClient, createdCluster, status.TrueCondition(status.ReadyType), validateClusterUpdatingFunc()), - 1800, interval).Should(BeTrue()) + ClusterUpdateTimeout, interval).Should(BeTrue()) doCommonStatusChecks() @@ -359,7 +360,7 @@ var _ = Describe("AtlasCluster", func() { Expect(k8sClient.Create(context.Background(), createdCluster)).ToNot(HaveOccurred()) Eventually(testutil.WaitFor(k8sClient, createdCluster, status.TrueCondition(status.ReadyType), validateClusterCreatingFunc()), - 1800, interval).Should(BeTrue()) + ClusterUpdateTimeout, interval).Should(BeTrue()) doCommonStatusChecks() checkAtlasState() @@ -532,7 +533,7 @@ var _ = Describe("AtlasCluster", func() { Expect(k8sClient.Create(context.Background(), createdCluster)).ToNot(HaveOccurred()) Eventually(testutil.WaitFor(k8sClient, createdCluster, status.TrueCondition(status.ReadyType), validateClusterCreatingFunc()), - 1800, interval).Should(BeTrue()) + ClusterUpdateTimeout, interval).Should(BeTrue()) doCommonStatusChecks() checkAtlasState() @@ -553,7 +554,7 @@ var _ = Describe("AtlasCluster", func() { Expect(k8sClient.Create(context.Background(), createdCluster)).ToNot(HaveOccurred()) Eventually(testutil.WaitFor(k8sClient, createdCluster, status.TrueCondition(status.ReadyType), validateClusterCreatingFunc()), - 1800, interval).Should(BeTrue()) + ClusterUpdateTimeout, interval).Should(BeTrue()) doCommonStatusChecks() checkAtlasState() diff --git a/test/int/dbuser_test.go b/test/int/dbuser_test.go index 1bdcd0ee14..915560a2c5 100644 --- a/test/int/dbuser_test.go +++ b/test/int/dbuser_test.go @@ -144,7 +144,7 @@ var _ = Describe("AtlasDatabaseUser", func() { Expect(k8sClient.Create(context.Background(), createdClusterGCP)).ToNot(HaveOccurred()) Eventually(testutil.WaitFor(k8sClient, createdClusterAWS, status.TrueCondition(status.ReadyType), validateClusterCreatingFunc()), - 1800, interval).Should(BeTrue()) + ClusterUpdateTimeout, interval).Should(BeTrue()) Eventually(testutil.WaitFor(k8sClient, createdClusterGCP, status.TrueCondition(status.ReadyType), validateClusterCreatingFunc()), 500, interval).Should(BeTrue()) @@ -304,7 +304,7 @@ var _ = Describe("AtlasDatabaseUser", func() { // DatabaseUser will wait for the cluster to get created. Eventually(testutil.WaitFor(k8sClient, createdDBUser, status.TrueCondition(status.ReadyType)), - 1800, interval).Should(BeTrue()) + ClusterUpdateTimeout, interval).Should(BeTrue()) expectedConditionsMatchers := testutil.MatchConditions( status.TrueCondition(status.DatabaseUserReadyType), @@ -328,7 +328,7 @@ var _ = Describe("AtlasDatabaseUser", func() { Expect(k8sClient.Create(context.Background(), createdClusterGCP)).ToNot(HaveOccurred()) Eventually(testutil.WaitFor(k8sClient, createdClusterGCP, status.TrueCondition(status.ReadyType), validateClusterCreatingFunc()), - 1800, interval).Should(BeTrue()) + ClusterUpdateTimeout, interval).Should(BeTrue()) }) createdDBUser = mdbv1.DefaultDBUser(namespace.Name, "test-db-user", createdProject.Name).WithPasswordSecret(UserPasswordSecret) var connSecretInitial corev1.Secret @@ -384,10 +384,10 @@ var _ = Describe("AtlasDatabaseUser", func() { Expect(k8sClient.Create(context.Background(), createdClusterAzure)).ToNot(HaveOccurred()) Eventually(testutil.WaitFor(k8sClient, createdClusterGCP, status.TrueCondition(status.ReadyType), validateClusterCreatingFunc()), - 1800, interval).Should(BeTrue()) + ClusterUpdateTimeout, interval).Should(BeTrue()) Eventually(testutil.WaitFor(k8sClient, createdClusterAzure, status.TrueCondition(status.ReadyType), validateClusterCreatingFunc()), - 1800, interval).Should(BeTrue()) + ClusterUpdateTimeout, interval).Should(BeTrue()) }) createdDBUser = mdbv1.DefaultDBUser(namespace.Name, "test-db-user", createdProject.Name).WithPasswordSecret(UserPasswordSecret) @@ -445,7 +445,7 @@ var _ = Describe("AtlasDatabaseUser", func() { Expect(k8sClient.Create(context.Background(), createdClusterGCP)).To(Succeed()) Eventually(testutil.WaitFor(k8sClient, createdClusterGCP, status.TrueCondition(status.ReadyType), validateClusterCreatingFunc()), - 1800, interval).Should(BeTrue()) + ClusterUpdateTimeout, interval).Should(BeTrue()) }) By("Creating the expired Database User - no user created in Atlas", func() {