From a6f9412a316231e01a5c1f9af446a4e7e978f158 Mon Sep 17 00:00:00 2001 From: doodgeMatvey Date: Mon, 2 Oct 2023 14:48:17 +0300 Subject: [PATCH] issue-581, handling no external IPs or private cluster for PostgreSQL users is implemented --- .../postgresqluser_controller.go | 2 - controllers/clusters/postgresql_controller.go | 56 +++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/controllers/clusterresources/postgresqluser_controller.go b/controllers/clusterresources/postgresqluser_controller.go index c51f07236..601627e1d 100644 --- a/controllers/clusterresources/postgresqluser_controller.go +++ b/controllers/clusterresources/postgresqluser_controller.go @@ -166,8 +166,6 @@ func (r *PostgreSQLUserReconciler) Reconcile(ctx context.Context, req ctrl.Reque "User has been created for a cluster. Cluster ID: %s, username: %s", clusterID, newUsername) - // TODO: Add deletion user finalizers - continue } diff --git a/controllers/clusters/postgresql_controller.go b/controllers/clusters/postgresql_controller.go index fe443c5cb..4a26842de 100644 --- a/controllers/clusters/postgresql_controller.go +++ b/controllers/clusters/postgresql_controller.go @@ -296,6 +296,62 @@ func (r *PostgreSQLReconciler) handleCreateCluster( return models.ReconcileRequeue } + if pg.Spec.UserRefs != nil { + nodeList := &k8sCore.NodeList{} + + err = r.List(ctx, nodeList, &client.ListOptions{}) + if err != nil { + logger.Error(err, "Cannot list all nodes", + "cluster name", pg.Spec.Name, + "clusterID", pg.Status.ID, + ) + + r.EventRecorder.Eventf( + pg, models.Warning, models.CreationFailed, + "Fetching all nodes is failed. Reason: %v", + err, + ) + + return models.ReconcileRequeue + } + + var externalIPExists bool + for _, node := range nodeList.Items { + for _, nodeAddress := range node.Status.Addresses { + if nodeAddress.Type == k8sCore.NodeExternalIP { + externalIPExists = true + break + } + } + } + + if !externalIPExists || pg.Spec.PrivateNetworkCluster { + logger.Error(err, "Cannot create PostgreSQL user, if your cluster is private or has no external ips "+ + "you need to configure peering and remove user references from cluster specification", + "cluster name", pg.Spec.Name, + "clusterID", pg.Status.ID, + ) + + r.EventRecorder.Eventf( + pg, models.Warning, models.CreationFailed, + "Creating PostgreSQL user has been failed. Reason: %v", err, + ) + + return models.ReconcileRequeue + } + + err = r.startUsersCreationJob(pg) + if err != nil { + logger.Error(err, "Failed to start user PostreSQL creation job") + r.EventRecorder.Eventf(pg, models.Warning, models.CreationFailed, + "User creation job is failed. Reason: %v", err) + return models.ReconcileRequeue + } + + r.EventRecorder.Event(pg, models.Normal, models.Created, + "Cluster user creation job is started") + } + return models.ExitReconcile }