Skip to content

Commit

Permalink
add db connection
Browse files Browse the repository at this point in the history
  • Loading branch information
AmaliMatharaarachchi committed Feb 20, 2024
1 parent d943cc9 commit 0e1731e
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 9 deletions.
2 changes: 1 addition & 1 deletion adapter/internal/operator/controllers/dp/api_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2017,7 +2017,7 @@ func (apiReconciler *APIReconciler) handleOwnerReference(ctx context.Context, ob
if err := apiReconciler.client.Get(ctx, req.NamespacedName, &apiCR); err == nil {
apis = append(apis, apiCR)
} else {
loggers.LoggerAPKOperator.Errorf("Error while loading api: %+v", req)
loggers.LoggerAPKOperator.Errorf("Error while loading api: %+v, Error: %v", req, err)
}
}
// Prepare owner references for the route
Expand Down
12 changes: 12 additions & 0 deletions common-controller/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
logger "github.com/sirupsen/logrus"
commoncontroller "github.com/wso2/apk/common-controller/commoncontroller"
config "github.com/wso2/apk/common-controller/internal/config"
"github.com/wso2/apk/common-controller/internal/database"
"github.com/wso2/apk/common-controller/internal/server"
web "github.com/wso2/apk/common-controller/internal/web"
)
Expand All @@ -30,6 +31,17 @@ func main() {
logger.Info("Starting the Web server")
go web.StartWebServer()
go server.StartInternalServer()
if conf.CommonController.Database.Enabled {
logger.Info("Starting the Database connection")
go startDB()
}
logger.Info("Starting the Common Controller")
commoncontroller.InitCommonControllerServer(conf)

}

func startDB() {
database.ConnectToDB()
database.GetApplicationByUUID()
defer database.CloseDBConn()
}
11 changes: 8 additions & 3 deletions common-controller/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/wso2/apk/common-controller
go 1.19

