Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate digest if missing #222

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion registry/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package registry

import (
"context"
_ "crypto/sha256"
"fmt"
"net/http"

"github.com/docker/distribution/manifest/schema2"
digest "github.com/opencontainers/go-digest"
)

var useHead bool = true

// Digest returns the digest for an image.
func (r *Registry) Digest(ctx context.Context, image Image) (digest.Digest, error) {
if len(image.Digest) > 1 {
Expand Down Expand Up @@ -36,5 +39,38 @@ func (r *Registry) Digest(ctx context.Context, image Image) (digest.Digest, erro
return "", fmt.Errorf("got status code: %d", resp.StatusCode)
}

return digest.Parse(resp.Header.Get("Docker-Content-Digest"))
d := resp.Header.Get("Docker-Content-Digest")
if d == "" {
req, err := http.NewRequest("HEAD", url, nil)
if err != nil {
return "", err
}

req.Header.Add("Accept", schema2.MediaTypeManifest)
resp, err := r.Client.Do(req.WithContext(ctx))
if err != nil {
return "", err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNotFound {
return "", fmt.Errorf("got status code: %d", resp.StatusCode)
}

d = resp.Header.Get("Docker-Content-Digest")
if d == "" {
useHead = false
}
}
if d == "" {
// Get the v2 manifest.
m, err := r.Manifest(ctx, image.Path, image.Reference())
if err != nil {
return "", err
}
_, p, _ := m.Payload()
return digest.FromBytes(p), nil
}

return digest.Parse(d)
}