kubernetes源码阅读——kubectl

  • kubectl是Kubernetes官方提供的命令行工具(CLI),用户可以通过kubectl以命令行交互的方式对Kubernetes API Server进行操作,通信协议使用HTTP/JSON。
  • Kubectl命令示例:
kubectl get      pod   pod_name -n kube-system
       [command][type][name]   [flags]
  • kubectl的实现基于 Cobra库
  • 程序入口:
// cmd\kubectl\kubectl.go
func main() {
    rand.Seed(time.Now().UnixNano())

    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()

    if err := command.Execute(); err != nil {
        os.Exit(1)
    }
}
// staging\src\k8s.io\kubectl\pkg\cmd\cmd.go
// NewKubectlCommand creates the `kubectl` command and its nested children.
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
    // 创建kubectl主命令
    // Parent command to which all subcommands are added.
    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,
        // Hook before and after Run initialize and write profiles to disk,
        // respectively.
        PersistentPreRunE: func(*cobra.Command, []string) error {
            rest.SetDefaultWarningHandler(warningHandler)
            return initProfiling()
        },
        PersistentPostRunE: func(*cobra.Command, []string) error {
            if err := flushProfiling(); err != nil {
                return err
            }
            if warningsAsErrors {
                count := warningHandler.WarningCount()
                switch count {
                case 0:
                    // no warnings
                case 1:
                    return fmt.Errorf("%d warning received", count)
                default:
                    return fmt.Errorf("%d warnings received", count)
                }
            }
            return nil
        },
        BashCompletionFunction: bashCompletionFunc,
    }

    flags := cmds.PersistentFlags()
    flags.SetNormalizeFunc(cliflag.WarnWordSepNormalizeFunc) // Warn for "_" flags

    // Normalize all flags that are coming from other packages or pre-configurations
    // a.k.a. change all "_" to "-". e.g. glog package
    flags.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)

    addProfilingFlags(flags)

    flags.BoolVar(&warningsAsErrors, "warnings-as-errors", warningsAsErrors, "Treat warnings received from the server as errors and exit with a non-zero exit code")

    kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
    kubeConfigFlags.AddFlags(flags)
    matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags)
    matchVersionKubeConfigFlags.AddFlags(cmds.PersistentFlags())
    // Updates hooks to add kubectl command headers: SIG CLI KEP 859.
    addCmdHeaderHooks(cmds, kubeConfigFlags)

    cmds.PersistentFlags().AddGoFlagSet(flag.CommandLine)

    //实例化Factory接口。Factory是一个通用对象,它提供了与kube-apiserver的交互方式,以及验证资源对象等方法。
    f := cmdutil.NewFactory(matchVersionKubeConfigFlags)

    // Sending in 'nil' for the getLanguageFn() results in using
    // the LANG environment variable.
    //
    // TODO: Consider adding a flag or file preference for setting
    // the language, instead of just loading from the LANG env. variable.
    i18n.LoadTranslations("kubectl", nil)

    // From this point and forward we get warnings on flags that contain "_" separators
    cmds.SetGlobalNormalizationFunc(cliflag.WarnWordSepNormalizeFunc)

    ioStreams := genericclioptions.IOStreams{In: in, Out: out, ErrOut: err}

    // Proxy command is incompatible with CommandHeaderRoundTripper, so
    // clear the WrapConfigFn before running proxy command.
    proxyCmd := proxy.NewCmdProxy(f, ioStreams)
    proxyCmd.PreRun = func(cmd *cobra.Command, args []string) {
        kubeConfigFlags.WrapConfigFn = nil
    }
    // 创建子命令
    groups := templates.CommandGroups{
        {
            Message: "Basic Commands (Beginner):",
            Commands: []*cobra.Command{
                create.NewCmdCreate(f, ioStreams),
                expose.NewCmdExposeService(f, ioStreams),
                run.NewCmdRun(f, ioStreams),
                set.NewCmdSet(f, ioStreams),
            },
        },
        {
            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),
            },
        },
        {
            Message: "Deploy Commands:",
            Commands: []*cobra.Command{
                rollout.NewCmdRollout(f, ioStreams),
                scale.NewCmdScale(f, ioStreams),
                autoscale.NewCmdAutoscale(f, ioStreams),
            },
        },
        {
            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),
            },
        },
        {
            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),
                proxyCmd,
                cp.NewCmdCp(f, ioStreams),
                auth.NewCmdAuth(f, ioStreams),
                debug.NewCmdDebug(f, ioStreams),
            },
        },
        {
            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),
                kustomize.NewCmdKustomize(ioStreams),
            },
        },
        {
            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"}

    // Hide the "alpha" subcommand if there are no alpha commands in this build.
    alpha := 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,
            )
        }
    }

    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
}
// Factory provides abstractions that allow the Kubectl command to be extended across multiple types
// of resources and different API sets.
// The rings are here for a reason. In order for composers to be able to provide alternative factory implementations
// they need to provide low level pieces of *certain* functions so that when the factory calls back into itself
// it uses the custom version of the function. Rather than try to enumerate everything that someone would want to override
// we split the factory into rings, where each ring can depend on methods in an earlier ring, but cannot depend
// upon peer methods in its own ring.
// TODO: make the functions interfaces
// TODO: pass the various interfaces on the factory directly into the command constructors (so the
// commands are decoupled from the factory).
type Factory interface {
    genericclioptions.RESTClientGetter

    // 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 RESTClient for working with the specified RESTMapping or an error. This is intended
    // for working with arbitrary resources and is not guaranteed to point to a Kubernetes APIServer.
    ClientForMapping(mapping *meta.RESTMapping) (resource.RESTClient, error)
    // Returns a RESTClient for working with Unstructured objects.
    UnstructuredClientForMapping(mapping *meta.RESTMapping) (resource.RESTClient, error)

    // Returns a schema that can validate objects stored on disk.
    Validator(validate bool) (validation.Schema, error)
    // OpenAPISchema returns the parsed openapi schema definition
    OpenAPISchema() (openapi.Resources, error)
    // OpenAPIGetter returns a getter for the openapi schema document
    OpenAPIGetter() discovery.OpenAPISchemaInterface
}
  • 基于Cobra的工具包对命令行参数进行解析。kubectl是主命令,它的子命令包括Basic Commands (Beginner):create, expose, run, set。Basic Commands (Intermediate):explain, get, edit, delete 等
  • 以create为例:
