Skip to content

Commit

Permalink
fix(dbm-services): 创建语义检查pod指定资源限制 close #2179
Browse files Browse the repository at this point in the history
add config

fix

stage save
  • Loading branch information
ymakedaq authored and zhangzhw8 committed Nov 29, 2023
1 parent 51d0e25 commit 1b4da7a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 12 deletions.
51 changes: 41 additions & 10 deletions dbm-services/mysql/db-simulation/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ var GAppConfig = AppConfig{}

// AppConfig TODO
type AppConfig struct {
BkRepo BkRepoConfig `yaml:"bkrepo"`
Image Images
ListenAddr string `yaml:"listenAddr"`
RulePath string `yaml:"rulePath"`
SpiderRulePath string `yaml:"spiderRulePath"`
Bcs BcsConfig `yaml:"bcs"`
DbConf DbConfig `yaml:"dbconf"`
MirrorsAddress []ImgConfig `yaml:"mirrorsAddress"`
Debug bool `yaml:"debug"`
LoadRuleFromdb bool `yaml:"loadRuleFromdb"`
BkRepo BkRepoConfig `yaml:"bkrepo"`
Image Images
ListenAddr string `yaml:"listenAddr"`
RulePath string `yaml:"rulePath"`
SpiderRulePath string `yaml:"spiderRulePath"`
Bcs BcsConfig `yaml:"bcs"`
DbConf DbConfig `yaml:"dbconf"`
MirrorsAddress []ImgConfig `yaml:"mirrorsAddress"`
Debug bool `yaml:"debug"`
LoadRuleFromdb bool `yaml:"loadRuleFromdb"`
MySQLPodResource MySQLPodResource `yaml:"mysqlPodResource"`
TdbctlPodResource TdbctlPodResource `yaml:"tdbctlPodResource"`
}

// BkRepoConfig TODO
Expand Down Expand Up @@ -63,6 +65,24 @@ type DbConfig struct {
Port int `yaml:"port"`
}

// MySQLPodResource TODO
type MySQLPodResource struct {
Limits PodResource `yaml:"limits"`
Requests PodResource `yaml:"requests"`
}

// TdbctlPodResource TODO
type TdbctlPodResource struct {
Limits PodResource `yaml:"limits"`
Requests PodResource `yaml:"requests"`
}

// PodResource TODO
type PodResource struct {
Cpu string `yaml:"cpu"`
Mem string `yaml:"mem"`
}

// ImgConfig TODO
type ImgConfig struct {
Version string `yaml:"version"`
Expand Down Expand Up @@ -164,6 +184,17 @@ func init() {
}
}
logger.Info("app config %v", GAppConfig)
logger.Info("pod resource limit config %v", GAppConfig.MySQLPodResource)
}

// IsEmptyMySQLPodResourceConfig TODO
func IsEmptyMySQLPodResourceConfig() bool {
return GAppConfig.MySQLPodResource == MySQLPodResource{}
}

// IsEmptyTdbctlPodResourceConfig TODO
func IsEmptyTdbctlPodResourceConfig() bool {
return GAppConfig.TdbctlPodResource == TdbctlPodResource{}
}

