update vendor

This commit is contained in:
Ettore Di Giacinto
2021-10-23 20:47:32 +02:00
parent 6a9f19941a
commit ab251fefce
889 changed files with 80636 additions and 20210 deletions

View File

@@ -16,9 +16,11 @@ package crane
import (
"fmt"
"os"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/stream"
"github.com/google/go-containerregistry/pkg/v1/tarball"
)
@@ -26,12 +28,45 @@ import (
func Append(base v1.Image, paths ...string) (v1.Image, error) {
layers := make([]v1.Layer, 0, len(paths))
for _, path := range paths {
layer, err := tarball.LayerFromFile(path)
layer, err := getLayer(path)
if err != nil {
return nil, fmt.Errorf("reading tar %q: %v", path, err)
return nil, fmt.Errorf("reading layer %q: %v", path, err)
}
layers = append(layers, layer)
}
return mutate.AppendLayers(base, layers...)
}
func getLayer(path string) (v1.Layer, error) {
f, err := streamFile(path)
if err != nil {
return nil, err
}
if f != nil {
return stream.NewLayer(f), nil
}
return tarball.LayerFromFile(path)
}
// If we're dealing with a named pipe, trying to open it multiple times will
// fail, so we need to do a streaming upload.
//
// returns nil, nil for non-streaming files
func streamFile(path string) (*os.File, error) {
if path == "-" {
return os.Stdin, nil
}
fi, err := os.Stat(path)
if err != nil {
return nil, err
}
if !fi.Mode().IsRegular() {
return os.Open(path)
}
return nil, nil
}

View File

@@ -29,5 +29,7 @@ func Catalog(src string, opt ...Option) (res []string, err error) {
return nil, err
}
return remote.Catalog(context.TODO(), reg, o.remote...)
// This context gets overridden by remote.WithContext, which is set by
// crane.WithContext.
return remote.Catalog(context.Background(), reg, o.remote...)
}

View File

@@ -17,8 +17,7 @@ package crane
import (
"fmt"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/internal/legacy"
"github.com/google/go-containerregistry/internal/legacy"
"github.com/google/go-containerregistry/pkg/logs"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
@@ -59,7 +58,7 @@ func Copy(src, dst string, opt ...Option) error {
}
case types.DockerManifestSchema1, types.DockerManifestSchema1Signed:
// Handle schema 1 images separately.
if err := copySchema1(desc, srcRef, dstRef); err != nil {
if err := legacy.CopySchema1(desc, srcRef, dstRef, o.remote...); err != nil {
return fmt.Errorf("failed to copy schema 1 image: %v", err)
}
default:
@@ -87,16 +86,3 @@ func copyIndex(desc *remote.Descriptor, dstRef name.Reference, o options) error
}
return remote.WriteIndex(dstRef, idx, o.remote...)
}
func copySchema1(desc *remote.Descriptor, srcRef, dstRef name.Reference) error {
srcAuth, err := authn.DefaultKeychain.Resolve(srcRef.Context().Registry)
if err != nil {
return err
}
dstAuth, err := authn.DefaultKeychain.Resolve(dstRef.Context().Registry)
if err != nil {
return err
}
return legacy.CopySchema1(desc, srcRef, dstRef, srcAuth, dstAuth)
}

View File

