working commit
This commit is contained in:
+30
-30
@@ -17,7 +17,8 @@ type BlobExistsParams struct {
|
||||
type BlobExistsResult struct {
|
||||
DockerContentDigest string
|
||||
ContentLength string
|
||||
Exists bool
|
||||
ContentType string
|
||||
//Exists bool
|
||||
}
|
||||
|
||||
func (oper *Operator) BlobExists(ctx context.Context, params *BlobExistsParams) (*BlobExistsResult, int, error) {
|
||||
@@ -32,37 +33,30 @@ func (oper *Operator) BlobExists(ctx context.Context, params *BlobExistsParams)
|
||||
err = fmt.Errorf("Empty name")
|
||||
return res, http.StatusBadRequest, err
|
||||
}
|
||||
exists, blobDescr, err := oper.mdb.GetBlobByDigest(ctx, params.Digest)
|
||||
// Check blob descriptor
|
||||
descrExists, blobDescr, err := oper.mdb.GetBlobByNameDigest(ctx, params.Digest, params.Digest)
|
||||
if err != nil {
|
||||
return res, http.StatusInternalServerError, err
|
||||
}
|
||||
if !exists {
|
||||
if !descrExists {
|
||||
return res, http.StatusNotFound, err
|
||||
}
|
||||
// Check blob file
|
||||
blobExists, _, err := oper.store.BlobExists(params.Digest)
|
||||
if err != nil {
|
||||
return res, http.StatusInternalServerError, err
|
||||
}
|
||||
if !blobExists {
|
||||
return res, http.StatusNotFound, err
|
||||
}
|
||||
|
||||
res.ContentLength = strconv.FormatInt(blobDescr.Size, 10)
|
||||
res.DockerContentDigest = params.Digest
|
||||
res.Exists = exists
|
||||
res.DockerContentDigest = blobDescr.Digest
|
||||
res.ContentType = blobDescr.MediaType
|
||||
|
||||
return res, http.StatusOK, err
|
||||
}
|
||||
|
||||
// https://github.com/opencontainers/distribution-spec/blob/v1.1.1/spec.md
|
||||
//
|
||||
// PostUpload
|
||||
//
|
||||
// POST then PUT
|
||||
//
|
||||
// To push a blob monolithically by using a POST request followed by a PUT request, there are two steps:
|
||||
//
|
||||
// - Obtain a session id (upload URL)
|
||||
// - Upload the blob to said URL
|
||||
//
|
||||
// A chunked blob upload is accomplished in three phases:
|
||||
//
|
||||
// - Obtain a session ID (upload URL) (POST)
|
||||
// - Upload the chunks (PATCH)
|
||||
// Close the session (PUT)
|
||||
|
||||
type PostUploadParams struct {
|
||||
Name string
|
||||
Digest string
|
||||
@@ -106,11 +100,6 @@ type PatchUploadResult struct {
|
||||
Range string
|
||||
}
|
||||
|
||||
// The response for each successful chunk upload MUST be 202 Accepted, and MUST have the following headers:
|
||||
//
|
||||
// Location: <location>
|
||||
// Range: 0-<end-of-range>
|
||||
|
||||
func (oper *Operator) PatchUpload(ctx context.Context, params *PatchUploadParams) (*PatchUploadResult, int, error) {
|
||||
var err error
|
||||
res := &PatchUploadResult{}
|
||||
@@ -250,7 +239,11 @@ func (oper *Operator) GetBlob(ctx context.Context, params *GetBlobParams) (*GetB
|
||||
if err != nil {
|
||||
return res, http.StatusInternalServerError, err
|
||||
}
|
||||
if !blobExists {
|
||||
descrExist, blobDescr, err := oper.mdb.GetBlobByNameDigest(ctx, params.Name, params.Digest)
|
||||
if err != nil {
|
||||
return res, http.StatusInternalServerError, err
|
||||
}
|
||||
if !blobExists || !descrExist {
|
||||
return res, http.StatusNotFound, err
|
||||
}
|
||||
|
||||
@@ -259,6 +252,7 @@ func (oper *Operator) GetBlob(ctx context.Context, params *GetBlobParams) (*GetB
|
||||
return res, http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
res.ContentType = blobDescr.MediaType
|
||||
res.ContentLength = strconv.FormatInt(blobSize, 10)
|
||||
res.DockerContentDigest = params.Digest
|
||||
res.ReadCloser = readCloser
|
||||
@@ -284,19 +278,25 @@ func (oper *Operator) DeleteBlob(ctx context.Context, params *DeleteBlobParams)
|
||||
return res, http.StatusBadRequest, err
|
||||
}
|
||||
// Check namespace record
|
||||
exists, _, err := oper.mdb.GetBlobByNameDigest(ctx, params.Name, params.Digest)
|
||||
descrExists, _, err := oper.mdb.GetBlobByNameDigest(ctx, params.Name, params.Digest)
|
||||
if err != nil {
|
||||
return res, http.StatusInternalServerError, err
|
||||
}
|
||||
if !exists {
|
||||
oper.logg.Debugf("Blob %s:%s descr exists %v", params.Name, params.Digest, descrExists)
|
||||
|
||||
if !descrExists {
|
||||
return res, http.StatusNotFound, err
|
||||
}
|
||||
|
||||
err = oper.mdb.DeleteBlobByNameDigest(ctx, params.Name, params.Digest)
|
||||
if err != nil {
|
||||
return res, http.StatusInternalServerError, err
|
||||
}
|
||||
// Removing blob file if usage == 0
|
||||
blobUsage, err := oper.mdb.GetBlobUsage(ctx, params.Digest)
|
||||
|
||||
oper.logg.Debugf("Blob %s have usage %d", params.Digest, blobUsage)
|
||||
|
||||
if err != nil {
|
||||
return res, http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
@@ -154,18 +154,20 @@ func (oper *Operator) PutManifest(ctx context.Context, params *PutManifestParams
|
||||
incomingManifestDescr, incomingLayerDescrs, err := descrsFromManifest(name, reference, incomingManifest, incomingManifestBytes)
|
||||
// Always check layer files for availability
|
||||
var blobError error
|
||||
for _, layer := range incomingLayerDescrs {
|
||||
layerExists, _, err := oper.store.BlobExists(layer.Digest)
|
||||
for _, blobDescr := range incomingLayerDescrs {
|
||||
blobExists, _, err := oper.store.BlobExists(blobDescr.Digest)
|
||||
if err != nil {
|
||||
return res, http.StatusInternalServerError, err
|
||||
}
|
||||
if !layerExists {
|
||||
err := fmt.Errorf("Layer %s not found.", layer.Digest)
|
||||
if !blobExists {
|
||||
oper.logg.Warningf("Found incomleted blob binary for %s:%s", blobDescr.Name, blobDescr.Digest)
|
||||
err := fmt.Errorf("Layer %s not found.", blobDescr.Digest)
|
||||
blobError = errors.Join(blobError, err)
|
||||
}
|
||||
}
|
||||
if blobError != nil {
|
||||
return res, http.StatusInternalServerError, blobError
|
||||
// TODO: more relevant code?
|
||||
return res, http.StatusFailedDependency, blobError
|
||||
}
|
||||
if !manifestExists {
|
||||
// Store manifest and layesrs data
|
||||
@@ -213,6 +215,21 @@ func (oper *Operator) PutManifest(ctx context.Context, params *PutManifestParams
|
||||
}
|
||||
}
|
||||
|
||||
for _, blobDescr := range incomingLayerDescrs {
|
||||
// TODO: move the requests to db layer transaction
|
||||
blobDescrExists, _, err := oper.mdb.GetBlobByNameDigest(ctx, blobDescr.Name, blobDescr.Digest)
|
||||
if err != nil {
|
||||
return res, http.StatusInternalServerError, err
|
||||
}
|
||||
if !blobDescrExists {
|
||||
oper.logg.Warningf("Save incomleted blob descriptor for %s:%s", blobDescr.Name, blobDescr.Digest)
|
||||
err = oper.mdb.InsertBlob(ctx, &blobDescr)
|
||||
if err != nil {
|
||||
return res, http.StatusInternalServerError, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res.Location = fmt.Sprintf(`/v2/%s/manifests/%s`, params.Name, params.Reference)
|
||||
return res, http.StatusCreated, err
|
||||
|
||||
@@ -282,9 +299,21 @@ type DeleteManifestResult struct{}
|
||||
func (oper *Operator) DeleteManifest(ctx context.Context, params *DeleteManifestParams) (*DeleteManifestResult, int, error) {
|
||||
var err error
|
||||
res := &DeleteManifestResult{}
|
||||
manifestDescr := descr.Manifest{}
|
||||
|
||||
if params.Name == "" {
|
||||
err = fmt.Errorf("Empty name")
|
||||
return res, http.StatusBadRequest, err
|
||||
}
|
||||
if params.Reference == "" {
|
||||
err = fmt.Errorf("Empty reference")
|
||||
return res, http.StatusBadRequest, err
|
||||
}
|
||||
|
||||
var exists bool
|
||||
var reference string
|
||||
|
||||
manifestDescr := descr.Manifest{}
|
||||
|
||||
// Check manifest by digest as name
|
||||
if stringLikeSHADigest(params.Reference) {
|
||||
digest := normalizeSHADigest(params.Reference)
|
||||
@@ -354,3 +383,64 @@ func (oper *Operator) deleteManifestObjects(ctx context.Context, name, reference
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
type GetTagsParams struct {
|
||||
Name string
|
||||
}
|
||||
type GetTagsResult struct {
|
||||
TagDescr descr.Tags
|
||||
}
|
||||
|
||||
func (oper *Operator) GetTags(ctx context.Context, params *GetTagsParams) (*GetTagsResult, int, error) {
|
||||
var err error
|
||||
res := &GetTagsResult{
|
||||
TagDescr: descr.Tags{
|
||||
Name: params.Name,
|
||||
Tags: make([]string, 0),
|
||||
},
|
||||
}
|
||||
|
||||
if params.Name == "" {
|
||||
err = fmt.Errorf("Empty name")
|
||||
return res, http.StatusBadRequest, err
|
||||
}
|
||||
manifestDescrs, err := oper.mdb.ListManifestsByName(ctx, params.Name)
|
||||
if err != nil {
|
||||
return res, http.StatusInternalServerError, err
|
||||
}
|
||||
for _, manifestDescr := range manifestDescrs {
|
||||
res.TagDescr.Tags = append(res.TagDescr.Tags, manifestDescr.Reference)
|
||||
}
|
||||
return res, http.StatusOK, err
|
||||
}
|
||||
|
||||
type GetRefererParams struct {
|
||||
Name string
|
||||
Digest string
|
||||
}
|
||||
type GetRefererResult struct {
|
||||
Reference string
|
||||
}
|
||||
|
||||
func (oper *Operator) GetReferer(ctx context.Context, params *GetRefererParams) (*GetRefererResult, int, error) {
|
||||
var err error
|
||||
res := &GetRefererResult{}
|
||||
|
||||
if params.Name == "" {
|
||||
err = fmt.Errorf("Empty name")
|
||||
return res, http.StatusBadRequest, err
|
||||
}
|
||||
if params.Digest == "" {
|
||||
err = fmt.Errorf("Empty digest")
|
||||
return res, http.StatusBadRequest, err
|
||||
}
|
||||
manifests, err := oper.mdb.GetReferer(ctx, params.Name, params.Digest)
|
||||
if err != nil {
|
||||
return res, http.StatusInternalServerError, err
|
||||
}
|
||||
if len(manifests) == 0 {
|
||||
return res, http.StatusNotFound, err
|
||||
}
|
||||
res.Reference = manifests[0].Reference
|
||||
return res, http.StatusOK, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user