Skip to content

Commit

Permalink
Merge pull request #133 from Desiki-high/conveter-metric
Browse files Browse the repository at this point in the history
feat: add metric for converter
  • Loading branch information
imeoer authored May 11, 2023
2 parents 558cf3f + e03ebcd commit 7bc9cab
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 14 deletions.
4 changes: 2 additions & 2 deletions pkg/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ func (adp *LocalAdapter) Convert(ctx context.Context, source string) error {
return errors.Wrap(err, "create lease")
}
defer done(ctx)

return adp.cvt.Convert(ctx, source, target)
_, err = adp.cvt.Convert(ctx, source, target)
return err
}

func (adp *LocalAdapter) Dispatch(ctx context.Context, ref string, sync bool) error {
Expand Down
34 changes: 22 additions & 12 deletions pkg/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,15 @@ func (cvt *Converter) pull(ctx context.Context, source string) error {
return nil
}

func (cvt *Converter) Convert(ctx context.Context, source, target string) error {
func (cvt *Converter) Convert(ctx context.Context, source, target string) (*Metric, error) {
var metric Metric
sourceNamed, err := docker.ParseDockerRef(source)
if err != nil {
return errors.Wrap(err, "parse source reference")
return nil, errors.Wrap(err, "parse source reference")
}
targetNamed, err := docker.ParseDockerRef(target)
if err != nil {
return errors.Wrap(err, "parse target reference")
return nil, errors.Wrap(err, "parse target reference")
}
source = sourceNamed.String()
target = targetNamed.String()
Expand All @@ -104,21 +105,29 @@ func (cvt *Converter) Convert(ctx context.Context, source, target string) error
logger.Infof("try to pull with plain HTTP for %s", source)
cvt.provider.UsePlainHTTP()
if err := cvt.pull(ctx, source); err != nil {
return errors.Wrap(err, "try to pull image")
return nil, errors.Wrap(err, "try to pull image")
}
} else {
return errors.Wrap(err, "pull image")
return nil, errors.Wrap(err, "pull image")
}
}
logger.Infof("pulled image %s, elapse %s", source, time.Since(start))
metric.SourcePullElapsed = time.Since(start)
if err := metric.SetSourceImageSize(ctx, cvt, source); err != nil {
return nil, errors.Wrap(err, "get source image size")
}
logger.Infof("pulled image %s, elapse %s", source, metric.SourcePullElapsed)

logger.Infof("converting image %s", source)
start = time.Now()
desc, err := cvt.driver.Convert(ctx, cvt.provider, source)
if err != nil {
return errors.Wrap(err, "convert image")
return nil, errors.Wrap(err, "convert image")
}
metric.ConversionElapsed = time.Since(start)
if err := metric.SetTargetImageSize(ctx, cvt.provider.ContentStore(), desc); err != nil {
return nil, errors.Wrap(err, "get target image size")
}
logger.Infof("converted image %s, elapse %s", target, time.Since(start))
logger.Infof("converted image %s, elapse %s", target, metric.ConversionElapsed)

start = time.Now()
logger.Infof("pushing image %s", target)
Expand All @@ -127,13 +136,14 @@ func (cvt *Converter) Convert(ctx context.Context, source, target string) error
logger.Infof("try to push with plain HTTP for %s", target)
cvt.provider.UsePlainHTTP()
if err := cvt.provider.Push(ctx, *desc, target); err != nil {
return errors.Wrap(err, "try to push image")
return nil, errors.Wrap(err, "try to push image")
}
} else {
return errors.Wrap(err, "push image")
return nil, errors.Wrap(err, "push image")
}
}
logger.Infof("pushed image %s, elapse %s", target, time.Since(start))
metric.TargetPushElapsed = time.Since(start)
logger.Infof("pushed image %s, elapse %s", target, metric.TargetPushElapsed)

return nil
return &metric, nil
}
90 changes: 90 additions & 0 deletions pkg/converter/metric.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package converter

import (
"context"
"fmt"
"time"

"github.com/containerd/containerd/content"
"github.com/containerd/containerd/images"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

const (
MediaTypeDockerSchema2Manifest = "application/vnd.docker.distribution.manifest.v2+json"
MediaTypeDockerSchema2ManifestList = "application/vnd.docker.distribution.manifest.list.v2+json"
)

// Metric collected the metrics of conversion progress
type Metric struct {
// Total size of the source image with specified platforms in bytes
SourceImageSize int64
// Total size of the target image with specified platforms in bytes
TargetImageSize int64
// Elapsed time of pulling source image
SourcePullElapsed time.Duration
// Elapsed time of pushing target image
ConversionElapsed time.Duration
// Elapsed time of converting source image to target image
TargetPushElapsed time.Duration
}

func (metric *Metric) SetTargetImageSize(ctx context.Context, cs content.Store, desc *ocispec.Descriptor) error {
var err error
metric.TargetImageSize, err = metric.imageSize(ctx, cs, desc)
return err
}

func (metric *Metric) SetSourceImageSize(ctx context.Context, cvt *Converter, source string) error {
image, err := cvt.provider.Image(ctx, source)
if err != nil {
return err
}
metric.SourceImageSize, err = metric.imageSize(ctx, cvt.provider.ContentStore(), image)
return err
}

func (metric *Metric) imageSize(ctx context.Context, cs content.Store, image *ocispec.Descriptor) (int64, error) {
var imageSize int64
switch image.MediaType {
case ocispec.MediaTypeImageIndex, MediaTypeDockerSchema2ManifestList:
manifests, err := images.ChildrenHandler(cs)(ctx, *image)
if err != nil {
return imageSize, err
}
for _, manifest := range manifests {
children, err := images.ChildrenHandler(cs)(ctx, manifest)
if err != nil {
return imageSize, err
}
for _, desc := range children {
imageSize += desc.Size
}
}
case ocispec.MediaTypeImageManifest, MediaTypeDockerSchema2Manifest:
children, err := images.ChildrenHandler(cs)(ctx, *image)
if err != nil {
return imageSize, err
}
for _, desc := range children {
imageSize += desc.Size
}
default:
return imageSize, fmt.Errorf("unknown descriptor type %s", image.MediaType)
}
return imageSize, nil
}

0 comments on commit 7bc9cab

Please sign in to comment.