require (
github.com/jackc/pgx/v5 v5.5.2
github.com/onsi/ginkgo/v2 v2.9.5
github.com/onsi/gomega v1.27.7
github.com/sirupsen/logrus v1.9.0
Expand All @@ -18,7 +19,7 @@ require (
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/pelletier/go-toml v1.9.5
github.com/redis/go-redis/v9 v9.2.1
github.com/wso2/apk/adapter v0.0.0-20231207051518-6dd728943082
github.com/wso2/apk/adapter v0.0.0-20231214082511-af2c8b8a19f1
github.com/wso2/apk/common-go-libs v0.0.0-20231208100153-24bee7b4bd81
google.golang.org/grpc v1.58.3
)
Expand All @@ -38,6 +39,9 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
Expand All @@ -47,8 +51,9 @@ require (
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/vektah/gqlparser v1.3.1 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb // indirect
golang.org/x/sync v0.5.0 // indirect
)

require (
Expand Down Expand Up @@ -107,7 +112,7 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20230731190214-cbb8c96f2d6d // indirect
google.golang.org/protobuf v1.31.0
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.27.2 // indirect
Expand Down
19 changes: 14 additions & 5 deletions common-controller/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM=
github.com/agnivade/levenshtein v1.1.1 h1:QY8M92nrzkmr798gCo3kmMyqXFzdQVpxLlGPRBij0P8=
Expand Down Expand Up @@ -128,6 +127,14 @@ github.com/grpc-ecosystem/go-grpc-middleware v1.3.0/go.mod h1:z0ButlSOZa5vEBq9m2
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.5.2 h1:iLlpgp4Cp/gC9Xuscl7lFL1PhhW+ZLtXZcrfCt4C3tA=
github.com/jackc/pgx/v5 v5.5.2/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
Expand Down Expand Up @@ -240,8 +247,8 @@ golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY=
golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb h1:c0vyKkb6yr3KR7jEfJaOSv4lG7xPkbN6r52aJz1d8a8=
golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
Expand Down Expand Up @@ -275,6 +282,8 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down Expand Up @@ -359,8 +368,8 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
16 changes: 16 additions & 0 deletions common-controller/internal/config/default_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,21 @@ var defaultConfig = &Config{
RestPort: 18001,
RetryInterval: 5,
Persistence: persistence{Type: "K8s"}},
Database: database{
Enabled: true,
Name: "DATAPLANE",
Username: "wso2carbon",
Password: "wso2carbon",
Host: "wso2apk-db-service.apk",
Port: 5432,
PoolOptions: dbPool{
PoolMaxConns: 4,
PoolMinConns: 0,
PoolMaxConnLifetime: "1h",
PoolMaxConnIdleTime: "1h",
PoolHealthCheckPeriod: "1m",
PoolMaxConnLifetimeJitter: "1s",
},
},
},
}
34 changes: 34 additions & 0 deletions common-controller/internal/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type commoncontroller struct {
WebServer webServer
InternalAPIServer internalAPIServer
ControlPlane controlplane
Database database
}
type controlplane struct {
Enabled bool
Expand Down Expand Up @@ -102,3 +103,36 @@ type sts struct {
type webServer struct {
Port int64
}

type database struct {
Enabled bool
Name string
Username string
Password string
Host string
Port int
PoolOptions dbPool
}

type dbPool struct {
// PoolMaxConns is the maximum size of the pool. The default is the greater of 4 or runtime.NumCPU()
PoolMaxConns int

// PoolMinConns is the minimum size of the pool. After connection closes, the pool might dip below MinConns. A low
// number of MinConns might mean the pool is empty after MaxConnLifetime until the health check has a chance
// to create new connections.
PoolMinConns int

// PoolMaxConnLifetime is the duration since creation after which a connection will be automatically closed.
PoolMaxConnLifetime string

// PoolMaxConnIdleTime is the duration after which an idle connection will be automatically closed by the health check.
PoolMaxConnIdleTime string

// HealthCheckPeriod is the duration between checks of the health of idle connections.
PoolHealthCheckPeriod string

// PoolMaxConnLifetimeJitter is the duration after MaxConnLifetime to randomly decide to close a connection.
// This helps prevent all connections from being closed at the exact same time, starving the pool.
PoolMaxConnLifetimeJitter string
}
35 changes: 35 additions & 0 deletions common-controller/internal/database/dao.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* 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 database

import "github.com/wso2/apk/common-controller/internal/loggers"

// GetApplicationByUUID returns the Application details from the DB for a given application
func GetApplicationByUUID() {
loggers.LoggerAPI.Error("amaliiii ")
rows, _ := ExecDBQuery(queryTest)
for rows.Next() {
values, err := rows.Values()
if err == nil {
loggers.LoggerDatabase.Info(values)
} else {
loggers.LoggerDatabase.Error(err)
}
}

}
76 changes: 76 additions & 0 deletions common-controller/internal/database/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* 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 database

import (
"context"
"fmt"

"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/wso2/apk/adapter/pkg/logging"
"github.com/wso2/apk/common-controller/internal/config"
"github.com/wso2/apk/common-controller/internal/loggers"
)

var dbPool *pgxpool.Pool

func ConnectToDB() {
conf := config.ReadConfigs()
var err error
connString := fmt.Sprintf("postgresql://%s:%s@%s:%d/%s?pool_max_conns=%d&pool_min_conns=%d&"+
"pool_max_conn_lifetime=%s&pool_max_conn_idle_time=%s&pool_health_check_period=%s&pool_max_conn_lifetime_jitter=%s",
conf.CommonController.Database.Username,
conf.CommonController.Database.Password,
conf.CommonController.Database.Host,
conf.CommonController.Database.Port,
conf.CommonController.Database.Name,
conf.CommonController.Database.PoolOptions.PoolMaxConns,
conf.CommonController.Database.PoolOptions.PoolMinConns,
conf.CommonController.Database.PoolOptions.PoolMaxConnLifetime,
conf.CommonController.Database.PoolOptions.PoolMaxConnIdleTime,
conf.CommonController.Database.PoolOptions.PoolHealthCheckPeriod,
conf.CommonController.Database.PoolOptions.PoolMaxConnLifetimeJitter)
dbPool, err = pgxpool.New(context.Background(), connString)
if err != nil {
loggers.LoggerDatabase.ErrorC(logging.ErrorDetails{
Message: fmt.Sprintf("Unable to connect to database: %v", err.Error()),
Severity: logging.CRITICAL,
ErrorCode: 1100,
})
}
}

func ExecDBQuery(query string, args ...interface{}) (pgx.Rows, error) {
rows, err := dbPool.Query(context.Background(), query, args...)
if err != nil {
return nil, err
}
return rows, nil
}

func IsAliveConn(ctx context.Context) (isAlive bool) {
if err := dbPool.Ping(ctx); err != nil {
return true
}
return isAlive
}

func CloseDBConn() {
dbPool.Close()
}
22 changes: 22 additions & 0 deletions common-controller/internal/database/queries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* 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 database

const (
queryTest string = "select * from subscription;"
)
3 changes: 3 additions & 0 deletions common-controller/internal/loggers/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (
pkgAPI = "github.com/wso2/apk/adapter/internal/api"
pkgAPK = "github.com/wso2/apk/adapter/internal/adapter"
pkgRateLimiterXdsCallbacks = "github.com/wso2/apk/adapter/internal/discovery/xds/ratelimitercallbacks"
pkgDatabase = "github.com/wso2/apk/adapter/internal/database"
)

// logger package references
Expand All @@ -60,6 +61,7 @@ var (
LoggerAPI logging.Log
LoggerAPK logging.Log
LoggerRateLimiterXdsCallbacks logging.Log
LoggerDatabase logging.Log
)

func init() {
Expand All @@ -80,5 +82,6 @@ func UpdateLoggers() {
LoggerNotifier = logging.InitPackageLogger(pkgNotifier)
LoggerAPI = logging.InitPackageLogger(pkgAPI)
LoggerAPK = logging.InitPackageLogger(pkgAPK)
LoggerDatabase = logging.InitPackageLogger(pkgDatabase)
logrus.Info("Updated loggers")
}

0 comments on commit 0e1731e

Please sign in to comment.