Skip to content

Commit

Permalink
replace uses of Descriptor alias
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastiaan van Stijn <[email protected]>
  • Loading branch information
thaJeztah committed Oct 7, 2024
1 parent 740b311 commit 0ab7f32
Show file tree
Hide file tree
Showing 54 changed files with 417 additions and 381 deletions.
10 changes: 5 additions & 5 deletions blobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type Descriptor = v1.Descriptor
type BlobStatter interface {
// Stat provides metadata about a blob identified by the digest. If the
// blob is unknown to the describer, ErrBlobUnknown will be returned.
Stat(ctx context.Context, dgst digest.Digest) (Descriptor, error)
Stat(ctx context.Context, dgst digest.Digest) (v1.Descriptor, error)
}

// BlobDeleter enables deleting blobs from storage.
Expand Down Expand Up @@ -97,7 +97,7 @@ type BlobDescriptorService interface {
// Such a facility can be used to map blobs between digest domains, with
// the restriction that the algorithm of the descriptor must match the
// canonical algorithm (ie sha256) of the annotator.
SetDescriptor(ctx context.Context, dgst digest.Digest, desc Descriptor) error
SetDescriptor(ctx context.Context, dgst digest.Digest, desc v1.Descriptor) error

// Clear enables descriptors to be unlinked
Clear(ctx context.Context, dgst digest.Digest) error
Expand Down Expand Up @@ -139,7 +139,7 @@ type BlobServer interface {
type BlobIngester interface {
// Put inserts the content p into the blob service, returning a descriptor
// or an error.
Put(ctx context.Context, mediaType string, p []byte) (Descriptor, error)
Put(ctx context.Context, mediaType string, p []byte) (v1.Descriptor, error)

// Create allocates a new blob writer to add a blob to this service. The
// returned handle can be written to and later resumed using an opaque
Expand Down Expand Up @@ -168,7 +168,7 @@ type CreateOptions struct {
From reference.Canonical
// Stat allows to pass precalculated descriptor to link and return.
// Blob access check will be skipped if set.
Stat *Descriptor
Stat *v1.Descriptor
}
}

Expand Down Expand Up @@ -198,7 +198,7 @@ type BlobWriter interface {
// stream" to the blob. The returned descriptor may have a different
// digest depending on the blob store, referred to as the canonical
// descriptor.
Commit(ctx context.Context, provisional Descriptor) (canonical Descriptor, err error)
Commit(ctx context.Context, provisional v1.Descriptor) (canonical v1.Descriptor, err error)

// Cancel ends the blob write without storing any data and frees any
// associated resources. Any data written thus far will be lost. Cancel
Expand Down
9 changes: 5 additions & 4 deletions internal/client/blob_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/distribution/distribution/v3"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
)

type httpBlobUpload struct {
Expand Down Expand Up @@ -120,11 +121,11 @@ func (hbu *httpBlobUpload) StartedAt() time.Time {
return hbu.startedAt
}

func (hbu *httpBlobUpload) Commit(ctx context.Context, desc distribution.Descriptor) (distribution.Descriptor, error) {
func (hbu *httpBlobUpload) Commit(ctx context.Context, desc v1.Descriptor) (v1.Descriptor, error) {
// TODO(dmcgowan): Check if already finished, if so just fetch
req, err := http.NewRequestWithContext(hbu.ctx, http.MethodPut, hbu.location, nil)
if err != nil {
return distribution.Descriptor{}, err
return v1.Descriptor{}, err
}

values := req.URL.Query()
Expand All @@ -133,12 +134,12 @@ func (hbu *httpBlobUpload) Commit(ctx context.Context, desc distribution.Descrip

resp, err := hbu.client.Do(req)
if err != nil {
return distribution.Descriptor{}, err
return v1.Descriptor{}, err
}
defer resp.Body.Close()

if err := hbu.handleErrorResponse(resp); err != nil {
return distribution.Descriptor{}, err
return v1.Descriptor{}, err
}

return hbu.statter.Stat(ctx, desc.Digest)
Expand Down
67 changes: 34 additions & 33 deletions internal/client/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/distribution/distribution/v3/registry/storage/cache/memory"
"github.com/distribution/reference"
"github.com/opencontainers/go-digest"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
)

// Registry provides an interface for calling Repositories, which returns a catalog of repositories.
Expand Down Expand Up @@ -249,42 +250,42 @@ func (t *tags) All(ctx context.Context) ([]string, error) {
}
}

func descriptorFromResponse(response *http.Response) (distribution.Descriptor, error) {
desc := distribution.Descriptor{}
func descriptorFromResponse(response *http.Response) (v1.Descriptor, error) {
desc := v1.Descriptor{}
headers := response.Header

ctHeader := headers.Get("Content-Type")
if ctHeader == "" {
return distribution.Descriptor{}, errors.New("missing or empty Content-Type header")
return v1.Descriptor{}, errors.New("missing or empty Content-Type header")
}
desc.MediaType = ctHeader

digestHeader := headers.Get("Docker-Content-Digest")
if digestHeader == "" {
data, err := io.ReadAll(response.Body)
if err != nil {
return distribution.Descriptor{}, err
return v1.Descriptor{}, err
}
_, desc, err := distribution.UnmarshalManifest(ctHeader, data)
if err != nil {
return distribution.Descriptor{}, err
return v1.Descriptor{}, err
}
return desc, nil
}

dgst, err := digest.Parse(digestHeader)
if err != nil {
return distribution.Descriptor{}, err
return v1.Descriptor{}, err
}
desc.Digest = dgst

lengthHeader := headers.Get("Content-Length")
if lengthHeader == "" {
return distribution.Descriptor{}, errors.New("missing or empty Content-Length header")
return v1.Descriptor{}, errors.New("missing or empty Content-Length header")
}
length, err := strconv.ParseInt(lengthHeader, 10, 64)
if err != nil {
return distribution.Descriptor{}, err
return v1.Descriptor{}, err
}
desc.Size = length

Expand All @@ -294,14 +295,14 @@ func descriptorFromResponse(response *http.Response) (distribution.Descriptor, e
// Get issues a HEAD request for a Manifest against its named endpoint in order
// to construct a descriptor for the tag. If the registry doesn't support HEADing
// a manifest, fallback to GET.
func (t *tags) Get(ctx context.Context, tag string) (distribution.Descriptor, error) {
func (t *tags) Get(ctx context.Context, tag string) (v1.Descriptor, error) {
ref, err := reference.WithTag(t.name, tag)
if err != nil {
return distribution.Descriptor{}, err
return v1.Descriptor{}, err
}
u, err := t.ub.BuildManifestURL(ref)
if err != nil {
return distribution.Descriptor{}, err
return v1.Descriptor{}, err
}

newRequest := func(method string) (*http.Response, error) {
Expand All @@ -319,7 +320,7 @@ func (t *tags) Get(ctx context.Context, tag string) (distribution.Descriptor, er

resp, err := newRequest(http.MethodHead)
if err != nil {
return distribution.Descriptor{}, err
return v1.Descriptor{}, err
}
defer resp.Body.Close()

Expand All @@ -334,22 +335,22 @@ func (t *tags) Get(ctx context.Context, tag string) (distribution.Descriptor, er
// - to get error details in case of a failure
resp, err = newRequest(http.MethodGet)
if err != nil {
return distribution.Descriptor{}, err
return v1.Descriptor{}, err
}
defer resp.Body.Close()

if resp.StatusCode >= 200 && resp.StatusCode < 400 {
return descriptorFromResponse(resp)
}
return distribution.Descriptor{}, HandleHTTPResponseError(resp)
return v1.Descriptor{}, HandleHTTPResponseError(resp)
}
}

func (t *tags) Lookup(ctx context.Context, digest distribution.Descriptor) ([]string, error) {
func (t *tags) Lookup(ctx context.Context, digest v1.Descriptor) ([]string, error) {
panic("not implemented")
}

func (t *tags) Tag(ctx context.Context, tag string, desc distribution.Descriptor) error {
func (t *tags) Tag(ctx context.Context, tag string, desc v1.Descriptor) error {
panic("not implemented")
}

Expand Down Expand Up @@ -654,7 +655,7 @@ func sanitizeLocation(location, base string) (string, error) {
return baseURL.ResolveReference(locationURL).String(), nil
}

func (bs *blobs) Stat(ctx context.Context, dgst digest.Digest) (distribution.Descriptor, error) {
func (bs *blobs) Stat(ctx context.Context, dgst digest.Digest) (v1.Descriptor, error) {
return bs.statter.Stat(ctx, dgst)
}

Expand Down Expand Up @@ -711,21 +712,21 @@ func (bs *blobs) ServeBlob(ctx context.Context, w http.ResponseWriter, r *http.R
return err
}

func (bs *blobs) Put(ctx context.Context, mediaType string, p []byte) (distribution.Descriptor, error) {
func (bs *blobs) Put(ctx context.Context, mediaType string, p []byte) (v1.Descriptor, error) {
writer, err := bs.Create(ctx)
if err != nil {
return distribution.Descriptor{}, err
return v1.Descriptor{}, err
}
dgstr := digest.Canonical.Digester()
n, err := io.Copy(writer, io.TeeReader(bytes.NewReader(p), dgstr.Hash()))
if err != nil {
return distribution.Descriptor{}, err
return v1.Descriptor{}, err
}
if n < int64(len(p)) {
return distribution.Descriptor{}, fmt.Errorf("short copy: wrote %d of %d", n, len(p))
return v1.Descriptor{}, fmt.Errorf("short copy: wrote %d of %d", n, len(p))
}

return writer.Commit(ctx, distribution.Descriptor{
return writer.Commit(ctx, v1.Descriptor{
MediaType: mediaType,
Size: int64(len(p)),
Digest: dgstr.Digest(),
Expand Down Expand Up @@ -848,45 +849,45 @@ type blobStatter struct {
client *http.Client
}

func (bs *blobStatter) Stat(ctx context.Context, dgst digest.Digest) (distribution.Descriptor, error) {
func (bs *blobStatter) Stat(ctx context.Context, dgst digest.Digest) (v1.Descriptor, error) {
ref, err := reference.WithDigest(bs.name, dgst)
if err != nil {
return distribution.Descriptor{}, err
return v1.Descriptor{}, err
}
u, err := bs.ub.BuildBlobURL(ref)
if err != nil {
return distribution.Descriptor{}, err
return v1.Descriptor{}, err
}

req, err := http.NewRequestWithContext(ctx, http.MethodHead, u, nil)
if err != nil {
return distribution.Descriptor{}, err
return v1.Descriptor{}, err
}
resp, err := bs.client.Do(req)
if err != nil {
return distribution.Descriptor{}, err
return v1.Descriptor{}, err
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusNotFound {
return distribution.Descriptor{}, distribution.ErrBlobUnknown
return v1.Descriptor{}, distribution.ErrBlobUnknown
}

if err := HandleHTTPResponseError(resp); err != nil {
return distribution.Descriptor{}, err
return v1.Descriptor{}, err
}

lengthHeader := resp.Header.Get("Content-Length")
if lengthHeader == "" {
return distribution.Descriptor{}, fmt.Errorf("missing content-length header for request: %s", u)
return v1.Descriptor{}, fmt.Errorf("missing content-length header for request: %s", u)
}

length, err := strconv.ParseInt(lengthHeader, 10, 64)
if err != nil {
return distribution.Descriptor{}, fmt.Errorf("error parsing content-length: %v", err)
return v1.Descriptor{}, fmt.Errorf("error parsing content-length: %v", err)
}

return distribution.Descriptor{
return v1.Descriptor{
MediaType: resp.Header.Get("Content-Type"),
Size: length,
Digest: dgst,
Expand Down Expand Up @@ -931,6 +932,6 @@ func (bs *blobStatter) Clear(ctx context.Context, dgst digest.Digest) error {
return HandleHTTPResponseError(resp)
}

func (bs *blobStatter) SetDescriptor(ctx context.Context, dgst digest.Digest, desc distribution.Descriptor) error {
func (bs *blobStatter) SetDescriptor(ctx context.Context, dgst digest.Digest, desc v1.Descriptor) error {
return nil
}
14 changes: 7 additions & 7 deletions internal/client/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func TestBlobResume(t *testing.T) {
t.Fatalf("Unexpected ReadFrom length: %d; expected: %d", n, len(b1))
}

blob, err := upload.Commit(ctx, distribution.Descriptor{
blob, err := upload.Commit(ctx, v1.Descriptor{
Digest: dgst,
Size: int64(len(b1)),
})
Expand Down Expand Up @@ -538,7 +538,7 @@ func TestBlobUploadChunked(t *testing.T) {
}
}

blob, err := upload.Commit(ctx, distribution.Descriptor{
blob, err := upload.Commit(ctx, v1.Descriptor{
Digest: dgst,
Size: int64(len(b1)),
})
Expand Down Expand Up @@ -646,7 +646,7 @@ func TestBlobUploadMonolithic(t *testing.T) {
t.Fatalf("Unexpected ReadFrom length: %d; expected: %d", n, len(b1))
}

blob, err := upload.Commit(ctx, distribution.Descriptor{
blob, err := upload.Commit(ctx, v1.Descriptor{
Digest: dgst,
Size: int64(len(b1)),
})
Expand Down Expand Up @@ -752,7 +752,7 @@ func TestBlobUploadMonolithicDockerUploadUUIDFromURL(t *testing.T) {
t.Fatalf("Unexpected ReadFrom length: %d; expected: %d", n, len(b1))
}

blob, err := upload.Commit(ctx, distribution.Descriptor{
blob, err := upload.Commit(ctx, v1.Descriptor{
Digest: dgst,
Size: int64(len(b1)),
})
Expand Down Expand Up @@ -917,10 +917,10 @@ func TestBlobMount(t *testing.T) {
}

func newRandomOCIManifest(t *testing.T, blobCount int) (*ocischema.Manifest, digest.Digest, []byte) {
layers := make([]distribution.Descriptor, blobCount)
layers := make([]v1.Descriptor, blobCount)
for i := 0; i < blobCount; i++ {
dgst, blob := newRandomBlob((i % 5) * 16)
layers[i] = distribution.Descriptor{
layers[i] = v1.Descriptor{
MediaType: v1.MediaTypeImageLayer,
Digest: dgst,
Size: int64(len(blob)),
Expand All @@ -930,7 +930,7 @@ func newRandomOCIManifest(t *testing.T, blobCount int) (*ocischema.Manifest, dig
m := ocischema.Manifest{
Versioned: specs.Versioned{SchemaVersion: 2},
MediaType: v1.MediaTypeImageManifest,
Config: distribution.Descriptor{
Config: v1.Descriptor{
Digest: "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b",
Size: 123,
MediaType: v1.MediaTypeImageConfig,
Expand Down
14 changes: 7 additions & 7 deletions manifest/manifestlist/manifestlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ func init() {
}
}

func unmarshalManifestList(b []byte) (distribution.Manifest, distribution.Descriptor, error) {
func unmarshalManifestList(b []byte) (distribution.Manifest, v1.Descriptor, error) {
m := &DeserializedManifestList{}
if err := m.UnmarshalJSON(b); err != nil {
return nil, distribution.Descriptor{}, err
return nil, v1.Descriptor{}, err
}

if m.MediaType != MediaTypeManifestList {
return nil, distribution.Descriptor{}, fmt.Errorf("mediaType in manifest list should be '%s' not '%s'", MediaTypeManifestList, m.MediaType)
return nil, v1.Descriptor{}, fmt.Errorf("mediaType in manifest list should be '%s' not '%s'", MediaTypeManifestList, m.MediaType)
}

return m, distribution.Descriptor{
return m, v1.Descriptor{
Digest: digest.FromBytes(b),
Size: int64(len(b)),
MediaType: MediaTypeManifestList,
Expand Down Expand Up @@ -81,7 +81,7 @@ type PlatformSpec struct {

// A ManifestDescriptor references a platform-specific manifest.
type ManifestDescriptor struct {
distribution.Descriptor
v1.Descriptor

// Platform specifies which platform the manifest pointed to by the
// descriptor runs on.
Expand All @@ -101,8 +101,8 @@ type ManifestList struct {

// References returns the distribution descriptors for the referenced image
// manifests.
func (m ManifestList) References() []distribution.Descriptor {
dependencies := make([]distribution.Descriptor, len(m.Manifests))
func (m ManifestList) References() []v1.Descriptor {
dependencies := make([]v1.Descriptor, len(m.Manifests))
for i := range m.Manifests {
dependencies[i] = m.Manifests[i].Descriptor
dependencies[i].Platform = &v1.Platform{
Expand Down
Loading

0 comments on commit 0ab7f32

Please sign in to comment.