// staging\src\k8s.io\kubectl\pkg\cmd\create\create.go
// NewCmdCreate returns new initialized instance of create sub command
func NewCmdCreate(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
    o := NewCreateOptions(ioStreams)

    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))
            cmdutil.CheckErr(o.RunCreate(f, cmd))
        },
    }

    // bind flag structs
    o.RecordFlags.AddFlags(cmd)

    usage := "to use to create the resource"
    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 subcommands
    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))
    cmd.AddCommand(NewCmdCreateIngress(f, ioStreams))
    return cmd
}

// RunCreate performs the creation
func (o *CreateOptions) RunCreate(f cmdutil.Factory, cmd *cobra.Command) error {
    // raw only makes sense for a single file resource multiple objects aren't likely to do what you want.
    // the validator enforces this, so
    if len(o.Raw) > 0 {    // o.Raw: Raw URI to POST to the server.
        restClient, err := f.RESTClient()
        if err != nil {
            return err
        }
        return rawhttp.RawPost(restClient, o.IOStreams, o.Raw, o.FilenameOptions.Filenames[0])
    }

    if o.EditBeforeCreate {
        return RunEditOnCreate(f, o.PrintFlags, o.RecordFlags, o.IOStreams, cmd, &o.FilenameOptions, o.fieldManager)
    }
    schema, err := f.Validator(cmdutil.GetFlagBool(cmd, "validate"))
    if err != nil {
        return err
    }

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

    r := f.NewBuilder().  // Builder结构体保存了命令行获取的各种参数,并通过不同函数处理不同参数,将其转换成资源对象。
        Unstructured().   //通过函数Unstructured、Schema...Flatten对参数赋值和初始化,将参数保存到Builder对象中。
        Schema(schema).
        ContinueOnError().
        NamespaceParam(cmdNamespace).DefaultNamespace().
        FilenameParam(enforceNamespace, &o.FilenameOptions).
        LabelSelectorParam(o.Selector).
        Flatten().
        Do()  //Do函数完成对Result对象的创建。
    err = r.Err()
    if err != nil {
        return err
    }

    count := 0
    err = r.Visit(func(info *resource.Info, err error) error {
        if err != nil {
            return err
        }
        if err := util.CreateOrUpdateAnnotation(cmdutil.GetFlagBool(cmd, cmdutil.ApplyAnnotationsFlag), info.Object, scheme.DefaultJSONEncoder()); err != nil {
            return cmdutil.AddSourceToErr("creating", info.Source, err)
        }

        if err := o.Recorder.Record(info.Object); err != nil {
            klog.V(4).Infof("error recording current command: %v", err)
        }

        if o.DryRunStrategy != cmdutil.DryRunClient {
            if o.DryRunStrategy == cmdutil.DryRunServer {
                if err := o.DryRunVerifier.HasSupport(info.Mapping.GroupVersionKind); err != nil {
                    return cmdutil.AddSourceToErr("creating", info.Source, err)
                }
            }
            obj, err := resource.
                NewHelper(info.Client, info.Mapping).
                DryRun(o.DryRunStrategy == cmdutil.DryRunServer).
                WithFieldManager(o.fieldManager).
                Create(info.Namespace, true, info.Object)  //请求RESTClient,创建资源
            if err != nil {
                return cmdutil.AddSourceToErr("creating", info.Source, err)
            }
            info.Refresh(obj, true)
        }

        count++

        return o.PrintObj(info.Object)
    })
    if err != nil {
        return err
    }
    if count == 0 {
        return fmt.Errorf("no objects passed to create")
    }
    return nil
}
  • 例如FilenameParam,从资源描述文件中读取定义的资源类型和参数,转换成info对象:
// staging\src\k8s.io\cli-runtime\pkg\resource\builder.go
// FilenameParam groups input in two categories: URLs and files (files, directories, STDIN)
// If enforceNamespace is false, namespaces in the specs will be allowed to
// override the default namespace. If it is true, namespaces that don't match
// will cause an error.
// If ContinueOnError() is set prior to this method, objects on the path that are not
// recognized will be ignored (but logged at V(2)).
func (b *Builder) FilenameParam(enforceNamespace bool, filenameOptions *FilenameOptions) *Builder {
    if errs := filenameOptions.validate(); len(errs) > 0 {
        b.errs = append(b.errs, errs...)
        return b
    }
    recursive := filenameOptions.Recursive
    paths := filenameOptions.Filenames
    for _, s := range paths {
        switch {
        case s == "-":
            b.Stdin()
        case strings.Index(s, "http://") == 0 || strings.Index(s, "https://") == 0:
            url, err := url.Parse(s)
            if err != nil {
                b.errs = append(b.errs, fmt.Errorf("the URL passed to filename %q is not valid: %v", s, err))
                continue
            }
            b.URL(defaultHttpGetAttempts, url)
        default:
            if !recursive {
                b.singleItemImplied = true
            }
            b.Path(recursive, s)
        }
    }
    if filenameOptions.Kustomize != "" {
        b.paths = append(
            b.paths,
            &KustomizeVisitor{
                mapper:  b.mapper,
                dirPath: filenameOptions.Kustomize,
                schema:  b.schema,
                fSys:    filesys.MakeFsOnDisk(),
            })
    }

    if enforceNamespace {
        b.RequireNamespace()
    }

    return b
}

// Path accepts a set of paths that may be files, directories (all can containing
// one or more resources). Creates a FileVisitor for each file and then each
// FileVisitor is streaming the content to a StreamVisitor. If ContinueOnError() is set
// prior to this method being called, objects on the path that are unrecognized will be
// ignored (but logged at V(2)).
func (b *Builder) Path(recursive bool, paths ...string) *Builder {
    for _, p := range paths {
        _, err := os.Stat(p)
        if os.IsNotExist(err) {
            b.errs = append(b.errs, fmt.Errorf("the path %q does not exist", p))
            continue
        }
        if err != nil {
            b.errs = append(b.errs, fmt.Errorf("the path %q cannot be accessed: %v", p, err))
            continue
        }

        visitors, err := ExpandPathsToFileVisitors(b.mapper, p, recursive, FileExtensions, b.schema)
        if err != nil {
            b.errs = append(b.errs, fmt.Errorf("error reading %q: %v", p, err))
        }
        if len(visitors) > 1 {
            b.dir = true
        }

        b.paths = append(b.paths, visitors...)
    }
    if len(b.paths) == 0 && len(b.errs) == 0 {
        b.errs = append(b.errs, fmt.Errorf("error reading %v: recognized file extensions are %v", paths, FileExtensions))
    }
    return b
}
// staging\src\k8s.io\cli-runtime\pkg\resource\visitor.go
// ExpandPathsToFileVisitors will return a slice of FileVisitors that will handle files from the provided path.
// After FileVisitors open the files, they will pass an io.Reader to a StreamVisitor to do the reading. (stdin
// is also taken care of). Paths argument also accepts a single file, and will return a single visitor
func ExpandPathsToFileVisitors(mapper *mapper, paths string, recursive bool, extensions []string, schema ContentValidator) ([]Visitor, error) {
    var visitors []Visitor
    err := filepath.Walk(paths, func(path string, fi os.FileInfo, err error) error {
        if err != nil {
            return err
        }

        if fi.IsDir() {
            if path != paths && !recursive {
                return filepath.SkipDir
            }
            return nil
        }
        // Don't check extension if the filepath was passed explicitly
        if path != paths && ignoreFile(path, extensions) {
            return nil
        }

        visitor := &FileVisitor{
            Path:          path,
            StreamVisitor: NewStreamVisitor(nil, mapper, path, schema),
        }

        visitors = append(visitors, visitor)
        return nil
    })

    if err != nil {
        return nil, err
    }
    return visitors, nil
}

// FileVisitor is wrapping around a StreamVisitor, to handle open/close files
type FileVisitor struct {
    Path string
    *StreamVisitor
}

// Visit in a FileVisitor is just taking care of opening/closing files
func (v *FileVisitor) Visit(fn VisitorFunc) error {
    var f *os.File
    if v.Path == constSTDINstr {
        f = os.Stdin
    } else {
        var err error
        f, err = os.Open(v.Path)
        if err != nil {
            return err
        }
        defer f.Close()
    }

    // TODO: Consider adding a flag to force to UTF16, apparently some
    // Windows tools don't write the BOM
    utf16bom := unicode.BOMOverride(unicode.UTF8.NewDecoder())
    v.StreamVisitor.Reader = transform.NewReader(f, utf16bom)

    return v.StreamVisitor.Visit(fn)
}

// StreamVisitor reads objects from an io.Reader and walks them. A stream visitor can only be
// visited once.
// TODO: depends on objects being in JSON format before being passed to decode - need to implement
// a stream decoder method on runtime.Codec to properly handle this.
type StreamVisitor struct {
    io.Reader
    *mapper

    Source string
    Schema ContentValidator
}

// NewStreamVisitor is a helper function that is useful when we want to change the fields of the struct but keep calls the same.
func NewStreamVisitor(r io.Reader, mapper *mapper, source string, schema ContentValidator) *StreamVisitor {
    return &StreamVisitor{
        Reader: r,
        mapper: mapper,
        Source: source,
        Schema: schema,
    }
}

// Visit implements Visitor over a stream. StreamVisitor is able to distinct multiple resources in one stream.
func (v *StreamVisitor) Visit(fn VisitorFunc) error {
    d := yaml.NewYAMLOrJSONDecoder(v.Reader, 4096)
    for {
        ext := runtime.RawExtension{}
        if err := d.Decode(&ext); err != nil {
            if err == io.EOF {
                return nil
            }
            return fmt.Errorf("error parsing %s: %v", v.Source, err)
        }
        // TODO: This needs to be able to handle object in other encodings and schemas.
        ext.Raw = bytes.TrimSpace(ext.Raw)
        if len(ext.Raw) == 0 || bytes.Equal(ext.Raw, []byte("null")) {
            continue
        }
        if err := ValidateSchema(ext.Raw, v.Schema); err != nil {
            return fmt.Errorf("error validating %q: %v", v.Source, err)
        }
        info, err := v.infoForData(ext.Raw, v.Source)
        if err != nil {
            if fnErr := fn(info, err); fnErr != nil {
                return fnErr
            }
            continue
        }
        if err := fn(info, nil); err != nil {
            return err
        }
    }
}
  • infoForData函数将资源描述文本转换成Info对象:
// staging\src\k8s.io\cli-runtime\pkg\resource\mapper.go
// InfoForData creates an Info object for the given data. An error is returned
// if any of the decoding or client lookup steps fail. Name and namespace will be
// set into Info if the mapping's MetadataAccessor can retrieve them.
func (m *mapper) infoForData(data []byte, source string) (*Info, error) {
    obj, gvk, err := m.decoder.Decode(data, nil, nil)
    if err != nil {
        return nil, fmt.Errorf("unable to decode %q: %v", source, err)
    }

    name, _ := metadataAccessor.Name(obj)
    namespace, _ := metadataAccessor.Namespace(obj)
    resourceVersion, _ := metadataAccessor.ResourceVersion(obj)

    ret := &Info{
        Source:          source,
        Namespace:       namespace,
        Name:            name,
        ResourceVersion: resourceVersion,

        Object: obj,
    }

    if m.localFn == nil || !m.localFn() {
        restMapper, err := m.restMapperFn()
        if err != nil {
            return nil, err
        }
        mapping, err := restMapper.RESTMapping(gvk.GroupKind(), gvk.Version)
        if err != nil {
            return nil, fmt.Errorf("unable to recognize %q: %v", source, err)
        }
        ret.Mapping = mapping

        client, err := m.clientFn(gvk.GroupVersion())
        if err != nil {
            return nil, fmt.Errorf("unable to connect to a server to handle %q: %v", mapping.Resource, err)
        }
        ret.Client = client
    }

    return ret, nil
}
  • Do函数基于访问者(Visitor)模式。
// Visitor lets clients walk a list of resources.
type Visitor interface {
    Visit(VisitorFunc) error
}

// VisitorFunc implements the Visitor interface for a matching function.
// If there was a problem walking a list of resources, the incoming error
// will describe the problem and the function can decide how to handle that error.
// A nil returned indicates to accept an error to continue loops even when errors happen.
// This is useful for ignoring certain kinds of errors or aggregating errors in some way.
type VisitorFunc func(*Info, error) error
  • Visitor接口包含Visit方法,实现了Visit(VisitorFunc) error的结构体都可以成为Visitor。其中,VisitorFunc是一个匿名函数,它接收Info与error信息,Info结构用于存储RESTClient请求的返回结果,而VisitorFunc匿名函数则生成或处理Info结构。
  • Do函数创建Visitor并赋值给Result
// staging\src\k8s.io\cli-runtime\pkg\resource\builder.go
// Do returns a Result object with a Visitor for the resources identified by the Builder.
// The visitor will respect the error behavior specified by ContinueOnError. Note that stream
// inputs are consumed by the first execution - use Infos() or Object() on the Result to capture a list
// for further iteration.
func (b *Builder) Do() *Result {
    r := b.visitorResult()
    r.mapper = b.Mapper()
    if r.err != nil {
        return r
    }
    if b.flatten {
        r.visitor = NewFlattenListVisitor(r.visitor, b.objectTyper, b.mapper)
    }
    helpers := []VisitorFunc{}
    if b.defaultNamespace {
        helpers = append(helpers, SetNamespace(b.namespace))
    }
    if b.requireNamespace {
        helpers = append(helpers, RequireNamespace(b.namespace))
    }
    helpers = append(helpers, FilterNamespace)
    if b.requireObject {
        helpers = append(helpers, RetrieveLazy)
    }
    if b.continueOnError {
        r.visitor = NewDecoratedVisitor(ContinueOnErrorVisitor{r.visitor}, helpers...)
    } else {
        r.visitor = NewDecoratedVisitor(r.visitor, helpers...)
    }
    return r
}

func (b *Builder) visitorResult() *Result {
    if len(b.errs) > 0 {
        return &Result{err: utilerrors.NewAggregate(b.errs)}
    }

    if b.selectAll {
        selector := labels.Everything().String()
        b.labelSelector = &selector
    }

    // visit items specified by paths
    if len(b.paths) != 0 {
        return b.visitByPaths()
    }

    // visit selectors
    if b.labelSelector != nil || b.fieldSelector != nil {
        return b.visitBySelector()
    }

    // visit items specified by resource and name
    if len(b.resourceTuples) != 0 {
        return b.visitByResource()
    }

    // visit items specified by name
    if len(b.names) != 0 {
        return b.visitByName()
    }

    if len(b.resources) != 0 {
        for _, r := range b.resources {
            _, err := b.mappingFor(r)
            if err != nil {
                return &Result{err: err}
            }
        }
        return &Result{err: fmt.Errorf("resource(s) were provided, but no name was specified")}
    }
    return &Result{err: missingResourceError}
}

func (b *Builder) visitByPaths() *Result {
    result := &Result{
        singleItemImplied:  !b.dir && !b.stream && len(b.paths) == 1,
        targetsSingleItems: true,
    }

    if len(b.resources) != 0 {
        return result.withError(fmt.Errorf("when paths, URLs, or stdin is provided as input, you may not specify resource arguments as well"))
    }
    if len(b.names) != 0 {
        return result.withError(fmt.Errorf("name cannot be provided when a path is specified"))
    }
    if len(b.resourceTuples) != 0 {
        return result.withError(fmt.Errorf("resource/name arguments cannot be provided when a path is specified"))
    }

    var visitors Visitor
    if b.continueOnError {
        visitors = EagerVisitorList(b.paths)
    } else {
        visitors = VisitorList(b.paths)
    }

    if b.flatten {
        visitors = NewFlattenListVisitor(visitors, b.objectTyper, b.mapper)
    }

    // only items from disk can be refetched
    if b.latest {
        // must set namespace prior to fetching
        if b.defaultNamespace {
            visitors = NewDecoratedVisitor(visitors, SetNamespace(b.namespace))
        }
        visitors = NewDecoratedVisitor(visitors, RetrieveLatest)
    }
    if b.labelSelector != nil {
        selector, err := labels.Parse(*b.labelSelector)
        if err != nil {
            return result.withError(fmt.Errorf("the provided selector %q is not valid: %v", *b.labelSelector, err))
        }
        visitors = NewFilteredVisitor(visitors, FilterByLabelSelector(selector))
    }
    result.visitor = visitors
    result.sources = b.paths
    return result
}
  • 有RunCreate函数中的Visit执行Create(info.Namespace, true, info.Object), 会通过Helper请求RESTClient,创建资源。Helper对client-go的RESTClient进行了封装,在此基础上实现了Get、List、Watch、Delete、Create、Patch、Replace等方法。
// staging/src/k8s.io/cli-runtime/pkg/resource/helper.go
func (m *Helper) Create(namespace string, modify bool, obj runtime.Object) (runtime.Object, error) {
    return m.CreateWithOptions(namespace, modify, obj, nil)
}

func (m *Helper) CreateWithOptions(namespace string, modify bool, obj runtime.Object, options *metav1.CreateOptions) (runtime.Object, error) {
    if options == nil {
        options = &metav1.CreateOptions{}
    }
    if m.ServerDryRun {
        options.DryRun = []string{metav1.DryRunAll}
    }
    if m.FieldManager != "" {
        options.FieldManager = m.FieldManager
    }
    if modify {
        // Attempt to version the object based on client logic.
        version, err := metadataAccessor.ResourceVersion(obj)
        if err != nil {
            // We don't know how to clear the version on this object, so send it to the server as is
            return m.createResource(m.RESTClient, m.Resource, namespace, obj, options)
        }
        if version != "" {
            if err := metadataAccessor.SetResourceVersion(obj, ""); err != nil {
                return nil, err
            }
        }
    }

    return 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()
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,126评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,254评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,445评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,185评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,178评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,970评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,276评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,927评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,400评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,883评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,997评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,646评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,213评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,204评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,423评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,423评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,722评论 2 345

推荐阅读更多精彩内容