github.com/olljanat/moby@v1.13.1/client/image_pull.go (about)

     1  package client
     2  
     3  import (
     4  	"io"
     5  	"net/http"
     6  	"net/url"
     7  
     8  	"golang.org/x/net/context"
     9  
    10  	"github.com/docker/docker/api/types"
    11  	"github.com/docker/docker/api/types/reference"
    12  )
    13  
    14  // ImagePull requests the docker host to pull an image from a remote registry.
    15  // It executes the privileged function if the operation is unauthorized
    16  // and it tries one more time.
    17  // It's up to the caller to handle the io.ReadCloser and close it properly.
    18  //
    19  // FIXME(vdemeester): there is currently used in a few way in docker/docker
    20  // - if not in trusted content, ref is used to pass the whole reference, and tag is empty
    21  // - if in trusted content, ref is used to pass the reference name, and tag for the digest
    22  func (cli *Client) ImagePull(ctx context.Context, ref string, options types.ImagePullOptions) (io.ReadCloser, error) {
    23  	repository, tag, err := reference.Parse(ref)
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  
    28  	query := url.Values{}
    29  	query.Set("fromImage", repository)
    30  	if tag != "" && !options.All {
    31  		query.Set("tag", tag)
    32  	}
    33  
    34  	resp, err := cli.tryImageCreate(ctx, query, options.RegistryAuth)
    35  	if resp.statusCode == http.StatusUnauthorized && options.PrivilegeFunc != nil {
    36  		newAuthHeader, privilegeErr := options.PrivilegeFunc()
    37  		if privilegeErr != nil {
    38  			return nil, privilegeErr
    39  		}
    40  		resp, err = cli.tryImageCreate(ctx, query, newAuthHeader)
    41  	}
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  	return resp.body, nil
    46  }