Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions tools/prowgen/pkg/decorator/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,48 @@ const (

var variableSubstitutionRegex = regexp.MustCompile(`\$\([_a-zA-Z0-9.-]+(\.[_a-zA-Z0-9.-]+)*\)`)

func ApplyVariables(job spec.Job, params map[string]string, matrix map[string][]string) []spec.Job {
func applyArch(arch string, job spec.Job) spec.Job {
// For backwards compatibility, amd64 is not suffixed
if arch != "amd64" {
job.Name += "-" + arch
if job.NodeSelector == nil {
job.NodeSelector = map[string]string{}
}
// TODO apply everywhere
job.NodeSelector["kubernetes.io/arch"] = arch
}
return job
}

func ApplyVariables(job spec.Job, architectures []string, params map[string]string, matrix map[string][]string) []spec.Job {
yamlBS, err := yaml.Marshal(job)
if err != nil {
log.Fatalf("Failed to marshal the given Job: %v", err)
}

subsExps := getVarSubstitutionExpressions(string(yamlBS))
if len(subsExps) == 0 {
return []spec.Job{job}
}
jobs := make([]spec.Job, 0)

resolvedYAMLStr := applyParams(string(yamlBS), subsExps, params)
resolvedYAMLStrs := applyMatrix(resolvedYAMLStr, subsExps, matrix)
for _, arch := range architectures {
subsExps := getVarSubstitutionExpressions(string(yamlBS))
if len(subsExps) == 0 {
jobs = append(jobs, applyArch(arch, job))
continue
}
if params == nil {
params = map[string]string{}
}
params["arch"] = arch

jobs := make([]spec.Job, 0)
for _, jobYaml := range resolvedYAMLStrs {
job := spec.Job{}
if err := yaml.Unmarshal([]byte(jobYaml), &job); err != nil {
log.Fatalf("Failed to unmarshal the yaml to Job: %v", err)
resolvedYAMLStr := applyParams(string(yamlBS), subsExps, params)
resolvedYAMLStrs := applyMatrix(resolvedYAMLStr, subsExps, matrix)

for _, jobYaml := range resolvedYAMLStrs {
job := spec.Job{}
if err := yaml.Unmarshal([]byte(jobYaml), &job); err != nil {
log.Fatalf("Failed to unmarshal the yaml to Job: %v", err)
}
jobs = append(jobs, applyArch(arch, job))
}
jobs = append(jobs, job)
}
return jobs
}
Expand Down
15 changes: 13 additions & 2 deletions tools/prowgen/pkg/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ const (

DefaultAutogenHeader = "# THIS FILE IS AUTOGENERATED, DO NOT EDIT IT MANUALLY."

ArchAMD64 = "amd64"
ArchARM64 = "arm64"

TypePostsubmit = "postsubmit"
TypePresubmit = "presubmit"
TypePeriodic = "periodic"
Expand Down Expand Up @@ -82,7 +85,7 @@ func (cli *Client) ReadJobsConfig(file string) spec.JobsConfig {
log.Fatalf("Failed to read %q: %v", file, err)
}
jobsConfig := spec.JobsConfig{}
if err := yaml.Unmarshal(yamlFile, &jobsConfig); err != nil {
if err := yaml.UnmarshalStrict(yamlFile, &jobsConfig); err != nil {
log.Fatalf("Failed to unmarshal %q: %v", file, err)
}

Expand Down Expand Up @@ -184,6 +187,11 @@ func validateJobsConfig(fileName string, jobsConfig spec.JobsConfig) error {
err = multierror.Append(err, e)
}
}
for _, t := range job.Architectures {
if e := validate(t, sets.NewString(ArchAMD64, ArchARM64, TypePeriodic), "architectures"); e != nil {
err = multierror.Append(err, e)
}
}
for _, repo := range job.Repos {
if len(strings.Split(repo, "/")) != 2 {
err = multierror.Append(err, fmt.Errorf("%s: repo %v not valid, should take form org/repo", fileName, repo))
Expand Down Expand Up @@ -212,7 +220,10 @@ func (cli *Client) ConvertJobConfig(fileName string, jobsConfig spec.JobsConfig,
var periodics []config.Periodic

for _, parentJob := range jobsConfig.Jobs {
expandedJobs := decorator.ApplyVariables(parentJob, jobsConfig.Params, jobsConfig.Matrix)
if len(parentJob.Architectures) == 0 {
parentJob.Architectures = []string{ArchAMD64}
}
expandedJobs := decorator.ApplyVariables(parentJob, parentJob.Architectures, jobsConfig.Params, jobsConfig.Matrix)
for _, job := range expandedJobs {
brancher := config.Brancher{
Branches: []string{fmt.Sprintf("^%s$", branch)},
Expand Down
2 changes: 1 addition & 1 deletion tools/prowgen/pkg/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

func TestGenerateConfig(t *testing.T) {
bc := ReadBase(nil, "testdata/.base.yaml")
cli := &Client{BaseConfig: *bc}
cli := &Client{BaseConfig: bc}
tests := []struct {
name string
expectError bool
Expand Down
2 changes: 2 additions & 0 deletions tools/prowgen/pkg/spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ type Job struct {
Tags []string `json:"tags,omitempty"`
Types []string `json:"types,omitempty"`
Repos []string `json:"repos,omitempty"`
// Architectures defines architectures to build as. Defaults to amd64.
Architectures []string `json:"architectures,omitempty"`

GerritPresubmitLabel string `json:"gerrit_presubmit_label,omitempty"`
GerritPostsubmitLabel string `json:"gerrit_postsubmit_label,omitempty"`
Expand Down
1 change: 0 additions & 1 deletion tools/prowgen/pkg/testdata/long-job-name.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ jobs:
resources: custom
command: [prow/command.sh]
repos: [istio/istio]
modifier: [presubmit_skipped]
trigger: "/test basic"

requirements: [gocache]
Expand Down
134 changes: 134 additions & 0 deletions tools/prowgen/pkg/testdata/simple.gen.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion tools/prowgen/pkg/testdata/simple.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,20 @@ jobs:
resources: custom
command: [prow/command.sh]
repos: [istio/istio]
modifier: [presubmit_skipped]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if there was a reason to remove this line, but noting it was removed in the test-data as well.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it no longer is a real field - I made the yaml parser strict

trigger: "/test basic"

- name: multi-arch
types: [presubmit]
architectures: [amd64, arm64]
command: [prow/command.sh]
image: test

- name: multi-arch-param
types: [presubmit]
architectures: [amd64, arm64]
command: [prow/command.sh, $(params.arch)]
image: test

@ericvn ericvn Jun 17, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have an arm64 only test job?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means "make an arm job and make an amd job". So it's mostly syntax sugar - they are independent

requirements: [gocache]

resources_presets:
Expand Down