k8s的kubectl源码解析

kubectl 工具是 kubernetes API Server 的客户端。它的主要工作是向 kubernetes API Server 发起http请求。k8s是完全以资源为中心的系统,而kubectl会发起HTTP请求来操纵资源(对资源进行CURD操作),来对集群系统进行维护。

下面主要分析kubectl create nginx_pod.yaml这条命令的创建过程:

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
spec:
    containers:
    - name: nginx
      image: nginx:1.8

1、cobra命令行参数解析

k8s的客户端都是使用的cobra来构建的。使用方法见我另一篇文章

2、kubectl - create的调用逻辑

main

func main() {
     // 如果不调用rand.Seed,每次重新运行这个main函数,rand下的函数返回值始终一致
    // Seed即随机的种子,每次用时间戳作为种子,就能保证随机性
    rand.Seed(time.Now().UnixNano())

    // 创建了kubectl命令的默认参数
    command := cmd.NewDefaultKubectlCommand()

    // TODO: once we switch everything over to Cobra commands, we can go back to calling
    // cliflag.InitFlags() (by removing its pflag.Parse() call). For now, we have to set the
    // normalize func and add the go flag set by hand.
    pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
    pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
    // cliflag.InitFlags()

    // 日志的初始化与退出
    logs.InitLogs()
    defer logs.FlushLogs()

    // 运行command
    if err := command.Execute(); err != nil {
        os.Exit(1)
    }
}

match

func NewDefaultKubectlCommand() *cobra.Command {
    return NewDefaultKubectlCommandWithArgs(NewDefaultPluginHandler(plugin.ValidPluginFilenamePrefixes), os.Args, os.Stdin, os.Stdout, os.Stderr)
}

func NewDefaultKubectlCommandWithArgs(pluginHandler PluginHandler, args []string, in io.Reader, out, errout io.Writer) *cobra.Command {
  // 初始化NewKubectlCommand,采用标准输入、输出、错误输出
    cmd := NewKubectlCommand(in, out, errout)

    if pluginHandler == nil {
        return cmd
    }

    if len(args) > 1 {
        // 这里为传入的参数,即 create -f nginx_pod.yaml 部分
        cmdPathPieces := args[1:]

        // 调用cobra的Find去匹配args
        if _, _, err := cmd.Find(cmdPathPieces); err != nil {
            if err := HandlePluginCommand(pluginHandler, cmdPathPieces); err != nil {
                fmt.Fprintf(errout, "%v\n", err)
                os.Exit(1)
            }
        }
    }

    return cmd
}

command

代码比较长,只摘录关键的代码

