From e514dfd7811b2814ec1b30a2fc3b68128c58d2f0 Mon Sep 17 00:00:00 2001 From: Soohyun Kim Date: Wed, 22 Jul 2020 15:43:43 +0200 Subject: [PATCH] persist database and user info in status --- pkg/apis/kci/v1alpha1/database_types.go | 2 ++ pkg/controller/database/controller.go | 6 ----- pkg/controller/database/reconcileDatabase.go | 23 ++++++++++---------- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/pkg/apis/kci/v1alpha1/database_types.go b/pkg/apis/kci/v1alpha1/database_types.go index 9b49dc63..79ffd32e 100644 --- a/pkg/apis/kci/v1alpha1/database_types.go +++ b/pkg/apis/kci/v1alpha1/database_types.go @@ -30,6 +30,8 @@ type DatabaseStatus struct { InstanceRef *DbInstance `json:"instanceRef"` MonitorUserSecretName string `json:"monitorUserSecret,omitempty"` ProxyStatus DatabaseProxyStatus `json:"proxyStatus,omitempty"` + DatabaseName string `json:"database"` + UserName string `json:"user"` } // DatabaseProxyStatus defines whether proxy for database is enabled or not diff --git a/pkg/controller/database/controller.go b/pkg/controller/database/controller.go index 896eaec5..3a761fb1 100644 --- a/pkg/controller/database/controller.go +++ b/pkg/controller/database/controller.go @@ -207,12 +207,6 @@ func (r *ReconcileDatabase) Reconcile(request reconcile.Request) (reconcile.Resu // when database creation failed, don't requeue request. to prevent exceeding api limit (ex: against google api) return r.manageError(dbcr, err, false) } - kci.AddFinalizer(&dbcr.ObjectMeta, "db."+dbcr.Name) - err = r.client.Update(context.Background(), dbcr) - if err != nil { - logrus.Errorf("error resource updating - %s", err) - return r.manageError(dbcr, err, true) - } dbcr.Status.Phase = phaseInstanceAccessSecret case phaseInstanceAccessSecret: err := r.createInstanceAccessSecret(dbcr) diff --git a/pkg/controller/database/reconcileDatabase.go b/pkg/controller/database/reconcileDatabase.go index f173009e..37c1de44 100644 --- a/pkg/controller/database/reconcileDatabase.go +++ b/pkg/controller/database/reconcileDatabase.go @@ -114,6 +114,15 @@ func (r *ReconcileDatabase) createDatabase(dbcr *kciv1alpha1.Database) error { return err } + kci.AddFinalizer(&dbcr.ObjectMeta, "db."+dbcr.Name) + err = r.client.Update(context.Background(), dbcr) + if err != nil { + logrus.Errorf("error resource updating - %s", err) + return err + } + + dbcr.Status.DatabaseName = databaseCred.Name + dbcr.Status.UserName = databaseCred.Username logrus.Infof("DB: namespace=%s, name=%s successfully created", dbcr.Namespace, dbcr.Name) return nil } @@ -124,17 +133,9 @@ func (r *ReconcileDatabase) deleteDatabase(dbcr *kciv1alpha1.Database) error { return nil } - // Todo: save database in info and use it for deletion, instead of re-calculating dbname for deletion info - secretData, err := generateDatabaseSecretData(dbcr) - if err != nil { - logrus.Errorf("can not generate credentials for database - %s", err) - return err - } - - databaseCred, err := parseDatabaseSecretData(dbcr, secretData) - if err != nil { - // failed to parse database credential from secret - return err + databaseCred := database.Credentials{ + Name: dbcr.Status.DatabaseName, + Username: dbcr.Status.UserName, } db, err := determinDatabaseType(dbcr, databaseCred)