Skip to content

Commit

Permalink
not working
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayyn committed Jan 26, 2024
1 parent 9c27c57 commit c493003
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 59 deletions.
1 change: 0 additions & 1 deletion cmd/crane/cmd/append.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,5 @@ container image.`,

appendCmd.MarkFlagsMutuallyExclusive("oci-empty-base", "base")
appendCmd.MarkFlagRequired("new_tag")
appendCmd.MarkFlagRequired("new_layer")
return appendCmd
}
10 changes: 9 additions & 1 deletion cmd/crane/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"github.com/google/go-containerregistry/internal/cmd"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/logs"
"github.com/google/go-containerregistry/pkg/v1/layout"
"github.com/google/go-containerregistry/pkg/v1/local"

Check failure on line 33 in cmd/crane/cmd/root.go

View workflow job for this annotation

GitHub Actions / Lint

could not import github.com/google/go-containerregistry/pkg/v1/local (-: # github.com/google/go-containerregistry/pkg/v1/local
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/spf13/cobra"
)
Expand All @@ -47,6 +49,7 @@ func New(use, short string, options []crane.Option) *cobra.Command {
insecure := false
ndlayers := false
platform := &platformValue{}
uselocal := ""

wt := &warnTransport{}

Expand All @@ -68,6 +71,10 @@ func New(use, short string, options []crane.Option) *cobra.Command {
if ndlayers {
options = append(options, crane.WithNondistributable())
}
if uselocal != "" {
p, _ := layout.FromPath(uselocal)
options = append(options, crane.WithPuller(local.NewPuller(p)), crane.WithPusher(local.NewPusher(p)))
}
if Version != "" {
binary := "crane"
if len(os.Args[0]) != 0 {
Expand Down Expand Up @@ -137,7 +144,8 @@ func New(use, short string, options []crane.Option) *cobra.Command {
root.PersistentFlags().BoolVar(&insecure, "insecure", false, "Allow image references to be fetched without TLS")
root.PersistentFlags().BoolVar(&ndlayers, "allow-nondistributable-artifacts", false, "Allow pushing non-distributable (foreign) layers")
root.PersistentFlags().Var(platform, "platform", "Specifies the platform in the form os/arch[/variant][:osversion] (e.g. linux/amd64).")

root.PersistentFlags().StringVar(&uselocal, "local", "", "Use a local oci-layout as remote registry")
root.PersistentFlags().MarkHidden("local")
return root
}

Expand Down
Binary file added main
Binary file not shown.
Empty file added notes.txt
Empty file.
Binary file modified pkg/.DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions pkg/crane/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,14 @@ func WithNoClobber(noclobber bool) Option {
}

// WithPuller sets the puller for remote
func WithPuller(puller *remote.Puller) Option {
func WithPuller(puller remote.Puller) Option {
return func(o *Options) {
o.Remote = append(o.Remote, remote.WithPuller(puller))
}
}

// WithPuller sets the puller for remote
func WithPusher(pusher *remote.Pusher) Option {
func WithPusher(pusher remote.Pusher) Option {
return func(o *Options) {
o.Remote = append(o.Remote, remote.WithPusher(pusher))
}
Expand Down
12 changes: 11 additions & 1 deletion pkg/v1/local/puller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type puller struct {
path layout.Path
}

func NewLocalPuller(path layout.Path) remote.Puller {
func NewPuller(path layout.Path) remote.Puller {
return &puller{
path,
}
Expand Down Expand Up @@ -103,3 +103,13 @@ func (*puller) Lister(ctx context.Context, repo name.Repository) (*remote.Lister
func (*puller) Catalogger(ctx context.Context, reg name.Registry) (*remote.Catalogger, error) {
return nil, errors.ErrUnsupported

Check failure on line 104 in pkg/v1/local/puller.go

View workflow job for this annotation

GitHub Actions / e2e ubuntu-latest

undefined: errors.ErrUnsupported

Check failure on line 104 in pkg/v1/local/puller.go

View workflow job for this annotation

GitHub Actions / Build (1.19)

undefined: errors.ErrUnsupported

Check failure on line 104 in pkg/v1/local/puller.go

View workflow job for this annotation

GitHub Actions / Build (1.20)

undefined: errors.ErrUnsupported

Check failure on line 104 in pkg/v1/local/puller.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: errors.ErrUnsupported

Check failure on line 104 in pkg/v1/local/puller.go

View workflow job for this annotation

GitHub Actions / Presubmit

undefined: errors.ErrUnsupported

Check failure on line 104 in pkg/v1/local/puller.go

View workflow job for this annotation

GitHub Actions / Unit Tests (1.19)

undefined: errors.ErrUnsupported

Check failure on line 104 in pkg/v1/local/puller.go

View workflow job for this annotation

GitHub Actions / Unit Tests (1.20)

undefined: errors.ErrUnsupported

Check failure on line 104 in pkg/v1/local/puller.go

View workflow job for this annotation

GitHub Actions / e2e windows-latest

undefined: errors.ErrUnsupported
}

// Catalog implements remote.Puller.
func (*puller) Catalog(ctx context.Context, reg name.Registry) ([]string, error) {
return nil, errors.ErrUnsupported

Check failure on line 109 in pkg/v1/local/puller.go

View workflow job for this annotation

GitHub Actions / e2e ubuntu-latest

undefined: errors.ErrUnsupported

Check failure on line 109 in pkg/v1/local/puller.go

View workflow job for this annotation

GitHub Actions / Build (1.19)

undefined: errors.ErrUnsupported

Check failure on line 109 in pkg/v1/local/puller.go

View workflow job for this annotation

GitHub Actions / Build (1.20)

undefined: errors.ErrUnsupported

Check failure on line 109 in pkg/v1/local/puller.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: errors.ErrUnsupported

Check failure on line 109 in pkg/v1/local/puller.go

View workflow job for this annotation

GitHub Actions / Presubmit

undefined: errors.ErrUnsupported

Check failure on line 109 in pkg/v1/local/puller.go

View workflow job for this annotation

GitHub Actions / Unit Tests (1.19)

undefined: errors.ErrUnsupported

Check failure on line 109 in pkg/v1/local/puller.go

View workflow job for this annotation

GitHub Actions / Unit Tests (1.20)

undefined: errors.ErrUnsupported

Check failure on line 109 in pkg/v1/local/puller.go

View workflow job for this annotation

GitHub Actions / e2e windows-latest

undefined: errors.ErrUnsupported
}

// Referrers implements remote.Puller.
func (*puller) Referrers(ctx context.Context, d name.Digest, filter map[string]string) (v1.ImageIndex, error) {
return nil, errors.ErrUnsupported

Check failure on line 114 in pkg/v1/local/puller.go

View workflow job for this annotation

GitHub Actions / e2e ubuntu-latest

undefined: errors.ErrUnsupported

Check failure on line 114 in pkg/v1/local/puller.go

View workflow job for this annotation

GitHub Actions / Build (1.19)

undefined: errors.ErrUnsupported

Check failure on line 114 in pkg/v1/local/puller.go

View workflow job for this annotation

GitHub Actions / Build (1.20)

undefined: errors.ErrUnsupported

Check failure on line 114 in pkg/v1/local/puller.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: errors.ErrUnsupported

Check failure on line 114 in pkg/v1/local/puller.go

View workflow job for this annotation

GitHub Actions / Presubmit

undefined: errors.ErrUnsupported

Check failure on line 114 in pkg/v1/local/puller.go

View workflow job for this annotation

GitHub Actions / Unit Tests (1.19)

undefined: errors.ErrUnsupported

Check failure on line 114 in pkg/v1/local/puller.go

View workflow job for this annotation

GitHub Actions / Unit Tests (1.20)

undefined: errors.ErrUnsupported

Check failure on line 114 in pkg/v1/local/puller.go

View workflow job for this annotation

GitHub Actions / e2e windows-latest

undefined: errors.ErrUnsupported
}
146 changes: 126 additions & 20 deletions pkg/v1/local/pusher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,122 @@ import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"

"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/layout"
"github.com/google/go-containerregistry/pkg/v1/partial"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/types"
specsv1 "github.com/opencontainers/image-spec/specs-go/v1"
)

type manifest interface {
remote.Taggable
partial.Describable
}

type describable struct {
desc v1.Descriptor
}

func (d describable) Digest() (v1.Hash, error) {
return d.desc.Digest, nil
}

func (d describable) Size() (int64, error) {
return d.desc.Size, nil
}

func (d describable) MediaType() (types.MediaType, error) {
return d.desc.MediaType, nil
}

type tagManifest struct {
remote.Taggable
partial.Describable
}

func taggableToManifest(t remote.Taggable) (manifest, error) {
if m, ok := t.(manifest); ok {
return m, nil
}

if d, ok := t.(*remote.Descriptor); ok {
if d.MediaType.IsIndex() {
return d.ImageIndex()
}

if d.MediaType.IsImage() {
return d.Image()
}

if d.MediaType.IsSchema1() {
return d.Schema1()
}

return tagManifest{t, describable{d.ToDescriptor()}}, nil
}

desc := v1.Descriptor{
// A reasonable default if Taggable doesn't implement MediaType.
MediaType: types.DockerManifestSchema2,
}

b, err := t.RawManifest()
if err != nil {
return nil, err
}

if wmt, ok := t.(partial.WithMediaType); ok {
desc.MediaType, err = wmt.MediaType()
if err != nil {
return nil, err
}
}

desc.Digest, desc.Size, err = v1.SHA256(bytes.NewReader(b))
if err != nil {
return nil, err
}

return tagManifest{t, describable{desc}}, nil
}

func unpackTaggable(t remote.Taggable) ([]byte, *v1.Descriptor, error) {
if d, ok := t.(*remote.Descriptor); ok {
return d.Manifest, &d.Descriptor, nil
}
b, err := t.RawManifest()
if err != nil {
return nil, nil, err
}

// A reasonable default if Taggable doesn't implement MediaType.
mt := types.DockerManifestSchema2

if wmt, ok := t.(partial.WithMediaType); ok {
m, err := wmt.MediaType()
if err != nil {
return nil, nil, err
}
mt = m
}

h, sz, err := v1.SHA256(bytes.NewReader(b))
if err != nil {
return nil, nil, err
}

return b, &v1.Descriptor{
MediaType: mt,
Size: sz,
Digest: h,
}, nil
}

type pusher struct {
path layout.Path
}
Expand All @@ -27,36 +132,36 @@ func (lp *pusher) Delete(ctx context.Context, ref name.Reference) error {

// Push implements remote.Pusher.
func (lp *pusher) Push(ctx context.Context, ref name.Reference, t remote.Taggable) error {
b, err := t.RawManifest()
mf, err := taggableToManifest(t)
if err != nil {
return err
}
h, sz, err := v1.SHA256(bytes.NewReader(b))
b, desc, err := unpackTaggable(t)
if err != nil {
return err
}
if err := lp.path.WriteBlob(desc.Digest, io.NopCloser(bytes.NewBuffer(b))); err != nil {
return err
}

mt := types.DockerManifestSchema2

if wmt, ok := t.(partial.WithMediaType); ok {
m, err := wmt.MediaType()
if img, ok := mf.(v1.Image); ok {
cl, err := partial.ConfigLayer(img)
if err != nil {
return err
}
mt = m
}
if err := lp.path.WriteBlob(h, io.NopCloser(bytes.NewBuffer(b))); err != nil {
return err
dg, err := cl.Digest()
if err != nil {
return err
}
rc, err := img.RawConfigFile()
if err != nil {
return err
}
lp.path.WriteBlob(dg, io.NopCloser(bytes.NewBuffer(rc)))
fmt.Fprintln(os.Stderr, "image!")
}

return lp.path.AppendDescriptor(v1.Descriptor{
MediaType: mt,
Size: sz,
Digest: h,
Annotations: map[string]string{
specsv1.AnnotationRefName: ref.Context().RepositoryStr(),
},
})
return lp.path.AppendDescriptor(*desc)
}

// Upload implements remote.Pusher.
Expand All @@ -69,10 +174,11 @@ func (lp *pusher) Upload(ctx context.Context, repo name.Repository, l v1.Layer)
if err != nil {
return err
}
fmt.Fprintln(os.Stderr, digest)
return lp.path.WriteBlob(digest, rc)
}

func NewLocalPusher(path layout.Path) remote.Pusher {
func NewPusher(path layout.Path) remote.Pusher {
return &pusher{
path,
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/v1/remote/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ import (
"github.com/google/go-containerregistry/pkg/v1/types"
)

// Get returns a remote.Descriptor for the given reference. The response from
// the registry is left un-interpreted, for the most part. This is useful for
// querying what kind of artifact a reference represents.
// Get returns a partial.Artifact for the given reference.
//
// See Head if you don't need the response body.
func Artifact(ref name.Reference, options ...Option) (partial.Artifact, error) {
Expand All @@ -36,9 +34,10 @@ func Artifact(ref name.Reference, options ...Option) (partial.Artifact, error) {
// Handle options and fetch the manifest with the acceptable MediaTypes in the
// Accept header.
func artifact(ref name.Reference, acceptable []types.MediaType, options ...Option) (partial.Artifact, error) {
o, err := makeOptions(options...)
o, err := makeOptions(append(options, WithAcceptableMediaTypes(acceptable))...)
if err != nil {
return nil, err
}
return newPuller(o).artifact(o.context, ref, acceptable, o.platform)

return newPuller(o).Artifact(o.context, ref)
}
4 changes: 2 additions & 2 deletions pkg/v1/remote/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func CatalogPage(target name.Registry, last string, n int, options ...Option) ([
return nil, err
}

f, err := newPuller(o).fetcher(o.context, target)
f, err := makeFetcher(o.context, target, o)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -83,7 +83,7 @@ func Catalog(ctx context.Context, target name.Registry, options ...Option) ([]st
ctx = o.context
}

return newPuller(o).catalog(ctx, target, o.pageSize)
return newPuller(o).Catalog(ctx, target)
}

func (f *fetcher) catalogPage(ctx context.Context, reg name.Registry, next string, pageSize int) (*Catalogs, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/v1/remote/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type Descriptor struct {
platform v1.Platform
}

func (d *Descriptor) toDesc() v1.Descriptor {
func (d *Descriptor) ToDescriptor() v1.Descriptor {
return d.Descriptor
}

Expand Down
22 changes: 16 additions & 6 deletions pkg/v1/remote/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/google/go-containerregistry/pkg/logs"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
"github.com/google/go-containerregistry/pkg/v1/types"
)

// Option is a functional option for remote operations.
Expand All @@ -52,8 +53,9 @@ type options struct {
filter map[string]string

// Set by Reuse, we currently store one or the other.
puller *Puller
pusher *Pusher
puller *Puller
pusher *Pusher
acceptableMediaTypes []types.MediaType
}

var defaultPlatform = v1.Platform{
Expand Down Expand Up @@ -223,6 +225,14 @@ func WithPlatform(p v1.Platform) Option {
}
}

// WithAcceptableMediaTypes sets acceptable media types for artifacts
func WithAcceptableMediaTypes(acceptable []types.MediaType) Option {
return func(o *options) error {
o.acceptableMediaTypes = acceptable
return nil
}
}

// WithContext is a functional option for setting the context in http requests
// performed by a given function. Note that this context is used for _all_
// http requests, not just the initial volley. E.g., for remote.Image, the
Expand Down Expand Up @@ -332,17 +342,17 @@ func WithFilter(key string, value string) Option {
}

// WithPuller sets puller for remote
func WithPuller(puller *Puller) Option {
func WithPuller(puller Puller) Option {
return func(o *options) error {
o.puller = puller
o.puller = &puller
return nil
}
}

// WithPuller sets pusher for remote
func WithPusher(pusher *Pusher) Option {
func WithPusher(pusher Pusher) Option {
return func(o *options) error {
o.pusher = pusher
o.pusher = &pusher
return nil
}
}
Expand Down
Loading

0 comments on commit c493003

Please sign in to comment.