-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We need to collect the converter metric, such as conversion time, image size. Signed-off-by: Yadong Ding <ding_yadong@foxmail.com>
- Loading branch information
1 parent
558cf3f
commit 6896f73
Showing
3 changed files
with
114 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
"time" | ||
|
||
"github.com/containerd/containerd/content" | ||
"github.com/containerd/containerd/images" | ||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
const ( | ||
MediaTypeDockerSchema2Manifest = "application/vnd.docker.distribution.manifest.v2+json" | ||
MediaTypeDockerSchema2ManifestList = "application/vnd.docker.distribution.manifest.list.v2+json" | ||
) | ||
|
||
// Metric supports converter to collect metric | ||
type Metric struct { | ||
// converter source image size | ||
SourceImageSize int64 | ||
// converter target image size | ||
TargetImageSize int64 | ||
// converter pull source image time | ||
SourcePullElapsed time.Duration | ||
// converter convert image time | ||
ConversionElapsed time.Duration | ||
// converter push target image time | ||
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: | ||
index, err := images.ChildrenHandler(cs)(ctx, *image) | ||
if err != nil { | ||
return imageSize, err | ||
} | ||
for _, manifest := range index { | ||
layers, err := images.ChildrenHandler(cs)(ctx, manifest) | ||
if err != nil { | ||
return imageSize, err | ||
} | ||
for _, layer := range layers { | ||
imageSize += layer.Size | ||
} | ||
} | ||
case ocispec.MediaTypeImageManifest, MediaTypeDockerSchema2Manifest: | ||
layers, err := images.ChildrenHandler(cs)(ctx, *image) | ||
if err != nil { | ||
return imageSize, err | ||
} | ||
for _, layer := range layers { | ||
imageSize += layer.Size | ||
} | ||
default: | ||
return imageSize, errors.New("Unknown descriptor type") | ||
} | ||
return imageSize, nil | ||
} |