func NewKubectlCommand(in io.Reader, out, err io.Writer) *cobra.Command {
    warningHandler := rest.NewWarningWriter(err, rest.WarningWriterOptions{Deduplicate: true, Color: term.AllowsColorOutput(err)})
    warningsAsErrors := false

    // 创建主命令
    cmds := &cobra.Command{
        Use:   "kubectl",
        Short: i18n.T("kubectl controls the Kubernetes cluster manager"),
        Long: templates.LongDesc(`
      kubectl controls the Kubernetes cluster manager.

      Find more information at:
            https://kubernetes.io/docs/reference/kubectl/overview/`),
        Run: runHelp,
        // 初始化后,在运行指令前的钩子
        PersistentPreRunE: func(*cobra.Command, []string) error {
              // 这里是做 pprof 性能分析,跳转到对应代码可以看到,我们可以用参数 --profile xxx 来采集性能指标,默认保存在当前目录下的profile.pprof中
            return initProfiling()
        },
        // 运行指令后的钩子
        PersistentPostRunE: func(*cobra.Command, []string) error {
              // 保存pprof性能分析指标
            return flushProfiling()
        },
    // bash自动补齐功能,可通过 kubectl completion bash 命令查看
    // 具体安装可参考 https://kubernetes.io/docs/tasks/tools/install-kubectl/#enabling-shell-autocompletion
        BashCompletionFunction: bashCompletionFunc,
    }

  // 实例化Factory接口,工厂模式
    f := cmdutil.NewFactory(matchVersionKubeConfigFlags)

    // 省略实例化的过程代码

  // kubectl定义了7类命令,结合Message和各个子命令的package名来看
    groups := templates.CommandGroups{
        {
      // 1. 初级命令,包括 create/expose/run/set
            Message: "Basic Commands (Beginner):",
            Commands: []*cobra.Command{
                create.NewCmdCreate(f, ioStreams),
                expose.NewCmdExposeService(f, ioStreams),
                run.NewCmdRun(f, ioStreams),
                set.NewCmdSet(f, ioStreams),
            },
        },
        {
      // 2. 中级命令,包括explain/get/edit/delete
            Message: "Basic Commands (Intermediate):",
            Commands: []*cobra.Command{
                explain.NewCmdExplain("kubectl", f, ioStreams),
                get.NewCmdGet("kubectl", f, ioStreams),
                edit.NewCmdEdit(f, ioStreams),
                delete.NewCmdDelete(f, ioStreams),
            },
        },
        {
      // 3. 部署命令,包括 rollout/scale/autoscale
            Message: "Deploy Commands:",
            Commands: []*cobra.Command{
                rollout.NewCmdRollout(f, ioStreams),
                scale.NewCmdScale(f, ioStreams),
                autoscale.NewCmdAutoscale(f, ioStreams),
            },
        },
        {
      // 4. 集群管理命令,包括 cerfificate/cluster-info/top/cordon/drain/taint
            Message: "Cluster Management Commands:",
            Commands: []*cobra.Command{
                certificates.NewCmdCertificate(f, ioStreams),
                clusterinfo.NewCmdClusterInfo(f, ioStreams),
                top.NewCmdTop(f, ioStreams),
                drain.NewCmdCordon(f, ioStreams),
                drain.NewCmdUncordon(f, ioStreams),
                drain.NewCmdDrain(f, ioStreams),
                taint.NewCmdTaint(f, ioStreams),
            },
        },
        {
      // 5. 故障排查和调试,包括 describe/logs/attach/exec/port-forward/proxy/cp/auth
            Message: "Troubleshooting and Debugging Commands:",
            Commands: []*cobra.Command{
                describe.NewCmdDescribe("kubectl", f, ioStreams),
                logs.NewCmdLogs(f, ioStreams),
                attach.NewCmdAttach(f, ioStreams),
                cmdexec.NewCmdExec(f, ioStreams),
                portforward.NewCmdPortForward(f, ioStreams),
                proxy.NewCmdProxy(f, ioStreams),
                cp.NewCmdCp(f, ioStreams),
                auth.NewCmdAuth(f, ioStreams),
            },
        },
        {
      // 6. 高级命令,包括diff/apply/patch/replace/wait/convert/kustomize
            Message: "Advanced Commands:",
            Commands: []*cobra.Command{
                diff.NewCmdDiff(f, ioStreams),
                apply.NewCmdApply("kubectl", f, ioStreams),
                patch.NewCmdPatch(f, ioStreams),
                replace.NewCmdReplace(f, ioStreams),
                wait.NewCmdWait(f, ioStreams),
                convert.NewCmdConvert(f, ioStreams),
                kustomize.NewCmdKustomize(ioStreams),
            },
        },
        {
      // 7. 设置命令,包括label,annotate,completion
            Message: "Settings Commands:",
            Commands: []*cobra.Command{
                label.NewCmdLabel(f, ioStreams),
                annotate.NewCmdAnnotate("kubectl", f, ioStreams),
                completion.NewCmdCompletion(ioStreams.Out, ""),
            },
        },
    }
    groups.Add(cmds)

    filters := []string{"options"}

    // alpha相关的子命令
    alpha := cmdpkg.NewCmdAlpha(f, ioStreams)
    if !alpha.HasSubCommands() {
        filters = append(filters, alpha.Name())
    }

    templates.ActsAsRootCommand(cmds, filters, groups...)

  // 代码补全相关
    for name, completion := range bashCompletionFlags {
        if cmds.Flag(name) != nil {
            if cmds.Flag(name).Annotations == nil {
                cmds.Flag(name).Annotations = map[string][]string{}
            }
            cmds.Flag(name).Annotations[cobra.BashCompCustom] = append(
                cmds.Flag(name).Annotations[cobra.BashCompCustom],
                completion,
            )
        }
    }

  // 添加其余子命令,包括 alpha/config/plugin/version/api-versions/api-resources/options
    cmds.AddCommand(alpha)
    cmds.AddCommand(cmdconfig.NewCmdConfig(f, clientcmd.NewDefaultPathOptions(), ioStreams))
    cmds.AddCommand(plugin.NewCmdPlugin(f, ioStreams))
    cmds.AddCommand(version.NewCmdVersion(f, ioStreams))
    cmds.AddCommand(apiresources.NewCmdAPIVersions(f, ioStreams))
    cmds.AddCommand(apiresources.NewCmdAPIResources(f, ioStreams))
    cmds.AddCommand(options.NewCmdOptions(ioStreams.Out))

    return cmds
}

