-
Notifications
You must be signed in to change notification settings - Fork 61
/
describe.go
243 lines (211 loc) · 7.83 KB
/
describe.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright 2022 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
// Package v1 contains the public API version 1 used by other tools to interact with imgpkg
package v1
import (
"fmt"
"sort"
"carvel.dev/imgpkg/pkg/imgpkg/bundle"
"carvel.dev/imgpkg/pkg/imgpkg/lockconfig"
"carvel.dev/imgpkg/pkg/imgpkg/registry"
"carvel.dev/imgpkg/pkg/imgpkg/signature"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
regname "github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
)
// Author information from a Bundle
type Author struct {
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
}
// Website URL where more information of the Bundle can be found
type Website struct {
URL string `json:"url,omitempty"`
}
// Metadata Extra metadata present in a Bundle
type Metadata struct {
Metadata map[string]string `json:"metadata,omitempty"`
Authors []Author `json:"authors,omitempty"`
Websites []Website `json:"websites,omitempty"`
}
// Layers image layers info
type Layers struct {
Digest string `json:"digest,omitempty"`
}
// ImageInfo URLs where the image can be found as well as annotations provided in the Images Lock
type ImageInfo struct {
Image string `json:"image,omitempty"`
Origin string `json:"origin,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
ImageType bundle.ImageType `json:"imageType"`
Error string `json:"error,omitempty"`
Layers []Layers `json:"layers,omitempty"`
}
// Content Contents present in a Bundle
type Content struct {
Bundles map[string]Description `json:"bundles,omitempty"`
Images map[string]ImageInfo `json:"images,omitempty"`
}
// Description Metadata and Contents of a Bundle
type Description struct {
Image string `json:"image"`
Origin string `json:"origin"`
Annotations map[string]string `json:"annotations,omitempty"`
Metadata Metadata `json:"metadata,omitempty"`
Content Content `json:"content"`
Layers []Layers `json:"layers,omitempty"`
}
// DescribeOpts Options used when calling the Describe function
type DescribeOpts struct {
Logger bundle.Logger
Concurrency int
IncludeCosignArtifacts bool
}
// SignatureFetcher Interface to retrieve signatures associated with Images
type SignatureFetcher interface {
FetchForImageRefs(images []lockconfig.ImageRef) ([]lockconfig.ImageRef, error)
}
// Describe Given a Bundle URL fetch the information about the contents of the Bundle and Nested Bundles
func Describe(bundleImage string, opts DescribeOpts, registryOpts registry.Opts) (Description, error) {
reg, err := registry.NewSimpleRegistry(registryOpts)
if err != nil {
return Description{}, err
}
var signatureRetriever SignatureFetcher
if !opts.IncludeCosignArtifacts {
signatureRetriever = signature.NewNoop()
} else {
signatureRetriever = signature.NewSignatures(signature.NewCosign(reg), opts.Concurrency)
}
return DescribeWithRegistryAndSignatureFetcher(bundleImage, opts, reg, signatureRetriever)
}
// DescribeWithRegistryAndSignatureFetcher Given a Bundle URL fetch the information about the contents of the Bundle and Nested Bundles
func DescribeWithRegistryAndSignatureFetcher(bundleImage string, opts DescribeOpts, reg bundle.ImagesMetadata, sigFetcher SignatureFetcher) (Description, error) {
lockReader := bundle.NewImagesLockReader()
newBundle := bundle.NewBundleFromRef(bundleImage, reg, lockReader, bundle.NewRegistryFetcher(reg, lockReader))
isBundle, err := newBundle.IsBundle()
if err != nil {
return Description{}, fmt.Errorf("Unable to check if %s is a bundle: %s", bundleImage, err)
}
if !isBundle {
return Description{}, fmt.Errorf("Only bundles can be described, and %s is not a bundle", bundleImage)
}
allBundles, err := newBundle.FetchAllImagesRefs(opts.Concurrency, opts.Logger, sigFetcher)
if err != nil {
return Description{}, fmt.Errorf("Retrieving Images from bundle: %s", err)
}
topBundle := refWithDescription{
imgRef: bundle.NewBundleImageRef(lockconfig.ImageRef{Image: newBundle.DigestRef()}),
}
return topBundle.DescribeBundle(allBundles)
}
type refWithDescription struct {
imgRef bundle.ImageRef
bundle Description
}
func (r *refWithDescription) DescribeBundle(bundles []*bundle.Bundle) (Description, error) {
var visitedImgs map[string]refWithDescription
return r.describeBundleRec(visitedImgs, r.imgRef, bundles)
}
func (r *refWithDescription) describeBundleRec(visitedImgs map[string]refWithDescription, currentBundle bundle.ImageRef, bundles []*bundle.Bundle) (Description, error) {
desc, wasVisited := visitedImgs[currentBundle.Image]
if wasVisited {
return desc.bundle, nil
}
layers, err := getImageLayersInfo(currentBundle.Image)
if err != nil {
return desc.bundle, err
}
desc = refWithDescription{
imgRef: currentBundle,
bundle: Description{
Image: currentBundle.PrimaryLocation(),
Origin: currentBundle.Image,
Annotations: currentBundle.Annotations,
Metadata: Metadata{},
Content: Content{
Bundles: map[string]Description{},
Images: map[string]ImageInfo{},
},
Layers: layers,
},
}
var newBundle *bundle.Bundle
for _, b := range bundles {
if b.DigestRef() == currentBundle.PrimaryLocation() {
newBundle = b
break
}
}
if newBundle == nil {
return desc.bundle, fmt.Errorf("Internal inconsistency: bundle with ref '%s' could not be found in list of bundles", currentBundle.PrimaryLocation())
}
imagesRefs := newBundle.ImagesRefsWithErrors()
sort.Slice(imagesRefs, func(i, j int) bool {
return imagesRefs[i].Image < imagesRefs[j].Image
})
for _, ref := range imagesRefs {
if ref.IsBundle == nil {
return desc.bundle, fmt.Errorf("Internal inconsistency: IsBundle after processing must always have a value")
}
if *ref.IsBundle {
bundleDesc, err := r.describeBundleRec(visitedImgs, ref, bundles)
if err != nil {
return desc.bundle, err
}
digest, err := name.NewDigest(bundleDesc.Image)
if err != nil {
return desc.bundle, fmt.Errorf("Internal inconsistency: image %s should be fully resolved", bundleDesc.Image)
}
desc.bundle.Content.Bundles[digest.DigestStr()] = bundleDesc
} else {
if ref.Error == "" {
digest, err := name.NewDigest(ref.Image)
if err != nil {
return desc.bundle, fmt.Errorf("Internal inconsistency: image %s should be fully resolved", ref.Image)
}
layers, err = getImageLayersInfo(ref.Image)
if err != nil {
return desc.bundle, err
}
desc.bundle.Content.Images[digest.DigestStr()] = ImageInfo{
Image: ref.PrimaryLocation(),
Origin: ref.Image,
Annotations: ref.Annotations,
ImageType: ref.ImageType,
Layers: layers,
}
} else {
desc.bundle.Content.Images[ref.Image] = ImageInfo{
ImageType: ref.ImageType,
Error: ref.Error,
}
}
}
}
return desc.bundle, nil
}
func getImageLayersInfo(image string) ([]Layers, error) {
layers := []Layers{}
parsedImgRef, err := regname.ParseReference(image, regname.WeakValidation)
if err != nil {
return nil, fmt.Errorf("Error: %s in parsing image %s", err.Error(), image)
}
v1Img, err := remote.Image(parsedImgRef, remote.WithAuthFromKeychain(authn.DefaultKeychain))
if err != nil {
return nil, fmt.Errorf("Error: %s in getting remote access of image %s", err.Error(), image)
}
imgLayers, err := v1Img.Layers()
if err != nil {
return nil, fmt.Errorf("Error: %s in getting layers of image %s", err.Error(), image)
}
for _, imgLayer := range imgLayers {
digHash, err := imgLayer.Digest()
if err != nil {
return nil, fmt.Errorf("Error: %s in getting digest of layer's of image %s", err.Error(), image)
}
layers = append(layers, Layers{Digest: digHash.String()})
}
return layers, nil
}