/
oci.go
558 lines (472 loc) · 15.6 KB
/
oci.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// Copyright 2022 The kpt 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 oci
import (
"bytes"
"context"
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"fmt"
"strings"
"time"
"github.com/GoogleContainerTools/kpt/internal/pkg"
kptfile "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
"github.com/GoogleContainerTools/kpt/pkg/oci"
"github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1"
configapi "github.com/GoogleContainerTools/kpt/porch/api/porchconfig/v1alpha1"
"github.com/GoogleContainerTools/kpt/porch/pkg/repository"
"github.com/google/go-containerregistry/pkg/gcrane"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/google"
"github.com/google/go-containerregistry/pkg/v1/remote"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
)
func OpenRepository(name string, namespace string, content configapi.RepositoryContent, spec *configapi.OciRepository, deployment bool, storage *oci.Storage) (repository.Repository, error) {
return &ociRepository{
name: name,
namespace: namespace,
content: content,
spec: *spec.DeepCopy(),
deployment: deployment,
storage: storage,
}, nil
}
type ociRepository struct {
name string
namespace string
content configapi.RepositoryContent
spec configapi.OciRepository
deployment bool
storage *oci.Storage
}
var _ repository.Repository = &ociRepository{}
var _ repository.FunctionRepository = &ociRepository{}
func (r *ociRepository) Close() error {
return nil
}
// there is probably a more efficient way to do this
func (r *ociRepository) Version(ctx context.Context) (string, error) {
ctx, span := tracer.Start(ctx, "ociRepository::Version")
defer span.End()
if r.content != configapi.RepositoryContentPackage {
return "", nil
}
ociRepo, err := name.NewRepository(r.spec.Registry)
if err != nil {
return "", err
}
options := r.storage.CreateOptions(ctx)
tags, err := google.List(ociRepo, options...)
if err != nil {
return "", err
}
klog.Infof("tags: %#v", tags)
b := bytes.Buffer{}
for _, childName := range tags.Children {
path := fmt.Sprintf("%s/%s", r.spec.Registry, childName)
child, err := name.NewRepository(path, name.StrictValidation)
if err != nil {
klog.Warningf("Cannot create nested repository %q: %v", path, err)
continue
}
childTags, err := google.List(child, options...)
if err != nil {
klog.Warningf("Cannot list nested repository %q: %v", path, err)
continue
}
// klog.Infof("childTags: %#v", childTags)
for digest, m := range childTags.Manifests {
b.WriteString(digest)
mb, err := m.MarshalJSON()
if err != nil {
return "", err
}
b.Write(mb)
}
}
hash := sha256.Sum256(b.Bytes())
return hex.EncodeToString(hash[:]), nil
}
func (r *ociRepository) ListPackageRevisions(ctx context.Context, filter repository.ListPackageRevisionFilter) ([]repository.PackageRevision, error) {
if r.content != configapi.RepositoryContentPackage {
return []repository.PackageRevision{}, nil
}
ctx, span := tracer.Start(ctx, "ociRepository::ListPackageRevisions")
defer span.End()
ociRepo, err := name.NewRepository(r.spec.Registry)
if err != nil {
return nil, err
}
options := r.storage.CreateOptions(ctx)
tags, err := google.List(ociRepo, options...)
if err != nil {
return nil, err
}
klog.Infof("tags: %#v", tags)
var result []repository.PackageRevision
for _, childName := range tags.Children {
path := fmt.Sprintf("%s/%s", r.spec.Registry, childName)
child, err := name.NewRepository(path, name.StrictValidation)
if err != nil {
klog.Warningf("Cannot create nested repository %q: %v", path, err)
continue
}
childTags, err := google.List(child, options...)
if err != nil {
klog.Warningf("Cannot list nested repository %q: %v", path, err)
continue
}
// klog.Infof("childTags: %#v", childTags)
for digest, m := range childTags.Manifests {
for _, tag := range m.Tags {
created := m.Created
if created.IsZero() {
created = m.Uploaded
}
// ref := child.Tag(tag)
// ref := child.Digest(digest)
p := &ociPackageRevision{
// tagName: ImageTagName{
// Image: child.Name(),
// Tag: tag,
// },
digestName: oci.ImageDigestName{
Image: child.Name(),
Digest: digest,
},
packageName: childName,
workspaceName: v1alpha1.WorkspaceName(tag),
created: created,
parent: r,
resourceVersion: constructResourceVersion(m.Created),
}
p.uid = constructUID(p.packageName + ":" + string(p.workspaceName))
lifecycle, err := r.getLifecycle(ctx, p.digestName)
if err != nil {
return nil, err
}
p.lifecycle = lifecycle
revision, err := r.getRevisionNumber(ctx, p.digestName)
if err != nil {
return nil, err
}
p.revision = revision
tasks, err := r.loadTasks(ctx, p.digestName)
if err != nil {
return nil, err
}
p.tasks = tasks
if filter.Matches(p) {
result = append(result, p)
}
}
}
}
return result, nil
}
func (r *ociRepository) ListPackages(ctx context.Context, filter repository.ListPackageFilter) ([]repository.Package, error) {
return nil, fmt.Errorf("ListPackages not supported for OCI packages")
}
func (r *ociRepository) buildPackageRevision(ctx context.Context, name oci.ImageDigestName, packageName string,
workspace v1alpha1.WorkspaceName, revision string, created time.Time) (repository.PackageRevision, error) {
if r.content != configapi.RepositoryContentPackage {
return nil, fmt.Errorf("repository is not a package repo, type is %v", r.content)
}
ctx, span := tracer.Start(ctx, "ociRepository::buildPackageRevision")
defer span.End()
// for backwards compatibility with packages that existed before porch supported
// workspaces, we populate the workspaceName as the revision number if it is empty
if workspace == "" {
workspace = v1alpha1.WorkspaceName(revision)
}
p := &ociPackageRevision{
digestName: name,
packageName: packageName,
workspaceName: workspace,
revision: revision,
created: created,
parent: r,
resourceVersion: constructResourceVersion(created),
}
p.uid = constructUID(p.packageName + ":" + string(p.workspaceName))
lifecycle, err := r.getLifecycle(ctx, p.digestName)
if err != nil {
return nil, err
}
p.lifecycle = lifecycle
tasks, err := r.loadTasks(ctx, p.digestName)
if err != nil {
return nil, err
}
p.tasks = tasks
return p, nil
}
func GetFunctionMeta(reference string, ctx context.Context) (*functionMeta, error) {
ref, err := name.ParseReference(reference)
if err != nil {
return nil, fmt.Errorf("parse image reference %v: %v", reference, err)
}
image, err := remote.Image(ref, remote.WithAuthFromKeychain(gcrane.Keychain), remote.WithContext(ctx))
if err != nil {
return nil, fmt.Errorf("pull remote image %v: %v", reference, err)
}
manifest, err := image.Manifest()
if err != nil {
return nil, fmt.Errorf("get manifest from image %v: %v", reference, err)
}
return &functionMeta{
FunctionTypes: GetSliceFromAnnotation(FunctionTypesKey, manifest),
Description: GetSingleFromAnnotation(DescriptionKey, manifest),
DocumentationUrl: GetSingleFromAnnotation(DocumentationURLKey, manifest),
Keywords: GetSliceFromAnnotation(keywordsKey, manifest),
FunctionConfigs: GetDefaultFunctionConfig(manifest),
}, nil
}
func GetDefaultFunctionConfig(manifest *v1.Manifest) []functionConfig {
val, ok := manifest.Annotations[ConfigMapFnKey]
if !ok {
return nil
}
return []functionConfig{
{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
RequiredFields: AnnotationToSlice(val),
},
}
}
func GetSliceFromAnnotation(key string, manifest *v1.Manifest) []string {
slice, ok := manifest.Annotations[key]
if !ok {
return nil
}
return AnnotationToSlice(slice)
}
func GetSingleFromAnnotation(key string, manifest *v1.Manifest) string {
if val, ok := manifest.Annotations[key]; ok {
return val
}
return fmt.Sprintf("annotation %v unset", key)
}
func (r *ociRepository) ListFunctions(ctx context.Context) ([]repository.Function, error) {
// Repository whose content type is not Function contains no Function resources.
if r.content != configapi.RepositoryContentFunction {
klog.Infof("Repository %q doesn't contain functions; contains %s", r.name, r.content)
return []repository.Function{}, nil
}
ctx, span := tracer.Start(ctx, "ociRepository::ListFunctions")
defer span.End()
ociRepo, err := name.NewRepository(r.spec.Registry)
if err != nil {
return nil, err
}
options := r.storage.CreateOptions(ctx)
result := []repository.Function{}
err = google.Walk(ociRepo, func(repo name.Repository, tags *google.Tags, err error) error {
if err != nil {
klog.Warningf(" Walk %s encountered error: %v", repo, err)
return err
}
if tags == nil {
return nil
}
if cl := len(tags.Children); cl > 0 {
// Expect no manifests or tags
if ml, tl := len(tags.Manifests), len(tags.Tags); ml != 0 || tl != 0 {
return fmt.Errorf("OCI repository with children (%d) as well as Manifests (%d) or Tags (%d)", cl, ml, tl)
}
return nil
}
functionName := parseFunctionName(repo.RepositoryStr())
for digest, manifest := range tags.Manifests {
// Only consider tagged images.
for _, tag := range manifest.Tags {
created := manifest.Created
if created.IsZero() {
created = manifest.Uploaded
}
meta, err := GetFunctionMeta(repo.Digest(digest).Name(), ctx)
if err != nil {
klog.Warningf(" pull function %v error: %w", functionName, err)
continue
}
result = append(result, &ociFunction{
ref: repo.Digest(digest),
tag: repo.Tag(tag),
name: functionName,
version: tag,
meta: meta,
created: created,
parent: r,
})
}
}
return nil
}, options...)
if err != nil {
return nil, err
}
return result, nil
}
type ociPackageRevision struct {
digestName oci.ImageDigestName
packageName string
revision string
workspaceName v1alpha1.WorkspaceName
created time.Time
resourceVersion string
uid types.UID
parent *ociRepository
tasks []v1alpha1.Task
lifecycle v1alpha1.PackageRevisionLifecycle
}
var _ repository.PackageRevision = &ociPackageRevision{}
func (p *ociPackageRevision) GetResources(ctx context.Context) (*v1alpha1.PackageRevisionResources, error) {
resources, err := LoadResources(ctx, p.parent.storage, &p.digestName)
if err != nil {
return nil, err
}
key := p.Key()
return &v1alpha1.PackageRevisionResources{
TypeMeta: metav1.TypeMeta{
Kind: "PackageRevisionResources",
APIVersion: v1alpha1.SchemeGroupVersion.Identifier(),
},
ObjectMeta: metav1.ObjectMeta{
Name: p.KubeObjectName(),
Namespace: p.parent.namespace,
CreationTimestamp: metav1.Time{
Time: p.created,
},
ResourceVersion: p.resourceVersion,
UID: p.uid,
},
Spec: v1alpha1.PackageRevisionResourcesSpec{
PackageName: key.Package,
WorkspaceName: key.WorkspaceName,
Revision: key.Revision,
RepositoryName: key.Repository,
Resources: resources.Contents,
},
}, nil
}
func (p *ociPackageRevision) KubeObjectName() string {
hash := sha1.Sum([]byte(fmt.Sprintf("%s:%s:%s", p.parent.name, p.packageName, p.workspaceName)))
return p.parent.name + "-" + hex.EncodeToString(hash[:])
}
func (p *ociPackageRevision) KubeObjectNamespace() string {
return p.parent.namespace
}
func (p *ociPackageRevision) UID() types.UID {
return p.uid
}
func (p *ociPackageRevision) ResourceVersion() string {
return p.resourceVersion
}
func (p *ociPackageRevision) Key() repository.PackageRevisionKey {
return repository.PackageRevisionKey{
Repository: p.parent.name,
Package: p.packageName,
Revision: p.revision,
WorkspaceName: p.workspaceName,
}
}
func (p *ociPackageRevision) GetPackageRevision(ctx context.Context) (*v1alpha1.PackageRevision, error) {
key := p.Key()
kf, err := p.GetKptfile(ctx)
if err != nil {
return nil, err
}
return &v1alpha1.PackageRevision{
TypeMeta: metav1.TypeMeta{
Kind: "PackageRevision",
APIVersion: v1alpha1.SchemeGroupVersion.Identifier(),
},
ObjectMeta: metav1.ObjectMeta{
Name: p.KubeObjectName(),
Namespace: p.parent.namespace,
CreationTimestamp: metav1.Time{
Time: p.created,
},
ResourceVersion: p.resourceVersion,
UID: p.uid,
},
Spec: v1alpha1.PackageRevisionSpec{
PackageName: key.Package,
RepositoryName: key.Repository,
Revision: key.Revision,
WorkspaceName: key.WorkspaceName,
Lifecycle: p.Lifecycle(),
Tasks: p.tasks,
ReadinessGates: repository.ToApiReadinessGates(kf),
},
Status: v1alpha1.PackageRevisionStatus{
// TODO: UpstreamLock,
Deployment: p.parent.deployment,
Conditions: repository.ToApiConditions(kf),
},
}, nil
}
func (p *ociPackageRevision) GetKptfile(ctx context.Context) (kptfile.KptFile, error) {
resources, err := LoadResources(ctx, p.parent.storage, &p.digestName)
if err != nil {
return kptfile.KptFile{}, fmt.Errorf("error loading package resources for %v: %w", p.digestName, err)
}
kfString, found := resources.Contents[kptfile.KptFileName]
if !found {
return kptfile.KptFile{}, fmt.Errorf("packagerevision does not have a Kptfile")
}
kf, err := pkg.DecodeKptfile(strings.NewReader(kfString))
if err != nil {
return kptfile.KptFile{}, fmt.Errorf("error decoding Kptfile: %w", err)
}
return *kf, nil
}
func (p *ociPackageRevision) GetUpstreamLock(context.Context) (kptfile.Upstream, kptfile.UpstreamLock, error) {
return kptfile.Upstream{}, kptfile.UpstreamLock{}, fmt.Errorf("UpstreamLock is not supported for OCI packages (%s)", p.KubeObjectName())
}
func (p *ociPackageRevision) GetLock() (kptfile.Upstream, kptfile.UpstreamLock, error) {
return kptfile.Upstream{}, kptfile.UpstreamLock{}, fmt.Errorf("Lock is not supported for OCI packages (%s)", p.KubeObjectName())
}
func (p *ociPackageRevision) Lifecycle() v1alpha1.PackageRevisionLifecycle {
return p.lifecycle
}
// UpdateLifecycle should update the package revision lifecycle from DeletionProposed to Published or vice versa.
//
// This function is currently only partially implemented; it still needs to store whether the package has been
// proposed for deletion somewhere in OCI, probably as another OCI image with a "deletionProposed" tag.
func (p *ociPackageRevision) UpdateLifecycle(ctx context.Context, new v1alpha1.PackageRevisionLifecycle) error {
old := p.Lifecycle()
if old == v1alpha1.PackageRevisionLifecyclePublished {
if new != v1alpha1.PackageRevisionLifecycleDeletionProposed {
return fmt.Errorf("invalid new lifecycle value: %q", new)
}
// TODO: Create a "deletionProposed" OCI image tag.
p.lifecycle = v1alpha1.PackageRevisionLifecycleDeletionProposed
}
if old == v1alpha1.PackageRevisionLifecycleDeletionProposed {
if new != v1alpha1.PackageRevisionLifecyclePublished {
return fmt.Errorf("invalid new lifecycle value: %q", new)
}
// TODO: Delete the "deletionProposed" OCI image tag.
p.lifecycle = v1alpha1.PackageRevisionLifecyclePublished
}
return nil
}