-
Notifications
You must be signed in to change notification settings - Fork 9
/
canonical.go
75 lines (60 loc) · 1.88 KB
/
canonical.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
// 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"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"namespacelabs.dev/foundation/std/tasks"
)
func WithCanonicalManifest(ctx context.Context, img Image) (Image, error) {
digest, err := img.Digest()
if err != nil {
return nil, err
}
return tasks.Return(ctx, tasks.Action("oci.image.make-canonical").Arg("digest", digest), func(ctx context.Context) (Image, error) {
// mutate.Canonical() resets the build timestamps for each layer. That's
// too expensive, as it requires decompressing all layers. So we let the original
// layers as-is, but clear other sources of non-determinism out.
cf, err := img.ConfigFile()
if err != nil {
return nil, err
}
// Get rid of host-dependent random config
cfg := cf.DeepCopy()
cfg.Container = ""
cfg.Config.Hostname = ""
cfg.DockerVersion = ""
resulting, err := mutate.ConfigFile(img, cfg)
if err != nil {
return nil, err
}
resultingDigest, err := resulting.Digest()
if err == nil {
tasks.Attachments(ctx).AddResult("transformed", resultingDigest)
}
return resulting, err
})
}
func Canonical(ctx context.Context, original Image) (Image, error) {
img, err := WithCanonicalManifest(ctx, original)
if err != nil {
return nil, err
}
AttachDigestToAction(ctx, img)
return img, nil
}
func AttachDigestToAction(ctx context.Context, img Image) {
digest, err := img.Digest()
if err != nil {
tasks.Attachments(ctx).AddResult("digest", "<failed>")
} else {
tasks.Attachments(ctx).AddResult("digest", digest)
}
cfgName, err := img.ConfigName()
if err != nil {
tasks.Attachments(ctx).AddResult("config", "<failed>")
} else {
tasks.Attachments(ctx).AddResult("config", cfgName)
}
}