create

func NewCmdCreate(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
  // create子命令的相关选项
    o := NewCreateOptions(ioStreams)

  // create子命令的相关说明
    cmd := &cobra.Command{
        Use:                   "create -f FILENAME",
        DisableFlagsInUseLine: true,
        Short:                 i18n.T("Create a resource from a file or from stdin."),
        Long:                  createLong,
        Example:               createExample,
    // 验证参数并运行
        Run: func(cmd *cobra.Command, args []string) {
            if cmdutil.IsFilenameSliceEmpty(o.FilenameOptions.Filenames, o.FilenameOptions.Kustomize) {
                ioStreams.ErrOut.Write([]byte("Error: must specify one of -f and -k\n\n"))
                defaultRunFunc := cmdutil.DefaultSubCommandRun(ioStreams.ErrOut)
                defaultRunFunc(cmd, args)
                return
            }
            cmdutil.CheckErr(o.Complete(f, cmd))
            cmdutil.CheckErr(o.ValidateArgs(cmd, args))
      // 核心的运行代码逻辑是在这里的RunCreate
            cmdutil.CheckErr(o.RunCreate(f, cmd))
        },
    }

    o.RecordFlags.AddFlags(cmd)

    usage := "to use to create the resource"
  // 加入文件名选项的flag -f,保存到o.FilenameOptions.Filenames中,对应上面
    cmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, usage)
    cmdutil.AddValidateFlags(cmd)
    cmd.Flags().BoolVar(&o.EditBeforeCreate, "edit", o.EditBeforeCreate, "Edit the API resource before creating")
    cmd.Flags().Bool("windows-line-endings", runtime.GOOS == "windows",
        "Only relevant if --edit=true. Defaults to the line ending native to your platform.")
    cmdutil.AddApplyAnnotationFlags(cmd)
    cmdutil.AddDryRunFlag(cmd)
    cmd.Flags().StringVarP(&o.Selector, "selector", "l", o.Selector, "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)")
    cmd.Flags().StringVar(&o.Raw, "raw", o.Raw, "Raw URI to POST to the server.  Uses the transport specified by the kubeconfig file.")
    cmdutil.AddFieldManagerFlagVar(cmd, &o.fieldManager, "kubectl-create")

    o.PrintFlags.AddFlags(cmd)

    // create的子命令,指定create对象
    cmd.AddCommand(NewCmdCreateNamespace(f, ioStreams))
    cmd.AddCommand(NewCmdCreateQuota(f, ioStreams))
    cmd.AddCommand(NewCmdCreateSecret(f, ioStreams))
    cmd.AddCommand(NewCmdCreateConfigMap(f, ioStreams))
    cmd.AddCommand(NewCmdCreateServiceAccount(f, ioStreams))
    cmd.AddCommand(NewCmdCreateService(f, ioStreams))
    cmd.AddCommand(NewCmdCreateDeployment(f, ioStreams))
    cmd.AddCommand(NewCmdCreateClusterRole(f, ioStreams))
    cmd.AddCommand(NewCmdCreateClusterRoleBinding(f, ioStreams))
    cmd.AddCommand(NewCmdCreateRole(f, ioStreams))
    cmd.AddCommand(NewCmdCreateRoleBinding(f, ioStreams))
    cmd.AddCommand(NewCmdCreatePodDisruptionBudget(f, ioStreams))
    cmd.AddCommand(NewCmdCreatePriorityClass(f, ioStreams))
    cmd.AddCommand(NewCmdCreateJob(f, ioStreams))
    cmd.AddCommand(NewCmdCreateCronJob(f, ioStreams))
    return cmd
}

