mirror of
https://github.com/project-zot/zot.git
synced 2026-06-16 20:38:08 +08:00
refactor(artifact): remove oci artifact support (#1359)
* refactor(artifact): remove oci artifact support - add header to referrers call to indicated applied artifact type filters Signed-off-by: Laurentiu Niculae <niculae.laurentiu1@gmail.com> * feat(gc): simplify gc logic to increase coverage Signed-off-by: Laurentiu Niculae <niculae.laurentiu1@gmail.com> --------- Signed-off-by: Laurentiu Niculae <niculae.laurentiu1@gmail.com>
This commit is contained in:
+26
-95
@@ -105,32 +105,6 @@ func (img Image) Digest() (godigest.Digest, error) {
|
||||
return godigest.FromBytes(blob), nil
|
||||
}
|
||||
|
||||
type Artifact struct {
|
||||
Manifest ispec.Artifact
|
||||
Blobs []ArtifactBlobs
|
||||
Reference string
|
||||
}
|
||||
|
||||
func (a Artifact) Digest() (godigest.Digest, error) {
|
||||
blob, err := json.Marshal(a.Manifest)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return godigest.FromBytes(blob), nil
|
||||
}
|
||||
|
||||
func (a Artifact) ArtifactData() (repodb.ArtifactData, error) {
|
||||
blob, err := json.Marshal(a.Manifest)
|
||||
if err != nil {
|
||||
return repodb.ArtifactData{}, err
|
||||
}
|
||||
|
||||
return repodb.ArtifactData{
|
||||
ManifestBlob: blob,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type ArtifactBlobs struct {
|
||||
Blob []byte
|
||||
MediaType string
|
||||
@@ -588,6 +562,7 @@ func GetImageComponents(layerSize int) (ispec.Image, [][]byte, ispec.Manifest, e
|
||||
schemaVersion := 2
|
||||
|
||||
manifest := ispec.Manifest{
|
||||
MediaType: ispec.MediaTypeImageManifest,
|
||||
Versioned: specs.Versioned{
|
||||
SchemaVersion: schemaVersion,
|
||||
},
|
||||
@@ -642,6 +617,7 @@ func GetRandomImageComponents(layerSize int) (ispec.Image, [][]byte, ispec.Manif
|
||||
schemaVersion := 2
|
||||
|
||||
manifest := ispec.Manifest{
|
||||
MediaType: ispec.MediaTypeImageManifest,
|
||||
Versioned: specs.Versioned{
|
||||
SchemaVersion: schemaVersion,
|
||||
},
|
||||
@@ -657,7 +633,6 @@ func GetRandomImageComponents(layerSize int) (ispec.Image, [][]byte, ispec.Manif
|
||||
Size: int64(len(layers[0])),
|
||||
},
|
||||
},
|
||||
MediaType: ispec.MediaTypeImageManifest,
|
||||
}
|
||||
|
||||
return config, layers, manifest, nil
|
||||
@@ -702,6 +677,7 @@ func GetImageComponentsWithConfig(conf ispec.Image) (ispec.Image, [][]byte, ispe
|
||||
schemaVersion := 2
|
||||
|
||||
manifest := ispec.Manifest{
|
||||
MediaType: ispec.MediaTypeImageManifest,
|
||||
Versioned: specs.Versioned{
|
||||
SchemaVersion: schemaVersion,
|
||||
},
|
||||
@@ -760,6 +736,7 @@ func GetImageWithComponents(config ispec.Image, layers [][]byte) (Image, error)
|
||||
const schemaVersion = 2
|
||||
|
||||
manifest := ispec.Manifest{
|
||||
MediaType: ispec.MediaTypeImageManifest,
|
||||
Versioned: specs.Versioned{
|
||||
SchemaVersion: schemaVersion,
|
||||
},
|
||||
@@ -784,49 +761,6 @@ func GetImageWithComponents(config ispec.Image, layers [][]byte) (Image, error)
|
||||
}, nil
|
||||
}
|
||||
|
||||
func GetRandomArtifact(subject *ispec.Descriptor) (Artifact, error) {
|
||||
var randBlob [10]byte
|
||||
|
||||
_, err := rand.Read(randBlob[:])
|
||||
if err != nil {
|
||||
return Artifact{}, err
|
||||
}
|
||||
|
||||
artifactBlobs := []ArtifactBlobs{
|
||||
{
|
||||
Blob: randBlob[:],
|
||||
MediaType: "application/octet-stream",
|
||||
},
|
||||
}
|
||||
|
||||
blobsDescriptors := make([]ispec.Descriptor, 0, len(artifactBlobs))
|
||||
|
||||
for _, artifactBlob := range artifactBlobs {
|
||||
blobsDescriptors = append(blobsDescriptors, ispec.Descriptor{
|
||||
Digest: godigest.FromBytes(artifactBlob.Blob),
|
||||
MediaType: artifactBlob.MediaType,
|
||||
Size: int64(len(artifactBlob.Blob)),
|
||||
})
|
||||
}
|
||||
|
||||
artifactManifest := ispec.Artifact{
|
||||
MediaType: ispec.MediaTypeArtifactManifest,
|
||||
Blobs: blobsDescriptors,
|
||||
Subject: subject,
|
||||
}
|
||||
|
||||
artifactManifestBlob, err := json.Marshal(artifactManifest)
|
||||
if err != nil {
|
||||
return Artifact{}, err
|
||||
}
|
||||
|
||||
return Artifact{
|
||||
Manifest: artifactManifest,
|
||||
Blobs: artifactBlobs,
|
||||
Reference: godigest.FromBytes(artifactManifestBlob).String(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func GetCosignSignatureTagForManifest(manifest ispec.Manifest) (string, error) {
|
||||
manifestBlob, err := json.Marshal(manifest)
|
||||
if err != nil {
|
||||
@@ -906,6 +840,11 @@ func UploadImage(img Image, baseURL, repo string) error {
|
||||
|
||||
cdigest := godigest.FromBytes(cblob)
|
||||
|
||||
if img.Manifest.Config.MediaType == ispec.MediaTypeScratch {
|
||||
cblob = ispec.ScratchDescriptor.Data
|
||||
cdigest = ispec.ScratchDescriptor.Digest
|
||||
}
|
||||
|
||||
resp, err := resty.R().
|
||||
Post(baseURL + "/v2/" + repo + "/blobs/uploads/")
|
||||
if err = Error(err); err != nil {
|
||||
@@ -940,7 +879,7 @@ func UploadImage(img Image, baseURL, repo string) error {
|
||||
}
|
||||
|
||||
resp, err = resty.R().
|
||||
SetHeader("Content-type", "application/vnd.oci.image.manifest.v1+json").
|
||||
SetHeader("Content-type", ispec.MediaTypeImageManifest).
|
||||
SetBody(manifestBlob).
|
||||
Put(baseURL + "/v2/" + repo + "/manifests/" + img.Reference)
|
||||
|
||||
@@ -966,27 +905,6 @@ func DeleteImage(repo, reference, baseURL string) (int, error) {
|
||||
return resp.StatusCode(), err
|
||||
}
|
||||
|
||||
// UploadArtifactManifest is used in tests where we don't need to upload the blobs of the artifact.
|
||||
func UploadArtifactManifest(artifactManifest *ispec.Artifact, ref *string, baseURL, repo string) error {
|
||||
// put manifest
|
||||
artifactManifestBlob, err := json.Marshal(artifactManifest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
reference := godigest.FromBytes(artifactManifestBlob).String()
|
||||
|
||||
if ref != nil {
|
||||
reference = *ref
|
||||
}
|
||||
|
||||
_, err = resty.R().
|
||||
SetHeader("Content-type", ispec.MediaTypeArtifactManifest).
|
||||
SetBody(artifactManifestBlob).
|
||||
Put(baseURL + "/v2/" + repo + "/manifests/" + reference)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func UploadBlob(baseURL, repo string, blob []byte, artifactBlobMediaType string) error {
|
||||
resp, err := resty.R().Post(baseURL + "/v2/" + repo + "/blobs/uploads/")
|
||||
if err != nil {
|
||||
@@ -1252,7 +1170,11 @@ func SignWithNotation(keyName string, reference string, tdir string) error {
|
||||
PlainHTTP: plainHTTP,
|
||||
}
|
||||
|
||||
sigRepo := notreg.NewRepository(remoteRepo)
|
||||
repositoryOpts := notreg.RepositoryOptions{
|
||||
OCIImageManifest: true,
|
||||
}
|
||||
|
||||
sigRepo := notreg.NewRepositoryWithOptions(remoteRepo, repositoryOpts)
|
||||
|
||||
sigOpts := notation.RemoteSignOptions{
|
||||
SignOptions: notation.SignOptions{
|
||||
@@ -1334,7 +1256,11 @@ func VerifyWithNotation(reference string, tdir string) error {
|
||||
PlainHTTP: plainHTTP,
|
||||
}
|
||||
|
||||
repo := notreg.NewRepository(remoteRepo)
|
||||
repositoryOpts := notreg.RepositoryOptions{
|
||||
OCIImageManifest: true,
|
||||
}
|
||||
|
||||
repo := notreg.NewRepositoryWithOptions(remoteRepo, repositoryOpts)
|
||||
|
||||
manifestDesc, err := repo.Resolve(ctx, ref.Reference)
|
||||
if err != nil {
|
||||
@@ -1357,7 +1283,7 @@ func VerifyWithNotation(reference string, tdir string) error {
|
||||
PlainHTTP: plainHTTP,
|
||||
}
|
||||
|
||||
repo = notreg.NewRepository(remoteRepo)
|
||||
repo = notreg.NewRepositoryWithOptions(remoteRepo, repositoryOpts)
|
||||
|
||||
configs := map[string]string{}
|
||||
|
||||
@@ -1587,6 +1513,11 @@ func UploadImageWithBasicAuth(img Image, baseURL, repo, user, password string) e
|
||||
|
||||
cdigest := godigest.FromBytes(cblob)
|
||||
|
||||
if img.Manifest.Config.MediaType == ispec.MediaTypeScratch {
|
||||
cblob = ispec.ScratchDescriptor.Data
|
||||
cdigest = ispec.ScratchDescriptor.Digest
|
||||
}
|
||||
|
||||
resp, err := resty.R().
|
||||
SetBasicAuth(user, password).
|
||||
Post(baseURL + "/v2/" + repo + "/blobs/uploads/")
|
||||
|
||||
@@ -234,18 +234,6 @@ func TestControllerManager(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestUploadArtifact(t *testing.T) {
|
||||
Convey("Put request results in an error", t, func() {
|
||||
port := test.GetFreePort()
|
||||
baseURL := test.GetBaseURL(port)
|
||||
|
||||
artifact := ispec.Artifact{}
|
||||
|
||||
err := test.UploadArtifactManifest(&artifact, nil, baseURL, "test")
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestUploadBlob(t *testing.T) {
|
||||
Convey("Post request results in an error", t, func() {
|
||||
port := test.GetFreePort()
|
||||
|
||||
@@ -44,10 +44,6 @@ type RepoDBMock struct {
|
||||
|
||||
GetIndexDataFn func(indexDigest godigest.Digest) (repodb.IndexData, error)
|
||||
|
||||
SetArtifactDataFn func(digest godigest.Digest, artifactData repodb.ArtifactData) error
|
||||
|
||||
GetArtifactDataFn func(artifactDigest godigest.Digest) (repodb.ArtifactData, error)
|
||||
|
||||
SetReferrerFn func(repo string, referredDigest godigest.Digest, referrer repodb.ReferrerInfo) error
|
||||
|
||||
DeleteReferrerFn func(repo string, referredDigest godigest.Digest, referrerDigest godigest.Digest) error
|
||||
@@ -348,22 +344,6 @@ func (sdm RepoDBMock) PatchDB() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sdm RepoDBMock) SetArtifactData(digest godigest.Digest, artifactData repodb.ArtifactData) error {
|
||||
if sdm.SetArtifactDataFn != nil {
|
||||
return sdm.SetArtifactDataFn(digest, artifactData)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sdm RepoDBMock) GetArtifactData(artifactDigest godigest.Digest) (repodb.ArtifactData, error) {
|
||||
if sdm.GetArtifactDataFn != nil {
|
||||
return sdm.GetArtifactDataFn(artifactDigest)
|
||||
}
|
||||
|
||||
return repodb.ArtifactData{}, nil
|
||||
}
|
||||
|
||||
func (sdm RepoDBMock) SetReferrer(repo string, referredDigest godigest.Digest, referrer repodb.ReferrerInfo) error {
|
||||
if sdm.SetReferrerFn != nil {
|
||||
return sdm.SetReferrerFn(repo, referredDigest, referrer)
|
||||
|
||||
@@ -376,7 +376,7 @@ func TestExtractImageDetails(t *testing.T) {
|
||||
|
||||
olu := ocilayout.NewBaseOciLayoutUtils(storeController, testLogger)
|
||||
resDigest, resManifest, resIspecImage, resErr := olu.ExtractImageDetails("zot-test", "latest", testLogger)
|
||||
So(string(resDigest), ShouldContainSubstring, "sha256:c52f15d2d4")
|
||||
So(string(resDigest), ShouldContainSubstring, "sha256:8492645f16")
|
||||
So(resManifest.Config.Digest.String(), ShouldContainSubstring, configDigest.Encoded())
|
||||
|
||||
So(resIspecImage.Architecture, ShouldContainSubstring, "amd64")
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
type RouteHandler struct {
|
||||
Route string
|
||||
// HandlerFunc is the HTTP handler function that receives a writer for output and an HTTP request as input.
|
||||
HandlerFunc http.HandlerFunc
|
||||
// AllowedMethods specifies the HTTP methods allowed for the current route.
|
||||
AllowedMethods []string
|
||||
}
|
||||
|
||||
// Routes is a map that associates HTTP paths to their corresponding HTTP handlers.
|
||||
type HTTPRoutes []RouteHandler
|
||||
|
||||
func StartTestHTTPServer(routes HTTPRoutes, port string) *http.Server {
|
||||
baseURL := GetBaseURL(port)
|
||||
mux := mux.NewRouter()
|
||||
|
||||
mux.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
|
||||
_, err := w.Write([]byte("{}"))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}).Methods(http.MethodGet)
|
||||
|
||||
for _, routeHandler := range routes {
|
||||
mux.HandleFunc(routeHandler.Route, routeHandler.HandlerFunc).Methods(routeHandler.AllowedMethods...)
|
||||
}
|
||||
|
||||
server := &http.Server{ //nolint:gosec
|
||||
Addr: fmt.Sprintf(":%s", port),
|
||||
Handler: mux,
|
||||
}
|
||||
|
||||
go func() {
|
||||
if err := server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
WaitTillServerReady(baseURL + "/test")
|
||||
|
||||
return server
|
||||
}
|
||||
Reference in New Issue
Block a user