Docker 17.03-CE create plugin源码解析

故宫博物院

(题图:故宫 Apr 3,2016)

继续上一篇Docker17.03-CE插件开发的🌰,今天来看下docker create plugin的源码。

cli/command/plugin/create.go

Docker命令行docker plugin create调用的,使用的是cobra,这个命令行工具开发包很好用,推荐下。

执行这两个函数

func newCreateCommand(dockerCli *command.DockerCli) *cobra.Command 
//调用下面的函数,拼装成URL调用RESTful API接口
func runCreate(dockerCli *command.DockerCli, options pluginCreateOptions) error {
  ...
  if err = dockerCli.Client().PluginCreate(ctx, createCtx, createOptions); err != nil {
		return err
	}
  ...
}

api/server/router/plugin/plugin_routes.go

func (pr *pluginRouter) createPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  ...
  if err := pr.backend.CreateFromContext(ctx, r.Body, options); err != nil {
		return err
	}
  ...
}

createPlugin这个方法定义在api/server/route/plugin/backen.go的Backend接口中。

PluginCreate这个方法定义在docker/docker/client/Interface.go的PluginAPIClient接口中。

docker/client/plugin_create.go

// PluginCreate creates a plugin
func (cli *Client) PluginCreate(ctx context.Context, createContext io.Reader, createOptions types.PluginCreateOptions) error {
	headers := http.Header(make(map[string][]string))
	headers.Set("Content-Type", "application/x-tar")

	query := url.Values{}
	query.Set("name", createOptions.RepoName)

	resp, err := cli.postRaw(ctx, "/plugins/create", query, createContext, headers)
	if err != nil {
		return err
	}
	ensureReaderClosed(resp)
	return err
}

plugin在后端接收到请求后会执行下面的方法。最终create plugin的实现在plugin/backend_linux.go下:

// CreateFromContext creates a plugin from the given pluginDir which contains
// both the rootfs and the config.json and a repoName with optional tag.
func (pm *Manager) CreateFromContext(ctx context.Context, tarCtx io.ReadCloser, options *types.PluginCreateOptions) (err error) {}

至于docker create plugin时docker后台究竟做了什么,就看👆那个文件。