update code arrording to the review comments

Signed-off-by: wang yan <wangyan@vmware.com>
This commit is contained in:
wang yan 2020-07-02 13:30:59 +08:00
parent 67be511a85
commit 7d1507feaa
3 changed files with 41 additions and 57 deletions

View File

@ -15,13 +15,9 @@
package blob
import (
"fmt"
"github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/log"
blob_models "github.com/goharbor/harbor/src/pkg/blob/models"
"github.com/goharbor/harbor/src/pkg/distribution"
"github.com/goharbor/harbor/src/server/middleware"
"github.com/goharbor/harbor/src/server/middleware/requestid"
"net/http"
"strconv"
)
@ -34,35 +30,9 @@ import (
func PutBlobUploadMiddleware() func(http.Handler) http.Handler {
before := middleware.BeforeRequest(func(r *http.Request) error {
ctx := r.Context()
logger := log.G(ctx)
v := r.URL.Query()
digest := v.Get("digest")
// digest empty is handled by the blob controller GET method
bb, err := blobController.Get(r.Context(), digest)
if err != nil {
if errors.IsNotFoundErr(err) {
return nil
}
return err
}
switch bb.Status {
case blob_models.StatusNone, blob_models.StatusDelete, blob_models.StatusDeleteFailed:
err := blobController.Touch(r.Context(), bb)
if err != nil {
logger.Errorf("failed to update blob: %s status to StatusNone, error:%v", bb.Digest, err)
return errors.Wrapf(err, fmt.Sprintf("the request id is: %s", r.Header.Get(requestid.HeaderXRequestID)))
}
case blob_models.StatusDeleting:
logger.Warningf(fmt.Sprintf("the asking blob is in GC, mark it as non existing, request id: %s", r.Header.Get(requestid.HeaderXRequestID)))
return errors.New(nil).WithMessage(fmt.Sprintf("the asking blob is in GC, mark it as non existing, request id: %s", r.Header.Get(requestid.HeaderXRequestID))).WithCode(errors.NotFoundCode)
default:
return nil
}
return nil
return probeBlob(r, digest)
})
after := middleware.AfterResponse(func(w http.ResponseWriter, r *http.Request, statusCode int) error {

View File

@ -15,10 +15,7 @@
package blob
import (
"fmt"
"github.com/goharbor/harbor/src/lib/errors"
blob_models "github.com/goharbor/harbor/src/pkg/blob/models"
"github.com/goharbor/harbor/src/server/middleware/requestid"
"io/ioutil"
"net/http"
@ -47,29 +44,7 @@ func PutManifestMiddleware() func(http.Handler) http.Handler {
return errors.Wrapf(err, "unmarshal manifest failed").WithCode(errors.MANIFESTINVALID)
}
// bb here is the actually a manifest, which is also stored as a blob in DB and storage.
bb, err := blobController.Get(r.Context(), descriptor.Digest.String())
if err != nil {
if errors.IsNotFoundErr(err) {
return nil
}
return err
}
switch bb.Status {
case blob_models.StatusNone, blob_models.StatusDelete, blob_models.StatusDeleteFailed:
err := blobController.Touch(r.Context(), bb)
if err != nil {
logger.Errorf("failed to update manifest: %s status to StatusNone, error:%v", bb.Digest, err)
return errors.Wrapf(err, fmt.Sprintf("the request id is: %s", r.Header.Get(requestid.HeaderXRequestID)))
}
case blob_models.StatusDeleting:
logger.Warningf(fmt.Sprintf("the asking manifest is in GC, mark it as non existing, request id: %s", r.Header.Get(requestid.HeaderXRequestID)))
return errors.New(nil).WithMessage(fmt.Sprintf("the asking manifest is in GC, mark it as non existing, request id: %s", r.Header.Get(requestid.HeaderXRequestID))).WithCode(errors.NotFoundCode)
default:
return nil
}
return nil
return probeBlob(r, descriptor.Digest.String())
})
after := middleware.AfterResponse(func(w http.ResponseWriter, r *http.Request, statusCode int) error {

View File

@ -0,0 +1,39 @@
package blob
import (
"fmt"
"github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/pkg/blob/models"
"github.com/goharbor/harbor/src/server/middleware/requestid"
"net/http"
)
// probeBlob handles config/layer and manifest status in the PUT Blob & Manifest middleware, and update the status before it passed into proxy(distribution).
func probeBlob(r *http.Request, digest string) error {
logger := log.G(r.Context())
// digest empty is handled by the blob controller GET method
bb, err := blobController.Get(r.Context(), digest)
if err != nil {
if errors.IsNotFoundErr(err) {
return nil
}
return err
}
switch bb.Status {
case models.StatusNone, models.StatusDelete, models.StatusDeleteFailed:
err := blobController.Touch(r.Context(), bb)
if err != nil {
logger.Errorf("failed to update blob: %s status to StatusNone, error:%v", bb.Digest, err)
return errors.Wrapf(err, fmt.Sprintf("the request id is: %s", r.Header.Get(requestid.HeaderXRequestID)))
}
case models.StatusDeleting:
logger.Warningf(fmt.Sprintf("the asking blob is in GC, mark it as non existing, request id: %s", r.Header.Get(requestid.HeaderXRequestID)))
return errors.New(nil).WithMessage(fmt.Sprintf("the asking blob is in GC, mark it as non existing, request id: %s", r.Header.Get(requestid.HeaderXRequestID))).WithCode(errors.NotFoundCode)
default:
return nil
}
return nil
}