Skip to content

Commit

Permalink
feat: add env to pipelinerun event && support more configs of mysql
Browse files Browse the repository at this point in the history
Signed-off-by: yeka <[email protected]>
  • Loading branch information
iamyeka committed Sep 12, 2024
1 parent 6cdb919 commit c80f116
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
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

0 comments on commit c80f116

Please sign in to comment.