Skip to content

Commit

Permalink
collect info from pgbouncer config secret
Browse files Browse the repository at this point in the history
Signed-off-by: Hiranmoy Das Chowdhury <[email protected]>
  • Loading branch information
HiranmoyChowdhury committed Aug 29, 2024
1 parent ccd70e6 commit 3c77b6f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
9 changes: 5 additions & 4 deletions pgbouncer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ type XormClientList struct {
Mutex sync.Mutex
WG sync.WaitGroup

context context.Context
pb *dbapi.PgBouncer
auth *Auth
dbName string
context context.Context
pb *dbapi.PgBouncer
pbContainerPort rune
auth *Auth
dbName string
}
35 changes: 22 additions & 13 deletions pgbouncer/kubedb_client_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ type Auth struct {
}

type KubeDBClientBuilder struct {
kc client.Client
pgbouncer *dbapi.PgBouncer
url string
podName string
backendDBName string
ctx context.Context
databaseRef *dbapi.Database
auth *Auth
kc client.Client
pgbouncer *dbapi.PgBouncer
url string
podName string
pbContainerPort *int32
backendDBName string
ctx context.Context
databaseRef *dbapi.Database
auth *Auth
}

func NewKubeDBClientBuilder(kc client.Client, pb *dbapi.PgBouncer) *KubeDBClientBuilder {
Expand All @@ -65,6 +66,11 @@ func (o *KubeDBClientBuilder) WithURL(url string) *KubeDBClientBuilder {
return o
}

func (o *KubeDBClientBuilder) WithPbPort(listenPort int32) *KubeDBClientBuilder {
o.pbContainerPort = &listenPort
return o
}

func (o *KubeDBClientBuilder) WithAuth(auth *Auth) *KubeDBClientBuilder {
if auth != nil && auth.UserName != "" && auth.Password != "" {
o.auth = auth
Expand All @@ -82,7 +88,7 @@ func (o *KubeDBClientBuilder) WithDatabaseRef(db *dbapi.Database) *KubeDBClientB
return o
}

func (o *KubeDBClientBuilder) WithPostgresDBName(dbName string) *KubeDBClientBuilder {
func (o *KubeDBClientBuilder) WithDatabaseName(dbName string) *KubeDBClientBuilder {
if dbName == "" {
o.backendDBName = o.databaseRef.DatabaseName
} else {
Expand Down Expand Up @@ -176,22 +182,23 @@ func (o *KubeDBClientBuilder) getConnectionString() (string, error) {
}

var listeningPort int = kubedb.PgBouncerDatabasePort
if o.pgbouncer.Spec.ConnectionPool.Port != nil {
listeningPort = int(*o.pgbouncer.Spec.ConnectionPool.Port)
if o.pbContainerPort != nil {
listeningPort = int(*o.pbContainerPort)
}
// 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, listeningPort, o.backendDBName, TLSModeDisable)
return connector, nil
}

func GetXormClientList(kc client.Client, pb *dbapi.PgBouncer, ctx context.Context, auth *Auth, dbName string) (*XormClientList, error) {
func GetXormClientList(kc client.Client, pb *dbapi.PgBouncer, ctx context.Context, auth *Auth, dbName string, listenPort rune) (*XormClientList, error) {
clientlist := &XormClientList{
List: []*XormClient{},
}
clientlist.context = ctx
clientlist.pb = pb
clientlist.auth = auth
clientlist.dbName = dbName
clientlist.pbContainerPort = listenPort

for i := 0; int32(i) < *pb.Spec.Replicas; i++ {
podName := fmt.Sprintf("%s-%d", pb.OffshootName(), i)
Expand All @@ -216,7 +223,9 @@ func GetXormClientList(kc client.Client, pb *dbapi.PgBouncer, ctx context.Contex
}

func (l *XormClientList) addXormClient(kc client.Client, podName string) {
xormClient, err := NewKubeDBClientBuilder(kc, l.pb).WithContext(l.context).WithDatabaseRef(&l.pb.Spec.Database).WithPod(podName).WithAuth(l.auth).WithPostgresDBName(l.dbName).GetPgBouncerXormClient()
xormClient, err := NewKubeDBClientBuilder(kc, l.pb).WithContext(l.context).WithDatabaseRef(&l.pb.Spec.Database).
WithPod(podName).WithAuth(l.auth).WithDatabaseName(l.dbName).WithPbPort(l.pbContainerPort).GetPgBouncerXormClient()

l.Mutex.Lock()
defer l.Mutex.Unlock()
if err != nil {
Expand Down

0 comments on commit 3c77b6f

Please sign in to comment.