-
Notifications
You must be signed in to change notification settings - Fork 249
/
load.go
299 lines (282 loc) · 11.1 KB
/
load.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
package load
import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/sirupsen/logrus"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/test-infra/prow/config"
"sigs.k8s.io/yaml"
"github.com/openshift/ci-tools/pkg/api"
"github.com/openshift/ci-tools/pkg/registry"
"github.com/openshift/ci-tools/pkg/util/gzip"
"github.com/openshift/ci-tools/pkg/validation"
)
type RegistryFlag uint8
const (
RefSuffix = "-ref.yaml"
ChainSuffix = "-chain.yaml"
WorkflowSuffix = "-workflow.yaml"
ObserverSuffix = "-observer.yaml"
CommandsSuffix = "-commands" // excluding the file extension
MetadataSuffix = ".metadata.json"
)
const (
RegistryFlat = RegistryFlag(1) << iota
RegistryMetadata
RegistryDocumentation
)
// Registry takes the path to a registry config directory and returns the full set of references, chains,
// and workflows that the registry's Resolver needs to resolve a user's MultiStageTestConfiguration
func Registry(root string, flags RegistryFlag) (registry.ReferenceByName, registry.ChainByName, registry.WorkflowByName, api.ClusterProfilesMap, map[string]string, api.RegistryMetadata, registry.ObserverByName, error) {
flat := flags&RegistryFlat != 0
references := registry.ReferenceByName{}
chains := registry.ChainByName{}
workflows := registry.WorkflowByName{}
observers := registry.ObserverByName{}
var profiles api.ClusterProfilesMap
var clusterProfilesConfigPath string
var documentation map[string]string
var metadata api.RegistryMetadata
if flags&RegistryDocumentation != 0 {
documentation = map[string]string{}
}
if flags&RegistryMetadata != 0 {
metadata = api.RegistryMetadata{}
}
err := filepath.WalkDir(root, func(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
if strings.HasPrefix(info.Name(), "..") {
if info.IsDir() {
return filepath.SkipDir
}
return nil
}
// find the cluster profiles config file
if info.IsDir() && info.Name() == "cluster-profiles" {
configPath := filepath.Join(path, "cluster-profiles-config.yaml")
if _, err := os.Stat(configPath); err == nil {
clusterProfilesConfigPath = configPath
}
return filepath.SkipDir
}
if info.IsDir() {
return nil
}
if filepath.Ext(info.Name()) == ".md" || info.Name() == "OWNERS" {
return nil
}
raw, err := gzip.ReadFileMaybeGZIP(path)
if err != nil {
return err
}
dir := filepath.Dir(path)
var prefix string
if !flat {
relpath, err := filepath.Rel(root, path)
if err != nil {
return fmt.Errorf("failed to determine relative path for %s: %w", path, err)
}
prefix = strings.ReplaceAll(filepath.Dir(relpath), "/", "-")
// Verify that file prefix is correct based on directory path
if !strings.HasPrefix(filepath.Base(relpath), prefix) {
return fmt.Errorf("file %s has incorrect prefix. Prefix should be %s", path, prefix)
}
}
if strings.HasSuffix(path, RefSuffix) {
name, doc, ref, err := loadReference(raw, dir, prefix, flat)
if err != nil {
return fmt.Errorf("failed to load registry file %s: %w", path, err)
}
if !flat && name != prefix {
return fmt.Errorf("name of reference in file %s should be %s", path, prefix)
}
if strings.TrimSuffix(filepath.Base(path), RefSuffix) != name {
return fmt.Errorf("filename %s does not match name of reference; filename should be %s", filepath.Base(path), fmt.Sprint(prefix, RefSuffix))
}
references[name] = ref
if documentation != nil {
documentation[name] = doc
}
} else if strings.HasSuffix(path, ChainSuffix) {
var chain api.RegistryChainConfig
err := yaml.UnmarshalStrict(raw, &chain)
if err != nil {
return fmt.Errorf("failed to load registry file %s: %w", path, err)
}
if !flat && chain.Chain.As != prefix {
return fmt.Errorf("name of chain in file %s should be %s", path, prefix)
}
if strings.TrimSuffix(filepath.Base(path), ChainSuffix) != chain.Chain.As {
return fmt.Errorf("filename %s does not match name of chain; filename should be %s", filepath.Base(path), fmt.Sprint(prefix, ChainSuffix))
}
if documentation != nil {
documentation[chain.Chain.As] = chain.Chain.Documentation
}
chain.Chain.Documentation = ""
chains[chain.Chain.As] = chain.Chain
} else if strings.HasSuffix(path, WorkflowSuffix) {
name, doc, workflow, err := loadWorkflow(raw)
if err != nil {
return fmt.Errorf("failed to load registry file %s: %w", path, err)
}
if !flat && name != prefix {
return fmt.Errorf("name of workflow in file %s should be %s", path, prefix)
}
if strings.TrimSuffix(filepath.Base(path), WorkflowSuffix) != name {
return fmt.Errorf("filename %s does not match name of workflow; filename should be %s", filepath.Base(path), fmt.Sprint(prefix, WorkflowSuffix))
}
workflows[name] = workflow
if documentation != nil {
documentation[name] = doc
}
} else if strings.HasSuffix(path, MetadataSuffix) {
if metadata == nil {
return nil
}
var data api.RegistryInfo
err := json.Unmarshal(raw, &data)
if err != nil {
return fmt.Errorf("failed to load metadata file %s: %w", path, err)
}
metadata[filepath.Base(data.Path)] = data
} else if strings.HasSuffix(path, ObserverSuffix) {
var observer api.RegistryObserverConfig
err := yaml.UnmarshalStrict(raw, &observer)
if err != nil {
return fmt.Errorf("failed to load registry file %s: %w", path, err)
}
if !flat && observer.Observer.Name != prefix {
return fmt.Errorf("name of observer in file %s should be %s", path, prefix)
}
if strings.TrimSuffix(filepath.Base(path), ObserverSuffix) != observer.Observer.Name {
return fmt.Errorf("filename %s does not match name of chain; filename should be %s", filepath.Base(path), fmt.Sprint(prefix, ObserverSuffix))
}
if !flat && observer.Observer.Commands != fmt.Sprintf("%s%s%s", prefix, CommandsSuffix, filepath.Ext(observer.Observer.Commands)) {
return fmt.Errorf("observer %s has invalid command file path; command should be set to %s (with an optional extension like .sh)", observer.Observer.Name, fmt.Sprintf("%s%s", prefix, CommandsSuffix))
}
command, err := gzip.ReadFileMaybeGZIP(filepath.Join(dir, observer.Observer.Commands))
if err != nil {
return err
}
observer.Observer.Commands = string(command)
if documentation != nil {
documentation[observer.Observer.Name] = observer.Observer.Documentation
}
observer.Observer.Documentation = ""
observers[observer.Observer.Name] = observer.Observer.Observer
} else if strings.HasSuffix(path, fmt.Sprintf("%s%s", CommandsSuffix, filepath.Ext(path))) {
// ignore
} else if filepath.Base(path) == config.ConfigVersionFileName {
if version, err := gzip.ReadFileMaybeGZIP(path); err == nil {
logrus.WithField("version", string(version)).Info("Resolved configuration version")
}
} else {
return fmt.Errorf("invalid file name: %s", path)
}
return nil
})
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, err
}
// create graph to verify that there are no cycles
if _, err = registry.NewGraph(references, chains, workflows, observers); err != nil {
return nil, nil, nil, nil, nil, nil, nil, err
}
err = registry.Validate(references, chains, workflows, observers)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, err
}
profiles, err = ClusterProfilesConfig(clusterProfilesConfigPath)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, err
}
// validate the integrity of each reference
v := validation.NewValidator(nil, nil)
var validationErrors []error
for _, r := range references {
if err := v.IsValidReference(r); err != nil {
validationErrors = append(validationErrors, err...)
}
}
if len(validationErrors) > 0 {
return nil, nil, nil, nil, nil, nil, nil, utilerrors.NewAggregate(validationErrors)
}
return references, chains, workflows, profiles, documentation, metadata, observers, nil
}
func loadReference(bytes []byte, baseDir, prefix string, flat bool) (string, string, api.LiteralTestStep, error) {
step := api.RegistryReferenceConfig{}
err := yaml.UnmarshalStrict(bytes, &step)
if err != nil {
return "", "", api.LiteralTestStep{}, err
}
if !flat && step.Reference.Commands != fmt.Sprintf("%s%s%s", prefix, CommandsSuffix, filepath.Ext(step.Reference.Commands)) {
return "", "", api.LiteralTestStep{}, fmt.Errorf("reference %s has invalid command file path; command should be set to %s (with an optional extension like .sh)", step.Reference.As, fmt.Sprintf("%s%s", prefix, CommandsSuffix))
}
command, err := gzip.ReadFileMaybeGZIP(filepath.Join(baseDir, step.Reference.Commands))
if err != nil {
return "", "", api.LiteralTestStep{}, err
}
step.Reference.Commands = string(command)
return step.Reference.As, step.Reference.Documentation, step.Reference.LiteralTestStep, nil
}
func loadWorkflow(bytes []byte) (string, string, api.MultiStageTestConfiguration, error) {
workflow := api.RegistryWorkflowConfig{}
err := yaml.UnmarshalStrict(bytes, &workflow)
if err != nil {
return "", "", api.MultiStageTestConfiguration{}, err
}
if workflow.Workflow.Steps.Workflow != nil {
return "", "", api.MultiStageTestConfiguration{}, errors.New("workflows cannot contain other workflows")
}
return workflow.Workflow.As, workflow.Workflow.Documentation, workflow.Workflow.Steps, nil
}
// ClusterProfilesConfig loads cluster profile information from its config in the release repository
func ClusterProfilesConfig(configPath string) (api.ClusterProfilesMap, error) {
configContents, err := os.ReadFile(configPath)
if err != nil {
return nil, fmt.Errorf("failed to read cluster profiles config: %w", err)
}
var profileOwnersList api.ClusterProfilesList
if err = yaml.Unmarshal(configContents, &profileOwnersList); err != nil {
return nil, fmt.Errorf("failed to unmarshall file %v. Please check that the formatting in the file is correct. Full error: %w", configPath, err)
}
// TODO: The following code can be erased once profiles are completely moved
// from code in ci-tools to the config file in openshift/release
profileOwnersMap := make(api.ClusterProfilesMap)
for _, p := range profileOwnersList {
profileOwnersMap[p.Profile] = p
}
mergedMap := make(api.ClusterProfilesMap)
for _, profileName := range api.ClusterProfiles() {
profile, found := profileOwnersMap[profileName]
if found {
mergedMap[profileName] = profile
} else {
mergedMap[profileName] = api.ClusterProfileDetails{Profile: profileName}
}
}
return mergedMap, nil
}
// ClusterClaimsOwnersConfig loads cluster claim owners information from its config in the release repository
func ClusterClaimOwnersConfig(configPath string) (api.ClusterClaimOwnersMap, error) {
configContents, err := os.ReadFile(configPath)
if err != nil {
return nil, fmt.Errorf("failed to read cluster claim config: %w", err)
}
var clusterClaimList []api.ClusterClaimDetails
if err = yaml.Unmarshal(configContents, &clusterClaimList); err != nil {
return nil, fmt.Errorf("failed to unmarshall cluster claim config: %w", err)
}
clusterClaimOwnersMap := make(api.ClusterClaimOwnersMap)
for _, c := range clusterClaimList {
clusterClaimOwnersMap[c.Claim] = c
}
return clusterClaimOwnersMap, nil
}