这种思想和 Argo Workflow 比较像,但粒度比 Argo 更细,关注在操作函数上:
下面简单描述该服务的设计和实现。
1. 服务形态
2. 设计
该方案的核心在于配置管理的设计,将操作流程配置化,自上而下有如下概念:
抽象目标场景中的通用操作,这些通用操作即为可在 yaml 中使用的原语,对应上述 Plugin:
上述 4 个概念的关系如下:
示例可参见文章开头的 yaml 文件,对应形式二。
3. 核心实现
CRD 设计:
package v1alpha1import ( corev1 \"k8s.io/api/core/v1\" metav1 \"k8s.io/apimachinery/pkg/apis/meta/v1\")// BeidouType is the type related to Beidou BeidouType stringconst ( // BeidouTask represents the Task execution type. BeidouTask BeidouType = \"Task\")// +genclient// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/// Beidou represents a crd used to describe Beidou struct { `json:\",inline\"` `json:\"metadata,omitempty\" protobuf:\"bytes,1,opt,name=metadata\"` Spec BeidouSpec `json:\"spec,omitempty\" protobuf:\"bytes,2,opt,name=spec\"` Status BeidouStatus `json:\"status,omitempty\" protobuf:\"bytes,3,opt,name=status\"`}// BeidouSpec is the spec of a BeidouSpec struct { Steps []BeidouStep `json:\"steps\" protobuf:\"bytes,1,opt,name=steps\"` References []BeidouReference `json:\"references\" protobuf:\"bytes,2,opt,name=references\"`}// BeidouStep is the spec of BeidouStep struct { Name string `json:\"name\" protobuf:\"bytes,1,opt,name=name\"` Operations []BeidouOperation `json:\"operations\" protobuf:\"bytes,2,opt,name=operations\"`}// BeidouOperation is the spec of BeidouOperation struct { Name string `json:\"name\" protobuf:\"bytes,1,opt,name=name\"` Type BeidouType `json:\"type\" protobuf:\"bytes,2,opt,name=type\"` Op string `json:\"op\" protobuf:\"bytes,3,opt,name=op\"` Args []BeidouArg `json:\"args\" protobuf:\"bytes,4,opt,name=args\"`}// BeidouArg is the spec of arg.type BeidouArg struct { Name string `json:\"name\" protobuf:\"bytes,1,opt,name=name\"` Value string `json:\"value,omitempty\" protobuf:\"bytes,2,opt,name=value\"` Reference BeidouOperationReference `json:\"reference,omitempty\" protobuf:\"bytes,3,opt,name=reference\"` Tolerations [] `json:\"tolerations,omitempty\" protobuf:\"bytes,4,opt,name=tolerations\"` Checking []string `json:\"checking,omitempty\" protobuf:\"bytes,5,opt,name=checking\"`}// BeidouOperationReference is the spec of operation BeidouOperationReference struct { ID string `json:\"id\" protobuf:\"bytes,1,opt,name=id\"`}// BeidouReference is the spec of BeidouReference struct { ID string `json:\"id\" protobuf:\"bytes,1,opt,name=id\"` Steps []BeidouStep `json:\"steps\" protobuf:\"bytes,2,opt,name=steps\"`}// BeidouStatus represents the current state of a BeidouStatus struct { Message string `json:\"message\" protobuf:\"bytes,1,opt,name=message\"`}// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/// BeidouList is a collection of BeidouList struct { `json:\",inline\"` `json:\"metadata\" protobuf:\"bytes,1,opt,name=metadata\"` Items []Beidou `json:\"items\" protobuf:\"bytes,2,opt,name=items\"`}
核心流程:
// ExecSteps executes ExecSteps(ctx , steps [], references []) error { logger, _ := (CtxLogger).(*) var hasMonitored bool for i, step := range steps { for j, op := range { switch op.Op { case \"DeploymentCreationEfficiency\": if !hasMonitored { defer func() { err := () if err != nil { (\"Failed to output: %s\", err) } }() } hasMonitored = true } err := ExecOperation(ctx, op, references) if err != nil { return (\"failed to run operation %s: %s\", op.Name, err) } } } return nil}// ExecOperation executes ExecOperation(ctx , op , references []) error { switch op.Type { case : if !(op.Op) { return ErrNotRegistered } if !(op.Op) { return ExecTask(ctx, op.Op, op.Args) } return ExecTaskWithRefer(ctx, op.Op, op.Args, references) } return nil}// ExecTask executes a ExecTask(ctx , opname string, args []) error { switch opname { case : var ns string for _, arg := range args { switch arg.Name { case \"NS\": ns = } } return (ctx, ns) // ... } // ...}// ExecTaskWithRefer executes a task with ExecTaskWithRefer(ctx , opname string, args [], references []) error { switch opname { case : var times int var steps [] var err error for _, arg := range args { switch arg.Name { case \"TIMES\": times, err = () if err != nil { return ErrParseArgs } case \"ACTION\": for _, refer := range references { if refer.ID == { steps = break } } } } return RepeatNTimes(ctx, times, steps) } return ErrNotImplemented}
文章来源:《电脑编程技巧与维护》 网址: http://www.dnbcjqywh.cn/zonghexinwen/2020/0924/505.html
电脑编程技巧与维护投稿 | 电脑编程技巧与维护编辑部| 电脑编程技巧与维护版面费 | 电脑编程技巧与维护论文发表 | 电脑编程技巧与维护最新目录
Copyright © 2018 《电脑编程技巧与维护》杂志社 版权所有
投稿电话: 投稿邮箱: