Containerd Server学习 Part 1

【编者的话】containerd v1.0.0的源码分析,以docker-containerd --config /var/run/docker/containerd/containerd.toml为入口,看大神们如何组织Go语言代码

containerd.io
github: containerd v1.0.0

准备:

go get -v github.com/containerd/containerd
cd $GOPATH/src/github.com/containerd/containerd
git checkout v1.0.0 -b v1.0.0

分析

命令行
  • 位置:cmd/containerd/main.go
  • 作用:定义containerd 命令行相关参数与配置文件
  • 依赖:github.com/urfave/cli
package main

import (
    "fmt"
    "os"

    "github.com/containerd/containerd/version"
    "github.com/urfave/cli"
)

const usage = `
                    __        _                     __
  _________  ____  / /_____ _(_)___  ___  _________/ /
 / ___/ __ \/ __ \/ __/ __ ` + "`" + `/ / __ \/ _ \/ ___/ __  /
/ /__/ /_/ / / / / /_/ /_/ / / / / /  __/ /  / /_/ /
\___/\____/_/ /_/\__/\__,_/_/_/ /_/\___/_/   \__,_/

high performance container runtime
`

func main() {
    app := cli.NewApp()
    app.Name = "containerd"
    app.Version = version.Version
    app.Usage = usage
    app.Flags = []cli.Flag{
        cli.StringFlag{
            Name:  "config,c",
            Usage: "path to the configuration file",
            Value: defaultConfigPath,
        },
    }
    app.Commands = []cli.Command{}
    app.Action = func(context *cli.Context) error {
        return nil
    }
    if err := app.Run(os.Args); err != nil {
        fmt.Fprintf(os.Stderr, "containerd: %s\n", err)
        os.Exit(1)
    }
}

配置文件解析
// LoadConfig loads the containerd server config from the provided path
func LoadConfig(path string, v *Config) error {
    if v == nil {
        return errors.Wrapf(errdefs.ErrInvalidArgument, "argument v must not be nil")
    }
    md, err := toml.DecodeFile(path, v)
    if err != nil {
        return err
    }
    v.md = md
    return nil
}
添加参数
  • 位置:cmd/containerd/main.go
  • 作用:应用参数到相关配置
func applyFlags(context *cli.Context, config *server.Config) error {
    // the order for config vs flag values is that flags will always override
    // the config values if they are set
    if err := setLevel(context, config); err != nil {
        return err
    }
    for _, v := range []struct {
        name string
        d    *string
    }{
        {
            name: "root",
            d:    &config.Root,
        },
        {
            name: "state",
            d:    &config.State,
        },
        {
            name: "address",
            d:    &config.GRPC.Address,
        },
    } {
        if s := context.GlobalString(v.name); s != "" {
            *v.d = s
        }
    }
    return nil
}
日志
...
var ctx    = log.WithModule(gocontext.Background(), "containerd")
...
log.G(ctx).WithFields(logrus.Fields{
            "version":  version.Version,
            "revision": version.Revision,
        }).Info("starting containerd")
...

# log/context.go
func WithModule(ctx context.Context, module string) context.Context {
    parent := GetModulePath(ctx)
    if parent != "" {
        if path.Base(parent) == module {
            return ctx
        }
        module = path.Join(parent, module)
    }

    ctx = WithLogger(ctx, GetLogger(ctx).WithField("module", module))
    return context.WithValue(ctx, moduleKey{}, module)
}

整理:

https://github.com/llitfkitfk/containerd/tree/part-1

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,073评论 19 139
  • Docker — 云时代的程序分发方式 要说最近一年云计算业界有什么大事件?Google Compute Engi...
    ahohoho阅读 15,718评论 15 147
  • 表白大一物流管理二班解梦玉 新的学校能让我爱你爱的更深 让我们的之间的感情更好 我在聊城 你在齐河 不远不近...
    山工院表白墙阅读 1,099评论 0 0
  • 复地相思白月光, 树影摇落满地伤。 总有痴人做痴梦, 半塘莲花夜生香。
    Harvest收获阅读 4,216评论 96 101
  • 01 我喜欢上海的原因,真的很简单。不是因为上海美食多。也不是因为上海有很多漂...
    泛善可陈阅读 2,523评论 2 13

友情链接更多精彩内容