Skip to content

Commit

Permalink
Add cloudnative components API supported (caoyingjunz#448)
Browse files Browse the repository at this point in the history
  • Loading branch information
caoyingjunz authored Jul 6, 2024
1 parent 14b3183 commit 7949341
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 48 deletions.
17 changes: 10 additions & 7 deletions api/server/router/plan/plan_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,21 @@ func (t *planRouter) stopPlan(c *gin.Context) {
}

type DistributionsMeta struct {
Centos []string `json:"centos,omitempty"`
Debian []string `json:"debian,omitempty"`
Ubuntu []string `json:"ubuntu,omitempty"`
Centos []string `json:"centos,omitempty"`
Ubuntu []string `json:"ubuntu,omitempty"`
Debian []string `json:"debian,omitempty"`
OpenEuler []string `json:"openEuler,omitempty"`
Rocky []string `json:"rocky,omitempty"`
}

func (t *planRouter) getDistributions(c *gin.Context) {
r := httputils.NewResponse()

r.Result = &DistributionsMeta{
Centos: []string{"centos7"},
Ubuntu: []string{"ubuntu18.04", "ubuntu20.04", "ubuntu22.04", "ubuntu24.04"},
Debian: []string{"debian10", "debian11"},
Centos: []string{"centos7"},
Ubuntu: []string{"ubuntu18.04", "ubuntu20.04", "ubuntu22.04", "ubuntu24.04"},
Debian: []string{"debian10", "debian11"},
OpenEuler: []string{"openEuler22.03"},
Rocky: []string{"rocky8.5", "rocky9.2", "rocky9.3"},
}

httputils.SetSuccess(c, r)
Expand Down
4 changes: 4 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ worker:
- debian11
- ubuntu20.04
- ubuntu22.04
- rocky8.5
- rocky9.2
- rocky9.3
- openEuler22.03
1 change: 1 addition & 0 deletions pkg/controller/plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type plan struct {
// 1. 创建部署计划
// 2. 创建部署配置
// 3. 创建节点列表
// 3. 创建扩展组件
func (p *plan) Create(ctx context.Context, req *types.CreatePlanRequest) error {
object, err := p.factory.Plan().Create(ctx, &model.Plan{
Name: req.Name,
Expand Down
10 changes: 10 additions & 0 deletions pkg/controller/plan/plan_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,18 @@ func (p *plan) buildPlanConfig(ctx context.Context, req *types.CreatePlanConfigR
if err != nil {
return nil, err
}
componentConfig, err := req.Component.Marshal()
if err != nil {
return nil, err
}

return &model.Config{
Region: req.Region,
OSImage: req.OSImage,
Kubernetes: kubeConfig,
Network: networkConfig,
Runtime: runtimeConfig,
Component: componentConfig,
}, nil
}

Expand All @@ -170,6 +175,10 @@ func (p *plan) modelConfig2Type(o *model.Config) (*types.PlanConfig, error) {
if err := rs.Unmarshal(o.Runtime); err != nil {
return nil, err
}
cs := &types.ComponentSpec{}
if err := cs.Unmarshal(o.Component); err != nil {
return nil, err
}

return &types.PlanConfig{
PixiuMeta: types.PixiuMeta{
Expand All @@ -186,5 +195,6 @@ func (p *plan) modelConfig2Type(o *model.Config) (*types.PlanConfig, error) {
Kubernetes: *ks,
Network: *ns,
Runtime: *rs,
Component: *cs,
}, nil
}
6 changes: 4 additions & 2 deletions pkg/controller/plan/plan_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package plan

import (
"context"
"strings"

"k8s.io/klog/v2"

Expand Down Expand Up @@ -115,10 +116,11 @@ func (p *plan) buildNodeFromRequest(planId int64, req *types.CreatePlanNodeReque
if err != nil {
return nil, err
}

return &model.Node{
Name: req.Name,
PlanId: planId,
Role: req.Role,
Role: strings.Join(req.Role, ","),
CRI: req.CRI,
Ip: req.Ip,
Auth: auth,
Expand Down Expand Up @@ -220,7 +222,7 @@ func (p *plan) modelNode2Type(o *model.Node) (*types.PlanNode, error) {
},
PlanId: o.PlanId,
Name: o.Name,
Role: o.Role,
Role: strings.Split(o.Role, ","),
Ip: o.Ip,
Auth: auth,
}, nil
Expand Down
27 changes: 16 additions & 11 deletions pkg/controller/plan/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"fmt"
"path/filepath"
"strings"
"text/template"

"github.com/caoyingjunz/pixiu/pkg/db/model"
Expand Down Expand Up @@ -118,21 +119,25 @@ func ParseMultinode(data TaskData, workDir string) (Multinode, error) {
nodeAuth.Key.File = rsa
planNode := types.PlanNode{Name: node.Name, Auth: nodeAuth}

roles := strings.Split(node.Role, ",")
if runtime.IsDocker() {
if node.Role == model.MasterRole {
multinode.DockerMaster = append(multinode.DockerMaster, planNode)
}
if node.Role == model.NodeRole {
multinode.DockerNode = append(multinode.DockerNode, planNode)
for _, role := range roles {
if role == model.MasterRole {
multinode.DockerMaster = append(multinode.DockerMaster, planNode)
}
if role == model.NodeRole {
multinode.DockerNode = append(multinode.DockerNode, planNode)
}
}
}

if runtime.IsContainerd() {
if node.Role == model.MasterRole {
multinode.ContainerdMaster = append(multinode.ContainerdMaster, planNode)
}
if node.Role == model.NodeRole {
multinode.ContainerdNode = append(multinode.ContainerdNode, planNode)
for _, role := range roles {
if role == model.MasterRole {
multinode.ContainerdMaster = append(multinode.ContainerdMaster, planNode)
}
if role == model.NodeRole {
multinode.ContainerdNode = append(multinode.ContainerdNode, planNode)
}
}
}
}
Expand Down
19 changes: 10 additions & 9 deletions pkg/db/model/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ func (plan *Plan) TableName() string {
return "plans"
}

type KubeRole int
type KubeRole string

const (
NodeRole KubeRole = iota // kubernetes node role
MasterRole // kubernetes master role
MasterRole string = "master" // kubernetes master role
NodeRole string = "node" // kubernetes node role
)

type CRI string
Expand All @@ -52,12 +52,12 @@ const (
type Node struct {
pixiu.Model

Name string `json:"name"` // 主机名,相同plan内不允许重复
PlanId int64 `json:"plan_id"`
Role KubeRole `json:"role"` // k8s 节点的角色,master 为 1 和 node 为 0
CRI CRI `json:"cri"`
Ip string `json:"ip"`
Auth string `json:"auth"`
Name string `json:"name"` // 主机名,相同plan内不允许重复
PlanId int64 `json:"plan_id"`
Role string `json:"role"` // k8s 节点的角色,master 和 node
CRI CRI `json:"cri"`
Ip string `json:"ip"`
Auth string `json:"auth"`
}

func (node *Node) TableName() string {
Expand All @@ -73,6 +73,7 @@ type Config struct {
Kubernetes string `json:"kubernetes"`
Network string `json:"network"`
Runtime string `json:"runtime"`
Component string `json:"component"`
}

func (config *Config) TableName() string {
Expand Down
15 changes: 15 additions & 0 deletions pkg/types/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,21 @@ func (rs *RuntimeSpec) Unmarshal(s string) error {
return nil
}

func (cs ComponentSpec) Marshal() (string, error) {
data, err := json.Marshal(cs)
if err != nil {
return "", err
}
return string(data), nil
}

func (cs *ComponentSpec) Unmarshal(s string) error {
if err := json.Unmarshal([]byte(s), cs); err != nil {
return err
}
return nil
}

func (rs *RuntimeSpec) IsDocker() bool {
return rs.Runtime == string(model.DockerCRI)
}
Expand Down
27 changes: 14 additions & 13 deletions pkg/types/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,22 @@ type (
}

CreatePlanNodeRequest struct {
Name string `json:"name" binding:"omitempty"` // required
PlanId int64 `json:"plan_id"`
Role model.KubeRole `json:"role"` // k8s 节点的角色,master 为 1 和 node 为 0
CRI model.CRI `json:"cri"`
Ip string `json:"ip"`
Auth PlanNodeAuth `json:"auth"`
Name string `json:"name" binding:"omitempty"` // required
PlanId int64 `json:"plan_id"`
Role []string `json:"role"` // k8s 节点的角色,master 和 node
CRI model.CRI `json:"cri"`
Ip string `json:"ip"`
Auth PlanNodeAuth `json:"auth"`
}

UpdatePlanNodeRequest struct {
ResourceVersion int64 `json:"resource_version" binding:"required"` // required
Name string `json:"name" binding:"omitempty"` // required
PlanId int64 `json:"plan_id"`
Role model.KubeRole `json:"role"` // k8s 节点的角色,master 为 1 和 node 为 0
CRI model.CRI `json:"cri"`
Ip string `json:"ip"`
Auth PlanNodeAuth `json:"auth"`
ResourceVersion int64 `json:"resource_version" binding:"required"` // required
Name string `json:"name" binding:"omitempty"` // required
PlanId int64 `json:"plan_id"`
Role []string `json:"role"` // k8s 节点的角色,master 为 1 和 node 为 0
CRI model.CRI `json:"cri"`
Ip string `json:"ip"`
Auth PlanNodeAuth `json:"auth"`
}

CreatePlanConfigRequest struct {
Expand All @@ -127,6 +127,7 @@ type (
Kubernetes KubernetesSpec `json:"kubernetes"`
Network NetworkSpec `json:"network"`
Runtime RuntimeSpec `json:"runtime"`
Component ComponentSpec `json:"component"` // 支持的扩展组件配置
}

UpdatePlanConfigRequest struct {
Expand Down
35 changes: 29 additions & 6 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ type PlanNode struct {
PixiuMeta `json:",inline"`
TimeMeta `json:",inline"`

Name string `json:"name"` // required
PlanId int64 `json:"plan_id,omitempty"`
Role model.KubeRole `json:"role"` // k8s 节点的角色,master 为 1 和 node 为 0
CRI model.CRI `json:"cri"`
Ip string `json:"ip"`
Auth PlanNodeAuth `json:"auth,omitempty"`
Name string `json:"name"` // required
PlanId int64 `json:"plan_id,omitempty"`
Role []string `json:"role"` // k8s 节点的角色,master 和 node
CRI model.CRI `json:"cri"`
Ip string `json:"ip"`
Auth PlanNodeAuth `json:"auth,omitempty"`
}

type AuthType string
Expand Down Expand Up @@ -171,6 +171,8 @@ type PlanConfig struct {
Kubernetes KubernetesSpec `json:"kubernetes"`
Network NetworkSpec `json:"network"`
Runtime RuntimeSpec `json:"runtime"`
Component ComponentSpec `json:"component"` // 支持的扩展组件配置

}

// TimeSpec 通用时间规格
Expand Down Expand Up @@ -242,3 +244,24 @@ type NetworkSpec struct {
type RuntimeSpec struct {
Runtime string `json:"runtime"`
}

type ComponentSpec struct {
Helm *Helm `json:"helm,omitempty"` // 忽略,则使用默认值
Prometheus *Prometheus `json:"prometheus,omitempty"`
Grafana *Grafana `json:"grafana,omitempty"`
}

type Helm struct {
EnableHelm string `json:"enable_helm"`
HelmRelease string `json:"helm_release"`
}

type Prometheus struct {
EnablePrometheus string `json:"enable_prometheus"`
}

type Grafana struct {
EnableGrafana string `json:"enable_prometheus"`
GrafanaAdminUser string `json:"grafana_admin_user"`
GrafanaAdminPassword string `json:"grafana_admin_password"`
}

0 comments on commit 7949341

Please sign in to comment.