github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/docker/registry/internal/dockerhub.go (about)

     1  // Copyright 2021 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package internal
     5  
     6  import (
     7  	"net/http"
     8  	"net/url"
     9  	"strings"
    10  
    11  	"github.com/juju/errors"
    12  
    13  	"github.com/juju/juju/docker"
    14  )
    15  
    16  const (
    17  	dockerServerAddress = "index.docker.io"
    18  )
    19  
    20  type dockerhub struct {
    21  	*baseClient
    22  }
    23  
    24  func newDockerhub(repoDetails docker.ImageRepoDetails, transport http.RoundTripper) (RegistryInternal, error) {
    25  	c, err := newBase(repoDetails, transport, normalizeRepoDetailsCommon)
    26  	if err != nil {
    27  		return nil, errors.Trace(err)
    28  	}
    29  	return &dockerhub{c}, nil
    30  }
    31  
    32  func (c *dockerhub) String() string {
    33  	return "docker.io"
    34  }
    35  
    36  // Match checks if the repository details matches current provider format.
    37  func (c *dockerhub) Match() bool {
    38  	return strings.Contains(c.repoDetails.ServerAddress, "docker.io")
    39  }
    40  
    41  // DecideBaseURL decides the API url to use.
    42  func (c *dockerhub) DecideBaseURL() error {
    43  	c.repoDetails.ServerAddress = dockerServerAddress
    44  	if err := c.baseClient.DecideBaseURL(); err != nil {
    45  		return errors.Trace(err)
    46  	}
    47  	url, err := url.Parse(c.repoDetails.ServerAddress)
    48  	if err != nil {
    49  		return errors.Trace(err)
    50  	}
    51  	url.Scheme = "https"
    52  	addr := url.String()
    53  	if !strings.HasSuffix(addr, "/") {
    54  		// This "/" matters because docker uses url string for the credential key and expects the trailing slash.
    55  		addr += "/"
    56  	}
    57  	c.repoDetails.ServerAddress = addr
    58  	logger.Tracef("dockerhub repoDetails %s", c.repoDetails)
    59  	return nil
    60  }