Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support different storage #380

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ dist
# licensefmt
hack/tools/licfmt/licfmt

# SQLite
pixiu.db

vendor
33 changes: 26 additions & 7 deletions cmd/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ limitations under the License.

package config

import "errors"
import (
"errors"
)

type LogFormat string

Expand All @@ -29,7 +31,7 @@ var ErrInvalidLogFormat = errors.New("invalid log format")

type Config struct {
Default DefaultOptions `yaml:"default"`
Mysql MysqlOptions `yaml:"mysql"`
Db DbConfig `yaml:"db"`
}

type DefaultOptions struct {
Expand All @@ -43,10 +45,27 @@ type DefaultOptions struct {
LogOptions `yaml:",inline"`
}

func (o DefaultOptions) Valid() error {
if err := o.LogOptions.Valid(); err != nil {
return err
type DbConfig struct {
Sqlite *SqliteOptions `yaml:"sqlite"`
Mysql *MysqlOptions `yaml:"mysql"`
}

func (d DbConfig) Valid() error {
if d.Sqlite != nil {
return d.Sqlite.Valid()
}
if d.Mysql != nil {
return d.Mysql.Valid()
}
return errors.New("invalid database config")
}

type SqliteOptions struct {
Db string `yaml:"db"`
}

func (o *SqliteOptions) Valid() error {
// TODO
return nil
}

Expand All @@ -59,7 +78,7 @@ type MysqlOptions struct {
Name string `yaml:"name"`
}

func (o MysqlOptions) Valid() error {
func (o *MysqlOptions) Valid() error {
// TODO
return nil
}
Expand All @@ -81,7 +100,7 @@ func (c *Config) Valid() (err error) {
if err = c.Default.Valid(); err != nil {
return
}
if err = c.Mysql.Valid(); err != nil {
if err = c.Db.Valid(); err != nil {
return
}
return
Expand Down
53 changes: 9 additions & 44 deletions cmd/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,18 @@ limitations under the License.
package options

import (
"fmt"
"os"

pixiuConfig "github.com/caoyingjunz/pixiulib/config"
"github.com/gin-gonic/gin"
"github.com/spf13/cobra"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"

"github.com/caoyingjunz/pixiu/cmd/app/config"
"github.com/caoyingjunz/pixiu/pkg/controller"
"github.com/caoyingjunz/pixiu/pkg/db"
pixiuConfig "github.com/caoyingjunz/pixiulib/config"
)

const (
maxIdleConns = 10
maxOpenConns = 100

defaultListen = 8080
defaultTokenKey = "pixiu"
defaultConfigFile = "/etc/pixiu/config.yaml"
Expand All @@ -48,6 +41,8 @@ type Options struct {
ComponentConfig config.Config
HttpEngine *gin.Engine

// 数据库
db.DB
// 数据库接口
Factory db.ShareDaoFactory
// 貔貅主控制接口
Expand Down Expand Up @@ -112,47 +107,17 @@ func (o *Options) BindFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.ConfigFile, "configfile", defaultConfigFile, "The location of the pixiu configuration file")
}

func (o *Options) register() error {
func (o *Options) register() (err error) {
// 注册数据库
if err := o.registerDatabase(); err != nil {
return err
}
o.registerDatabase()

// TODO: 注册其他依赖
return nil
return
}

func (o *Options) registerDatabase() error {
sqlConfig := o.ComponentConfig.Mysql
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local",
sqlConfig.User,
sqlConfig.Password,
sqlConfig.Host,
sqlConfig.Port,
sqlConfig.Name)

opt := &gorm.Config{}
if o.ComponentConfig.Default.Mode == "debug" {
opt.Logger = logger.Default.LogMode(logger.Info)
}

DB, err := gorm.Open(mysql.Open(dsn), opt)
if err != nil {
return err
}
// 设置数据库连接池
sqlDB, err := DB.DB()
if err != nil {
return err
}
sqlDB.SetMaxIdleConns(maxIdleConns)
sqlDB.SetMaxOpenConns(maxOpenConns)

o.Factory, err = db.NewDaoFactory(DB, o.ComponentConfig.Default.AutoMigrate)
if err != nil {
return err
}
return nil
func (o *Options) registerDatabase() {
o.DB = db.NewDB(o.ComponentConfig.Db, o.ComponentConfig.Default.Mode == "debug")
o.Factory = db.NewDaoFactory(o.DB)
}

// Validate validates all the required options.
Expand Down
23 changes: 21 additions & 2 deletions cmd/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ func NewServerCommand(version string) *cobra.Command {

// Run 优雅启动貔貅服务
func Run(opt *options.Options) error {
// Try to connect database.
klog.Info("connecting to database")
if err := opt.DB.Open(); err != nil {
return fmt.Errorf("failed to open database connection: %v", err)
}

// Auto apply the latest data models.
if opt.ComponentConfig.Default.AutoMigrate {
klog.Info("migrating data models")
if err := opt.Factory.Migrate(); err != nil {
return fmt.Errorf("failed to migrate database: %v", err)
}
}

srv := &http.Server{
Addr: fmt.Sprintf(":%d", opt.ComponentConfig.Default.Listen),
Handler: opt.HttpEngine,
Expand All @@ -99,10 +113,10 @@ func Run(opt *options.Options) error {
}()

// Wait for interrupt signal to gracefully shut down the server with a timeout of 5 seconds.
quit := make(chan os.Signal)
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
klog.Infof("shutting pixiu server down ...")
klog.Infof("shutting pixiu server down...")

// The context is used to inform the server it has 5 seconds to finish the request
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
Expand All @@ -112,5 +126,10 @@ func Run(opt *options.Options) error {
klog.Fatalf("pixiu server forced to shutdown: %v", err)
}

// Close the database connection.
if err := opt.DB.Close(); err != nil {
klog.Fatalf("failed to close database connection: %v", err)
}

return nil
}
41 changes: 22 additions & 19 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
default:
# 运行模式,可选 debug 和 release
mode: debug
# 服务监听端口
listen: 8090
# jwt 签名的 key
jwt_key: pixiu
# 自动创建指定模型的数据库表结构,不会更新已存在的数据库表
auto_migrate: true

log_format: json

# 数据库地址信息
mysql:
host: peng
user: root
password: Pixiu868686
port: 3306
name: pixiu
default:
# 运行模式,可选 debug 和 release
mode: debug
# 服务监听端口
listen: 8090
# jwt 签名的 key
jwt_key: pixiu
# 自动创建指定模型的数据库表结构,不会更新已存在的数据库表
auto_migrate: true

log_format: json

db:
# 数据库地址信息
# mysql:
# host: peng
# user: root
# password: Pixiu868686
# port: 3306
# name: pixiu
sqlite:
db: pixiu.db
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ require (
github.com/lib/pq v1.10.2 // indirect
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-sqlite3 v1.14.12 // indirect
github.com/mittwald/go-helm-client v0.8.1
github.com/pelletier/go-toml/v2 v2.2.0 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
Expand All @@ -43,6 +42,7 @@ require (
golang.org/x/time v0.1.0
google.golang.org/protobuf v1.33.0 // indirect
gorm.io/driver/mysql v1.3.6
gorm.io/driver/sqlite v1.3.6
gorm.io/gorm v1.23.8
helm.sh/helm/v3 v3.6.3
k8s.io/api v0.22.1
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1524,6 +1524,9 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.3.6 h1:BhX1Y/RyALb+T9bZ3t07wLnPZBukt+IRkMn8UZSNbGM=
gorm.io/driver/mysql v1.3.6/go.mod h1:sSIebwZAVPiT+27jK9HIwvsqOGKx3YMPmrA3mBJR10c=
gorm.io/driver/sqlite v1.3.6 h1:Fi8xNYCUplOqWiPa3/GuCeowRNBRGTf62DEmhMDHeQQ=
gorm.io/driver/sqlite v1.3.6/go.mod h1:Sg1/pvnKtbQ7jLXxfZa+jSHvoX8hoZA8cn4xllOMTgE=
gorm.io/gorm v1.23.4/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
gorm.io/gorm v1.23.8 h1:h8sGJ+biDgBA1AD1Ha9gFCx7h8npU7AsLdlkX0n2TpE=
gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (c *cluster) Create(ctx context.Context, req *types.CreateClusterRequest) e
}

var cs *client.ClusterSet
var txFunc db.TxFunc = func() (err error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var txFunc = func() error 去掉类型,没必要为它多写一个包

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是之前既有的类型定义,在db包下。
新开一个包写它也是为了解决循环依赖

var txFunc = func() (err error) {
cs, err = client.NewClusterSet(req.KubeConfig)
return err
}
Expand Down
Loading
Loading