-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
118 lines (93 loc) · 2.27 KB
/
image.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
package clade
import (
"encoding/hex"
"errors"
"github.com/distribution/distribution/v3/reference"
"gopkg.in/yaml.v3"
)
const AnnotationDerefId = "clade.deref.id"
type BaseImage struct {
Primary *ImageReference
Secondaries []*ImageReference
}
func (i *BaseImage) unmarshalYamlScalar(node *yaml.Node) error {
i.Secondaries = nil
return node.Decode(&i.Primary)
}
func (i *BaseImage) unmarshalYamlMap(node *yaml.Node) error {
var ref struct {
Name string
Tags string
With []*ImageReference
}
if err := node.Decode(&ref); err != nil {
return err
}
i.Secondaries = ref.With
i.Primary = &ImageReference{}
if err := i.Primary.FromNameTag(ref.Name, ref.Tags); err != nil {
return err
}
return nil
}
func (i *BaseImage) UnmarshalYAML(node *yaml.Node) error {
switch node.Kind {
case yaml.ScalarNode:
return i.unmarshalYamlScalar(node)
case yaml.MappingNode:
return i.unmarshalYamlMap(node)
}
return &yaml.TypeError{Errors: []string{"must be string or map"}}
}
type Image struct {
reference.Named `yaml:"-"`
Skip bool `yaml:"skip"`
Tags []Pipeline `yaml:"tags"`
From BaseImage `yaml:"from"`
Args map[string]Pipeline `yaml:"args"`
Dockerfile string `yaml:"dockerfile"`
ContextPath string `yaml:"context"`
Platform *BoolAlgebra `yaml:"platform"`
}
type ResolvedBaseImage struct {
Primary ResolvedImageReference
Secondaries []ResolvedImageReference
}
func (i *ResolvedBaseImage) All() []ResolvedImageReference {
return append([]ResolvedImageReference{i.Primary}, i.Secondaries...)
}
type ResolvedImage struct {
reference.Named
Skip bool
Tags []string
From *ResolvedBaseImage
Args map[string]string
Dockerfile string
ContextPath string
Platform *BoolAlgebra
}
func (i *ResolvedImage) Tagged() (reference.NamedTagged, error) {
if len(i.Tags) == 0 {
return nil, errors.New("not tagged")
}
tagged, err := reference.WithTag(i.Named, i.Tags[0])
if err != nil {
return nil, err
}
return tagged, nil
}
func CalcDerefId(dgsts ...[]byte) string {
max_len := 0
for _, dgst := range dgsts {
if max_len < len(dgst) {
max_len = len(dgst)
}
}
rst := make([]byte, max_len)
for _, dgst := range dgsts {
for i, v := range dgst {
rst[i] = rst[i] ^ v
}
}
return hex.EncodeToString(rst)
}