From 76c04b0219a27fb6b59392056aa2b3a3470cfc49 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Thu, 5 Mar 2020 10:54:45 +0800 Subject: [PATCH] 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 --- .../postgresql/0030_2.0.0_schema.up.sql | 3 ++ .../artifact/abstractor/resolver/cnab/cnab.go | 7 +++- .../abstractor/resolver/image/index.go | 7 +++- src/pkg/artifact/dao/model.go | 11 ++++-- src/pkg/artifact/manager.go | 5 --- src/pkg/artifact/manager_test.go | 8 ---- src/pkg/artifact/model.go | 38 +++++++++++++++++-- 7 files changed, 54 insertions(+), 25 deletions(-) diff --git a/make/migrations/postgresql/0030_2.0.0_schema.up.sql b/make/migrations/postgresql/0030_2.0.0_schema.up.sql index cb034c713..aacc89a07 100644 --- a/make/migrations/postgresql/0030_2.0.0_schema.up.sql +++ b/make/migrations/postgresql/0030_2.0.0_schema.up.sql @@ -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) diff --git a/src/api/artifact/abstractor/resolver/cnab/cnab.go b/src/api/artifact/abstractor/resolver/cnab/cnab.go index bb61abeca..638d3b28c 100644 --- a/src/api/artifact/abstractor/resolver/cnab/cnab.go +++ b/src/api/artifact/abstractor/resolver/cnab/cnab.go @@ -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 && diff --git a/src/api/artifact/abstractor/resolver/image/index.go b/src/api/artifact/abstractor/resolver/image/index.go index e8f7cff97..4b5d92f17 100644 --- a/src/api/artifact/abstractor/resolver/image/index.go +++ b/src/api/artifact/abstractor/resolver/image/index.go @@ -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 diff --git a/src/pkg/artifact/dao/model.go b/src/pkg/artifact/dao/model.go index 38736b391..2ce24dc7e 100644 --- a/src/pkg/artifact/dao/model.go +++ b/src/pkg/artifact/dao/model.go @@ -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 diff --git a/src/pkg/artifact/manager.go b/src/pkg/artifact/manager.go index 76a74c7f5..b1b3eea48 100644 --- a/src/pkg/artifact/manager.go +++ b/src/pkg/artifact/manager.go @@ -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 diff --git a/src/pkg/artifact/manager_test.go b/src/pkg/artifact/manager_test.go index e0805f000..53cd9797d 100644 --- a/src/pkg/artifact/manager_test.go +++ b/src/pkg/artifact/manager_test.go @@ -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() { diff --git a/src/pkg/artifact/model.go b/src/pkg/artifact/model.go index 2959e483c..fba655318 100644 --- a/src/pkg/artifact/model.go +++ b/src/pkg/artifact/model.go @@ -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 }