services/server/server.go

func New(ctx context.Context, config srvconfig.Config) (Server, error)

  1. for _, p := range plugins {
  2. ...
  3. // check for grpc services that should be registered with the server
  4. if src, ok := instance.(plugin.Service); ok {
  5. grpcServices = append(grpcServices, src)
  6. }
  7. ...
  8. }
  9. //注册grpc service
  10. for _, service := range grpcServices {
  11. if err := service.Register(grpcServer); err != nil {
  12. return nil, err
  13. }
  14. }

grpc service列表:

image.png

一个典型的service:task service

services/tasks/service.go

  1. func (s *service) Start(ctx context.Context, r *api.StartRequest) (*api.StartResponse, error) {
  2. return s.local.Start(ctx, r)
  3. }
  1. func (l *local) Start(ctx context.Context, r *api.StartRequest, _ ...grpc.CallOption) (*api.StartResponse, error) {
  2. t, err := l.getTask(ctx, r.ContainerID)
  3. if err != nil {
  4. return nil, err
  5. }
  6. // shim的client,v1指向 runtime/v1/linux/process.go
  7. p := runtime.Process(t)
  8. if r.ExecID != "" {
  9. if p, err = t.Process(ctx, r.ExecID); err != nil {
  10. return nil, errdefs.ToGRPC(err)
  11. }
  12. }
  13. if err := p.Start(ctx); err != nil {
  14. return nil, errdefs.ToGRPC(err)
  15. }
  16. state, err := p.State(ctx)
  17. if err != nil {
  18. return nil, errdefs.ToGRPC(err)
  19. }
  20. return &api.StartResponse{
  21. Pid: state.Pid,
  22. }, nil
  23. }