@@ -14,6 +14,8 @@
package crane
import "github.com/google/go-containerregistry/pkg/logs"
// Digest returns the sha256 hash of the remote image at ref.
func Digest(ref string, opt ...Option) (string, error) {
o := makeOptions(opt...)
@@ -22,6 +24,11 @@ func Digest(ref string, opt ...Option) (string, error) {
if err != nil {
return "", err
}
if !desc.MediaType.IsIndex() {
return desc.Digest.String(), nil
}
// TODO: does not work for indexes which contain schema v1 manifests
img, err := desc.Image()
if err != nil {
return "", err
@@ -32,9 +39,14 @@ func Digest(ref string, opt ...Option) (string, error) {
}
return digest.String(), nil
}
desc, err := head(ref, opt...)
desc, err := Head(ref, opt...)
if err != nil {
return "", err
logs.Warn.Printf("HEAD request failed, falling back on GET: %v", err)
rdesc, err := getManifest(ref, opt...)
if err != nil {
return "", err
}
return rdesc.Digest.String(), nil
}
return desc.Digest.String(), nil
}

View File

@@ -44,7 +44,9 @@ func getManifest(r string, opt ...Option) (*remote.Descriptor, error) {
return remote.Get(ref, o.remote...)
}
func head(r string, opt ...Option) (*v1.Descriptor, error) {
// Head performs a HEAD request for a manifest and returns a content descriptor
// based on the registry's response.
func Head(r string, opt ...Option) (*v1.Descriptor, error) {
o := makeOptions(opt...)
ref, err := name.ParseReference(r, o.name...)
if err != nil {

View File

@@ -0,0 +1,237 @@
// Copyright 2020 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package crane
import (
"errors"
"fmt"
"github.com/containerd/stargz-snapshotter/estargz"
"github.com/google/go-containerregistry/pkg/logs"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"github.com/google/go-containerregistry/pkg/v1/types"
)
// Optimize optimizes a remote image or index from src to dst.
// THIS API IS EXPERIMENTAL AND SUBJECT TO CHANGE WITHOUT WARNING.
func Optimize(src, dst string, prioritize []string, opt ...Option) error {
pset := newStringSet(prioritize)
o := makeOptions(opt...)
srcRef, err := name.ParseReference(src, o.name...)
if err != nil {
return fmt.Errorf("parsing reference %q: %v", src, err)
}
dstRef, err := name.ParseReference(dst, o.name...)
if err != nil {
return fmt.Errorf("parsing reference for %q: %v", dst, err)
}
logs.Progress.Printf("Optimizing from %v to %v", srcRef, dstRef)
desc, err := remote.Get(srcRef, o.remote...)
if err != nil {
return fmt.Errorf("fetching %q: %v", src, err)
}
switch desc.MediaType {
case types.OCIImageIndex, types.DockerManifestList:
// Handle indexes separately.
if o.platform != nil {
// If platform is explicitly set, don't optimize the whole index, just the appropriate image.
if err := optimizeAndPushImage(desc, dstRef, pset, o); err != nil {
return fmt.Errorf("failed to optimize image: %v", err)
}
} else {
if err := optimizeAndPushIndex(desc, dstRef, pset, o); err != nil {
return fmt.Errorf("failed to optimize index: %v", err)
}
}
case types.DockerManifestSchema1, types.DockerManifestSchema1Signed:
return errors.New("docker schema 1 images are not supported")
default:
// Assume anything else is an image, since some registries don't set mediaTypes properly.
if err := optimizeAndPushImage(desc, dstRef, pset, o); err != nil {
return fmt.Errorf("failed to optimize image: %v", err)
}
}
return nil
}
func optimizeAndPushImage(desc *remote.Descriptor, dstRef name.Reference, prioritize stringSet, o options) error {
img, err := desc.Image()
if err != nil {
return err
}
missing, oimg, err := optimizeImage(img, prioritize)
if err != nil {
return err
}
if len(missing) > 0 {
return fmt.Errorf("the following prioritized files were missing from image: %v", missing.List())
}
return remote.Write(dstRef, oimg, o.remote...)
}
func optimizeImage(img v1.Image, prioritize stringSet) (stringSet, v1.Image, error) {
cfg, err := img.ConfigFile()
if err != nil {
return nil, nil, err
}
ocfg := cfg.DeepCopy()
ocfg.History = nil
ocfg.RootFS.DiffIDs = nil
oimg, err := mutate.ConfigFile(empty.Image, ocfg)
if err != nil {
return nil, nil, err
}
layers, err := img.Layers()
if err != nil {
return nil, nil, err
}
missingFromImage := newStringSet(prioritize.List())
olayers := make([]mutate.Addendum, 0, len(layers))
for _, layer := range layers {
missingFromLayer := []string{}
olayer, err := tarball.LayerFromOpener(layer.Uncompressed,
tarball.WithEstargz,
tarball.WithEstargzOptions(
estargz.WithPrioritizedFiles(prioritize.List()),
estargz.WithAllowPrioritizeNotFound(&missingFromLayer),
))
if err != nil {
return nil, nil, err
}
missingFromImage = missingFromImage.Intersection(newStringSet(missingFromLayer))
olayers = append(olayers, mutate.Addendum{
Layer: olayer,
MediaType: types.DockerLayer,
})
}
oimg, err = mutate.Append(oimg, olayers...)
if err != nil {
return nil, nil, err
}
return missingFromImage, oimg, nil
}
func optimizeAndPushIndex(desc *remote.Descriptor, dstRef name.Reference, prioritize stringSet, o options) error {
idx, err := desc.ImageIndex()
if err != nil {
return err
}
missing, oidx, err := optimizeIndex(idx, prioritize)
if err != nil {
return err
}
if len(missing) > 0 {
return fmt.Errorf("the following prioritized files were missing from all images: %v", missing.List())
}
return remote.WriteIndex(dstRef, oidx, o.remote...)
}
func optimizeIndex(idx v1.ImageIndex, prioritize stringSet) (stringSet, v1.ImageIndex, error) {
im, err := idx.IndexManifest()
if err != nil {
return nil, nil, err
}
missingFromIndex := newStringSet(prioritize.List())
// Build an image for each child from the base and append it to a new index to produce the result.
adds := make([]mutate.IndexAddendum, 0, len(im.Manifests))
for _, desc := range im.Manifests {
img, err := idx.Image(desc.Digest)
if err != nil {
return nil, nil, err
}
missingFromImage, oimg, err := optimizeImage(img, prioritize)
if err != nil {
return nil, nil, err
}
missingFromIndex = missingFromIndex.Intersection(missingFromImage)
adds = append(adds, mutate.IndexAddendum{
Add: oimg,
Descriptor: v1.Descriptor{
URLs: desc.URLs,
MediaType: desc.MediaType,
Annotations: desc.Annotations,
Platform: desc.Platform,
},
})
}
idxType, err := idx.MediaType()
if err != nil {
return nil, nil, err
}
return missingFromIndex, mutate.IndexMediaType(mutate.AppendManifests(empty.Index, adds...), idxType), nil
}
type stringSet map[string]struct{}
func newStringSet(in []string) stringSet {
ss := stringSet{}
for _, s := range in {
ss[s] = struct{}{}
}
return ss
}
func (s stringSet) List() []string {
result := make([]string, 0, len(s))
for k := range s {
result = append(result, k)
}
return result
}
func (s stringSet) Intersection(rhs stringSet) stringSet {
// To appease ST1016
lhs := s
// Make sure len(lhs) >= len(rhs)
if len(lhs) < len(rhs) {
return rhs.Intersection(lhs)
}
result := stringSet{}
for k := range lhs {
if _, ok := rhs[k]; ok {
result[k] = struct{}{}
}
}
return result
}

View File

@@ -15,6 +15,7 @@
package crane
import (
"context"
"net/http"
"github.com/google/go-containerregistry/pkg/authn"
@@ -97,3 +98,10 @@ func WithUserAgent(ua string) Option {
o.remote = append(o.remote, remote.WithUserAgent(ua))
}
}
// WithContext is a functional option for setting the context.
func WithContext(ctx context.Context) Option {
return func(o *options) {
o.remote = append(o.remote, remote.WithContext(ctx))
}
}

View File

@@ -37,7 +37,7 @@ func Pull(src string, opt ...Option) (v1.Image, error) {
o := makeOptions(opt...)
ref, err := name.ParseReference(src, o.name...)
if err != nil {
return nil, fmt.Errorf("parsing tag %q: %v", src, err)
return nil, fmt.Errorf("parsing reference %q: %v", src, err)
}
return remote.Image(ref, o.remote...)
@@ -45,26 +45,36 @@ func Pull(src string, opt ...Option) (v1.Image, error) {
// Save writes the v1.Image img as a tarball at path with tag src.
func Save(img v1.Image, src, path string) error {
ref, err := name.ParseReference(src)
if err != nil {
return fmt.Errorf("parsing ref %q: %v", src, err)
}
imgMap := map[string]v1.Image{src: img}
return MultiSave(imgMap, path)
}
// WriteToFile wants a tag to write to the tarball, but we might have
// been given a digest.
// If the original ref was a tag, use that. Otherwise, if it was a
// digest, tag the image with :i-was-a-digest instead.
tag, ok := ref.(name.Tag)
if !ok {
d, ok := ref.(name.Digest)
if !ok {
return fmt.Errorf("ref wasn't a tag or digest")
// MultiSave writes collection of v1.Image img with tag as a tarball.
func MultiSave(imgMap map[string]v1.Image, path string) error {
tagToImage := map[name.Tag]v1.Image{}
for src, img := range imgMap {
ref, err := name.ParseReference(src)
if err != nil {
return fmt.Errorf("parsing ref %q: %v", src, err)
}
tag = d.Repository.Tag(iWasADigestTag)
}
// WriteToFile wants a tag to write to the tarball, but we might have
// been given a digest.
// If the original ref was a tag, use that. Otherwise, if it was a
// digest, tag the image with :i-was-a-digest instead.
tag, ok := ref.(name.Tag)
if !ok {
d, ok := ref.(name.Digest)
if !ok {
return fmt.Errorf("ref wasn't a tag or digest")
}
tag = d.Repository.Tag(iWasADigestTag)
}
tagToImage[tag] = img
}
// no progress channel (for now)
return tarball.WriteToFile(path, tag, img)
return tarball.MultiWriteToFile(path, tagToImage)
}
// PullLayer returns the given layer from a registry.
@@ -80,9 +90,20 @@ func PullLayer(ref string, opt ...Option) (v1.Layer, error) {
// SaveLegacy writes the v1.Image img as a legacy tarball at path with tag src.
func SaveLegacy(img v1.Image, src, path string) error {
ref, err := name.ParseReference(src)
if err != nil {
return fmt.Errorf("parsing ref %q: %v", src, err)
imgMap := map[string]v1.Image{src: img}
return MultiSave(imgMap, path)
}
// MultiSaveLegacy writes collection of v1.Image img with tag as a legacy tarball.
func MultiSaveLegacy(imgMap map[string]v1.Image, path string) error {
refToImage := map[name.Reference]v1.Image{}
for src, img := range imgMap {
ref, err := name.ParseReference(src)
if err != nil {
return fmt.Errorf("parsing ref %q: %v", src, err)
}
refToImage[ref] = img
}
w, err := os.Create(path)
@@ -91,12 +112,19 @@ func SaveLegacy(img v1.Image, src, path string) error {
}
defer w.Close()
return legacy.Write(ref, img, w)
return legacy.MultiWrite(refToImage, w)
}
// SaveOCI writes the v1.Image img as an OCI Image Layout at path. If a layout
// already exists at that path, it will add the image to the index.
func SaveOCI(img v1.Image, path string) error {
imgMap := map[string]v1.Image{"": img}
return MultiSaveOCI(imgMap, path)
}
// MultiSaveOCI writes collection of v1.Image img as an OCI Image Layout at path. If a layout
// already exists at that path, it will add the image to the index.
func MultiSaveOCI(imgMap map[string]v1.Image, path string) error {
p, err := layout.FromPath(path)
if err != nil {
p, err = layout.Write(path, empty.Index)
@@ -104,5 +132,10 @@ func SaveOCI(img v1.Image, path string) error {
return err
}
}
return p.AppendImage(img)
for _, img := range imgMap {
if err = p.AppendImage(img); err != nil {
return err
}
}
return nil
}

View File

@@ -24,19 +24,31 @@ import (
)
// Load reads the tarball at path as a v1.Image.
func Load(path string) (v1.Image, error) {
// TODO: Allow tag?
return tarball.ImageFromPath(path, nil)
func Load(path string, opt ...Option) (v1.Image, error) {
return LoadTag(path, "")
}
// LoadTag reads a tag from the tarball at path as a v1.Image.
// If tag is "", will attempt to read the tarball as a single image.
func LoadTag(path, tag string, opt ...Option) (v1.Image, error) {
if tag == "" {
return tarball.ImageFromPath(path, nil)
}
o := makeOptions(opt...)
t, err := name.NewTag(tag, o.name...)
if err != nil {
return nil, fmt.Errorf("parsing tag %q: %v", tag, err)
}
return tarball.ImageFromPath(path, &t)
}
// Push pushes the v1.Image img to a registry as dst.
func Push(img v1.Image, dst string, opt ...Option) error {
o := makeOptions(opt...)
tag, err := name.NewTag(dst, o.name...)
tag, err := name.ParseReference(dst, o.name...)
if err != nil {
return fmt.Errorf("parsing tag %q: %v", dst, err)
return fmt.Errorf("parsing reference %q: %v", dst, err)
}
return remote.MultiWrite(map[name.Reference]remote.Taggable{
tag: img,
}, o.remote...)
return remote.Write(tag, img, o.remote...)
}