Unify parameters for functions in Processor interface

Signed-off-by: Yiyang Huang <huangyiyang@caicloud.io>
This commit is contained in:
Yiyang Huang 2020-07-08 15:57:58 +08:00 committed by Yiyang Huang
parent 72cf16a99e
commit b98b8b9159
19 changed files with 52 additions and 50 deletions

View File

@ -62,17 +62,17 @@ func (a *abstractor) AbstractMetadata(ctx context.Context, artifact *artifact.Ar
case "", "application/json", schema1.MediaTypeSignedManifest:
a.abstractManifestV1Metadata(artifact)
case v1.MediaTypeImageManifest, schema2.MediaTypeManifest:
if err = a.abstractManifestV2Metadata(content, artifact); err != nil {
if err = a.abstractManifestV2Metadata(artifact, content); err != nil {
return err
}
case v1.MediaTypeImageIndex, manifestlist.MediaTypeManifestList:
if err = a.abstractIndexMetadata(ctx, content, artifact); err != nil {
if err = a.abstractIndexMetadata(ctx, artifact, content); err != nil {
return err
}
default:
return fmt.Errorf("unsupported manifest media type: %s", artifact.ManifestMediaType)
}
return processor.Get(artifact.MediaType).AbstractMetadata(ctx, content, artifact)
return processor.Get(artifact.MediaType).AbstractMetadata(ctx, artifact, content)
}
// the artifact is enveloped by docker manifest v1
@ -86,7 +86,7 @@ func (a *abstractor) abstractManifestV1Metadata(artifact *artifact.Artifact) {
}
// the artifact is enveloped by OCI manifest or docker manifest v2
func (a *abstractor) abstractManifestV2Metadata(content []byte, artifact *artifact.Artifact) error {
func (a *abstractor) abstractManifestV2Metadata(artifact *artifact.Artifact, content []byte) error {
manifest := &v1.Manifest{}
if err := json.Unmarshal(content, manifest); err != nil {
return err
@ -104,7 +104,7 @@ func (a *abstractor) abstractManifestV2Metadata(content []byte, artifact *artifa
}
// the artifact is enveloped by OCI index or docker manifest list
func (a *abstractor) abstractIndexMetadata(ctx context.Context, content []byte, art *artifact.Artifact) error {
func (a *abstractor) abstractIndexMetadata(ctx context.Context, art *artifact.Artifact, content []byte) error {
// the identity of index is still in progress, we use the manifest mediaType
// as the media type of artifact
art.MediaType = art.ManifestMediaType

View File

@ -185,7 +185,7 @@ func (c *controller) ensureArtifact(ctx context.Context, repository, digest stri
}
// populate the artifact type
artifact.Type = processor.Get(artifact.MediaType).GetArtifactType()
artifact.Type = processor.Get(artifact.MediaType).GetArtifactType(ctx, artifact)
// create it
// use orm.WithTransaction here to avoid the issue:
@ -600,7 +600,7 @@ func (c *controller) populateLabels(ctx context.Context, art *Artifact) {
}
func (c *controller) populateAdditionLinks(ctx context.Context, artifact *Artifact) {
types := processor.Get(artifact.MediaType).ListAdditionTypes()
types := processor.Get(artifact.MediaType).ListAdditionTypes(ctx, &artifact.Artifact)
if len(types) > 0 {
version := lib.GetAPIVersion(ctx)
for _, t := range types {

View File

@ -36,7 +36,7 @@ type IndexProcessor struct {
}
// AbstractMetadata abstracts metadata of artifact
func (m *IndexProcessor) AbstractMetadata(ctx context.Context, content []byte, artifact *artifact.Artifact) error {
func (m *IndexProcessor) AbstractMetadata(ctx context.Context, artifact *artifact.Artifact, content []byte) error {
return nil
}
@ -47,11 +47,11 @@ func (m *IndexProcessor) AbstractAddition(ctx context.Context, artifact *artifac
}
// GetArtifactType returns the artifact type
func (m *IndexProcessor) GetArtifactType() string {
func (m *IndexProcessor) GetArtifactType(ctx context.Context, artifact *artifact.Artifact) string {
return ""
}
// ListAdditionTypes returns the supported addition types
func (m *IndexProcessor) ListAdditionTypes() []string {
func (m *IndexProcessor) ListAdditionTypes(ctx context.Context, artifact *artifact.Artifact) []string {
return nil
}

View File

@ -40,7 +40,7 @@ type ManifestProcessor struct {
}
// AbstractMetadata abstracts metadata of artifact
func (m *ManifestProcessor) AbstractMetadata(ctx context.Context, content []byte, artifact *artifact.Artifact) error {
func (m *ManifestProcessor) AbstractMetadata(ctx context.Context, artifact *artifact.Artifact, content []byte) error {
// get manifest
manifest := &v1.Manifest{}
if err := json.Unmarshal(content, manifest); err != nil {
@ -79,11 +79,11 @@ func (m *ManifestProcessor) AbstractAddition(ctx context.Context, artifact *arti
}
// GetArtifactType returns the artifact type
func (m *ManifestProcessor) GetArtifactType() string {
func (m *ManifestProcessor) GetArtifactType(ctx context.Context, artifact *artifact.Artifact) string {
return ""
}
// ListAdditionTypes returns the supported addition types
func (m *ManifestProcessor) ListAdditionTypes() []string {
func (m *ManifestProcessor) ListAdditionTypes(ctx context.Context, artifact *artifact.Artifact) []string {
return nil
}

View File

@ -138,7 +138,7 @@ func (m *manifestTestSuite) TestAbstractMetadata() {
art := &artifact.Artifact{}
m.regCli.On("PullBlob").Return(0, ioutil.NopCloser(strings.NewReader(config)), nil)
m.processor.AbstractMetadata(nil, []byte(manifest), art)
m.processor.AbstractMetadata(nil, art, []byte(manifest))
m.Len(art.ExtraAttrs, 9)
// reset the mock
@ -148,7 +148,7 @@ func (m *manifestTestSuite) TestAbstractMetadata() {
m.processor.properties = []string{"os"}
art = &artifact.Artifact{}
m.regCli.On("PullBlob").Return(0, ioutil.NopCloser(strings.NewReader(config)), nil)
m.processor.AbstractMetadata(nil, []byte(manifest), art)
m.processor.AbstractMetadata(nil, art, []byte(manifest))
m.Require().Len(art.ExtraAttrs, 1)
m.Equal("linux", art.ExtraAttrs["os"])
}

View File

@ -121,10 +121,10 @@ func (p *processor) AbstractAddition(ctx context.Context, artifact *artifact.Art
return nil, nil
}
func (p *processor) GetArtifactType() string {
func (p *processor) GetArtifactType(ctx context.Context, artifact *artifact.Artifact) string {
return ArtifactTypeChart
}
func (p *processor) ListAdditionTypes() []string {
func (p *processor) ListAdditionTypes(ctx context.Context, artifact *artifact.Artifact) []string {
return []string{AdditionTypeValues, AdditionTypeReadme, AdditionTypeDependencies}
}

View File

@ -128,11 +128,11 @@ func (p *processorTestSuite) TestAbstractAddition() {
}
func (p *processorTestSuite) TestGetArtifactType() {
p.Assert().Equal(ArtifactTypeChart, p.processor.GetArtifactType())
p.Assert().Equal(ArtifactTypeChart, p.processor.GetArtifactType(nil, nil))
}
func (p *processorTestSuite) TestListAdditionTypes() {
additions := p.processor.ListAdditionTypes()
additions := p.processor.ListAdditionTypes(nil, nil)
p.EqualValues([]string{AdditionTypeValues, AdditionTypeReadme, AdditionTypeDependencies}, additions)
}

View File

@ -45,7 +45,7 @@ type processor struct {
manifestProcessor *base.ManifestProcessor
}
func (p *processor) AbstractMetadata(ctx context.Context, manifest []byte, art *artifact.Artifact) error {
func (p *processor) AbstractMetadata(ctx context.Context, art *artifact.Artifact, manifest []byte, ) error {
cfgManiDgt := ""
// try to get the digest of the manifest that the config layer is referenced by
for _, reference := range art.References {
@ -69,9 +69,9 @@ func (p *processor) AbstractMetadata(ctx context.Context, manifest []byte, art *
}
// abstract the metadata from config layer
return p.manifestProcessor.AbstractMetadata(ctx, payload, art)
return p.manifestProcessor.AbstractMetadata(ctx, art, payload)
}
func (p *processor) GetArtifactType() string {
func (p *processor) GetArtifactType(ctx context.Context, artifact *artifact.Artifact) string {
return ArtifactTypeCNAB
}

View File

@ -93,7 +93,7 @@ func (p *processorTestSuite) TestAbstractMetadata() {
p.Require().Nil(err)
p.regCli.On("PullManifest").Return(mani, "", nil)
p.regCli.On("PullBlob").Return(0, ioutil.NopCloser(strings.NewReader(config)), nil)
err = p.processor.AbstractMetadata(nil, nil, art)
err = p.processor.AbstractMetadata(nil, art, nil)
p.Require().Nil(err)
p.Len(art.ExtraAttrs, 7)
p.Equal("0.1.1", art.ExtraAttrs["version"].(string))
@ -101,7 +101,7 @@ func (p *processorTestSuite) TestAbstractMetadata() {
}
func (p *processorTestSuite) TestGetArtifactType() {
p.Assert().Equal(ArtifactTypeCNAB, p.processor.GetArtifactType())
p.Assert().Equal(ArtifactTypeCNAB, p.processor.GetArtifactType(nil, nil))
}
func TestProcessorTestSuite(t *testing.T) {

View File

@ -35,7 +35,7 @@ type defaultProcessor struct {
mediaType string
}
func (d *defaultProcessor) GetArtifactType() string {
func (d *defaultProcessor) GetArtifactType(ctx context.Context, artifact *artifact.Artifact) string {
// try to parse the type from the media type
strs := artifactTypeRegExp.FindStringSubmatch(d.mediaType)
if len(strs) == 2 {
@ -44,10 +44,10 @@ func (d *defaultProcessor) GetArtifactType() string {
// can not get the artifact type from the media type, return unknown
return ArtifactTypeUnknown
}
func (d *defaultProcessor) ListAdditionTypes() []string {
func (d *defaultProcessor) ListAdditionTypes(ctx context.Context, artifact *artifact.Artifact) []string {
return nil
}
func (d *defaultProcessor) AbstractMetadata(ctx context.Context, manifest []byte, artifact *artifact.Artifact) error {
func (d *defaultProcessor) AbstractMetadata(ctx context.Context, artifact *artifact.Artifact, manifest []byte) error {
// do nothing currently
// we can extend this function to abstract the metadata in the future if needed
return nil

View File

@ -26,27 +26,27 @@ type defaultProcessorTestSuite struct {
func (d *defaultProcessorTestSuite) TestGetArtifactType() {
mediaType := ""
processor := &defaultProcessor{mediaType: mediaType}
typee := processor.GetArtifactType()
typee := processor.GetArtifactType(nil, nil)
d.Equal(ArtifactTypeUnknown, typee)
mediaType = "unknown"
processor = &defaultProcessor{mediaType: mediaType}
typee = processor.GetArtifactType()
typee = processor.GetArtifactType(nil, nil)
d.Equal(ArtifactTypeUnknown, typee)
mediaType = "application/vnd.oci.image.config.v1+json"
processor = &defaultProcessor{mediaType: mediaType}
typee = processor.GetArtifactType()
typee = processor.GetArtifactType(nil, nil)
d.Equal("IMAGE", typee)
mediaType = "application/vnd.cncf.helm.chart.config.v1+json"
processor = &defaultProcessor{mediaType: mediaType}
typee = processor.GetArtifactType()
typee = processor.GetArtifactType(nil, nil)
d.Equal("HELM.CHART", typee)
mediaType = "application/vnd.sylabs.sif.config.v1+json"
processor = &defaultProcessor{mediaType: mediaType}
typee = processor.GetArtifactType()
typee = processor.GetArtifactType(nil, nil)
d.Equal("SIF", typee)
}

View File

@ -15,10 +15,12 @@
package image
import (
"context"
"github.com/docker/distribution/manifest/manifestlist"
"github.com/goharbor/harbor/src/controller/artifact/processor"
"github.com/goharbor/harbor/src/controller/artifact/processor/base"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/pkg/artifact"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
)
@ -40,6 +42,6 @@ type indexProcessor struct {
*base.IndexProcessor
}
func (i *indexProcessor) GetArtifactType() string {
func (i *indexProcessor) GetArtifactType(ctx context.Context, artifact *artifact.Artifact) string {
return ArtifactTypeImage
}

View File

@ -29,7 +29,7 @@ func (i *indexProcessTestSuite) SetupTest() {
}
func (i *indexProcessTestSuite) TestGetArtifactType() {
i.Assert().Equal(ArtifactTypeImage, i.processor.GetArtifactType())
i.Assert().Equal(ArtifactTypeImage, i.processor.GetArtifactType(nil, nil))
}
func TestIndexProcessTestSuite(t *testing.T) {

View File

@ -36,7 +36,7 @@ func init() {
type manifestV1Processor struct {
}
func (m *manifestV1Processor) AbstractMetadata(ctx context.Context, manifest []byte, artifact *artifact.Artifact) error {
func (m *manifestV1Processor) AbstractMetadata(ctx context.Context, artifact *artifact.Artifact, manifest []byte) error {
mani := &schema1.Manifest{}
if err := json.Unmarshal(manifest, mani); err != nil {
return err
@ -53,10 +53,10 @@ func (m *manifestV1Processor) AbstractAddition(ctx context.Context, artifact *ar
WithMessage("addition %s isn't supported for %s(manifest version 1)", addition, ArtifactTypeImage)
}
func (m *manifestV1Processor) GetArtifactType() string {
func (m *manifestV1Processor) GetArtifactType(ctx context.Context, artifact *artifact.Artifact) string {
return ArtifactTypeImage
}
func (m *manifestV1Processor) ListAdditionTypes() []string {
func (m *manifestV1Processor) ListAdditionTypes(ctx context.Context, artifact *artifact.Artifact) []string {
return nil
}

View File

@ -78,7 +78,7 @@ func (m *manifestV1ProcessorTestSuite) TestAbstractMetadata() {
}
`
artifact := &artifact.Artifact{}
err := m.processor.AbstractMetadata(nil, []byte(manifest), artifact)
err := m.processor.AbstractMetadata(nil, artifact, []byte(manifest))
m.Require().Nil(err)
m.Assert().Equal("amd64", artifact.ExtraAttrs["architecture"].(string))
}
@ -89,11 +89,11 @@ func (m *manifestV1ProcessorTestSuite) TestAbstractAddition() {
}
func (m *manifestV1ProcessorTestSuite) TestGetArtifactType() {
m.Assert().Equal(ArtifactTypeImage, m.processor.GetArtifactType())
m.Assert().Equal(ArtifactTypeImage, m.processor.GetArtifactType(nil, nil))
}
func (m *manifestV1ProcessorTestSuite) TestListAdditionTypes() {
additions := m.processor.ListAdditionTypes()
additions := m.processor.ListAdditionTypes(nil, nil)
m.Len(additions, 0)
}

View File

@ -86,10 +86,10 @@ func (m *manifestV2Processor) AbstractAddition(ctx context.Context, artifact *ar
}, nil
}
func (m *manifestV2Processor) GetArtifactType() string {
func (m *manifestV2Processor) GetArtifactType(ctx context.Context, artifact *artifact.Artifact) string {
return ArtifactTypeImage
}
func (m *manifestV2Processor) ListAdditionTypes() []string {
func (m *manifestV2Processor) ListAdditionTypes(ctx context.Context, artifact *artifact.Artifact) []string {
return []string{AdditionTypeBuildHistory}
}

View File

@ -153,11 +153,11 @@ func (m *manifestV2ProcessorTestSuite) TestAbstractAddition() {
}
func (m *manifestV2ProcessorTestSuite) TestGetArtifactType() {
m.Assert().Equal(ArtifactTypeImage, m.processor.GetArtifactType())
m.Assert().Equal(ArtifactTypeImage, m.processor.GetArtifactType(nil, nil))
}
func (m *manifestV2ProcessorTestSuite) TestListAdditionTypes() {
additions := m.processor.ListAdditionTypes()
additions := m.processor.ListAdditionTypes(nil, nil)
m.EqualValues([]string{AdditionTypeBuildHistory}, additions)
}

View File

@ -35,12 +35,12 @@ type Addition struct {
// Processor processes specified artifact
type Processor interface {
// GetArtifactType returns the type of one kind of artifact specified by media type
GetArtifactType() string
GetArtifactType(ctx context.Context, artifact *artifact.Artifact) string
// ListAdditionTypes returns the supported addition types of one kind of artifact specified by media type
ListAdditionTypes() []string
ListAdditionTypes(ctx context.Context, artifact *artifact.Artifact) []string
// AbstractMetadata abstracts the metadata for the specific artifact type into the artifact model,
// the metadata can be got from the manifest or other layers referenced by the manifest.
AbstractMetadata(ctx context.Context, manifest []byte, artifact *artifact.Artifact) error
AbstractMetadata(ctx context.Context, artifact *artifact.Artifact, manifest []byte) error
// AbstractAddition abstracts the addition of the artifact.
// The additions are different for different artifacts:
// build history for image; values.yaml, readme and dependencies for chart, etc

View File

@ -23,13 +23,13 @@ import (
type fakeProcessor struct{}
func (f *fakeProcessor) GetArtifactType() string {
func (f *fakeProcessor) GetArtifactType(ctx context.Context, artifact *artifact.Artifact) string {
return ""
}
func (f *fakeProcessor) ListAdditionTypes() []string {
func (f *fakeProcessor) ListAdditionTypes(ctx context.Context, artifact *artifact.Artifact) []string {
return nil
}
func (f *fakeProcessor) AbstractMetadata(ctx context.Context, manifest []byte, artifact *artifact.Artifact) error {
func (f *fakeProcessor) AbstractMetadata(ctx context.Context, artifact *artifact.Artifact, manifest []byte) error {
return nil
}
func (f *fakeProcessor) AbstractAddition(ctx context.Context, artifact *artifact.Artifact, additionType string) (*Addition, error) {