2018-02-05 21:05:59 +00:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2016-09-06 18:46:37 +00:00
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"net/url"
|
|
|
|
|
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"
|
2019-02-09 18:19:22 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2016-09-06 18:46:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ImagePush requests the docker host to push an image to a remote registry.
|
|
|
|
// It executes the privileged function if the operation is unauthorized
|
|
|
|
// and it tries one more time.
|
|
|
|
// It's up to the caller to handle the io.ReadCloser and close it properly.
|
2017-01-26 00:54:18 +00:00
|
|
|
func (cli *Client) ImagePush(ctx context.Context, image string, options types.ImagePushOptions) (io.ReadCloser, error) {
|
|
|
|
ref, err := reference.ParseNormalizedNamed(image)
|
2016-09-06 18:46:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-01-26 00:54:18 +00:00
|
|
|
if _, isCanonical := ref.(reference.Canonical); isCanonical {
|
2016-09-06 18:46:37 +00:00
|
|
|
return nil, errors.New("cannot push a digest reference")
|
|
|
|
}
|
|
|
|
|
2017-01-26 00:54:18 +00:00
|
|
|
tag := ""
|
|
|
|
name := reference.FamiliarName(ref)
|
|
|
|
|
|
|
|
if nameTaggedRef, isNamedTagged := ref.(reference.NamedTagged); isNamedTagged {
|
2016-09-06 18:46:37 +00:00
|
|
|
tag = nameTaggedRef.Tag()
|
|
|
|
}
|
|
|
|
|
|
|
|
query := url.Values{}
|
|
|
|
query.Set("tag", tag)
|
|
|
|
|
2017-01-26 00:54:18 +00:00
|
|
|
resp, err := cli.tryImagePush(ctx, name, query, options.RegistryAuth)
|
2019-02-09 18:19:22 +00:00
|
|
|
if errdefs.IsUnauthorized(err) && options.PrivilegeFunc != nil {
|
2016-09-06 18:46:37 +00:00
|
|
|
newAuthHeader, privilegeErr := options.PrivilegeFunc()
|
|
|
|
if privilegeErr != nil {
|
|
|
|
return nil, privilegeErr
|
|
|
|
}
|
2017-01-26 00:54:18 +00:00
|
|
|
resp, err = cli.tryImagePush(ctx, name, query, newAuthHeader)
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp.body, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *Client) tryImagePush(ctx context.Context, imageID string, query url.Values, registryAuth string) (serverResponse, error) {
|
|
|
|
headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
|
|
|
|
return cli.post(ctx, "/images/"+imageID+"/push", query, nil, headers)
|
|
|
|
}
|