Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue-135, saving and updating the PostgreSQL default password were implemented #447

Merged
merged 1 commit into from
Jul 5, 2023

Conversation

DoodgeMatvey
Copy link
Contributor

@DoodgeMatvey DoodgeMatvey commented Jun 29, 2023

Closes #446
We need to get default user password and store it in secret to be able to create new PostgreSQL user.
In this Pr, I updated the current logic for creating secret with default user password and username when cluster is creating.
Also, fixed and improved logic for updating default password when secret is updated.

}

func (pg *PostgreSQL) GetUserSecret(ctx context.Context, k8sClient client.Client) (*k8sCore.Secret, error) {
userSecret := &k8sCore.Secret{}
userSecretNamespacedName := types.NamespacedName{
Name: pg.Status.DefaultUserSecretName,
Name: models.DefaultUserSecretPrefix + pg.Name,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if user already has secret with this name that is not related to pg cluster?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We work only with secrets that have DefaultSecretLabel == "true", I described it in findSecretObject method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I don't think there will be such name default-user-password-$postgresClusterName

@@ -356,9 +355,15 @@ func (pg *PostgreSQL) NewUserSecret() *k8sCore.Secret {
ObjectMeta: metav1.ObjectMeta{
Name: models.DefaultUserSecretPrefix + pg.Name,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If user already has a secret with the same name it can be an issue

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I answered in the previous comment, I handled it

Comment on lines 273 to 289
err = r.createDefaultPassword(pg, logger)
if err != nil {
logger.Error(err, "Cannot create default password for PostgreSQL",
"cluster name", pg.Spec.Name,
"clusterID", pg.Status.ID,
)

return models.ReconcileRequeue
}

return models.ExitReconcile
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You set UpdatingEvent in the createPassword(), but use exitReconcile.
I suggest you either don't set any events in the createPassword() or set an updatedEvent

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

Comment on lines +98 to +99
case models.SecretEvent:
return r.handleUpdateDefaultUserPassword(ctx, pg, logger), nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move the handleUpdatePassword() to the handleUpdate() to prevent interuption of the secret event by update event

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to update secret only if it is secret event I think

Comment on lines +1266 to +1291
if s.Labels[models.DefaultSecretLabel] != "true" {
return []reconcile.Request{}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better to make this label resource-related, e.g. pgDefaultSecretLabel, so this condition can filter secrets from other resources

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We won't use or store default passwords for other entities, this logic will be only for postgresql

@@ -22,6 +22,7 @@ type PGCluster struct {
PostgreSQLVersion string `json:"postgresqlVersion"`
DataCentres []*PGDataCentre `json:"dataCentres"`
SynchronousModeStrict bool `json:"synchronousModeStrict"`
DefaultUserPassword string `json:"defaultUserPassword,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't store password in the spec

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, fixed

Comment on lines +275 to +278
logger.Error(err, "Cannot create default password for PostgreSQL",
"cluster name", pg.Spec.Name,
"clusterID", pg.Status.ID,
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add an event (r.EventRecorder.Eventf) message here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, added for all such cases

Comment on lines 659 to 668
logger.Error(err, "PostgreSQL cannot get user secret",
"cluster name", pg.Spec.Name,
"cluster ID", pg.Status.ID,
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change the error message here, like PostgreSQL cannot get user secret -> Cannot get the user secret for the PostgreSQL cluster
and add an event

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

controllers/clusters/postgresql_controller.go Outdated Show resolved Hide resolved
controllers/clusters/postgresql_controller.go Outdated Show resolved Hide resolved

defaultUserPassword, err := pg.DefaultPasswordFromInstAPI(iData)
if err != nil {
l.Error(err, "Cannot convert PostgreSQL cluster status from Instaclustr",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot get default user creds for PostgreSQL cluster from the Instaclustr API maybe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

}

if secret != nil {
return nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest you add here a log info that a secret already exists.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, added

}

func (pg *PostgreSQL) GetUserSecret(ctx context.Context, k8sClient client.Client) (*k8sCore.Secret, error) {
userSecret := &k8sCore.Secret{}
userSecretNamespacedName := types.NamespacedName{
Name: pg.Status.DefaultUserSecretName,
Name: models.DefaultUserSecretPrefix + pg.Name,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use printf and some constant format to have the same name in different parts of code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, fixed

@taaraora taaraora merged commit 582cf36 into main Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

issue-135, Implement saving and updating the PostgreSQL default password
5 participants