Skip to content

Commit

Permalink
issue-135, saving and updating the PostgreSQL default password were i…
Browse files Browse the repository at this point in the history
…mplemented
  • Loading branch information
DoodgeMatvey authored and taaraora committed Jul 5, 2023
1 parent 47a9342 commit 582cf36
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 122 deletions.
37 changes: 28 additions & 9 deletions apis/clusters/v1beta1/postgresql_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ type PgSpec struct {

// PgStatus defines the observed state of PostgreSQL
type PgStatus struct {
ClusterStatus `json:",inline"`
DefaultUserSecretName string `json:"defaultUserSecretName,omitempty"`
ClusterStatus `json:",inline"`
}

//+kubebuilder:object:root=true
Expand Down Expand Up @@ -303,18 +302,18 @@ func (pdc *PgDataCentre) ArePGBouncersEqual(iPGBs []*PgBouncer) bool {
}

func (pg *PostgreSQL) GetUserPassword(secret *k8sCore.Secret) string {
password := secret.Data[models.DefaultUserPassword]
password := secret.Data[models.Password]
if len(password) == 0 {
return ""
}

return string(password[:len(password)-1])
return string(password)
}

func (pg *PostgreSQL) GetUserSecret(ctx context.Context, k8sClient client.Client) (*k8sCore.Secret, error) {
userSecret := &k8sCore.Secret{}
userSecretNamespacedName := types.NamespacedName{
Name: pg.Status.DefaultUserSecretName,
Name: fmt.Sprintf(models.DefaultUserSecretNameTemplate, models.DefaultUserSecretPrefix, pg.Name),
Namespace: pg.Namespace,
}
err := k8sClient.Get(ctx, userSecretNamespacedName, userSecret)
Expand Down Expand Up @@ -347,18 +346,24 @@ func (pg *PostgreSQL) GetUserSecretName(ctx context.Context, k8sClient client.Cl
return userSecretList.Items[0].Name, nil
}

func (pg *PostgreSQL) NewUserSecret() *k8sCore.Secret {
func (pg *PostgreSQL) NewUserSecret(defaultUserPassword string) *k8sCore.Secret {
return &k8sCore.Secret{
TypeMeta: metav1.TypeMeta{
Kind: models.SecretKind,
APIVersion: models.K8sAPIVersionV1,
},
ObjectMeta: metav1.ObjectMeta{
Name: models.DefaultUserSecretPrefix + pg.Name,
Name: fmt.Sprintf(models.DefaultUserSecretNameTemplate, models.DefaultUserSecretPrefix, pg.Name),
Namespace: pg.Namespace,
Labels: map[string]string{models.ControlledByLabel: pg.Name},
Labels: map[string]string{
models.ControlledByLabel: pg.Name,
models.DefaultSecretLabel: "true",
},
},
StringData: map[string]string{
models.Username: models.DefaultPgUsernameValue,
models.Password: defaultUserPassword,
},
StringData: map[string]string{models.DefaultUserPassword: ""},
}
}

Expand Down Expand Up @@ -519,6 +524,20 @@ func (pg *PostgreSQL) FromInstAPI(iData []byte) (*PostgreSQL, error) {
}, nil
}

func (pg *PostgreSQL) DefaultPasswordFromInstAPI(iData []byte) (string, error) {
type defaultPasswordResponse struct {
DefaultUserPassword string `json:"defaultUserPassword,omitempty"`
}

dpr := &defaultPasswordResponse{}
err := json.Unmarshal(iData, dpr)
if err != nil {
return "", err
}

return dpr.DefaultUserPassword, nil
}

func (pgs *PgSpec) FromInstAPI(iPg *models.PGCluster) PgSpec {
return PgSpec{
Cluster: Cluster{
Expand Down
2 changes: 0 additions & 2 deletions config/crd/bases/clusters.instaclustr.com_postgresqls.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,6 @@ spec:
type: string
type: object
type: array
defaultUserSecretName:
type: string
id:
type: string
maintenanceEvents:
Expand Down
4 changes: 2 additions & 2 deletions controllers/clusterresources/cassandrauser_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ func (r *CassandraUserReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}

func (r *CassandraUserReconciler) getUserCreds(secret *k8sCore.Secret) (username, password string, err error) {
password = string(secret.Data["password"])
username = string(secret.Data["username"])
password = string(secret.Data[models.Password])
username = string(secret.Data[models.Username])

if len(username) == 0 || len(password) == 0 {
return "", "", models.ErrMissingSecretKeys
Expand Down
Loading

0 comments on commit 582cf36

Please sign in to comment.