runCreate

func (o *CreateOptions) RunCreate(f cmdutil.Factory, cmd *cobra.Command) error {
    // f为传入的Factory,主要是封装了与kube-apiserver交互客户端
    schema, err := f.Validator(cmdutil.GetFlagBool(cmd, "validate"))
    if err != nil {
        return err
    }

    cmdNamespace, enforceNamespace, err := f.ToRawKubeConfigLoader().Namespace()
    if err != nil {
        return err
    }

  // 实例化Builder,这块的逻辑比较复杂,我们先关注文件部分
    r := f.NewBuilder().
        Unstructured().
        Schema(schema).
        ContinueOnError().
        NamespaceParam(cmdNamespace).DefaultNamespace().
      // 读取文件信息,发现除了支持简单的本地文件,也支持标准输入和http/https协议访问的文件,保存为Visitor
        FilenameParam(enforceNamespace, &o.FilenameOptions).
        LabelSelectorParam(o.Selector).
        Flatten().
        Do()
    err = r.Err()
    if err != nil {
        return err
    }

    count := 0
  // 调用visit函数,创建资源
    err = r.Visit(func(info *resource.Info, err error) error {
        // 打印结果 xxxx created
        return o.PrintObj(info.Object)
    })
    return nil
}

3、创建资源对象过程

image
原理:客户端与服务端进行一次http请求的交互。主要流程可分为:实例化Factory接口、通过Builder和Visitor将资源对象描述文件(nginx_pod.yaml)文本格式转换成资源对象。将资源对象以http请求的方式发送给api server,并得到响应结果。最终根据Visitor匿名函数集的errors判断是否成功创建了资源对象。

4、实例化Factory接口

在执行每一个kubectl命令之前,都需要执行实例化 cmdutil Factory 接口对象的操作。Factory 是一个通用对象,它提供了与 kube-apiserver 的交互方式,以及验证资源对象等方法。cmdutil Factory接口代码如下:

// 实例化Factory接口,工厂模式
f := cmdutil.NewFactory(matchVersionKubeConfigFlags)

Factory接口如下:

type Factory interface {
    // DynamicClient returns a dynamic client ready for use
    DynamicClient() (dynamic.Interface, error)

    // KubernetesClientSet gives you back an external clientset
    KubernetesClientSet() (*kubernetes.Clientset, error)

    // Returns a RESTClient for accessing Kubernetes resources or an error.
    RESTClient() (*restclient.RESTClient, error)

    // NewBuilder returns an object that assists in loading objects from both disk and the server
    // and which implements the common patterns for CLI interactions with generic resources.
    NewBuilder() *resource.Builder

    // Returns a schema that can validate objects stored on disk.
    Validator(validate bool) (validation.Schema, error)

}

接口说明如下:

  1. DynamicClient:动态客户端。
  2. KubernetesClientSet:ClientSet客户端。
  3. RESTClient:RESTClient客户端。
  4. NewBuilder:实例化builder,builder用于将命令获取的参数转换成资源对象
  5. Validator:验证资源对象。

cmd Factory 接口封装了3种 client-go 客户端与 kube-apiserver 交互的方式,分别是DynamicClient、KubernetesClientSet(简称ClientSet)及RESTClient。3种交互方式各有不同的应用场景。

5、kubectl 设计模式中Visitor的实现

访问者模式:允许一个或者多个操作应用到对象上,解耦操作和对象本身

对于一个程序来说主要表现在:

1、表面:某个对象执行了一个方法

2、内部:对象内部调用了多个方法,最后统一返回结果

举个例子:

1、表面:调用一个查询接口

2、内部:先从缓存中查询,没查到再去热点数据库查询,还没查到则去归档数据库查询

Visitor

访问者模式的定义

// Visitor 即为访问者这个对象
type Visitor interface {
    Visit(VisitorFunc) error
}
// VisitorFunc对应这个对象的方法,也就是定义中的“操作”
type VisitorFunc func(*Info, error) error

基本的数据结构很简单,从当前的数据结构看有2个问题:

