2016-09-06 18:46:37 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2017-08-08 19:43:48 +00:00
|
|
|
"encoding/json"
|
2016-09-06 18:46:37 +00:00
|
|
|
"io"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2017-01-26 00:54:18 +00:00
|
|
|
"github.com/docker/distribution/reference"
|
2016-09-06 18:46:37 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-08-08 19:43:48 +00:00
|
|
|
"github.com/docker/docker/pkg/system"
|
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
2016-09-06 18:46:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ImageCreate creates a new image based in the parent options.
|
|
|
|
// It returns the JSON content in the response body.
|
|
|
|
func (cli *Client) ImageCreate(ctx context.Context, parentReference string, options types.ImageCreateOptions) (io.ReadCloser, error) {
|
2017-01-26 00:54:18 +00:00
|
|
|
ref, err := reference.ParseNormalizedNamed(parentReference)
|
2016-09-06 18:46:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
query := url.Values{}
|
2017-01-26 00:54:18 +00:00
|
|
|
query.Set("fromImage", reference.FamiliarName(ref))
|
|
|
|
query.Set("tag", getAPITagFromNamedRef(ref))
|
2017-08-08 19:43:48 +00:00
|
|
|
resp, err := cli.tryImageCreate(ctx, query, options.RegistryAuth, options.Platform)
|
2016-09-06 18:46:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp.body, nil
|
|
|
|
}
|
|
|
|
|
2017-08-08 19:43:48 +00:00
|
|
|
func (cli *Client) tryImageCreate(ctx context.Context, query url.Values, registryAuth string, platform specs.Platform) (serverResponse, error) {
|
2016-09-06 18:46:37 +00:00
|
|
|
headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
|
2017-08-08 19:43:48 +00:00
|
|
|
|
|
|
|
// TODO @jhowardmsft: system.IsPlatformEmpty is a temporary function. We need to move
|
|
|
|
// (in the reasonably short future) to a package which supports all the platform
|
|
|
|
// validation such as is proposed in https://github.com/containerd/containerd/pull/1403
|
|
|
|
if !system.IsPlatformEmpty(platform) {
|
|
|
|
platformJSON, err := json.Marshal(platform)
|
|
|
|
if err != nil {
|
|
|
|
return serverResponse{}, err
|
|
|
|
}
|
|
|
|
headers["X-Requested-Platform"] = []string{string(platformJSON[:])}
|
|
|
|
}
|
2016-09-06 18:46:37 +00:00
|
|
|
return cli.post(ctx, "/images/create", query, nil, headers)
|
|
|
|
}
|