Skip to content

Commit

Permalink
Add plan deploy worker images supported (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
caoyingjunz authored Jun 13, 2024
1 parent ddec909 commit 085d1db
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 21 deletions.
22 changes: 20 additions & 2 deletions cmd/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var ErrInvalidLogFormat = errors.New("invalid log format")
type Config struct {
Default DefaultOptions `yaml:"default"`
Mysql MysqlOptions `yaml:"mysql"`
Worker WorkerOptions `yaml:"worker"`
}

type DefaultOptions struct {
Expand All @@ -40,8 +41,6 @@ type DefaultOptions struct {
// 自动创建指定模型的数据库表结构,不会更新已存在的数据库表
AutoMigrate bool `yaml:"auto_migrate"`

WorkDir string `yaml:"work_dir"`

LogOptions `yaml:",inline"`
}

Expand Down Expand Up @@ -79,12 +78,31 @@ func (o LogOptions) Valid() error {
}
}

type WorkerOptions struct {
WorkDir string `yaml:"work_dir"`
Engines []Engine `yaml:"engines"`
}

type Engine struct {
Version string `yaml:"version"`
Image string `yaml:"image"`
}

func (w WorkerOptions) Valid() error {
// TODO
return nil
}

func (c *Config) Valid() (err error) {
if err = c.Default.Valid(); err != nil {
return
}
if err = c.Mysql.Valid(); err != nil {
return
}
if err = c.Worker.Valid(); err != nil {
return
}

return
}
17 changes: 2 additions & 15 deletions cmd/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (

"github.com/caoyingjunz/pixiu/cmd/app/config"
"github.com/caoyingjunz/pixiu/pkg/controller"
"github.com/caoyingjunz/pixiu/pkg/controller/plan"
"github.com/caoyingjunz/pixiu/pkg/db"
pixiuConfig "github.com/caoyingjunz/pixiulib/config"
)
Expand Down Expand Up @@ -98,8 +97,8 @@ func (o *Options) Complete() error {
if o.ComponentConfig.Default.LogFormat == "" {
o.ComponentConfig.Default.LogFormat = defaultLogFormat
}
if o.ComponentConfig.Default.WorkDir == "" {
o.ComponentConfig.Default.WorkDir = defaultWorkDir
if o.ComponentConfig.Worker.WorkDir == "" {
o.ComponentConfig.Worker.WorkDir = defaultWorkDir
}

if err := o.ComponentConfig.Valid(); err != nil {
Expand All @@ -126,22 +125,10 @@ func (o *Options) register() error {
return err
}

// 注册 worker 的工作目录
if err := o.registerWorkDir(); err != nil {
return err
}

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

func (o *Options) registerWorkDir() error {
if err := plan.Register(o.ComponentConfig.Default.WorkDir); err != nil {
return err
}
return nil
}

func (o *Options) registerDatabase() error {
sqlConfig := o.ComponentConfig.Mysql
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local",
Expand Down
9 changes: 9 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,12 @@ mysql:
password: Pixiu868686
port: 3306
name: pixiu

worker:
engines:
- version: v2
image: harbor.cloud.pixiuio.com/pixiuio/kubez-ansible:v2.0.1
- version: v3
image: harbor.cloud.pixiuio.com/pixiuio/kubez-ansible:v3.0.1
- version: offline
image: harbor.cloud.pixiuio.com/pixiuio/kubez-ansible:v2.0.1-offline
2 changes: 1 addition & 1 deletion pkg/controller/plan/bootstrap_servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (b BootStrap) Run() error {
defer cancel()

// 启动执行容器
if err = cli.StartAndWaitForContainer(ctx); err != nil {
if err = cli.StartAndWaitForContainer(ctx, "harbor.cloud.pixiuio.com/pixiuio/kubez-ansible:v3.0.1"); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/plan/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (b Deploy) Run() error {
defer cancel()

// 启动执行容器
if err = cli.StartAndWaitForContainer(ctx); err != nil {
if err = cli.StartAndWaitForContainer(ctx, "harbor.cloud.pixiuio.com/pixiuio/kubez-ansible:v3.0.1"); err != nil {
return err
}

Expand Down
15 changes: 15 additions & 0 deletions pkg/controller/plan/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package plan

import (
"context"
"fmt"
"time"

"k8s.io/apimachinery/pkg/util/wait"
Expand Down Expand Up @@ -161,6 +162,20 @@ func (p *plan) createPlanTasksIfNotExist(tasks ...Handler) error {
return nil
}

func (p *plan) WorkDir() string {
return p.cc.Worker.WorkDir
}

func (p *plan) getImageForWorker(v string) (string, error) {
engines := p.cc.Worker.Engines
for _, engine := range engines {
if engine.Version == v {
return engine.Image, nil
}
}
return "", fmt.Errorf("version(%s) worker image not found", v)
}

// 同步任务状态
// 任务启动时设置为运行中,结束时同步为结束状态(成功或者失败)
// TODO: 后续优化
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func NewContainer(action string, planId int64) (*Container, error) {
}

// StartAndWaitForContainer 创建,启动容器,并等待容器退出
func (c *Container) StartAndWaitForContainer(ctx context.Context) error {
func (c *Container) StartAndWaitForContainer(ctx context.Context, image string) error {
// 已经存在,则先删除运行的容器
if err := c.ClearContainer(ctx); err != nil {
return err
Expand All @@ -58,7 +58,7 @@ func (c *Container) StartAndWaitForContainer(ctx context.Context) error {
"author": "caoyingjunz",
"pixiuName": c.name,
},
Image: "jacky06/kubez-ansible:v3.0.1",
Image: image,
Env: []string{fmt.Sprintf("COMMAND=%s", c.action)},
}
hostConfig := &container.HostConfig{
Expand Down

0 comments on commit 085d1db

Please sign in to comment.