1、单个操作可以直接调用Visit方法,那多个操作如何实现呢?

2、在应用多个操作时,如果出现了error,该退出还是继续下一个操作呢?

VisitorList

封装多个Visitor为一个,出现错误就立即终止并返回

// VisitorList定义为[]Visitor,又实现了Visit方法,也就是将多个[]Visitor封装为一个Visitor
type VisitorList []Visitor

// Visit implements Visitor
// 发生error就立刻返回,不继续遍历
func (l VisitorList) Visit(fn VisitorFunc) error {
    for i := range l {
        if err := l[i].Visit(fn); err != nil {
            return err
        }
    }
    return nil
}

EagerVisitorList

封装多个Visitor为一个,出现错误暂存下来,全部遍历完再聚合所有的错误并返回

// EagerVisitorList 也是将多个[]Visitor封装为一个Visitor
type EagerVisitorList []Visitor

// 返回的错误暂存到[]error中,统一聚合
// Visit implements Visitor, and gathers errors that occur during processing until
// all sub visitors have been visited.
func (l EagerVisitorList) Visit(fn VisitorFunc) error {
    errs := []error(nil)
    for i := range l {
        if err := l[i].Visit(func(info *Info, err error) error {
            if err != nil {
                errs = append(errs, err)
                return nil
            }
            if err := fn(info, nil); err != nil {
                errs = append(errs, err)
            }
            return nil
        }); err != nil {
            errs = append(errs, err)
        }
    }
    return utilerrors.NewAggregate(errs)
}

DecoratedVisitor

这里借鉴了装饰器的设计模式,将一个Visitor调用多个VisitorFunc方法,封装为调用一个VisitorFunc

// 装饰器Visitor
type DecoratedVisitor struct {
    visitor    Visitor
    decorators []VisitorFunc
}

// visitor遍历调用decorators中所有函数,有失败立即返回
// Visit implements Visitor
func (v DecoratedVisitor) Visit(fn VisitorFunc) error {
    return v.visitor.Visit(func(info *Info, err error) error {
        if err != nil {
            return err
        }
        for i := range v.decorators {
            if err := v.decorators[i](info, nil); err != nil {
                return err
            }
        }
        return fn(info, nil)
    })
}

ContinueOnErrorVisitor

// 报错依旧继续
type ContinueOnErrorVisitor struct {
    Visitor
}

// 报错不立即返回,聚合所有错误后返回
func (v ContinueOnErrorVisitor) Visit(fn VisitorFunc) error {
    errs := []error{}
    err := v.Visitor.Visit(func(info *Info, err error) error {
        if err != nil {
            errs = append(errs, err)
            return nil
        }
        if err := fn(info, nil); err != nil {
            errs = append(errs, err)
        }
        return nil
    })
    if err != nil {
        errs = append(errs, err)
    }
    if len(errs) == 1 {
        return errs[0]
    }
    return utilerrors.NewAggregate(errs)
}

Visitor的实现

StreamVisitor

最基础的Visitor

type StreamVisitor struct {
    // 读取信息的来源,实现了Read这个接口,这个"流式"的概念,
    //包括了常见的HTTP、文件、标准输入等各类输入
    io.Reader
    *mapper

    Source string
    Schema ContentValidator
}

FileVisitor

文件的访问,包括标准输入,底层调用StreamVisitor来访问

// 表示文件路径或者STDIN
type FileVisitor struct {
    Path string
    *StreamVisitor
}

URLVisitor

HTTP用GET方法获取数据,底层也是复用StreamVisitor

type URLVisitor struct {
    URL *url.URL
    *StreamVisitor
    // 提供错误重试次数
    HttpAttemptCount int
}

KustomizeVisitor

自定义的Visitor,针对自定义的文件系统

type KustomizeVisitor struct {
    Path string
    *StreamVisitor
}

6、资源创建http请求发送

// 在RunCreate函数中,关键的发送函数
obj, err := resource.
                NewHelper(info.Client, info.Mapping).
                DryRun(o.DryRunStrategy == cmdutil.DryRunServer).
                WithFieldManager(o.fieldManager).
                Create(info.Namespace, true, info.Object)

