67 lines
1.9 KiB
Go
67 lines
1.9 KiB
Go
/*
|
|
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
|
*/
|
|
package imageoper
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
)
|
|
|
|
type CheckImagesParams struct {
|
|
Name string
|
|
}
|
|
type CheckImagesResult struct {
|
|
Repositories []string `json:"repositories"`
|
|
}
|
|
|
|
func (oper *Operator) CheckImages(ctx context.Context, params *CheckImagesParams) (*CheckImagesResult, int, error) {
|
|
var err error
|
|
res := &CheckImagesResult{
|
|
Repositories: make([]string, 0),
|
|
}
|
|
manDescrs, err := oper.mdb.ListAllManifests(ctx)
|
|
if err != nil {
|
|
return res, http.StatusInternalServerError, err
|
|
}
|
|
for _, manDescr := range manDescrs {
|
|
oper.logg.Debugf("Check image %s:%s", manDescr.Name, manDescr.Reference)
|
|
man := &ocispec.Manifest{}
|
|
err = json.Unmarshal([]byte(manDescr.Payload), man)
|
|
if err != nil {
|
|
return res, http.StatusInternalServerError, err
|
|
}
|
|
blobs := make([]ocispec.Descriptor, 0)
|
|
blobs = append(blobs, man.Config)
|
|
blobs = append(blobs, man.Layers...)
|
|
incorrectImage := false
|
|
for _, blob := range blobs {
|
|
oper.logg.Debugf("Check block %s", blob.Digest.String())
|
|
blobExists, blobDescr, err := oper.mdb.GetBlobByNameRefDigest(ctx, manDescr.Name, manDescr.Reference, blob.Digest.String())
|
|
if err != nil {
|
|
return res, http.StatusInternalServerError, err
|
|
}
|
|
blobExists, blobSize, err := oper.store.BlobExists(blobDescr.Name, blobDescr.Digest)
|
|
if err != nil {
|
|
return res, http.StatusInternalServerError, err
|
|
}
|
|
if !blobExists || blobSize != blobDescr.Size {
|
|
incorrectImage = true
|
|
}
|
|
}
|
|
if incorrectImage {
|
|
repo := manDescr.Name + ":" + manDescr.Reference
|
|
oper.logg.Debugf("Delete incomplete image: %s", repo)
|
|
res.Repositories = append(res.Repositories, repo)
|
|
err = oper.deleteManifestObjects(ctx, manDescr.Name, manDescr.Reference)
|
|
if err != nil {
|
|
return res, http.StatusInternalServerError, err
|
|
}
|
|
}
|
|
}
|
|
return res, http.StatusOK, err
|
|
}
|