Skip to content

Commit

Permalink
Fix: registry error while redirect
Browse files Browse the repository at this point in the history
- One of the OCI test cases was failing when we tried using redirect to offload bandwidth to client side
- This PR fixes the issue, all the test cases are now passing

Signed-off-by: guacamole <[email protected]>
  • Loading branch information
guacamole committed Nov 23, 2021
1 parent d814435 commit 67fd1ab
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 61 deletions.
2 changes: 1 addition & 1 deletion registry/v2/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ func (r *registry) debugf(lm logMsg) {

func (r *registry) getHttpUrlFromSkylink(s string) string {
link := strings.TrimPrefix(s, "sia://")
return fmt.Sprintf("https://skyportal.xyz/%s", link)
return fmt.Sprintf("https://siasky.net/%s", link)
}
68 changes: 14 additions & 54 deletions registry/v2/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"strings"
"sync"

skynetsdk "github.com/NebulousLabs/go-skynet/v2"
"github.com/containerish/OpenRegistry/cache"
"github.com/containerish/OpenRegistry/skynet"
fluentbit "github.com/containerish/OpenRegistry/telemetry/fluent-bit"
Expand Down Expand Up @@ -551,52 +550,21 @@ func (r *registry) PullLayer(ctx echo.Context) error {
}
}

if layerRef.Skylink == "" {
detail := map[string]interface{}{
"error": "skylink is empty",
}
e := fmt.Errorf("skylink is empty").Error()
errMsg := r.errorResponse(RegistryErrorCodeBlobUnknown, e, detail)
ctx.Set(types.HttpEndpointErrorKey, errMsg)
return ctx.JSONBlob(http.StatusNotFound, errMsg)
}

resp, err := r.skynet.Download(layerRef.Skylink)
if err != nil {
detail := map[string]interface{}{
"error": err.Error(),
}
errMsg := r.errorResponse(RegistryErrorCodeBlobUnknown, err.Error(), detail)
ctx.Set(types.HttpEndpointErrorKey, errMsg)
return ctx.JSONBlob(http.StatusNotFound, errMsg)
_, ok := r.skynet.Metadata(layerRef.Skylink)
if ok {
url := fmt.Sprintf("https://siasky.net/%s",
strings.Replace(layerRef.Skylink, "sia://", "", 1))
http.Redirect(ctx.Response(), ctx.Request(), url, http.StatusTemporaryRedirect)
return nil
}

bz, err := io.ReadAll(resp)
if err != nil {
errMsg := r.errorResponse(RegistryErrorCodeBlobUploadInvalid, err.Error(), nil)
ctx.Set(types.HttpEndpointErrorKey, errMsg)
return ctx.JSONBlob(http.StatusInternalServerError, errMsg)
detail := map[string]interface{}{
"error": "skylink is empty",
}
_ = resp.Close()

dig := digest(bz)
if dig != clientDigest {
details := map[string]interface{}{
"clientDigest": clientDigest,
"computedDigest": dig,
}
errMsg := r.errorResponse(
RegistryErrorCodeBlobUploadUnknown,
"client digest is different than computed digest",
details,
)
ctx.Set(types.HttpEndpointErrorKey, errMsg)
return ctx.JSONBlob(http.StatusNotFound, errMsg)
}

ctx.Response().Header().Set("Content-Length", fmt.Sprintf("%d", len(bz)))
ctx.Response().Header().Set("Docker-Content-Digest", dig)
return ctx.Blob(http.StatusOK, "application/octet-stream", bz)
e := fmt.Errorf("skylink is empty").Error()
errMsg := r.errorResponse(RegistryErrorCodeBlobUnknown, e, detail)
ctx.Set(types.HttpEndpointErrorKey, errMsg)
return ctx.JSONBlob(http.StatusNotFound, errMsg)
}

//BlobMount to be implemented by guacamole at a later stage
Expand All @@ -614,14 +582,6 @@ func (r *registry) StartUpload(ctx echo.Context) error {
clientDigest := ctx.QueryParam("digest")

if clientDigest != "" {
var headers []skynetsdk.Header

for k, v := range ctx.Request().Header {
headers = append(headers, skynetsdk.Header{
Key: k, Value: v[0],
})
}

bz, err := io.ReadAll(ctx.Request().Body)
if err != nil {
details := map[string]interface{}{
Expand Down Expand Up @@ -655,7 +615,7 @@ func (r *registry) StartUpload(ctx echo.Context) error {
return ctx.JSONBlob(http.StatusBadRequest, errMsg)
}

skylink, err := r.skynet.Upload(namespace, dig, bz, headers...)
skylink, err := r.skynet.Upload(namespace, dig, bz)
if err != nil {
errMsg := r.errorResponse(RegistryErrorCodeBlobUploadInvalid, err.Error(), nil)
lm := logMsg{
Expand Down Expand Up @@ -686,7 +646,7 @@ func (r *registry) StartUpload(ctx echo.Context) error {
return ctx.JSONBlob(http.StatusInternalServerError, errMsg)
}
ctx.Response().Header().Set("Location", link)
return ctx.NoContent(http.StatusAccepted)
return ctx.NoContent(http.StatusCreated)
}

id := uuid.Generate()
Expand Down
7 changes: 1 addition & 6 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@ func RegisterNSRoutes(nsRouter *echo.Group, reg registry.Registry) {
nsRouter.Add(http.MethodHead, ManifestsReference, reg.ManifestExists)

// ALL THE PUT METHODS
// PUT /v2/<name>/blobs/uploads/<uuid>?digest=<digest>
// router.Add(http.MethodPut, "/blobs/uploads/:uuid", reg.MonolithicUpload)

nsRouter.Add(http.MethodPut, BlobsUploads, reg.CompleteUpload)

// PUT /v2/<name>/blobs/uploads/<uuid>?digest=<digest>
nsRouter.Add(http.MethodPut, BlobsUploadsUUID, reg.CompleteUpload)

Expand All @@ -88,7 +83,7 @@ func RegisterNSRoutes(nsRouter *echo.Group, reg registry.Registry) {
// POST /v2/<name>/blobs/uploads/
nsRouter.Add(http.MethodPost, BlobsUploads, reg.StartUpload)

// POST /v2/<name>/blobs/uploads/
// POST /v2/<name>/blobs/uploads/<uuid>
nsRouter.Add(http.MethodPost, BlobsUploadsUUID, reg.PushLayer)

// PATCH
Expand Down

0 comments on commit 67fd1ab

Please sign in to comment.