// 进入create函数,查看到
m.createResource(m.RESTClient, m.Resource, namespace, obj, options)

// 对应的实现为
func (m *Helper) createResource(c RESTClient, resource, namespace string, obj runtime.Object, options *metav1.CreateOptions) (runtime.Object, error) {
    return c.Post().
        NamespaceIfScoped(namespace, m.NamespaceScoped).
        Resource(resource).
        VersionedParams(options, metav1.ParameterCodec).
        Body(obj).
        Do(context.TODO()).
        Get()
}

/*

到这里,我们发现了2个关键性的定义:
1. RESTClient 与kube-apiserver交互的RESTful风格的客户端
2. runtime.Object 资源对象的抽象,包括Pod/Deployment/Service等各类资源
*/

RESTful Client

// 从传入参数来看,数据来源于Info这个结构
r.Visit(func(info *resource.Info, err error) error{})

// 而info来源于前面的Builder,前面部分都是将Builder参数化,核心的生成为Do函数
r := f.NewBuilder().
        Unstructured().
        Schema(schema).
        ContinueOnError().
        NamespaceParam(cmdNamespace).DefaultNamespace().
        FilenameParam(enforceNamespace, &o.FilenameOptions).
        LabelSelectorParam(o.Selector).
        Flatten().
        Do()

// 大致看一下这些函数,我们可以在Unstructured()中看到getClient函数,其实这就是我们要找的函数
func (b *Builder) getClient(gv schema.GroupVersion) (RESTClient, error) 

// 从返回值来看,client包括默认的REST client和配置选项
NewClientWithOptions(client, b.requestTransforms...)

// 这个Client会在kubernetes项目中大量出现,它是与kube-apiserver交互的核心组件,以后再深入。

Object

Object这个对象是怎么获取到的呢?因为我们的数据源是来自文件的,那么我们最直观的想法就是FileVisitor

func (v *FileVisitor) Visit(fn VisitorFunc) error {
    // 省略读取这块的代码,底层调用的是StreamVisitor的逻辑
    return v.StreamVisitor.Visit(fn)
}

func (v *StreamVisitor) Visit(fn VisitorFunc) error {
    d := yaml.NewYAMLOrJSONDecoder(v.Reader, 4096)
    for {
        // 这里就是返回info的地方
        info, err := v.infoForData(ext.Raw, v.Source)
  }
}

// 再往下一层看,来到mapper层,也就是kubernetes的资源对象映射关系
func (m *mapper) infoForData(data []byte, source string) (*Info, error){
  // 这里就是我们返回Object的地方,其中GVK是Group/Version/Kind的缩写,后续我们会涉及
  obj, gvk, err := m.decoder.Decode(data, nil, nil)
}

这时,我们想回头去看,这个mapper是在什么时候被定义的?

// 在Builder初始化中,我们就找到了
func (b *Builder) Unstructured() *Builder {
    b.mapper = &mapper{
        localFn:      b.isLocal,
        restMapperFn: b.restMapperFn,
        clientFn:     b.getClient,
    // 我们查找资源用到的是这个decoder
        decoder:      &metadataValidatingDecoder{unstructured.UnstructuredJSONScheme},
    }
    return b
}

// 逐层往下找,对应的Decode方法的实现,就是对应的数据解析成data:
func (s unstructuredJSONScheme) decode(data []byte) (runtime.Object, error) {
    // 细节暂时忽略
}

给api server发送Post请求

// RESTful接口风格中,POST请求对应的就是CREATE方法
c.Post().
        NamespaceIfScoped(namespace, m.NamespaceScoped).
        Resource(resource).
        VersionedParams(options, metav1.ParameterCodec).
        Body(obj).
        Do(context.TODO()). 
        Get() 

// Do方法,发送请求
err := r.request(ctx, func(req *http.Request, resp *http.Response) {
        result = r.transformResponse(resp, req)
    })

// Get方法,获取请求的返回结果,用来打印状态
switch t := out.(type) {
    case *metav1.Status:
    if t.Status != metav1.StatusSuccess {
        return nil, errors.FromObject(t)
    }
}
posted @ 2022-04-04 21:35  liweiboy  阅读(1427)  评论(0编辑  收藏  举报