-
Notifications
You must be signed in to change notification settings - Fork 200
/
bundle.go
59 lines (48 loc) · 1.87 KB
/
bundle.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
package cnab
import (
"bytes"
"context"
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/cnabio/cnab-to-oci/relocation"
"github.com/opencontainers/go-digest"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
type BundleReference struct {
Reference OCIReference
Digest digest.Digest
Definition ExtendedBundle
RelocationMap relocation.ImageRelocationMap
}
func (r BundleReference) String() string {
return r.Reference.String()
}
// AddToTrace appends the bundle reference attributes to the current span.
func (r BundleReference) AddToTrace(cxt context.Context) {
span := trace.SpanFromContext(cxt)
span.SetAttributes(attribute.String("reference", r.String()))
var bunJson bytes.Buffer
_, _ = r.Definition.WriteTo(&bunJson)
span.SetAttributes(attribute.String("bundleDefinition", bunJson.String()))
relocationMappingJson, _ := json.Marshal(r.RelocationMap)
span.SetAttributes(attribute.String("relocationMapping", string(relocationMappingJson)))
}
// CalculateTemporaryImageTag returns the temporary tag applied to images that we
// use to push it and then retrieve the repository digest for an image.
func CalculateTemporaryImageTag(bunRef OCIReference) (OCIReference, error) {
imageName, err := ParseOCIReference(bunRef.Repository())
if err != nil {
return OCIReference{}, fmt.Errorf("could not calculate temporary image tag for %s: %w", bunRef.String(), err)
}
referenceHash := md5.Sum([]byte(bunRef.String()))
// prefix the temporary tag with porter- so that people can identify its purpose (otherwise it's just some random number as far as people can tell)
imgTag := "porter-" + hex.EncodeToString(referenceHash[:])
imageRef, err := imageName.WithTag(imgTag)
if err != nil {
return OCIReference{}, fmt.Errorf("could not apply tag, %s, to the image %s: %w", imgTag, imageName.String(), err)
}
return imageRef, nil
}