-
Notifications
You must be signed in to change notification settings - Fork 248
/
main.go
197 lines (171 loc) · 5.9 KB
/
main.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
package main
import (
"flag"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/sets"
prowconfig "k8s.io/test-infra/prow/config"
"sigs.k8s.io/yaml"
"github.com/openshift/ci-tools/pkg/api"
"github.com/openshift/ci-tools/pkg/dispatcher"
"github.com/openshift/ci-tools/pkg/jobconfig"
"github.com/openshift/ci-tools/pkg/util"
"github.com/openshift/ci-tools/pkg/util/gzip"
)
const (
cioperatorLatestImage = "ci-operator:latest"
)
type options struct {
prowJobConfigDir string
configPath string
help bool
}
func bindOptions(flag *flag.FlagSet) *options {
opt := &options{}
flag.StringVar(&opt.prowJobConfigDir, "prow-jobs-dir", "", "Path to a root of directory structure with Prow job config files (ci-operator/jobs in openshift/release)")
flag.StringVar(&opt.configPath, "config-path", "", "Path to the config file (core-services/sanitize-prow-jobs/_config.yaml in openshift/release)")
flag.BoolVar(&opt.help, "h", false, "Show help for ci-operator-prowgen")
return opt
}
func determinizeJobs(prowJobConfigDir string, config *dispatcher.Config) error {
ch := make(chan string)
errCh := make(chan error)
produce := func() error {
defer close(ch)
return filepath.WalkDir(prowJobConfigDir, func(path string, info fs.DirEntry, err error) error {
if err != nil {
errCh <- fmt.Errorf("failed to walk file/directory %q: %w", path, err)
return nil
}
if info.IsDir() || !strings.HasSuffix(path, ".yaml") {
return nil
}
ch <- path
return nil
})
}
map_ := func() error {
for path := range ch {
data, err := gzip.ReadFileMaybeGZIP(path)
if err != nil {
errCh <- fmt.Errorf("failed to read file %q: %w", path, err)
continue
}
jobConfig := &prowconfig.JobConfig{}
if err := yaml.Unmarshal(data, jobConfig); err != nil {
errCh <- fmt.Errorf("failed to unmarshal file %q: %w", path, err)
continue
}
if err := defaultJobConfig(jobConfig, path, config); err != nil {
errCh <- fmt.Errorf("failed to default job config %q: %w", path, err)
}
serialized, err := yaml.Marshal(jobConfig)
if err != nil {
errCh <- fmt.Errorf("failed to marshal file %q: %w", path, err)
continue
}
if err := os.WriteFile(path, serialized, 0644); err != nil {
errCh <- fmt.Errorf("failed to write file %q: %w", path, err)
continue
}
}
return nil
}
if err := util.ProduceMap(0, produce, map_, errCh); err != nil {
return fmt.Errorf("failed to determinize all Prow jobs: %w", err)
}
return nil
}
func defaultJobConfig(jc *prowconfig.JobConfig, path string, config *dispatcher.Config) error {
for k := range jc.PresubmitsStatic {
for idx := range jc.PresubmitsStatic[k] {
cluster, err := config.GetClusterForJob(jc.PresubmitsStatic[k][idx].JobBase, path)
if err != nil {
return err
}
jc.PresubmitsStatic[k][idx].JobBase.Cluster = string(cluster)
if string(cluster) == string(api.ClusterARM01) && isCIOperatorLatest(jc.PresubmitsStatic[k][idx].JobBase.Spec.Containers[0].Image) {
jc.PresubmitsStatic[k][idx].JobBase.Spec.Containers[0].Image = "ci-operator-arm64:latest"
}
// Enforce that even hand-crafted jobs have explicit branch regexes
// Presubmits are generally expected to hit also on "feature branches",
// so we generate regexes for both exact match and feature branch patterns
featureBranches := sets.New[string]()
for _, branch := range jc.PresubmitsStatic[k][idx].Branches {
featureBranches.Insert(jobconfig.FeatureBranch(branch))
featureBranches.Insert(jobconfig.ExactlyBranch(branch))
}
jc.PresubmitsStatic[k][idx].Branches = sets.List(featureBranches)
}
}
for k := range jc.PostsubmitsStatic {
for idx := range jc.PostsubmitsStatic[k] {
cluster, err := config.GetClusterForJob(jc.PostsubmitsStatic[k][idx].JobBase, path)
if err != nil {
return err
}
jc.PostsubmitsStatic[k][idx].JobBase.Cluster = string(cluster)
if string(cluster) == string(api.ClusterARM01) && isCIOperatorLatest(jc.PostsubmitsStatic[k][idx].JobBase.Spec.Containers[0].Image) {
jc.PostsubmitsStatic[k][idx].JobBase.Spec.Containers[0].Image = "ci-operator-arm64:latest"
}
// Enforce that even hand-crafted jobs have explicit branch regexes
// Postsubmits are generally expected to only hit on exact match branches
// so we do not generate a regex for feature branch pattern like we do
// for presubmits above
for item := range jc.PostsubmitsStatic[k][idx].Branches {
jc.PostsubmitsStatic[k][idx].Branches[item] = jobconfig.ExactlyBranch(jc.PostsubmitsStatic[k][idx].Branches[item])
}
}
}
for idx := range jc.Periodics {
cluster, err := config.GetClusterForJob(jc.Periodics[idx].JobBase, path)
if err != nil {
return err
}
jc.Periodics[idx].JobBase.Cluster = string(cluster)
}
return nil
}
func main() {
flagSet := flag.NewFlagSet("", flag.ExitOnError)
opt := bindOptions(flagSet)
if err := flagSet.Parse(os.Args[1:]); err != nil {
logrus.WithError(err).Fatal("Failed to parse flags")
}
if opt.help {
flagSet.Usage()
os.Exit(0)
}
if len(opt.prowJobConfigDir) == 0 {
logrus.Fatal("mandatory argument --prow-jobs-dir wasn't set")
}
if len(opt.configPath) == 0 {
logrus.Fatal("mandatory argument --config-path wasn't set")
}
config, err := dispatcher.LoadConfig(opt.configPath)
if err != nil {
logrus.WithError(err).Fatalf("Failed to load config from %q", opt.configPath)
}
if err := config.Validate(); err != nil {
logrus.WithError(err).Fatal("Failed to validate the config")
}
args := flagSet.Args()
if len(args) == 0 {
args = append(args, "")
}
for _, subDir := range args {
subDir = filepath.Join(opt.prowJobConfigDir, subDir)
if err := determinizeJobs(subDir, config); err != nil {
logrus.WithError(err).Fatal("Failed to determinize")
}
}
}
func isCIOperatorLatest(image string) bool {
parts := strings.Split(image, "/")
lastPart := parts[len(parts)-1]
return lastPart == cioperatorLatestImage
}