Files
Abhishek Kondur 12cc967ba1 Fixes/v1.5.1 (#3938)
* fix(go): set persistent keep alive when registering host using sso;

* fix(go): run posture check violations on delete;

* fix(go): upsert node on approving pending host;

* fix(go): resolve concurrency issues during group delete cleanup;

* fix(go): update doc links;

* fix(go): add created and updated fields to host;

* fix(go): skip delete and update superadmin on sync users;

* fix(go): use conn directly for now;

* fix(go): remove acl for idp groups;

* fix(go): quote fields;

* fix(go): use filters with count;

* feat(go): add a search query;

* fix(go): cleanup acls;

* fix(go): review fixes;

* fix(go): remove additional loop;

* fix(go): fix

* v1.5.1: separate out idp sync and reset signals for HA

* v1.5.1: add grps with name for logging

* v1.5.1: clear posture check violations when all checks are deleted

* v1.5.1: set static when default host

* v1.5.1: fix db status check

* rm set max conns

* v1.5.1: reset auto assigned gw when disconnected

* fix(go): skip global network admin and user groups when splitting;

* v1.5.1: fix update node call from client

* fix(go): separate out migration from normal usage;

* fix(go): skip default groups;

* fix(go): create policies for existing groups on network create;

* fix(go): skip fatal log on clickhouse conn;

* fix(go): add posture check cleanup;

---------

Co-authored-by: VishalDalwadi <dalwadivishal26@gmail.com>
Co-authored-by: Vishal Dalwadi <51291657+VishalDalwadi@users.noreply.github.com>
2026-03-28 01:08:59 +05:30

117 lines
2.5 KiB
Go

package db
import (
"fmt"
"os"
"strconv"
"time"
"github.com/gravitl/netmaker/config"
"github.com/gravitl/netmaker/servercfg"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
// postgresConnector for initializing and
// connecting to a postgres database.
type postgresConnector struct{}
// postgresConnector.connect connects and
// initializes a connection to postgres.
func (pg *postgresConnector) connect() (*gorm.DB, error) {
pgConf := servercfg.GetSQLConf()
dsn := fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=%s connect_timeout=5",
pgConf.Host,
pgConf.Port,
pgConf.Username,
pgConf.Password,
pgConf.DB,
pgConf.SSLMode,
)
gormDB, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
if err != nil {
return nil, err
}
sqlDB, err := gormDB.DB()
if err != nil {
return gormDB, err
}
sqlDB.SetMaxIdleConns(5)
sqlDB.SetMaxOpenConns(10)
sqlDB.SetConnMaxLifetime(5 * time.Minute)
sqlDB.SetConnMaxIdleTime(2 * time.Minute)
return gormDB, nil
}
func GetSQLConf() config.SQLConfig {
var cfg config.SQLConfig
cfg.Host = GetSQLHost()
cfg.Port = GetSQLPort()
cfg.Username = GetSQLUser()
cfg.Password = GetSQLPass()
cfg.DB = GetSQLDB()
cfg.SSLMode = GetSQLSSLMode()
return cfg
}
func GetSQLHost() string {
host := "localhost"
if os.Getenv("SQL_HOST") != "" {
host = os.Getenv("SQL_HOST")
} else if config.Config.SQL.Host != "" {
host = config.Config.SQL.Host
}
return host
}
func GetSQLPort() int32 {
port := int32(5432)
envport, err := strconv.Atoi(os.Getenv("SQL_PORT"))
if err == nil && envport != 0 {
port = int32(envport)
} else if config.Config.SQL.Port != 0 {
port = config.Config.SQL.Port
}
return port
}
func GetSQLUser() string {
user := "postgres"
if os.Getenv("SQL_USER") != "" {
user = os.Getenv("SQL_USER")
} else if config.Config.SQL.Username != "" {
user = config.Config.SQL.Username
}
return user
}
func GetSQLPass() string {
pass := "nopass"
if os.Getenv("SQL_PASS") != "" {
pass = os.Getenv("SQL_PASS")
} else if config.Config.SQL.Password != "" {
pass = config.Config.SQL.Password
}
return pass
}
func GetSQLDB() string {
db := "netmaker"
if os.Getenv("SQL_DB") != "" {
db = os.Getenv("SQL_DB")
} else if config.Config.SQL.DB != "" {
db = config.Config.SQL.DB
}
return db
}
func GetSQLSSLMode() string {
sslmode := "disable"
if os.Getenv("SQL_SSL_MODE") != "" {
sslmode = os.Getenv("SQL_SSL_MODE")
} else if config.Config.SQL.SSLMode != "" {
sslmode = config.Config.SQL.SSLMode
}
return sslmode
}