/
image_layer.go
228 lines (188 loc) · 5.54 KB
/
image_layer.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package models
import (
"encoding/json"
"fmt"
strings "strings"
)
func (l *ImageLayer) Validate() error {
var validationError ValidationError
if l.GetUrl() == "" {
validationError = validationError.Append(ErrInvalidField{"url"})
}
if l.GetDestinationPath() == "" {
validationError = validationError.Append(ErrInvalidField{"destination_path"})
}
if !l.LayerType.Valid() {
validationError = validationError.Append(ErrInvalidField{"layer_type"})
}
if !l.MediaType.Valid() {
validationError = validationError.Append(ErrInvalidField{"media_type"})
}
if (l.DigestValue != "" || l.LayerType == LayerTypeExclusive) && l.DigestAlgorithm == DigestAlgorithmInvalid {
validationError = validationError.Append(ErrInvalidField{"digest_algorithm"})
}
if (l.DigestAlgorithm != DigestAlgorithmInvalid || l.LayerType == LayerTypeExclusive) && l.DigestValue == "" {
validationError = validationError.Append(ErrInvalidField{"digest_value"})
}
if l.DigestValue != "" && !l.DigestAlgorithm.Valid() {
validationError = validationError.Append(ErrInvalidField{"digest_algorithm"})
}
if !validationError.Empty() {
return validationError
}
return nil
}
func validateImageLayers(layers []*ImageLayer, legacyDownloadUser string) ValidationError {
var validationError ValidationError
requiresLegacyDownloadUser := false
if len(layers) > 0 {
for _, layer := range layers {
err := layer.Validate()
if err != nil {
validationError = validationError.Append(ErrInvalidField{"image_layer"})
validationError = validationError.Append(err)
}
if layer.LayerType == LayerTypeExclusive {
requiresLegacyDownloadUser = true
}
}
}
if requiresLegacyDownloadUser && legacyDownloadUser == "" {
validationError = validationError.Append(ErrInvalidField{"legacy_download_user"})
}
return validationError
}
type ImageLayers []*ImageLayer
func (layers ImageLayers) FilterByType(layerType ImageLayer_Type) ImageLayers {
var filtered ImageLayers
for _, layer := range layers {
if layer.GetLayerType() == layerType {
filtered = append(filtered, layer)
}
}
return filtered
}
func (layers ImageLayers) ToDownloadActions(legacyDownloadUser string, existingAction *Action) *Action {
downloadActions := []ActionInterface{}
for _, layer := range layers.FilterByType(LayerTypeExclusive) {
digestAlgorithmName := strings.ToLower(layer.DigestAlgorithm.String())
downloadActions = append(downloadActions, &DownloadAction{
Artifact: layer.Name,
From: layer.Url,
To: layer.DestinationPath,
CacheKey: digestAlgorithmName + ":" + layer.DigestValue, // digest required for exclusive layers
User: legacyDownloadUser,
ChecksumAlgorithm: digestAlgorithmName,
ChecksumValue: layer.DigestValue,
})
}
var action *Action
if len(downloadActions) > 0 {
parallelDownloadActions := Parallel(downloadActions...)
if existingAction != nil {
action = WrapAction(Serial(parallelDownloadActions, UnwrapAction(existingAction)))
} else {
action = WrapAction(parallelDownloadActions)
}
} else {
action = existingAction
}
return action
}
func (layers ImageLayers) ToCachedDependencies() []*CachedDependency {
cachedDependencies := []*CachedDependency{}
for _, layer := range layers.FilterByType(LayerTypeShared) {
c := &CachedDependency{
Name: layer.Name,
From: layer.Url,
To: layer.DestinationPath,
ChecksumValue: layer.DigestValue,
}
if layer.DigestAlgorithm == DigestAlgorithmInvalid {
c.ChecksumAlgorithm = ""
} else {
c.ChecksumAlgorithm = strings.ToLower(layer.DigestAlgorithm.String())
}
if layer.DigestValue == "" {
c.CacheKey = layer.Url
} else {
c.CacheKey = c.ChecksumAlgorithm + ":" + layer.DigestValue
}
cachedDependencies = append(cachedDependencies, c)
}
return cachedDependencies
}
func (d ImageLayer_DigestAlgorithm) Valid() bool {
switch d {
case DigestAlgorithmSha256:
return true
case DigestAlgorithmSha512:
return true
default:
return false
}
}
func (m ImageLayer_MediaType) Valid() bool {
switch m {
case MediaTypeTar:
return true
case MediaTypeTgz:
return true
case MediaTypeZip:
return true
default:
return false
}
}
func (t ImageLayer_Type) Valid() bool {
switch t {
case LayerTypeExclusive:
return true
case LayerTypeShared:
return true
default:
return false
}
}
func (d *ImageLayer_DigestAlgorithm) UnmarshalJSON(data []byte) error {
var name string
if err := json.Unmarshal(data, &name); err != nil {
return err
}
if v, found := ImageLayer_DigestAlgorithm_value[name]; found {
*d = ImageLayer_DigestAlgorithm(v)
return nil
}
return fmt.Errorf("invalid digest_algorithm: %s", name)
}
func (d ImageLayer_DigestAlgorithm) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}
func (m *ImageLayer_MediaType) UnmarshalJSON(data []byte) error {
var name string
if err := json.Unmarshal(data, &name); err != nil {
return err
}
if v, found := ImageLayer_MediaType_value[name]; found {
*m = ImageLayer_MediaType(v)
return nil
}
return fmt.Errorf("invalid media_type: %s", name)
}
func (m ImageLayer_MediaType) MarshalJSON() ([]byte, error) {
return json.Marshal(m.String())
}
func (t *ImageLayer_Type) UnmarshalJSON(data []byte) error {
var name string
if err := json.Unmarshal(data, &name); err != nil {
return err
}
if v, found := ImageLayer_Type_value[name]; found {
*t = ImageLayer_Type(v)
return nil
}
return fmt.Errorf("invalid type: %s", name)
}
func (t ImageLayer_Type) MarshalJSON() ([]byte, error) {
return json.Marshal(t.String())
}