Skip to content

Commit

Permalink
remove support for oci artifact.manifest
Browse files Browse the repository at this point in the history
Signed-off-by: razzle <[email protected]>
  • Loading branch information
Noxsios committed May 12, 2023
1 parent ebae326 commit ceb6da1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 84 deletions.
43 changes: 4 additions & 39 deletions src/pkg/packager/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand All @@ -23,7 +22,6 @@ import (
"oras.land/oras-go/v2/content"
"oras.land/oras-go/v2/content/file"
"oras.land/oras-go/v2/registry"
"oras.land/oras-go/v2/registry/remote/errcode"
)

// handlePackagePath If provided package is a URL download it to a temp directory.
Expand Down Expand Up @@ -214,29 +212,6 @@ func (p *Packager) handleOciPackage(url string, out string, concurrency int, com
return nil
}

// isManifestUnsupported returns true if the error is an unsupported artifact manifest error.
//
// This function was copied verbatim from https://github.com/oras-project/oras/blob/main/cmd/oras/push.go
func isManifestUnsupported(err error) bool {
var errResp *errcode.ErrorResponse
if !errors.As(err, &errResp) || errResp.StatusCode != http.StatusBadRequest {
return false
}

var errCode errcode.Error
if !errors.As(errResp, &errCode) {
return false
}

// As of November 2022, ECR is known to return UNSUPPORTED error when
// pulling an OCI artifact manifest.
switch errCode.Code {
case errcode.ErrorCodeManifestInvalid, errcode.ErrorCodeUnsupported:
return true
}
return false
}

func getOCIPackageSize(src *utils.OrasRemote) (int64, error) {
var total int64

Expand Down Expand Up @@ -272,21 +247,11 @@ func getLayers(dst *utils.OrasRemote) ([]ocispec.Descriptor, error) {
return nil, err
}
manifest := ocispec.Manifest{}
artifact := ocispec.Artifact{}
var layers []ocispec.Descriptor
// if the manifest is an artifact, unmarshal it as an artifact
// otherwise, unmarshal it as a manifest
if descriptor.MediaType == ocispec.MediaTypeArtifactManifest {
if err = json.Unmarshal(pulled, &artifact); err != nil {
return nil, err
}
layers = artifact.Blobs
} else {
if err = json.Unmarshal(pulled, &manifest); err != nil {
return nil, err
}
layers = manifest.Layers

if err = json.Unmarshal(pulled, &manifest); err != nil {
return nil, err
}
layers := manifest.Layers

return layers, nil
}
Expand Down
48 changes: 3 additions & 45 deletions src/pkg/packager/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,31 +145,11 @@ func (p *Packager) publish(ref registry.Reference) error {
copyOpts.OnCopySkipped = utils.PrintLayerExists
copyOpts.PostCopy = utils.PrintLayerExists

var root ocispec.Descriptor

// try to push an ArtifactManifest first
// not every registry supports ArtifactManifests, so fallback to an ImageManifest if the push fails
// see https://oras.land/implementors/#registries-supporting-oci-artifacts
root, err = p.publishArtifact(dst, src, descs, copyOpts)
root, err := p.publishImage(dst, src, descs, copyOpts)
if err != nil {
// reset the progress bar between attempts
dst.Transport.ProgressBar.Stop()

// log the error, the expected error is a 400 manifest invalid
message.Debug("ArtifactManifest push failed with the following error, falling back to an ImageManifest push:", err)
// also warn the user that the push failed
message.Warn("ArtifactManifest push failed, falling back to an ImageManifest push")

// if the error returned from the push is not an expected error, then return the error
if !isManifestUnsupported(err) {
return err
}
// fallback to an ImageManifest push
root, err = p.publishImage(dst, src, descs, copyOpts)
if err != nil {
return err
}
return err
}

dst.Transport.ProgressBar.Successf("Published %s [%s]", ref, root.MediaType)
fmt.Println()
if strings.HasSuffix(ref.Reference, "-skeleton") {
Expand Down Expand Up @@ -201,28 +181,6 @@ func (p *Packager) publish(ref registry.Reference) error {
return nil
}

func (p *Packager) publishArtifact(dst *utils.OrasRemote, src *file.Store, descs []ocispec.Descriptor, copyOpts oras.CopyOptions) (root ocispec.Descriptor, err error) {
var total int64
for _, desc := range descs {
total += desc.Size
}
packOpts := p.cfg.PublishOpts.PackOptions

// first attempt to do a ArtifactManifest push
root, err = p.pack(dst.Context, ocispec.MediaTypeArtifactManifest, descs, src, packOpts)
if err != nil {
return root, err
}
total += root.Size

dst.Transport.ProgressBar = message.NewProgressBar(total, fmt.Sprintf("Publishing %s:%s", dst.Reference.Repository, dst.Reference.Reference))
defer dst.Transport.ProgressBar.Stop()

// attempt to push the artifact manifest
_, err = oras.Copy(dst.Context, src, root.Digest.String(), dst, dst.Reference.Reference, copyOpts)
return root, err
}

func (p *Packager) publishImage(dst *utils.OrasRemote, src *file.Store, descs []ocispec.Descriptor, copyOpts oras.CopyOptions) (root ocispec.Descriptor, err error) {
var total int64
for _, desc := range descs {
Expand Down

0 comments on commit ceb6da1

Please sign in to comment.