Persistent the URLs and annotations of artifact references in database

Persistent the URLs and annotations of artifact references in database

Signed-off-by: Wenkai Yin <yinw@vmware.com>
This commit is contained in:
Wenkai Yin 2020-03-05 10:54:45 +08:00
parent a5d9a3b65d
commit 76c04b0219
7 changed files with 54 additions and 25 deletions

View File

@ -105,7 +105,10 @@ CREATE TABLE artifact_reference
id SERIAL PRIMARY KEY NOT NULL,
parent_id int NOT NULL,
child_id int NOT NULL,
child_digest varchar(255) NOT NULL ,
platform varchar(255),
urls varchar(1024),
annotations jsonb,
FOREIGN KEY (parent_id) REFERENCES artifact(id),
FOREIGN KEY (child_id) REFERENCES artifact(id),
CONSTRAINT unique_reference UNIQUE (parent_id, child_id)

View File

@ -67,8 +67,11 @@ func (r *resolver) ResolveMetadata(ctx context.Context, manifest []byte, art *ar
return err
}
art.References = append(art.References, &artifact.Reference{
ChildID: ar.ID,
Platform: mani.Platform,
ChildID: ar.ID,
ChildDigest: digest,
Platform: mani.Platform,
URLs: mani.URLs,
Annotations: mani.Annotations,
})
// try to get the digest of the manifest that the config layer is referenced by
if mani.Annotations != nil &&

View File

@ -63,8 +63,11 @@ func (i *indexResolver) ResolveMetadata(ctx context.Context, manifest []byte, ar
return err
}
art.References = append(art.References, &artifact.Reference{
ChildID: ar.ID,
Platform: mani.Platform,
ChildID: ar.ID,
ChildDigest: digest,
Platform: mani.Platform,
URLs: mani.URLs,
Annotations: mani.Annotations,
})
}
return nil

View File

@ -49,10 +49,13 @@ func (a *Artifact) TableName() string {
// ArtifactReference records the child artifact referenced by parent artifact
type ArtifactReference struct {
ID int64 `orm:"pk;auto;column(id)"`
ParentID int64 `orm:"column(parent_id)"`
ChildID int64 `orm:"column(child_id)"`
Platform string `orm:"column(platform)"` // json string
ID int64 `orm:"pk;auto;column(id)"`
ParentID int64 `orm:"column(parent_id)"`
ChildID int64 `orm:"column(child_id)"`
ChildDigest string `orm:"column(child_digest)"`
Platform string `orm:"column(platform)"` // json string
URLs string `orm:"column(urls)"` // json string
Annotations string `orm:"column(annotations);type(jsonb)"`
}
// TableName for artifact reference

View File

@ -135,11 +135,6 @@ func (m *manager) ListReferences(ctx context.Context, query *q.Query) ([]*Refere
for _, reference := range references {
ref := &Reference{}
ref.From(reference)
art, err := m.dao.Get(ctx, reference.ChildID)
if err != nil {
return nil, err
}
ref.ChildDigest = art.Digest
refs = append(refs, ref)
}
return refs, nil

View File

@ -122,9 +122,6 @@ func (m *managerTestSuite) TestAssemble() {
ChildID: 3,
},
}, nil)
m.dao.On("Get").Return(&dao.Artifact{
Digest: "digest",
}, nil)
artifact, err := m.mgr.assemble(nil, art)
m.Require().Nil(err)
m.dao.AssertExpectations(m.T())
@ -244,15 +241,10 @@ func (m *managerTestSuite) TestListReferences() {
ChildID: 2,
},
}, nil)
m.dao.On("Get").Return(&dao.Artifact{
ID: 1,
Digest: "digest",
}, nil)
references, err := m.mgr.ListReferences(nil, nil)
m.Require().Nil(err)
m.Require().Len(references, 1)
m.Equal(int64(1), references[0].ID)
m.Equal("digest", references[0].ChildDigest)
}
func (m *managerTestSuite) TestDeleteReference() {

View File

@ -114,8 +114,10 @@ type Reference struct {
ID int64 `json:"id"`
ParentID int64 `json:"parent_id"`
ChildID int64 `json:"child_id"`
ChildDigest string `json:"child_digest"` // As we only provide the API based on digest rather than ID, the digest of child artifact is needed
ChildDigest string `json:"child_digest"`
Platform *v1.Platform
URLs []string `json:"urls"`
Annotations map[string]string `json:"annotations"`
}
// From converts the data level reference to business level
@ -123,20 +125,34 @@ func (r *Reference) From(ref *dao.ArtifactReference) {
r.ID = ref.ID
r.ParentID = ref.ParentID
r.ChildID = ref.ChildID
r.ChildDigest = ref.ChildDigest
if len(ref.Platform) > 0 {
r.Platform = &v1.Platform{}
if err := json.Unmarshal([]byte(ref.Platform), r.Platform); err != nil {
log.Errorf("failed to unmarshal the platform of reference: %v", err)
}
}
if len(ref.URLs) > 0 {
r.URLs = []string{}
if err := json.Unmarshal([]byte(ref.URLs), &r.URLs); err != nil {
log.Errorf("failed to unmarshal the URLs of reference: %v", err)
}
}
if len(ref.Annotations) > 0 {
r.Annotations = map[string]string{}
if err := json.Unmarshal([]byte(ref.Annotations), &r.Annotations); err != nil {
log.Errorf("failed to unmarshal the annotations of reference: %v", err)
}
}
}
// To converts the reference to data level object
func (r *Reference) To() *dao.ArtifactReference {
ref := &dao.ArtifactReference{
ID: r.ID,
ParentID: r.ParentID,
ChildID: r.ChildID,
ID: r.ID,
ParentID: r.ParentID,
ChildID: r.ChildID,
ChildDigest: r.ChildDigest,
}
if r.Platform != nil {
platform, err := json.Marshal(r.Platform)
@ -145,5 +161,19 @@ func (r *Reference) To() *dao.ArtifactReference {
}
ref.Platform = string(platform)
}
if len(r.URLs) > 0 {
urls, err := json.Marshal(r.URLs)
if err != nil {
log.Errorf("failed to marshal the URLs of reference: %v", err)
}
ref.URLs = string(urls)
}
if len(r.Annotations) > 0 {
annotations, err := json.Marshal(r.Annotations)
if err != nil {
log.Errorf("failed to marshal the annotations of reference: %v", err)
}
ref.Annotations = string(annotations)
}
return ref
}