From 9f8990c23d29469f8f338f63ac5b66954ffa0e93 Mon Sep 17 00:00:00 2001 From: Hiranmoy Das Chowdhury Date: Fri, 26 Apr 2024 18:39:11 +0600 Subject: [PATCH] xorm client list building --- pgbouncer/client.go | 6 +++++ pgbouncer/kubedb_client_builder.go | 36 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/pgbouncer/client.go b/pgbouncer/client.go index 85476775a..342bbef1e 100644 --- a/pgbouncer/client.go +++ b/pgbouncer/client.go @@ -18,6 +18,7 @@ package pgbouncer import ( "database/sql" + "sync" "xorm.io/xorm" ) @@ -29,3 +30,8 @@ type Client struct { type XormClient struct { *xorm.Engine } + +type XormClientList struct { + list []XormClient + mu sync.Mutex +} diff --git a/pgbouncer/kubedb_client_builder.go b/pgbouncer/kubedb_client_builder.go index e36e7253b..b017d09e9 100644 --- a/pgbouncer/kubedb_client_builder.go +++ b/pgbouncer/kubedb_client_builder.go @@ -19,7 +19,9 @@ package pgbouncer import ( "context" "fmt" + corev1 "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" @@ -170,3 +172,37 @@ func (o *KubeDBClientBuilder) getConnectionString() (string, error) { 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 *api.PgBouncer, ctx context.Context) (*XormClientList, error) { + Clientlist := &XormClientList{ + list: []XormClient{}, + } + + podList := &corev1.PodList{} + err := kc.List(context.Background(), podList, client.MatchingLabels(pb.PodLabels())) + + for _, postgresRef := range pb.Spec.Databases { + for _, pods := range podList.Items { + + } + + } + return Clientlist, nil +} +func (l *XormClientList) getXormClient(kc client.Client, pb *api.PgBouncer, ctx context.Context, podName string, postgresRef *api.Databases) { + NewKubeDBClientBuilder(kc, pb).WithContext(ctx).WithDatabaseRef(postgresRef).WithPod(podName).GetPgBouncerXormClient() + if err != nil { + klog.V(5).ErrorS(err, fmt.Sprintf("failed to create xorm client for pgbouncer %v to make pool with postgres pod %s/%s", key, postgresRef.DatabaseRef.Namespace, postgresRef.DatabaseRef.Name)) + if hcs.HasFailed(health.HealthCheckClientFailure, err) { + // Since the client was unable to connect the database, + // update "AcceptingConnection" to "false". + // update "Ready" to "false" + if err := c.updateConditionsForUnhealthy(ctx, db, err); err != nil { + return + } + } + klog.Error(err) + return + } + dbXormClient = append(dbXormClient, *XormClient) +}