Skip to content

Commit

Permalink
Merge pull request #147 from hyy0322/feat-support-manifest-annotations
Browse files Browse the repository at this point in the history
feat: support manifest annotations
  • Loading branch information
imeoer authored Jun 12, 2023
2 parents 5a10288 + 1fc3caf commit 147445a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
42 changes: 32 additions & 10 deletions pkg/adapter/annotation/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ import (
"github.com/pkg/errors"

providerContent "github.com/goharbor/acceleration-service/pkg/content"
nydusutils "github.com/goharbor/acceleration-service/pkg/driver/nydus/utils"
"github.com/goharbor/acceleration-service/pkg/utils"
)

type Appended struct {
DriverName string
DriverVersion string
SourceDigest string
DriverName string
DriverVersion string
SourceDigest string
ExtraAnnotations map[string]string
}

const (
Expand All @@ -55,7 +57,13 @@ func annotate(annotations map[string]string, appended Appended) map[string]strin
if appended.DriverVersion != "" {
annotations[AnnotationAccelerationDriverVersion] = appended.DriverVersion
}
annotations[AnnotationAccelerationSourceDigest] = appended.SourceDigest
if appended.SourceDigest != "" {
annotations[AnnotationAccelerationSourceDigest] = appended.SourceDigest
}

for k, v := range appended.ExtraAnnotations {
annotations[k] = v
}

return annotations
}
Expand All @@ -65,7 +73,8 @@ func Append(ctx context.Context, provider providerContent.Provider, desc *ocispe
var err error

switch desc.MediaType {
case ocispec.MediaTypeImageManifest:
// Refer: https://github.com/goharbor/harbor/blob/main/src/controller/artifact/abstractor.go#L75
case ocispec.MediaTypeImageManifest, images.MediaTypeDockerSchema2Manifest:
var manifest ocispec.Manifest
labels, err = utils.ReadJSON(ctx, provider.ContentStore(), &manifest, *desc)
if err != nil {
Expand All @@ -80,23 +89,36 @@ func Append(ctx context.Context, provider providerContent.Provider, desc *ocispe

return desc, nil

case ocispec.MediaTypeImageIndex:
// Refer: https://github.com/goharbor/harbor/blob/main/src/controller/artifact/abstractor.go#L79
case ocispec.MediaTypeImageIndex, images.MediaTypeDockerSchema2ManifestList:
var index ocispec.Index
labels, err = utils.ReadJSON(ctx, provider.ContentStore(), &index, *desc)
if err != nil {
return nil, errors.Wrap(err, "read manifest index")
}

index.Annotations = annotate(index.Annotations, appended)
for idx, maniDesc := range index.Manifests {
if maniDesc.Platform != nil && maniDesc.Platform.OSFeatures[0] == nydusutils.ManifestOSFeatureNydus {
var manifest ocispec.Manifest
labels, err = utils.ReadJSON(ctx, provider.ContentStore(), &manifest, maniDesc)
if err != nil {
return nil, errors.Wrap(err, "read manifest")
}

manifest.Annotations = annotate(maniDesc.Annotations, appended)
newManiDesc, err := utils.WriteJSON(ctx, provider.ContentStore(), manifest, maniDesc, "", labels)
if err != nil {
return nil, errors.Wrap(err, "write manifest")
}
index.Manifests[idx] = *newManiDesc
}
}
desc, err := utils.WriteJSON(ctx, provider.ContentStore(), index, *desc, "", labels)
if err != nil {
return nil, errors.Wrap(err, "write manifest index")
}

return desc, nil

case images.MediaTypeDockerSchema2Manifest, images.MediaTypeDockerSchema2ManifestList:
return nil, fmt.Errorf("docker manifest not support to append annotation")
}

return nil, fmt.Errorf("invalid mediatype %s", desc.MediaType)
Expand Down
9 changes: 9 additions & 0 deletions pkg/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/containerd/containerd/platforms"
"github.com/containerd/containerd/reference/docker"
"github.com/goharbor/acceleration-service/pkg/adapter/annotation"
"github.com/goharbor/acceleration-service/pkg/content"
"github.com/goharbor/acceleration-service/pkg/driver"
"github.com/goharbor/acceleration-service/pkg/errdefs"
Expand Down Expand Up @@ -123,6 +124,14 @@ func (cvt *Converter) Convert(ctx context.Context, source, target string) (*Metr
if err != nil {
return nil, errors.Wrap(err, "convert image")
}
desc, err = annotation.Append(ctx, cvt.provider, desc, annotation.Appended{
DriverName: cvt.driver.Name(),
DriverVersion: cvt.driver.Version(),
ExtraAnnotations: desc.Annotations,
})
if err != nil {
return nil, errors.Wrap(err, "append annotation")
}
metric.ConversionElapsed = time.Since(start)
if err := metric.SetTargetImageSize(ctx, cvt, desc); err != nil {
return nil, errors.Wrap(err, "get target image size")
Expand Down
20 changes: 15 additions & 5 deletions pkg/driver/nydus/nydus.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,27 @@ import (
"os"
"strconv"

"github.com/goharbor/acceleration-service/pkg/adapter/annotation"
accelcontent "github.com/goharbor/acceleration-service/pkg/content"
"github.com/goharbor/acceleration-service/pkg/driver/nydus/parser"
nydusutils "github.com/goharbor/acceleration-service/pkg/driver/nydus/utils"
"github.com/goharbor/acceleration-service/pkg/errdefs"
"github.com/goharbor/acceleration-service/pkg/utils"

"github.com/containerd/containerd/content"
"github.com/containerd/containerd/images/converter"
"github.com/containerd/containerd/platforms"
"github.com/containerd/nydus-snapshotter/pkg/backend"
nydusify "github.com/containerd/nydus-snapshotter/pkg/converter"
"github.com/goharbor/acceleration-service/pkg/driver/nydus/parser"
"github.com/goharbor/acceleration-service/pkg/errdefs"
"github.com/opencontainers/image-spec/specs-go"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

accelcontent "github.com/goharbor/acceleration-service/pkg/content"
nydusutils "github.com/goharbor/acceleration-service/pkg/driver/nydus/utils"
"github.com/goharbor/acceleration-service/pkg/utils"
const (
// annotationNydusFlag is used to indicate a image which is nydus format.
annotationNydusFlag = "containerd.io/snapshot/nydus"
)

type chunkDictInfo struct {
Expand Down Expand Up @@ -177,6 +183,10 @@ func (d *Driver) Convert(ctx context.Context, provider accelcontent.Provider, so
if err != nil {
return nil, err
}
desc.Annotations = map[string]string{
annotationNydusFlag: "true",
annotation.AnnotationAccelerationSourceDigest: image.Digest.String(),
}
if d.mergeManifest {
return d.makeManifestIndex(ctx, provider.ContentStore(), *image, *desc)
}
Expand Down

0 comments on commit 147445a

Please sign in to comment.