Skip to content

Commit

Permalink
Completed multinode render Func (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
caoyingjunz authored Jun 8, 2024
1 parent 2c611b3 commit c39f601
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 19 deletions.
1 change: 1 addition & 0 deletions pkg/controller/plan/plan_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (p *plan) CreateNode(ctx context.Context, pid int64, req *types.CreatePlanN
Name: req.Name,
PlanId: pid,
Role: req.Role,
CRI: req.CRI,
Ip: req.Ip,
Auth: auth,
}); err != nil {
Expand Down
55 changes: 40 additions & 15 deletions pkg/controller/plan/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"k8s.io/klog/v2"

"github.com/caoyingjunz/pixiu/pkg/db/model"
"github.com/caoyingjunz/pixiu/pkg/types"
"github.com/caoyingjunz/pixiu/pkg/util"
pixiutpl "github.com/caoyingjunz/pixiu/template"
)
Expand All @@ -49,7 +50,11 @@ func (r Render) Run() error {
return err
}
// 渲染 multiNode
if err := r.doRender("multinode", pixiutpl.MultiModeTemplate, ParseMultinode(r.data)); err != nil {
multiNode, err := ParseMultinode(r.data)
if err != nil {
return err
}
if err := r.doRender("multinode", pixiutpl.MultiModeTemplate, multiNode); err != nil {
return err
}
// 渲染 globals
Expand Down Expand Up @@ -98,29 +103,49 @@ func getFileForRender(planId int64, f string) (string, error) {
}

type Multinode struct {
DockerMaster []string
DockerNode []string
ContainerdMaster []string
ContainerdNode []string
DockerMaster []types.PlanNode
DockerNode []types.PlanNode
ContainerdMaster []types.PlanNode
ContainerdNode []types.PlanNode
}

func ParseMultinode(data TaskData) Multinode {
func ParseMultinode(data TaskData) (Multinode, error) {
multinode := Multinode{
DockerMaster: make([]string, 0),
DockerNode: make([]string, 0),
ContainerdMaster: make([]string, 0),
ContainerdNode: make([]string, 0),
DockerMaster: make([]types.PlanNode, 0),
DockerNode: make([]types.PlanNode, 0),
ContainerdMaster: make([]types.PlanNode, 0),
ContainerdNode: make([]types.PlanNode, 0),
}
for _, node := range data.Nodes {
if node.Role == model.MasterRole {
multinode.DockerMaster = append(multinode.DockerMaster, node.Name)
nodeAuth := types.PlanNodeAuth{}
err := nodeAuth.Unmarshal(node.Auth)
if err != nil {
return multinode, err
}
planNode := types.PlanNode{
Name: node.Name,
Auth: nodeAuth,
}

if node.CRI == model.DockerCRI {
if node.Role == model.MasterRole {
multinode.DockerMaster = append(multinode.DockerMaster, planNode)
}
if node.Role == model.NodeRole {
multinode.DockerNode = append(multinode.DockerNode, planNode)
}
}
if node.Role == model.NodeRole {
multinode.DockerNode = append(multinode.DockerNode, node.Name)
if node.CRI == model.ContainerdCRI {
if node.Role == model.MasterRole {
multinode.ContainerdMaster = append(multinode.ContainerdMaster, planNode)
}
if node.Role == model.NodeRole {
multinode.ContainerdNode = append(multinode.ContainerdNode, planNode)
}
}
}

return multinode
return multinode, nil
}

type Global struct {
Expand Down
8 changes: 8 additions & 0 deletions pkg/db/model/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,20 @@ const (
MasterRole // kubernetes master role
)

type CRI string

const (
DockerCRI CRI = "docker"
ContainerdCRI CRI = "containerd"
)

type Node struct {
pixiu.Model

Name string `gorm:"index:idx_name,unique" json:"name"`
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"`
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/types/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ type (
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"`
}
Expand All @@ -104,6 +105,7 @@ type (
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"`
}
Expand Down
1 change: 1 addition & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ type PlanNode struct {
Name string `json:"name"` // 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,omitempty"`
}
Expand Down
16 changes: 12 additions & 4 deletions template/multinode.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,30 @@ package template
const MultiModeTemplate = `# Render below by Pixiu engine
[docker-master]
{{- range .DockerMaster }}
{{ . }}
{{- if eq .Auth.Type "password" }}
{{ .Name }} ansible_ssh_user={{ .Auth.Password.User }} ansible_ssh_pass={{ .Auth.Password.Password }}
{{- end }}
{{- end }}
[docker-node]
{{- range .DockerNode }}
{{ . }}
{{- if eq .Auth.Type "password" }}
{{ .Name }} ansible_ssh_user={{ .Auth.Password.User }} ansible_ssh_pass={{ .Auth.Password.Password }}
{{- end }}
{{- end }}
[containerd-master]
{{- range .ContainerdMaster }}
{{ . }}
{{- if eq .Auth.Type "password" }}
{{ .Name }} ansible_ssh_user={{ .Auth.Password.User }} ansible_ssh_pass={{ .Auth.Password.Password }}
{{- end }}
{{- end }}
[containerd-node]
{{- range .ContainerdNode }}
{{ . }}
{{- if eq .Auth.Type "password" }}
{{ .Name }} ansible_ssh_user={{ .Auth.Password.User }} ansible_ssh_pass={{ .Auth.Password.Password }}
{{- end }}
{{- end }}
[storage]
Expand Down

0 comments on commit c39f601

Please sign in to comment.