Skip to content

Commit

Permalink
PostgreSQL user creation logic was implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
DoodgeMatvey committed Sep 15, 2023
1 parent e62448c commit 4e030b4
Show file tree
Hide file tree
Showing 28 changed files with 1,187 additions and 84 deletions.
9 changes: 9 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,15 @@ resources:
kind: Topic
path: github.com/instaclustr/operator/apis/kafkamanagement/v1beta1
version: v1beta1
- api:
crdVersion: v1
namespaced: true
controller: true
domain: instaclustr.com
group: clusterresources
kind: PostgreSQLUser
path: github.com/instaclustr/operator/apis/clusterresources/v1beta1
version: v1beta1
webhooks:
defaulting: true
webhookVersion: v1
Expand Down
88 changes: 88 additions & 0 deletions apis/clusterresources/v1beta1/postgresqluser_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
Copyright 2022.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/instaclustr/operator/pkg/models"
)

// PostgreSQLUserSpec defines the desired state of PostgreSQLUser
type PostgreSQLUserSpec struct {
SecretRef *SecretReference `json:"secretRef"`
}

// PostgreSQLUserStatus defines the observed state of PostgreSQLUser
type PostgreSQLUserStatus struct {
// ClustersInfo efficiently stores data about clusters that related to this user.
// The keys of the map represent the cluster IDs, values are cluster info that consists of default secret namespaced name or event.
ClustersInfo map[string]ClusterInfo `json:"clustersInfo,omitempty"`
}

type ClusterInfo struct {
DefaultSecretNamespacedName NamespacedName `json:"defaultSecretNamespacedName"`
Event string `json:"event,omitempty"`
}

type NamespacedName struct {
Namespace string `json:"namespace"`
Name string `json:"name"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status

// PostgreSQLUser is the Schema for the postgresqlusers API
type PostgreSQLUser struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec PostgreSQLUserSpec `json:"spec,omitempty"`
Status PostgreSQLUserStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true

// PostgreSQLUserList contains a list of PostgreSQLUser
type PostgreSQLUserList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []PostgreSQLUser `json:"items"`
}

func (r *PostgreSQLUser) NewPatch() client.Patch {
old := r.DeepCopy()
return client.MergeFrom(old)
}

func init() {
SchemeBuilder.Register(&PostgreSQLUser{}, &PostgreSQLUserList{})
}

func (r *PostgreSQLUser) ToInstAPI(username, password string) *models.InstaUser {
return &models.InstaUser{
Username: username,
Password: password,
InitialPermission: "standard",
}
}

func (r *PostgreSQLUser) GetDeletionFinalizer() string {
return models.DeletionFinalizer + "_" + r.Namespace + "_" + r.Name
}
132 changes: 132 additions & 0 deletions apis/clusterresources/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 21 additions & 23 deletions apis/clusters/v1beta1/postgresql_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type PgSpec struct {
ClusterConfigurations map[string]string `json:"clusterConfigurations,omitempty"`
Description string `json:"description,omitempty"`
SynchronousModeStrict bool `json:"synchronousModeStrict,omitempty"`
UserRefs []*UserReference `json:"userRefs,omitempty"`
}

// PgStatus defines the observed state of PostgreSQL
Expand Down Expand Up @@ -308,29 +309,6 @@ func (pdc *PgDataCentre) ArePGBouncersEqual(iPGBs []*PgBouncer) bool {
return true
}

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

return string(password)
}

func (pg *PostgreSQL) GetUserSecret(ctx context.Context, k8sClient client.Client) (*k8sCore.Secret, error) {
userSecret := &k8sCore.Secret{}
userSecretNamespacedName := types.NamespacedName{
Name: fmt.Sprintf(models.DefaultUserSecretNameTemplate, models.DefaultUserSecretPrefix, pg.Name),
Namespace: pg.Namespace,
}
err := k8sClient.Get(ctx, userSecretNamespacedName, userSecret)
if err != nil {
return nil, err
}

return userSecret, nil
}

func (pg *PostgreSQL) GetUserSecretName(ctx context.Context, k8sClient client.Client) (string, error) {
var err error

Expand Down Expand Up @@ -365,6 +343,7 @@ func (pg *PostgreSQL) NewUserSecret(defaultUserPassword string) *k8sCore.Secret
Labels: map[string]string{
models.ControlledByLabel: pg.Name,
models.DefaultSecretLabel: "true",
models.ClusterIDLabel: pg.Status.ID,
},
},
StringData: map[string]string{
Expand Down Expand Up @@ -627,3 +606,22 @@ func (pgs *PgStatus) DCsFromInstAPI(iDCs []*models.PGDataCentre) (dcs []*DataCen
}
return
}

func GetDefaultPgUserSecret(
ctx context.Context,
name string,
ns string,
k8sClient client.Client,
) (*k8sCore.Secret, error) {
userSecret := &k8sCore.Secret{}
userSecretNamespacedName := types.NamespacedName{
Name: fmt.Sprintf(models.DefaultUserSecretNameTemplate, models.DefaultUserSecretPrefix, name),
Namespace: ns,
}
err := k8sClient.Get(ctx, userSecretNamespacedName, userSecret)
if err != nil {
return nil, err
}

return userSecret, nil
}
11 changes: 11 additions & 0 deletions apis/clusters/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4e030b4

Please sign in to comment.