-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathmain.go
More file actions
194 lines (162 loc) · 6.12 KB
/
main.go
File metadata and controls
194 lines (162 loc) · 6.12 KB
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
package main
import (
_ "embed"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/aws/eks-distro-prow-jobs/templater/jobs"
"github.com/aws/eks-distro-prow-jobs/templater/jobs/types"
"github.com/aws/eks-distro-prow-jobs/templater/jobs/utils"
)
var jobsFolder = "jobs"
var orgsSupported = []string{"aws"}
var jobTypes = []string{"periodic", "postsubmit", "presubmit"}
//go:embed templates/presubmits.yaml
var presubmitTemplate string
//go:embed templates/postsubmits.yaml
var postsubmitTemplate string
//go:embed templates/periodics.yaml
var periodicTemplate string
//go:embed templates/warning.txt
var editWarning string
//go:generate cp ../BUILDER_BASE_TAG_FILE ./BUILDER_BASE_TAG_FILE
//go:embed BUILDER_BASE_TAG_FILE
var builderBaseTag string
var buildkitImageTag = "v0.12.3-rootless"
func main() {
jobsFolderPath, err := getJobsFolderPath()
if err != nil {
fmt.Printf("Error getting jobs folder path: %v", err)
os.Exit(1)
}
for _, org := range orgsSupported {
if err = os.RemoveAll(filepath.Join(jobsFolderPath, org)); err != nil {
fmt.Printf("Error removing jobs folder path: %v", err)
os.Exit(1)
}
}
for _, jobType := range jobTypes {
jobList, err := jobs.GetJobList(jobType)
if err != nil {
fmt.Printf("Error getting job list: %v\n", err)
os.Exit(1)
}
template, err := useTemplate(jobType)
if err != nil {
fmt.Printf("Error getting job list: %v\n", err)
os.Exit(1)
}
for repoName, jobConfigs := range jobList {
for fileName, jobConfig := range jobConfigs {
envVars := jobConfig.EnvVars
if jobConfig.UseDockerBuildX {
envVars = append(envVars, &types.EnvVar{Name: "BUILDKITD_IMAGE", Value: "moby/buildkit:" + buildkitImageTag})
envVars = append(envVars, &types.EnvVar{Name: "USE_BUILDX", Value: "true"})
}
templateBuilderBaseTag := builderBaseTag
if jobConfig.UseMinimalBuilderBase {
templateBuilderBaseTag = strings.Replace(builderBaseTag, "standard", "minimal", 1)
}
branches := jobConfig.Branches
if jobType == "postsubmit" && len(branches) == 0 {
branches = append(branches, "^main$")
}
cluster, bucket, serviceAccountName := clusterDetails(jobType, jobConfig.Cluster, jobConfig.ServiceAccountName)
data := map[string]interface{}{
"architecture": jobConfig.Architecture,
"repoName": repoName,
"prowjobName": jobConfig.JobName,
"runIfChanged": jobConfig.RunIfChanged,
"skipIfOnlyChanged": jobConfig.SkipIfOnlyChanged,
"branches": branches,
"cronExpression": jobConfig.CronExpression,
"maxConcurrency": jobConfig.MaxConcurrency,
"timeout": jobConfig.Timeout,
"extraRefs": jobConfig.ExtraRefs,
"imageBuild": jobConfig.ImageBuild,
"useDockerBuildX": jobConfig.UseDockerBuildX,
"prCreation": jobConfig.PRCreation,
"runtimeImage": jobConfig.RuntimeImage,
"localRegistry": jobConfig.LocalRegistry,
"serviceAccountName": serviceAccountName,
"command": strings.Join(jobConfig.Commands, "\n&&\n"),
"builderBaseTag": templateBuilderBaseTag,
"buildkitImageTag": buildkitImageTag,
"resources": jobConfig.Resources,
"envVars": envVars,
"volumes": jobConfig.Volumes,
"volumeMounts": jobConfig.VolumeMounts,
"editWarning": editWarning,
"automountServiceAccountToken": jobConfig.AutomountServiceAccountToken,
"cluster": cluster,
"bucket": bucket,
"projectPath": jobConfig.ProjectPath,
"diskUsage": true,
"runAsUser": jobConfig.RunAsUser,
"runAsGroup": jobConfig.RunAsGroup,
}
err := GenerateProwjob(fileName, template, data)
if err != nil {
fmt.Printf("Error generating Prowjob %s: %v\n", fileName, err)
os.Exit(1)
}
}
}
}
}
func GenerateProwjob(prowjobFileName, templateContent string, data map[string]interface{}) error {
bytes, err := utils.ExecuteTemplate(templateContent, data)
if err != nil {
return fmt.Errorf("error executing template: %v", err)
}
jobsFolderPath, err := getJobsFolderPath()
if err != nil {
return fmt.Errorf("error getting jobs folder path: %v", err)
}
prowjobPath := filepath.Join(jobsFolderPath, data["repoName"].(string), prowjobFileName)
if err = os.MkdirAll(filepath.Dir(prowjobPath), 0o755); err != nil {
return fmt.Errorf("error creating Prowjob directory: %v", err)
}
if err = ioutil.WriteFile(prowjobPath, bytes, 0o644); err != nil {
return fmt.Errorf("error writing to path %s: %v", prowjobPath, err)
}
return nil
}
func getJobsFolderPath() (string, error) {
gitRootOutput, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
if err != nil {
return "", fmt.Errorf("error running the git command: %v", err)
}
gitRoot := strings.Fields(string(gitRootOutput))[0]
return filepath.Join(gitRoot, jobsFolder), nil
}
func useTemplate(jobType string) (string, error) {
switch jobType {
case "periodic":
return periodicTemplate, nil
case "postsubmit":
return postsubmitTemplate, nil
case "presubmit":
return presubmitTemplate, nil
default:
return "", fmt.Errorf("Unsupported job type: %s", jobType)
}
}
func clusterDetails(jobType string, cluster string, serviceAccountName string) (string, string, string) {
if cluster == "prow-postsubmits-cluster" {
jobType = "postsubmit"
}
cluster = "prow-presubmits-cluster"
bucket := "s3://prowpresubmitsdataclusterstack-prowbucket7c73355c-vfwwxd2eb4gp"
if jobType == "postsubmit" || jobType == "periodic" {
cluster = "prow-postsubmits-cluster"
bucket = "s3://prowdataclusterstack-316434458-prowbucket7c73355c-1n9f9v93wpjcm"
}
if len(serviceAccountName) == 0 {
serviceAccountName = jobType + "s-build-account"
}
return cluster, bucket, serviceAccountName
}