-
Notifications
You must be signed in to change notification settings - Fork 249
/
types.go
403 lines (348 loc) · 12.4 KB
/
types.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package ocpbuilddata
import (
"bytes"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"sync"
"golang.org/x/sync/errgroup"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"sigs.k8s.io/yaml"
)
type OCPImageConfig struct {
Content *OCPImageConfigContent `json:"content"`
From OCPImageConfigFrom `json:"from"`
Push OCPImageConfigPush `json:"push"`
Name string `json:"name"`
SourceFileName string `json:"-"`
Version MajorMinor `json:"-"`
PublicRepo OrgRepo `json:"-"`
}
func (o OCPImageConfig) validate() error {
var errs []error
if o.Content != nil && o.Content.Source.Alias != "" && o.Content.Source.Git != nil {
errs = append(errs, errors.New("both content.source.alias and content.source.git are set"))
}
if err := o.From.validate(); err != nil {
errs = append(errs, fmt.Errorf(".from failed validation: %w", err))
}
for idx, cfg := range o.From.Builder {
if err := cfg.validate(); err != nil {
errs = append(errs, fmt.Errorf(".from.%d failed validation: %w", idx, err))
}
}
return utilerrors.NewAggregate(errs)
}
func (o OCPImageConfig) PromotesTo() string {
return fmt.Sprintf("registry.ci.openshift.org/ocp/%s.%s:%s", o.Version.Major, o.Version.Minor, strings.TrimPrefix(o.Name, "openshift/ose-"))
}
type OCPImageConfigContent struct {
Source OCPImageConfigSource `json:"source"`
}
type OrgRepo struct {
Org string
Repo string
}
func (o OrgRepo) String() string {
return o.Org + "/" + o.Repo
}
type OCPImageConfigSource struct {
Dockerfile string `json:"dockerfile"`
Alias string `json:"alias"`
Path string `json:"path"`
// +Optional, mutually exclusive with alias
Git *OCPImageConfigSourceGit `json:"git,omitempty"`
}
type OCPImageConfigSourceGit struct {
URL string `json:"url"`
Branch OCPImageConfigSourceGitBRanch `json:"branch"`
}
type OCPImageConfigSourceGitBRanch struct {
Taget string `json:"target"`
}
type OCPImageConfigFrom struct {
Builder []OCPImageConfigFromStream `json:"builder"`
OCPImageConfigFromStream `json:",inline"`
}
type OCPImageConfigFromStream struct {
Stream string `json:"stream"`
Member string `json:"member"`
}
func (icfs OCPImageConfigFromStream) validate() error {
if icfs.Stream == "" && icfs.Member == "" {
return errors.New("both stream and member were unset")
}
if icfs.Stream != "" && icfs.Member != "" {
return fmt.Errorf("both stream(%s) and member(%s) were set", icfs.Stream, icfs.Member)
}
return nil
}
type OCPImageConfigPush struct {
Also []string `json:"also,omitempty"`
AdditionalTags []string `json:"additional_tags,omitempty"`
}
func (oic *OCPImageConfig) Dockerfile() string {
if oic.Content.Source.Dockerfile == "" {
oic.Content.Source.Dockerfile = "Dockerfile"
}
return filepath.Join(oic.Content.Source.Path, oic.Content.Source.Dockerfile)
}
func (oic *OCPImageConfig) Stages() ([]string, error) {
var result []string
var errs []error
for idx, builder := range oic.From.Builder {
if builder.Stream == "" {
errs = append(errs, fmt.Errorf("couldn't dereference from.builder.%d", idx))
}
result = append(result, builder.Stream)
}
if oic.From.Stream == "" {
errs = append(errs, errors.New("couldn't dereference from.stream"))
}
return append(result, oic.From.Stream), utilerrors.NewAggregate(errs)
}
func (oic *OCPImageConfig) setPublicOrgRepo(mappings []PublicPrivateMapping) {
var name string
if oic.Content == nil || oic.Content.Source.Git == nil || oic.Content.Source.Git.URL == "" {
name = oic.Name
}
if name == "" && oic.Content != nil && oic.Content.Source.Git != nil {
name = strings.TrimSuffix(strings.TrimPrefix(oic.Content.Source.Git.URL, "git@github.com:"), ".git")
}
oic.PublicRepo.Org = publicRepo(name, mappings)
if split := strings.Split(oic.PublicRepo.Org, "/"); len(split) == 2 {
oic.PublicRepo.Org = split[0]
oic.PublicRepo.Repo = split[1]
}
}
type StreamMap map[string]StreamElement
type StreamElement struct {
Image string `json:"image"`
UpstreamImage string `json:"upstream_image"`
Mirror *bool `json:"mirror"`
Aliases []string `json:"aliases,omitempty"`
}
type GroupYAML struct {
Sources map[string]OCPImageConfigSourceGit `json:"sources"`
PublicUpstreams []PublicPrivateMapping `json:"public_upstreams,omitempty"`
}
type PublicPrivateMapping struct {
Private string `json:"private"`
Public string `json:"public"`
}
func publicRepo(orgRepo string, mappings []PublicPrivateMapping) string {
orgRepo = "https://github.com/" + orgRepo
var replacementFrom, replacementTo string
for _, mapping := range mappings {
if !strings.HasPrefix(orgRepo, mapping.Private) {
continue
}
if len(replacementFrom) > len(mapping.Private) {
continue
}
replacementFrom = mapping.Private
replacementTo = mapping.Public
}
if replacementTo == "" {
return strings.TrimPrefix(orgRepo, "https://github.com/")
}
return strings.TrimPrefix(strings.Replace(orgRepo, replacementFrom, replacementTo, 1), "https://github.com/")
}
// LoadImageConfigs loads and dereferences all image configs from the provided ocp-build-data repo root
func LoadImageConfigs(ocpBuildDataDir string, majorMinor MajorMinor) ([]OCPImageConfig, error) {
configsUnverified, err := gatherAllOCPImageConfigs(ocpBuildDataDir, majorMinor)
if err != nil {
return nil, fmt.Errorf("failed to read all image configs: %w", err)
}
streamMap, err := readStreamMap(ocpBuildDataDir, majorMinor)
if err != nil {
return nil, fmt.Errorf("failed to read streams file: %w", err)
}
streamMap = resolveStreamAliases(streamMap)
groupYAML, err := readGroupYAML(ocpBuildDataDir, majorMinor)
if err != nil {
return nil, fmt.Errorf("failed to read group file: %w", err)
}
var errs []error
var configs []OCPImageConfig
for _, cfg := range configsUnverified {
if err := cfg.validate(); err != nil {
errs = append(errs, fmt.Errorf("error validating %s: %w", cfg.SourceFileName, err))
continue
}
if err := dereferenceConfig(&cfg, configsUnverified, streamMap, groupYAML); err != nil {
errs = append(errs, fmt.Errorf("failed dereferencing config for %s: %w", cfg.SourceFileName, err))
continue
}
configs = append(configs, cfg)
}
return configs, utilerrors.NewAggregate(errs)
}
func dereferenceConfig(
config *OCPImageConfig,
allConfigs map[string]OCPImageConfig,
streamMap StreamMap,
groupYAML GroupYAML,
) error {
var errs []error
var err error
if config.From.Stream != "" {
config.From.Stream, err = replaceStream(config.From.Stream, streamMap)
if err != nil {
errs = append(errs, fmt.Errorf("failed to replace .from.stream: %w", err))
}
}
if config.From.Member != "" {
config.From.Stream, err = streamForMember(config.From.Member, allConfigs)
if err != nil {
errs = append(errs, fmt.Errorf("failed to replace .from.member: %w", err))
}
config.From.Member = ""
}
if config.From.Stream == "" {
errs = append(errs, errors.New("failed to find replacement for .from.stream"))
}
for blder := range config.From.Builder {
if config.From.Builder[blder].Stream != "" {
config.From.Builder[blder].Stream, err = replaceStream(config.From.Builder[blder].Stream, streamMap)
if err != nil {
errs = append(errs, fmt.Errorf("failed to replace .from.%d.stream: %w", blder, err))
}
}
if config.From.Builder[blder].Member != "" {
config.From.Builder[blder].Stream, err = streamForMember(config.From.Builder[blder].Member, allConfigs)
if err != nil {
errs = append(errs, fmt.Errorf("failed to replace .from.%d.member: %w", blder, err))
}
config.From.Builder[blder].Member = ""
}
if config.From.Builder[blder].Stream == "" {
errs = append(errs, fmt.Errorf("failed to dereference from.builder.%d", blder))
}
}
if config.Content.Source.Alias != "" {
if _, hasReplacement := groupYAML.Sources[config.Content.Source.Alias]; !hasReplacement {
return fmt.Errorf("groups.yaml has no replacement for alias %s", config.Content.Source.Alias)
}
// Create a new pointer and set its value to groupYAML.Sources[config.Content.Source.Alias]
// rather than directly creating a pointer to the latter.
config.Content.Source.Git = &OCPImageConfigSourceGit{}
*config.Content.Source.Git = groupYAML.Sources[config.Content.Source.Alias]
}
config.setPublicOrgRepo(groupYAML.PublicUpstreams)
return utilerrors.NewAggregate(errs)
}
func replaceStream(streamName string, streamMap StreamMap) (string, error) {
replacement, hasReplacement := streamMap[streamName]
if !hasReplacement {
return "", fmt.Errorf("streamMap has no replacement for stream %s", streamName)
}
if replacement.UpstreamImage == "" {
return "", fmt.Errorf("stream.yml.%s.upstream_image is an empty string", streamName)
}
//TODO: this is a temporary workaround until https://github.com/openshift-eng/ocp-build-data/commit/155694be74cb2f020f6fafeaf6e4b3fba89646a2 is reverted/changed
// We need to allow the: 'golang', 'rhel-9-golang', and 'etcd_golang' streams through in the meantime
if streamName != "golang" && streamName != "golang-1.19" && streamName != "rhel-9-golang" && streamName != "etcd_golang" && streamName != "etcd_golang-1.19" && streamName != "rhel-9-golang-1.19" && streamName != "etcd_rhel9_golang" {
if replacement.Mirror == nil || !*replacement.Mirror {
return "", fmt.Errorf("stream.yaml.%s.mirror is set to false, can not dereference", streamName)
}
}
return replacement.UpstreamImage, nil
}
func configFileNamberForMemberString(memberString string) string {
return "images/" + memberString + ".yml"
}
func streamForMember(
memberName string,
allConfigs map[string]OCPImageConfig,
) (string, error) {
cfgFile := configFileNamberForMemberString(memberName)
cfg, cfgExists := allConfigs[cfgFile]
if !cfgExists {
return "", fmt.Errorf("no config %s found", cfgFile)
}
return cfg.PromotesTo(), nil
}
func readStreamMap(ocpBuildDataDir string, majorMinor MajorMinor) (StreamMap, error) {
streamMap := StreamMap{}
return streamMap, readYAML(filepath.Join(ocpBuildDataDir, "streams.yml"), &streamMap, majorMinor)
}
// resolveStreamAliases duplicates the stream for each alias configured
// so that the stream can be directly found from the map using the alias
func resolveStreamAliases(streamMap StreamMap) StreamMap {
result := make(StreamMap)
for name, stream := range streamMap {
result[name] = stream
for _, alias := range stream.Aliases {
result[alias] = stream
}
}
return result
}
func readGroupYAML(ocpBuildDataDir string, majorMinor MajorMinor) (GroupYAML, error) {
groupYAML := GroupYAML{}
return groupYAML, readYAML(filepath.Join(ocpBuildDataDir, "group.yml"), &groupYAML, majorMinor)
}
type MajorMinor struct {
Major string
Minor string
}
func (mm MajorMinor) String() string {
return mm.Major + "." + mm.Minor
}
func gatherAllOCPImageConfigs(ocpBuildDataDir string, majorMinor MajorMinor) (map[string]OCPImageConfig, error) {
result := map[string]OCPImageConfig{}
resultLock := &sync.Mutex{}
errGroup := &errgroup.Group{}
path := filepath.Join(ocpBuildDataDir, "images")
if err := filepath.WalkDir(path, func(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
// We only want to operate on "yaml" ("yml") files, so we don't load disabled configs
if !strings.HasSuffix(info.Name(), "yaml") && !strings.HasSuffix(info.Name(), "yml") {
return nil
}
errGroup.Go(func() error {
config := OCPImageConfig{}
if err := readYAML(path, &config, majorMinor); err != nil {
return err
}
// Distgit only repositories
if config.Content == nil {
return nil
}
config.SourceFileName = strings.TrimPrefix(path, ocpBuildDataDir+"/")
config.Version = majorMinor
resultLock.Lock()
result[config.SourceFileName] = config
resultLock.Unlock()
return nil
})
return nil
}); err != nil {
return nil, fmt.Errorf("failed to walk")
}
if err := errGroup.Wait(); err != nil {
return nil, fmt.Errorf("failed to read all files: %w", err)
}
return result, nil
}
func readYAML(path string, unmarshalTarget interface{}, majorMinor MajorMinor) error {
data, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read %s: %w", path, err)
}
data = bytes.ReplaceAll(data, []byte("{MAJOR}"), []byte(majorMinor.Major))
data = bytes.ReplaceAll(data, []byte("{MINOR}"), []byte(majorMinor.Minor))
if err := yaml.Unmarshal(data, unmarshalTarget); err != nil {
return fmt.Errorf("unmarshaling failed: %w", err)
}
return nil
}