Skip to content

Commit

Permalink
Optimise plan config APIs (#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
caoyingjunz authored Jun 10, 2024
1 parent 805de39 commit 266a475
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 27 deletions.
4 changes: 2 additions & 2 deletions api/server/router/plan/config_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ func (t *planRouter) getPlanConfig(c *gin.Context) {
r := httputils.NewResponse()

var (
opt planConfigMeta
opt planMeta
err error
)
if err = httputils.ShouldBindAny(c, nil, &opt, nil); err != nil {
httputils.SetFailed(c, r, err)
return
}
if r.Result, err = t.c.Plan().GetConfig(c, opt.PlanId, opt.ConfigId); err != nil {
if r.Result, err = t.c.Plan().GetConfig(c, opt.PlanId); err != nil {
httputils.SetFailed(c, r, err)
return
}
Expand Down
3 changes: 1 addition & 2 deletions api/server/router/plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,11 @@ func (t *planRouter) initRoutes(ginEngine *gin.Engine) {
planRoute.POST("/:planId/configs", t.createPlanConfig)
planRoute.PUT("/:planId/configs/:configId", t.updatePlanConfig)
planRoute.DELETE("/:planId/configs/:configId", t.deletePlanConfig)
planRoute.GET("/:planId/configs/:configId", t.getPlanConfig)
planRoute.GET("/:planId/configs", t.getPlanConfig)

// 执行指定任务
planRoute.POST("/:planId/tasks/:taskId", t.runTasks)
// 查询任务列表
planRoute.POST("/:planId/tasks", t.listTasks)

}
}
4 changes: 3 additions & 1 deletion pkg/controller/plan/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"time"

"github.com/caoyingjunz/pixiu/pkg/db/model"
"github.com/caoyingjunz/pixiu/pkg/util/container"
)

Expand Down Expand Up @@ -63,7 +64,8 @@ type DeployChart struct {
handlerTask
}

func (b DeployChart) Name() string { return "部署基础组件" }
func (b DeployChart) Name() string { return "部署基础组件" }
func (b DeployChart) Step() model.PlanStep { return model.CompletedPlanStep }

// Run 以容器的形式执行 BootStrap 任务,如果存在旧的容器,则先删除在执行
func (b DeployChart) Run() error {
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ type Interface interface {
GetNode(ctx context.Context, pid int64, nodeId int64) (*types.PlanNode, error)
ListNodes(ctx context.Context, pid int64) ([]types.PlanNode, error)

CreateConfig(ctx context.Context, pid int64, req *types.CreatePlanConfigRequest) error
CreateConfig(ctx context.Context, planId int64, req *types.CreatePlanConfigRequest) error
UpdateConfig(ctx context.Context, pid int64, cfgId int64, req *types.UpdatePlanConfigRequest) error
DeleteConfig(ctx context.Context, pid int64, cfgId int64) error
GetConfig(ctx context.Context, pid int64, cfgId int64) (*types.PlanConfig, error)
GetConfig(ctx context.Context, planId int64) (*types.PlanConfig, error)

// Run 启动 worker 处理协程
Run(ctx context.Context, workers int) error
Expand Down
61 changes: 41 additions & 20 deletions pkg/controller/plan/plan_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package plan

import (
"context"
"fmt"

"k8s.io/klog/v2"

Expand All @@ -26,29 +27,28 @@ import (
"github.com/caoyingjunz/pixiu/pkg/types"
)

func (p *plan) CreateConfig(ctx context.Context, pid int64, req *types.CreatePlanConfigRequest) error {
ks, err := req.Kubernetes.Marshal()
if err != nil {
return err
func (p *plan) preCreateConfig(ctx context.Context, planId int64, req *types.CreatePlanConfigRequest) error {
_, err := p.factory.Plan().GetConfigByPlan(ctx, planId)
if err == nil {
return fmt.Errorf("plan(%d) 配置已存在", planId)
}
ns, err := req.Network.Marshal()
if err != nil {

return nil
}

func (p *plan) CreateConfig(ctx context.Context, pid int64, req *types.CreatePlanConfigRequest) error {
// 创建前检查
if err := p.preCreateConfig(ctx, pid, req); err != nil {
return err
}
rs, err := req.Runtime.Marshal()

planConfig, err := p.buildPlanConfig(ctx, req)
if err != nil {
return err
}

if _, err = p.factory.Plan().CreatConfig(ctx, &model.Config{
Name: req.Name,
PlanId: pid,
Region: req.Region,
Kubernetes: ks,
Network: ns,
Runtime: rs,
Description: req.Description,
}); err != nil {
planConfig.PlanId = pid
// 创建配置
if _, err = p.factory.Plan().CreatConfig(ctx, planConfig); err != nil {
klog.Errorf("failed to create plan(%s) config(%d): %v", req.Name, pid, err)
return err
}
Expand All @@ -71,16 +71,37 @@ func (p *plan) DeleteConfig(ctx context.Context, pid int64, cfgId int64) error {
return nil
}

func (p *plan) GetConfig(ctx context.Context, pid int64, cfgId int64) (*types.PlanConfig, error) {
object, err := p.factory.Plan().GetConfig(ctx, cfgId)
func (p *plan) GetConfig(ctx context.Context, pid int64) (*types.PlanConfig, error) {
object, err := p.factory.Plan().GetConfigByPlan(ctx, pid)
if err != nil {
klog.Errorf("failed to get plan(%d) config(%d): %v", pid, cfgId, err)
klog.Errorf("failed to get plan(%d) config: %v", pid, err)
return nil, errors.ErrServerInternal
}

return p.modelConfig2Type(object)
}

func (p *plan) buildPlanConfig(ctx context.Context, req *types.CreatePlanConfigRequest) (*model.Config, error) {
kubeConfig, err := req.Kubernetes.Marshal()
if err != nil {
return nil, err
}
networkConfig, err := req.Network.Marshal()
if err != nil {
return nil, err
}
runtimeConfig, err := req.Runtime.Marshal()
if err != nil {
return nil, err
}

return &model.Config{
Kubernetes: kubeConfig,
Network: networkConfig,
Runtime: runtimeConfig,
}, nil
}

func (p *plan) modelConfig2Type(o *model.Config) (*types.PlanConfig, error) {
ks := &types.KubernetesSpec{}
if err := ks.Unmarshal(o.Kubernetes); err != nil {
Expand Down

0 comments on commit 266a475

Please sign in to comment.