现在的位置:主页 > 综合新闻 >

Kubernetes 新玩法:在 yaml 中编程(2)

来源:电脑编程技巧与维护 【在线投稿】 栏目:综合新闻 时间:2020-09-24

【作者】网站采编

【关键词】

【摘要】这种思想和 Argo Workflow 比较像,但粒度比 Argo 更细,关注在操作函数上: 下面简单描述该服务的设计和实现。 1. 服务形态 使用者在 yaml 中,通过 声明式

这种思想和 Argo Workflow 比较像,但粒度比 Argo 更细,关注在操作函数上:

下面简单描述该服务的设计和实现。

1. 服务形态

  • 使用者在 yaml 中,通过 声明式 的方式描述操作逻辑;
  • 以 all-in-one 的二进制工具或 Operator 的方式交付;
  • 服务内置常见原语的实现,以关键字的方式在 yaml 中提供;
  • 支持配置原生 K8s 资源。

2. 设计

该方案的核心在于配置管理的设计,将操作流程配置化,自上而下有如下概念:

  • Service:Modules 或 Tasks 的编排;
  • Module:一种任务场景,是操作单元的集合(其中包含 templates/ 目录,表征模板文件的集合,可用来配置 K8s 原生资源);
  • Task:操作单元,使用 plugin 及参数执行操作;
  • Plugin:操作指令,类似开发语言中的函数。

抽象目标场景中的通用操作,这些通用操作即为可在 yaml 中使用的原语,对应上述 Plugin:

  • K8s 相关CreateNamespaceDeleteNamespacePrepareSecretPrepareConfigMapPrepareBatchDeploymentsWaitForBatchDeploymentsReadyetc.
  • 观测性相关DeploymentCreationEfficiencyPodCreationEfficiencyetc.
  • 检测项相关CheckPodAnnotationsCheckPodObjectInfoCheckPodInnerStatesetc.
  • 控制语句相关RepeatNTimesetc.

上述 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

上一篇:C/C++编程笔记:计算机网络基础知识总结!超全面
下一篇:百万年薪必备!AI行业常用编程语言大盘点,看看