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

feat: add env to pipelinerun event && support more configs of mysql #250

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions lib/orm/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,41 @@ import (

// MySQL ...
type MySQL struct {
Host string `json:"host"`
Port int `json:"port"`
Username string `json:"username"`
Password string `json:"password,omitempty"`
Database string `json:"database"`
PrometheusEnabled bool `json:"prometheusEnabled"`
Host string `json:"host"`
Port int `json:"port"`
Username string `json:"username"`
Password string `json:"password,omitempty"`
Database string `json:"database"`
PrometheusEnabled bool `json:"prometheusEnabled"`
SlowThreshold time.Duration `json:"slowThreshold"`
MaxIdleConns int `json:"maxIdleConns"`
MaxOpenConns int `json:"maxOpenConns"`
}

func NewMySQLDB(db *MySQL) (*gorm.DB, error) {
conn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", db.Username,
db.Password, db.Host, db.Port, db.Database)

// Set default value for SlowThreshold if not provided
if db.SlowThreshold == 0 {
db.SlowThreshold = 200 * time.Millisecond
}
// Set default value for MaxIdleConns if not provided
if db.MaxIdleConns == 0 {
db.MaxIdleConns = 10
}
// Set default value for MaxOpenConns if not provided
if db.MaxOpenConns == 0 {
db.MaxOpenConns = 100
}

sqlDB, err := sql.Open("mysql", conn)
if err != nil {
return nil, err
}

sqlDB.SetMaxIdleConns(10)
sqlDB.SetMaxOpenConns(100)
sqlDB.SetMaxIdleConns(db.MaxIdleConns)
sqlDB.SetMaxOpenConns(db.MaxOpenConns)
sqlDB.SetConnMaxLifetime(time.Hour)
sqlDB.SetConnMaxIdleTime(time.Hour)

Expand All @@ -62,6 +78,13 @@ func NewMySQLDB(db *MySQL) (*gorm.DB, error) {
TablePrefix: "tb_",
SingularTable: true,
},
Logger: logger.New(log.New(os.Stdout, "\r\n", log.LstdFlags),
logger.Config{
SlowThreshold: db.SlowThreshold,
LogLevel: logger.Warn,
IgnoreRecordNotFoundError: false,
Colorful: true,
}),
})

if db.PrometheusEnabled {
Expand Down
2 changes: 2 additions & 0 deletions pkg/eventhandler/wlgenerator/wlgenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ type PipelinerunInfo struct {
ResourceCommonInfo
ClusterID uint `json:"clusterID,omitempty"`
ClusterName string `json:"clusterName,omitempty"`
ClusterEnv string `json:"clusterEnv,omitempty"`
Action string `json:"action,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Expand Down Expand Up @@ -329,6 +330,7 @@ func (w *WebhookLogGenerator) makeRequestBody(ctx context.Context, dep *messageD
}
return ""
}(),
ClusterEnv: dep.cluster.EnvironmentName,
Action: dep.pipelinerun.Action,
Title: dep.pipelinerun.Title,
Description: dep.pipelinerun.Description,
Expand Down
Loading