-
Notifications
You must be signed in to change notification settings - Fork 9
/
resolveimage.go
52 lines (39 loc) · 1.28 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
// 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"
)
// Resolves the image tag into a digest. If one is already specified, this is a no-op.
func ResolveDigest(ref string, opts RegistryAccess) NamedImageID {
return MakeNamedImageID(ref, &resolveDigest{ref: ref, opts: opts})
}
type resolveDigest struct {
ref string
opts RegistryAccess
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
}