// loadConfig 加载配置文件
Expand Down
39 changes: 38 additions & 1 deletion dbm-services/mysql/db-simulation/app/service/kubernets.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -136,6 +137,7 @@ func (k *DbPodSets) CreateClusterPod() (err error) {
Name: "MYSQL_ROOT_PASSWORD",
Value: k.BaseInfo.RootPwd,
}},
Resources: k.getResourceLimit(),
ImagePullPolicy: v1.PullIfNotPresent,
Image: k.DbImage,
Args: []string{"mysqld", "--defaults-file=/etc/my.cnf", "--port=20000", fmt.Sprintf("--character-set-server=%s",
Expand All @@ -156,6 +158,7 @@ func (k *DbPodSets) CreateClusterPod() (err error) {
Name: "MYSQL_ROOT_PASSWORD",
Value: k.BaseInfo.RootPwd,
}},
Resources: k.getResourceLimit(),
ImagePullPolicy: v1.PullIfNotPresent,
Image: k.SpiderImage,
Args: []string{"mysqld", "--defaults-file=/etc/my.cnf", "--port=25000", fmt.Sprintf("--character-set-server=%s",
Expand All @@ -177,6 +180,7 @@ func (k *DbPodSets) CreateClusterPod() (err error) {
Name: "MYSQL_ROOT_PASSWORD",
Value: k.BaseInfo.RootPwd,
}},
Resources: k.gettdbctlResourceLimit(),
ImagePullPolicy: v1.PullIfNotPresent,
Image: k.TdbCtlImage,
Args: []string{"mysqld", "--defaults-file=/etc/my.cnf", "--port=26000", "--tc-admin=1",
Expand Down Expand Up @@ -267,6 +271,38 @@ func (k *DbPodSets) createpod(pod *v1.Pod, probePort int) (err error) {
return err
}

func (k *DbPodSets) getResourceLimit() v1.ResourceRequirements {
if !config.IsEmptyMySQLPodResourceConfig() {
return v1.ResourceRequirements{
Limits: v1.ResourceList{
v1.ResourceCPU: resource.MustParse(config.GAppConfig.MySQLPodResource.Limits.Cpu),
v1.ResourceMemory: resource.MustParse(config.GAppConfig.MySQLPodResource.Limits.Mem),
},
Requests: v1.ResourceList{
v1.ResourceCPU: resource.MustParse(config.GAppConfig.MySQLPodResource.Requests.Cpu),
v1.ResourceMemory: resource.MustParse(config.GAppConfig.MySQLPodResource.Requests.Mem),
},
}
}
return v1.ResourceRequirements{}
}

func (k *DbPodSets) gettdbctlResourceLimit() v1.ResourceRequirements {
if !config.IsEmptyTdbctlPodResourceConfig() {
return v1.ResourceRequirements{
Limits: v1.ResourceList{
v1.ResourceCPU: resource.MustParse(config.GAppConfig.TdbctlPodResource.Limits.Cpu),
v1.ResourceMemory: resource.MustParse(config.GAppConfig.TdbctlPodResource.Limits.Mem),
},
Requests: v1.ResourceList{
v1.ResourceCPU: resource.MustParse(config.GAppConfig.TdbctlPodResource.Requests.Cpu),
v1.ResourceMemory: resource.MustParse(config.GAppConfig.TdbctlPodResource.Requests.Mem),
},
}
}
return v1.ResourceRequirements{}
}

// CreateMySQLPod TODO
func (k *DbPodSets) CreateMySQLPod() (err error) {
c := &v1.Pod{
Expand All @@ -281,7 +317,8 @@ func (k *DbPodSets) CreateMySQLPod() (err error) {
},
Spec: v1.PodSpec{
Containers: []v1.Container{{
Name: app.MySQL,
Resources: k.getResourceLimit(),
Name: app.MySQL,
Env: []v1.EnvVar{{
Name: "MYSQL_ROOT_PASSWORD",
Value: k.BaseInfo.RootPwd,
Expand Down
2 changes: 1 addition & 1 deletion helm-charts/bk-dbm/charts/db-simulation/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: v2
appVersion: 0.0.1-alpha.40
appVersion: 0.0.1-alpha.41
description: A Helm chart for Kubernetes
name: db-simulation
type: application
Expand Down
15 changes: 15 additions & 0 deletions helm-charts/bk-dbm/templates/dbsimulation-configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,18 @@ data:
host: "{{ $dbsimulationDB.host }}"
port: "{{ $dbsimulationDB.port }}"
debug: false
tdbctlPodResource:
limits:
cpu: "2"
mem: "4Gi"
requests:
cpu: "4"
mem: "8Gi"
mysqlPodResource:
limits:
cpu: "4"
mem: "8Gi"
requests:
cpu: "8"
mem: "16Gi"

0 comments on commit 1b4da7a

Please sign in to comment.