diff --git a/pkg/rehearse/jobs.go b/pkg/rehearse/jobs.go index ddfd76e2ff0..4ab98cf9073 100644 --- a/pkg/rehearse/jobs.go +++ b/pkg/rehearse/jobs.go @@ -241,12 +241,12 @@ func ConfigureRehearsalJobs(toBeRehearsed config.Presubmits, ciopConfigs config. // The job selection is done by iterating in an unspecified order, which avoids picking the same job // So if a template will be changed, find the jobs that are using a template in combination with the `aws`,`openstack`,`gcs` and `libvirt` cluster types. func AddRandomJobsForChangedTemplates(templates []config.ConfigMapSource, toBeRehearsed config.Presubmits, prConfigPresubmits map[string][]prowconfig.Presubmit, loggers Loggers, prNumber int) config.Presubmits { + clusterTypes := getClusterTypes(prConfigPresubmits) rehearsals := make(config.Presubmits) for _, template := range templates { templateFile := filepath.Base(template.Filename) - for _, clusterType := range []string{"aws", "gcs", "openstack", "libvirt", "vsphere", "gcp"} { - + for _, clusterType := range clusterTypes { if isAlreadyRehearsed(toBeRehearsed, clusterType, templateFile) { continue } @@ -261,6 +261,27 @@ func AddRandomJobsForChangedTemplates(templates []config.ConfigMapSource, toBeRe return rehearsals } +func getClusterTypes(jobs map[string][]prowconfig.Presubmit) []string { + ret := sets.NewString() + for _, jobs := range jobs { + for _, j := range jobs { + if j.Spec != nil && j.Spec.Containers != nil { + for _, c := range j.Spec.Containers { + for _, e := range c.Env { + if e.Name == clusterTypeEnvName { + ret.Insert(e.Value) + } + } + } + } + } + } + if len(ret) == 0 { + return nil + } + return ret.List() +} + func isAlreadyRehearsed(toBeRehearsed config.Presubmits, clusterType, templateFile string) bool { for _, jobs := range toBeRehearsed { for _, job := range jobs { diff --git a/pkg/rehearse/jobs_test.go b/pkg/rehearse/jobs_test.go index 6a979cc512b..b7b547fb591 100644 --- a/pkg/rehearse/jobs_test.go +++ b/pkg/rehearse/jobs_test.go @@ -961,3 +961,51 @@ func createVolumesHelper(name, key string) []v1.Volume { return volumes } + +func TestGetClusterTypes(t *testing.T) { + makeJob := func(clusterType string) prowconfig.Presubmit { + ret := prowconfig.Presubmit{ + JobBase: prowconfig.JobBase{ + Agent: string(pjapi.KubernetesAgent), + }, + } + if clusterType != "" { + ret.Spec = &v1.PodSpec{ + Containers: []v1.Container{{ + Env: []v1.EnvVar{{ + Name: clusterTypeEnvName, + Value: clusterType, + }}, + }}, + } + } + return ret + } + type Jobs map[string][]prowconfig.Presubmit + for _, tc := range []struct { + id string + jobs Jobs + want []string + }{{ + id: "no types", + jobs: Jobs{"org/repo": {makeJob("")}}, + }, { + id: "one type", + jobs: Jobs{"org/repo": {makeJob(""), makeJob("aws")}}, + want: []string{"aws"}, + }, { + id: "multiple types", + jobs: Jobs{ + "org/repo": {makeJob(""), makeJob("aws")}, + "org/sitory": {makeJob("azure"), makeJob("vsphere")}, + }, + want: []string{"aws", "azure", "vsphere"}, + }} { + t.Run(tc.id, func(t *testing.T) { + ret := getClusterTypes(tc.jobs) + if !reflect.DeepEqual(tc.want, ret) { + t.Fatal(diff.ObjectDiff(tc.want, ret)) + } + }) + } +}