Skip to content

Commit

Permalink
client struct modified
Browse files Browse the repository at this point in the history
Signed-off-by: Hiranmoy Das Chowdhury <[email protected]>
  • Loading branch information
HiranmoyChowdhury committed May 27, 2024
1 parent 512141a commit c856ef3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
8 changes: 8 additions & 0 deletions pgbouncer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ limitations under the License.
package pgbouncer

import (
"context"
"database/sql"
api "kubedb.dev/apimachinery/apis/kubedb/v1alpha2"
"sync"

"xorm.io/xorm"
Expand All @@ -35,4 +37,10 @@ type XormClientList struct {
List []*XormClient
Mutex sync.Mutex
WG sync.WaitGroup

context context.Context
pb *api.PgBouncer
auth *Auth
dbType string
dbName string
}
24 changes: 12 additions & 12 deletions pgbouncer/kubedb_client_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

_ "github.com/lib/pq"
core "k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
appbinding "kmodules.xyz/custom-resources/apis/appcatalog/v1alpha1"
Expand Down Expand Up @@ -202,35 +201,36 @@ func GetXormClientList(kc client.Client, pb *api.PgBouncer, ctx context.Context,
clientlist := &XormClientList{
List: []*XormClient{},
}
clientlist.context = ctx
clientlist.pb = pb
clientlist.auth = auth
clientlist.dbName = dbName
clientlist.dbType = dbType

podList := &corev1.PodList{}
for i := 0; int32(i) < *pb.Spec.Replicas; i++ {
podName := fmt.Sprintf("%s-%d", pb.OffshootName(), i)
pod := corev1.Pod{}
pod := core.Pod{}
err := kc.Get(ctx, types.NamespacedName{Name: podName, Namespace: pb.Namespace}, &pod)
if err != nil {
return clientlist, err
}
podList.Items = append(podList.Items, pod)
}

for _, pod := range podList.Items {
clientlist.WG.Add(1)
go clientlist.addXormClient(kc, pb, ctx, pod.Name, auth, dbType, dbName)
go clientlist.addXormClient(kc, podName)
}

clientlist.WG.Wait()
if len(clientlist.List) != len(podList.Items) {
if len(clientlist.List) != int(*pb.Spec.Replicas) {
return nil, fmt.Errorf("Failed to generate Xorm Client List")
}
return clientlist, nil
}

func (l *XormClientList) addXormClient(kc client.Client, pb *api.PgBouncer, ctx context.Context, podName string, auth *Auth, dbType string, dbName string) {
xormClient, err := NewKubeDBClientBuilder(kc, pb).WithContext(ctx).WithDatabaseRef(&pb.Spec.Database).WithPod(podName).WithAuth(auth).WithBackendDBType(dbType).WithPostgresDBName(dbName).GetPgBouncerXormClient()
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).WithBackendDBType(l.dbType).WithPostgresDBName(l.dbName).GetPgBouncerXormClient()
l.Mutex.Lock()
defer l.Mutex.Unlock()
if err != nil {
klog.V(5).ErrorS(err, fmt.Sprintf("failed to create xorm client for pgbouncer %s/%s ", pb.Namespace, pb.Name))
klog.V(5).ErrorS(err, fmt.Sprintf("failed to create xorm client for pgbouncer %s/%s ", l.pb.Namespace, l.pb.Name))
} else {
l.List = append(l.List, xormClient)
}
Expand Down

0 comments on commit c856ef3

Please sign in to comment.