...

Source file src/github.com/google/go-containerregistry/pkg/v1/remote/layer.go

Documentation: github.com/google/go-containerregistry/pkg/v1/remote

     1  // Copyright 2019 Google LLC All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package remote
    16  
    17  import (
    18  	"context"
    19  	"io"
    20  
    21  	"github.com/google/go-containerregistry/internal/redact"
    22  	"github.com/google/go-containerregistry/internal/verify"
    23  	"github.com/google/go-containerregistry/pkg/name"
    24  	v1 "github.com/google/go-containerregistry/pkg/v1"
    25  	"github.com/google/go-containerregistry/pkg/v1/types"
    26  )
    27  
    28  // remoteImagelayer implements partial.CompressedLayer
    29  type remoteLayer struct {
    30  	ctx     context.Context
    31  	fetcher fetcher
    32  	digest  v1.Hash
    33  }
    34  
    35  // Compressed implements partial.CompressedLayer
    36  func (rl *remoteLayer) Compressed() (io.ReadCloser, error) {
    37  	// We don't want to log binary layers -- this can break terminals.
    38  	ctx := redact.NewContext(rl.ctx, "omitting binary blobs from logs")
    39  	return rl.fetcher.fetchBlob(ctx, verify.SizeUnknown, rl.digest)
    40  }
    41  
    42  // Compressed implements partial.CompressedLayer
    43  func (rl *remoteLayer) Size() (int64, error) {
    44  	resp, err := rl.fetcher.headBlob(rl.ctx, rl.digest)
    45  	if err != nil {
    46  		return -1, err
    47  	}
    48  	defer resp.Body.Close()
    49  	return resp.ContentLength, nil
    50  }
    51  
    52  // Digest implements partial.CompressedLayer
    53  func (rl *remoteLayer) Digest() (v1.Hash, error) {
    54  	return rl.digest, nil
    55  }
    56  
    57  // MediaType implements v1.Layer
    58  func (rl *remoteLayer) MediaType() (types.MediaType, error) {
    59  	return types.DockerLayer, nil
    60  }
    61  
    62  // See partial.Exists.
    63  func (rl *remoteLayer) Exists() (bool, error) {
    64  	return rl.fetcher.blobExists(rl.ctx, rl.digest)
    65  }
    66  
    67  // Layer reads the given blob reference from a registry as a Layer. A blob
    68  // reference here is just a punned name.Digest where the digest portion is the
    69  // digest of the blob to be read and the repository portion is the repo where
    70  // that blob lives.
    71  func Layer(ref name.Digest, options ...Option) (v1.Layer, error) {
    72  	o, err := makeOptions(options...)
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  	return newPuller(o).Layer(o.context, ref)
    77  }
    78  

View as plain text