Skip to content

Commit

Permalink
removed get pusher and refactored AddTags and AddArtifact
Browse files Browse the repository at this point in the history
  • Loading branch information
Skarlso committed Sep 25, 2024
1 parent 9e523c0 commit 9f85b55
Showing 1 changed file with 24 additions and 35 deletions.
59 changes: 24 additions & 35 deletions api/oci/extensions/repositories/ocireg/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/mandelsoft/goutils/errors"
"github.com/opencontainers/go-digest"
"oras.land/oras-go/v2/content"
"oras.land/oras-go/v2/errdef"
"oras.land/oras-go/v2/registry"

Expand Down Expand Up @@ -51,36 +50,6 @@ func (n *NamespaceContainer) SetImplementation(impl support.NamespaceAccessImpl)
n.impl = impl
}

func (n *NamespaceContainer) getPusher(vers string) (content.Pusher, error) {
err := n.assureCreated()
if err != nil {
return nil, err
}

ref := n.repo.GetRef(n.impl.GetNamespace(), vers)
n.repo.GetContext().Logger().Trace("get pusher", "ref", ref)
pusher := n.ociRepo
if ok, _ := artdesc.IsDigest(vers); !ok {
var err error

pusher, err = n.repo.getResolver(ref, n.impl.GetNamespace())
if err != nil {
return nil, fmt.Errorf("unable get resolver: %w", err)
}
}

return pusher, nil
}

func (n *NamespaceContainer) push(vers string, blob cpi.BlobAccess) error {
p, err := n.getPusher(vers)
if err != nil {
return fmt.Errorf("unable to get pusher: %w", err)
}
n.repo.GetContext().Logger().Trace("pushing", "version", vers)
return push(dummyContext, p, blob)
}

func (n *NamespaceContainer) IsReadOnly() bool {
return n.repo.IsReadOnly()
}
Expand Down Expand Up @@ -195,17 +164,33 @@ func (n *NamespaceContainer) AddArtifact(artifact cpi.Artifact, tags ...string)

n.repo.GetContext().Logger().Debug("adding artifact", "digest", blob.Digest(), "mimetype", blob.MimeType())

if err := n.assureCreated(); err != nil {
return nil, err
}

if len(tags) > 0 {
for _, tag := range tags {
if err := n.push(tag, blob); err != nil {
return nil, err
if err := n.pushTag(blob, tag); err != nil {
return nil, fmt.Errorf("failed to push tag %s: %w", tag, err)
}
}
}

return blob, err
}

func (n *NamespaceContainer) pushTag(blob blobaccess.BlobAccess, tag string) error {
reader, err := blob.Reader()
if err != nil {
return err
}
expectedDescriptor := *artdesc.DefaultBlobDescriptor(blob)
if err := n.ociRepo.PushReference(context.Background(), expectedDescriptor, reader, tag); err != nil {
return fmt.Errorf("unable to push: %w", err)
}
return nil
}

func (n *NamespaceContainer) AddTags(digest digest.Digest, tags ...string) error {
ref := n.repo.GetRef(n.impl.GetNamespace(), digest.String())
desc, err := n.ociRepo.Resolve(context.Background(), ref)
Expand All @@ -218,10 +203,14 @@ func (n *NamespaceContainer) AddTags(digest digest.Digest, tags ...string) error
return fmt.Errorf("error creating new data access: %w", err)
}

if err := n.assureCreated(); err != nil {
return err
}

blob := blobaccess.ForDataAccess(desc.Digest, desc.Size, desc.MediaType, acc)
for _, tag := range tags {
if err := n.push(tag, blob); err != nil {
return fmt.Errorf("unable to push: %w", err)
if err := n.pushTag(blob, tag); err != nil {
return fmt.Errorf("failed to push tag %s: %w", tag, err)
}
}

Expand Down

0 comments on commit 9f85b55

Please sign in to comment.