Skip to content

Commit

Permalink
Adds log output of clients that are registered to debug
Browse files Browse the repository at this point in the history
Signed-off-by: JoshVanL <[email protected]>
  • Loading branch information
JoshVanL committed Aug 31, 2020
1 parent 00d0e4a commit 9437639
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 6 deletions.
4 changes: 4 additions & 0 deletions pkg/client/acr/acr.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func New(opts Options) (*Client, error) {
}, nil
}

func (c *Client) Name() string {
return "acr"
}

func (c *Client) Tags(ctx context.Context, host, repo, image string) ([]api.ImageTag, error) {
client, err := c.getACRClient(ctx, host)
if err != nil {
Expand Down
13 changes: 11 additions & 2 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (
// ImageClient represents a image registry client that can list available tags
// for image URLs.
type ImageClient interface {
// Returns the name of the client
Name() string

// IsHost will return true if this client is appropriate for the given
// host.
IsHost(host string) bool
Expand Down Expand Up @@ -75,7 +78,7 @@ func New(ctx context.Context, log *logrus.Entry, opts Options) (*Client, error)
return nil, fmt.Errorf("failed to create fallback client: %s", err)
}

return &Client{
c := &Client{
clients: append(
selfhostedClients,
acrClient,
Expand All @@ -85,7 +88,13 @@ func New(ctx context.Context, log *logrus.Entry, opts Options) (*Client, error)
quay.New(opts.Quay),
),
fallbackClient: fallbackClient,
}, nil
}

for _, client := range append(c.clients, fallbackClient) {
log.Debugf("registered client %q", client.Name())
}

return c, nil
}

// Tags returns the full list of image tags available, for a given image URL.
Expand Down
4 changes: 4 additions & 0 deletions pkg/client/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func New(ctx context.Context, opts Options) (*Client, error) {
}, nil
}

func (c *Client) Name() string {
return "dockerhub"
}

func (c *Client) Tags(ctx context.Context, _, repo, image string) ([]api.ImageTag, error) {
url := fmt.Sprintf(lookupURL, repo, image)

Expand Down
4 changes: 4 additions & 0 deletions pkg/client/ecr/ecr.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func New(opts Options) *Client {
}
}

func (c *Client) Name() string {
return "ecr"
}

func (c *Client) Tags(ctx context.Context, host, repo, image string) ([]api.ImageTag, error) {
matches := ecrPattern.FindStringSubmatch(host)
if len(matches) < 3 {
Expand Down
4 changes: 4 additions & 0 deletions pkg/client/gcr/gcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func New(opts Options) *Client {
}
}

func (c *Client) Name() string {
return "gcr"
}

func (c *Client) Tags(ctx context.Context, host, repo, image string) ([]api.ImageTag, error) {
if repo == "google-containers" {
host = "gcr.io"
Expand Down
4 changes: 4 additions & 0 deletions pkg/client/quay/quay.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func New(opts Options) *Client {
}
}

func (c *Client) Name() string {
return "quay"
}

func (c *Client) Tags(ctx context.Context, _, repo, image string) ([]api.ImageTag, error) {
url := fmt.Sprintf(lookupURL, repo, image)

Expand Down
23 changes: 19 additions & 4 deletions pkg/client/selfhosted/selfhosted.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
// /v2/{repo/image}/manifests/{tag}
manifestPath = "%s/v2/%s/manifests/%s"
// Token endpoint
tokenPath = "%s/v2/token"
tokenPath = "/v2/token"

// HTTP headers to request API version
dockerAPIv1Header = "application/vnd.docker.distribution.manifest.v1+json"
Expand Down Expand Up @@ -95,8 +95,13 @@ func New(ctx context.Context, log *logrus.Entry, opts *Options) (*Client, error)
}

token, err := client.setupBasicAuth(ctx, opts.Host)
if httpErr, ok := selfhostederrors.IsHTTPError(err); ok {
return nil, fmt.Errorf("failed to setup token auth (%d): %s",
httpErr.StatusCode, httpErr.Body)
}

if err != nil {
return nil, fmt.Errorf("failed to setup auth: %s", err)
return nil, fmt.Errorf("failed to setup token auth: %s", err)
}
client.Bearer = token
}
Expand All @@ -110,6 +115,15 @@ func New(ctx context.Context, log *logrus.Entry, opts *Options) (*Client, error)
return client, nil
}

// Name returns the name of the host URL for the selfhosted client
func (c *Client) Name() string {
if len(c.Host) == 0 {
return "dockerapi"
}

return c.Host
}

// Tags will fetch the image tags from a given image URL. It must first query
// the tags that are available, then query the 2.1 and 2.2 API endpoints to
// gather the image digest and created time.
Expand Down Expand Up @@ -221,15 +235,16 @@ func (c *Client) setupBasicAuth(ctx context.Context, url string) (string, error)

req, err := http.NewRequest(http.MethodPost, tokenURL, upReader)
if err != nil {
return "", err
return "", fmt.Errorf("failed to create basic auth request: %s", err)
}

req.Header.Set("Content-Type", "application/json")
req = req.WithContext(ctx)

resp, err := c.Do(req)
if err != nil {
return "", err
return "", fmt.Errorf("failed to send basic auth request %q: %s",
req.URL, err)
}

body, err := ioutil.ReadAll(resp.Body)
Expand Down

0 comments on commit 9437639

Please sign in to comment.