Skip to content

Commit

Permalink
Fixed pgpool db-client
Browse files Browse the repository at this point in the history
Signed-off-by: MobarakHsn <[email protected]>
  • Loading branch information
MobarakHsn committed Jan 18, 2024
1 parent 3fd840d commit 6d40866
Showing 1 changed file with 31 additions and 55 deletions.
86 changes: 31 additions & 55 deletions pgpool/kubedb_client_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,36 @@ package pgpool

import (
"context"
"database/sql"
"fmt"

_ "github.com/lib/pq"
core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
appbinding "kmodules.xyz/custom-resources/apis/appcatalog/v1alpha1"
api "kubedb.dev/apimachinery/apis/kubedb/v1alpha2"
"sigs.k8s.io/controller-runtime/pkg/client"
"xorm.io/xorm"
)

const (
DefaultPgpoolDB = "postgres"
DefaultBackendDBName = "postgres"
DefaultPgpoolPort = 9999
TLSModeDisable = "disable"
)

type KubeDBClientBuilder struct {
kc client.Client
db *api.Pgpool
url string
podName string
pgpoolDB string
ctx context.Context
kc client.Client
pgpool *api.Pgpool
url string
podName string
backendDBName string
ctx context.Context
}

func NewKubeDBClientBuilder(kc client.Client, db *api.Pgpool) *KubeDBClientBuilder {
func NewKubeDBClientBuilder(kc client.Client, pp *api.Pgpool) *KubeDBClientBuilder {
return &KubeDBClientBuilder{
kc: kc,
db: db,
kc: kc,
pgpool: pp,
}
}

Expand All @@ -62,7 +62,7 @@ func (o *KubeDBClientBuilder) WithPod(podName string) *KubeDBClientBuilder {
}

func (o *KubeDBClientBuilder) WithPgpoolDB(pgDB string) *KubeDBClientBuilder {
o.pgpoolDB = pgDB
o.backendDBName = pgDB
return o
}

Expand All @@ -87,6 +87,10 @@ func (o *KubeDBClientBuilder) GetPgpoolXormClient() (*XormClient, error) {
}
_, err = engine.Query("SELECT 1")
if err != nil {
err = engine.Close()
if err != nil {
return nil, err
}
return nil, err
}

Expand All @@ -96,73 +100,45 @@ func (o *KubeDBClientBuilder) GetPgpoolXormClient() (*XormClient, error) {
}, nil
}

func (o *KubeDBClientBuilder) GetPgpoolClient() (*Client, error) {
if o.ctx == nil {
o.ctx = context.Background()
}

connector, err := o.getConnectionString()
if err != nil {
return nil, err
}

// connect to database
db, err := sql.Open("postgres", connector)
if err != nil {
return nil, err
}

// ping to database to check the connection
if err := db.PingContext(o.ctx); err != nil {
closeErr := db.Close()
if closeErr != nil {
klog.Errorf("Failed to close client. error: %v", closeErr)
}
return nil, err
}

return &Client{db}, nil
}

func (o *KubeDBClientBuilder) getURL() string {
return fmt.Sprintf("%s.%s.%s.svc", o.podName, o.db.GoverningServiceName(), o.db.Namespace)
return fmt.Sprintf("%s.%s.%s.svc", o.podName, o.pgpool.GoverningServiceName(), o.pgpool.Namespace)
}

func (o *KubeDBClientBuilder) getPgpoolAuthCredentials() (string, string, error) {
db := o.db
func (o *KubeDBClientBuilder) getBackendAuth() (string, string, error) {
pp := o.pgpool
var secretName string
if db.Spec.Backend != nil {
if pp.Spec.Backend != nil {
apb := &appbinding.AppBinding{}
err := o.kc.Get(o.ctx, types.NamespacedName{
Name: db.Spec.Backend.Name,
Namespace: db.Namespace,
Name: pp.Spec.Backend.Name,
Namespace: pp.Namespace,
}, apb)
if err != nil {
return "", "", err
}
if apb.Spec.Secret == nil {
return "", "", fmt.Errorf("No database secret")
return "", "", fmt.Errorf("backend database auth secret not found")
}
secretName = apb.Spec.Secret.Name
}
var secret core.Secret
err := o.kc.Get(o.ctx, client.ObjectKey{Namespace: db.Namespace, Name: secretName}, &secret)
err := o.kc.Get(o.ctx, client.ObjectKey{Namespace: pp.Namespace, Name: secretName}, &secret)
if err != nil {
return "", "", err
}
user, ok := secret.Data[core.BasicAuthUsernameKey]
if !ok {
return "", "", fmt.Errorf("DB root user is not set")
return "", "", fmt.Errorf("error getting backend username")
}
pass, ok := secret.Data[core.BasicAuthPasswordKey]
if !ok {
return "", "", fmt.Errorf("DB root password is not set")
return "", "", fmt.Errorf("error getting backend password")
}
return string(user), string(pass), nil
}

func (o *KubeDBClientBuilder) getConnectionString() (string, error) {
user, pass, err := o.getPgpoolAuthCredentials()
user, pass, err := o.getBackendAuth()
if err != nil {
return "", err
}
Expand All @@ -171,10 +147,10 @@ func (o *KubeDBClientBuilder) getConnectionString() (string, error) {
o.url = o.getURL()
}

if o.pgpoolDB == "" {
o.pgpoolDB = DefaultPgpoolDB
if o.backendDBName == "" {
o.backendDBName = DefaultBackendDBName
}
//TODO ssl mode is disable now need to work on this after adding tls support
connector := fmt.Sprintf("user=%s password=%s host=%s port=%d connect_timeout=10 dbname=%s sslmode=%s", user, pass, o.url, 9999, o.pgpoolDB, "disable")
connector := fmt.Sprintf("user=%s password=%s host=%s port=%d connect_timeout=10 dbname=%s sslmode=%s", user, pass, o.url, DefaultPgpoolPort, o.backendDBName, TLSModeDisable)
return connector, nil
}

0 comments on commit 6d40866

Please sign in to comment.