github.com/rawahars/moby@v24.0.4+incompatible/client/image_push.go (about)

     1  package client // import "github.com/docker/docker/client"
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"io"
     7  	"net/url"
     8  
     9  	"github.com/docker/distribution/reference"
    10  	"github.com/docker/docker/api/types"
    11  	"github.com/docker/docker/api/types/registry"
    12  	"github.com/docker/docker/errdefs"
    13  )
    14  
    15  // ImagePush requests the docker host to push an image to a remote registry.
    16  // It executes the privileged function if the operation is unauthorized
    17  // and it tries one more time.
    18  // It's up to the caller to handle the io.ReadCloser and close it properly.
    19  func (cli *Client) ImagePush(ctx context.Context, image string, options types.ImagePushOptions) (io.ReadCloser, error) {
    20  	ref, err := reference.ParseNormalizedNamed(image)
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  
    25  	if _, isCanonical := ref.(reference.Canonical); isCanonical {
    26  		return nil, errors.New("cannot push a digest reference")
    27  	}
    28  
    29  	name := reference.FamiliarName(ref)
    30  	query := url.Values{}
    31  	if !options.All {
    32  		ref = reference.TagNameOnly(ref)
    33  		if tagged, ok := ref.(reference.Tagged); ok {
    34  			query.Set("tag", tagged.Tag())
    35  		}
    36  	}
    37  
    38  	resp, err := cli.tryImagePush(ctx, name, query, options.RegistryAuth)
    39  	if errdefs.IsUnauthorized(err) && options.PrivilegeFunc != nil {
    40  		newAuthHeader, privilegeErr := options.PrivilegeFunc()
    41  		if privilegeErr != nil {
    42  			return nil, privilegeErr
    43  		}
    44  		resp, err = cli.tryImagePush(ctx, name, query, newAuthHeader)
    45  	}
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	return resp.body, nil
    50  }
    51  
    52  func (cli *Client) tryImagePush(ctx context.Context, imageID string, query url.Values, registryAuth string) (serverResponse, error) {
    53  	headers := map[string][]string{registry.AuthHeader: {registryAuth}}
    54  	return cli.post(ctx, "/images/"+imageID+"/push", query, nil, headers)
    55  }