-
Notifications
You must be signed in to change notification settings - Fork 9
/
resolveimage.go
57 lines (43 loc) · 1.34 KB
/
resolveimage.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package oci
import (
"context"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/std/tasks"
)
type ResolveOpts struct {
PublicImage bool
RegistryAccess
}
// Resolves the image tag into a digest. If one is already specified, this is a no-op.
func ResolveDigest(ref string, opts ResolveOpts) NamedImageID {
return MakeNamedImageID(ref, &resolveDigest{ref: ref, opts: opts})
}
type resolveDigest struct {
ref string
opts ResolveOpts
compute.LocalScoped[ImageID]
}
func (r *resolveDigest) Inputs() *compute.In {
return compute.Inputs().Str("ref", r.ref).JSON("opts", r.opts)
}
func (r *resolveDigest) Action() *tasks.ActionEvent {
return tasks.Action("oci.resolve-image-digest").Arg("ref", r.ref)
}
func (r *resolveDigest) Compute(ctx context.Context, _ compute.Resolved) (ImageID, error) {
imageID, err := ParseImageID(r.ref)
if err != nil {
return ImageID{}, err
}
if imageID.Digest != "" {
return imageID, nil
}
desc, err := fetchRemoteDescriptor(ctx, r.ref, r.opts)
if err != nil {
return ImageID{}, err
}
tasks.Attachments(ctx).AddResult("digest", desc.Digest)
return imageID.WithDigest(desc.Digest), nil
}