【编者的话】containerd v1.0.0的源码分析,以docker-containerd --config /var/run/docker/containerd/containerd.toml
为入口,看大神们如何组织Go语言代码
分析
程序信号处理
- 位置:
cmd/containerd/main.go
...
var (
signals = make(chan os.Signal, 2048)
ctx = log.WithModule(gocontext.Background(), "containerd")
)
done := handleSignals(ctx, signals, serverC)
...
signal.Notify(signals, handledSignals...)
...
<-done
...
初始化服务
- 位置:
cmd/containerd/main.go
...
server, err := server.New(ctx, config)
if err != nil {
return err
}
...
开启debug / metrics / grpc服务
- 位置:
cmd/containerd/main.go
- 依赖:
github.com/docker/go-metrics
github.com/grpc-ecosystem/go-grpc-prometheus
...
if config.Debug.Address != "" {
l, err := sys.GetLocalListener(config.Debug.Address, config.Debug.UID, config.Debug.GID)
if err != nil {
return errors.Wrapf(err, "failed to get listener for debug endpoint")
}
serve(log.WithModule(ctx, "debug"), l, server.ServeDebug)
}
if config.Metrics.Address != "" {
l, err := net.Listen("tcp", config.Metrics.Address)
if err != nil {
return errors.Wrapf(err, "failed to get listener for metrics endpoint")
}
serve(log.WithModule(ctx, "metrics"), l, server.ServeMetrics)
}
l, err := sys.GetLocalListener(address, config.GRPC.UID, config.GRPC.GID)
if err != nil {
return errors.Wrapf(err, "failed to get listener for main endpoint")
}
serve(log.WithModule(ctx, "grpc"), l, server.ServeGRPC)
...
containerd架构
初始化服务解析
载入插件
- 位置:
server/server.go
plugins, err := loadPlugins(config)
if err != nil {
return nil, err
}
初始化 grpc服务 和 传输服务
- 位置:
server/server.go
type Server struct {
rpc *grpc.Server
events *exchange.Exchange
}
grpc服务
- 位置:
server/server.go
- 依赖:
google.golang.org/grpc
github.com/grpc-ecosystem/go-grpc-prometheus
rpc := grpc.NewServer(
grpc.UnaryInterceptor(interceptor),
grpc.StreamInterceptor(grpc_prometheus.StreamServerInterceptor),
)
传输服务
- 位置:
events/exchange/exchange.go
- 作用:负责传播事件
- 依赖:github.com/docker/go-events
// Exchange broadcasts events
type Exchange struct {
broadcaster *goevents.Broadcaster
}
网友评论