/
google_go_containerregistry.go
75 lines (63 loc) · 2.26 KB
/
google_go_containerregistry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package oci
import (
"fmt"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)
// Convert github.com/google/go-containerregistry/pkg/v1.IndexImage to
// github.com/opencontainers/image-spec/specs-go/v1.Index
func FromGoogleV1IndexImageToOCISpec(from v1.ImageIndex) (*ocispec.Index, error) {
index, err := from.IndexManifest()
if err != nil {
return nil, fmt.Errorf("could not get index manifest: %w", err)
}
return FromGoogleV1IndexManifestToOCISpec(*index)
}
// Convert github.com/google/go-containerregistry/pkg/v1.IndexManifest to
// github.com/opencontainers/image-spec/specs-go/v1.Index
func FromGoogleV1IndexManifestToOCISpec(from v1.IndexManifest) (*ocispec.Index, error) {
descs := []ocispec.Descriptor{}
for _, desc := range from.Manifests {
descs = append(descs, FromGoogleV1DescriptorToOCISpec(desc)...)
}
return &ocispec.Index{
Manifests: descs,
MediaType: string(from.MediaType),
}, nil
}
// Convert github.com/google/go-containerregistry/pkg/v1.Descriptor to
// github.com/opencontainers/image-spec/specs-go/v1.Descriptor
func FromGoogleV1DescriptorToOCISpec(from ...v1.Descriptor) []ocispec.Descriptor {
to := make([]ocispec.Descriptor, len(from))
for i := range from {
to[i] = ocispec.Descriptor{
MediaType: string(from[i].MediaType),
Digest: digest.Digest(from[i].Digest.String()),
Size: from[i].Size,
URLs: from[i].URLs,
Annotations: from[i].Annotations,
Data: from[i].Data,
Platform: FromGoogleV1PlatformToOCISpec(from[i].Platform),
}
}
return to
}
// Convert github.com/google/go-containerregistry/pkg/v1.Platform to
// github.com/opencontainers/image-spec/specs-go/v1.Platform
func FromGoogleV1PlatformToOCISpec(from *v1.Platform) *ocispec.Platform {
if from == nil {
return nil
}
return &ocispec.Platform{
Architecture: from.Architecture,
OS: from.OS,
OSVersion: from.OSVersion,
OSFeatures: from.OSFeatures,
Variant: from.Variant,
}
}