-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prowjob-generator allows to generate the periodic and presubmit configuration files in test-infra from a configuration file which simplifies and automates introducing and chaning tests for branches.
- Loading branch information
Showing
10 changed files
with
1,008 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
// ProwIgnoredConfig is the top-level configuration struct. Because we want to | ||
// store the configuration in test-infra as yaml file, we have to prevent prow | ||
// from trying to parse our configuration as prow configuration. Prow provides | ||
// the well-known `prow_ignored` key which is not parsed further by Prow. | ||
type ProwIgnoredConfig struct { | ||
ProwIgnored Config `json:"prow_ignored"` | ||
} | ||
|
||
// Config is the configuration file struct. | ||
type Config struct { | ||
Branches map[string]BranchConfig `json:"branches"` | ||
Versions map[string]VersionMapper `json:"versions"` | ||
} | ||
|
||
// BranchConfig is the branch-based configuration struct. | ||
type BranchConfig struct { | ||
Interval string `json:"interval"` | ||
KubekinsImage string `json:"kubekinsImage"` | ||
KubernetesVersionManagement string `json:"kubernetesVersionManagement"` | ||
KubebuilderEnvtestKubernetesVersion string `json:"kubebuilderEnvtestKubernetesVersion"` | ||
Upgrades []Upgrade `json:"upgrades"` | ||
} | ||
|
||
// Upgrade describes a kubernetes upgrade. | ||
type Upgrade struct { | ||
From string `json:"from"` | ||
To string `json:"to"` | ||
} | ||
|
||
// VersionMapper is a key value map strings to versions. | ||
type VersionMapper map[string]string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// main is the main package for prowjob-generator. | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"path" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/Masterminds/sprig" | ||
"github.com/pkg/errors" | ||
"k8s.io/klog/v2" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
var ( | ||
configFile = flag.String("config", "", "Path to the config file") | ||
outputDir = flag.String("output-dir", "", "Path to the directory to create the files in") | ||
fileNamePrefix = flag.String("file-name-prefix", "cluster-api-", "File name prefix for the created prowjob files") | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
if *outputDir == "" { | ||
klog.Fatal("Expected flag \"output-dir\" to be set") | ||
} | ||
|
||
if *configFile == "" { | ||
klog.Fatal("Expected flag \"config\" to be set") | ||
} | ||
|
||
rawConfig, err := os.ReadFile(*configFile) | ||
if err != nil { | ||
klog.Fatalf("Failed to read config file %q: %v", *configFile, err) | ||
} | ||
|
||
prowIgnoredConfig := ProwIgnoredConfig{} | ||
if err := yaml.Unmarshal(rawConfig, &prowIgnoredConfig); err != nil { | ||
klog.Fatalf("Failed to parse config file %q: %v", *configFile, err) | ||
} | ||
|
||
g, err := newGenerator(prowIgnoredConfig.ProwIgnored, *outputDir) | ||
if err != nil { | ||
klog.Fatalf("Failed to initialize generator: %v", err) | ||
} | ||
|
||
if err := g.generate(); err != nil { | ||
klog.Fatalf("Failed to generate prowjobs: %v", err) | ||
} | ||
} | ||
|
||
func k8sShortVersionString(s string) string { | ||
if strings.HasPrefix(s, "ci/latest-") { | ||
return "latest" | ||
} | ||
return strings.ReplaceAll(strings.TrimPrefix(s, "stable-"), ".", "-") | ||
} | ||
|
||
func (g *generator) k8sVersionLookup(version, component string) string { | ||
v, ok := g.config.Versions[version] | ||
if !ok { | ||
klog.Fatalf("Failed to lookup version (%q) in config", version) | ||
} | ||
c, ok := v[component] | ||
if !ok { | ||
klog.Fatalf("Failed to lookup component version (%q) for version %q in config", component, version) | ||
} | ||
return c | ||
} | ||
|
||
func (g *generator) lastUpgradeVersionFrom(branch string) string { | ||
upgrades := g.config.Branches[branch].Upgrades | ||
return upgrades[len(upgrades)-1].From | ||
} | ||
func (g *generator) lastUpgradeVersionTo(branch string) string { | ||
upgrades := g.config.Branches[branch].Upgrades | ||
return upgrades[len(upgrades)-1].To | ||
} | ||
|
||
func newGenerator(config Config, outputDir string) (*generator, error) { | ||
g := &generator{ | ||
config: config, | ||
outputDir: outputDir, | ||
} | ||
funcs := sprig.HermeticTxtFuncMap() | ||
funcs["k8sShortVersionString"] = k8sShortVersionString | ||
funcs["k8sVersionLookup"] = g.k8sVersionLookup | ||
funcs["lastUpgradeVersionFrom"] = g.lastUpgradeVersionFrom | ||
funcs["lastUpgradeVersionTo"] = g.lastUpgradeVersionTo | ||
|
||
var err error | ||
g.templates, err = template.New("").Funcs(funcs).ParseFS(templatesFS, "templates/*.yaml") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return g, err | ||
} | ||
|
||
type generator struct { | ||
templates *template.Template | ||
config Config | ||
outputDir string | ||
} | ||
|
||
var jobTypes = [][]string{ | ||
{"periodics"}, | ||
{"periodics", "upgrades"}, | ||
{"presubmits"}, | ||
} | ||
|
||
func (g *generator) generate() error { | ||
for _, jobType := range jobTypes { | ||
for branch := range g.config.Branches { | ||
if err := g.generateProwjobs(branch, jobType...); err != nil { | ||
return errors.Wrapf(err, "Generating prowjobs for type %s", strings.Join(jobType, "-")) | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (g *generator) generateProwjobs(branch string, jobType ...string) error { | ||
fileName := g.generateFilename(branch, jobType...) | ||
templateName := strings.Join(jobType, "-") + ".yaml" | ||
|
||
klog.Infof("Generating prowjobs to %q using template %q", fileName, templateName) | ||
|
||
data := map[string]interface{}{ | ||
"branch": branch, | ||
"config": g.config.Branches[branch], | ||
} | ||
|
||
var out bytes.Buffer | ||
if err := g.templates.ExecuteTemplate(&out, templateName, data); err != nil { | ||
return errors.Wrapf(err, "Executing template %q for branch %q", templateName, branch) | ||
} | ||
|
||
filePath := path.Join(g.outputDir, fileName) | ||
if err := os.WriteFile(filePath, out.Bytes(), 0644); err != nil { //nolint:gosec | ||
return errors.Wrapf(err, "Writing prowjob to %q", filePath) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (g *generator) generateFilename(branch string, jobType ...string) string { | ||
fileFriendlyBranchName := strings.ReplaceAll(branch, ".", "-") | ||
|
||
n := fmt.Sprintf("%s%s-%s", *fileNamePrefix, jobType[0], fileFriendlyBranchName) | ||
if len(jobType) == 2 { | ||
n = fmt.Sprintf("%s-%s", n, jobType[1]) | ||
} | ||
return n + ".yaml" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// main is the main package for prowjob-generator. | ||
package main | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func Test_newGenerator(t *testing.T) { | ||
_, err := newGenerator(Config{}, "") | ||
if err != nil { | ||
t.Errorf("newGenerator() error = %v", err) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"embed" | ||
) | ||
|
||
var ( | ||
//go:embed templates/*.yaml | ||
templatesFS embed.FS | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
_ "embed" | ||
"testing" | ||
"text/template" | ||
) | ||
|
||
func Test_Templates(t *testing.T) { | ||
if _, err := template.ParseFS(templatesFS, "templates/*.yaml"); err != nil { | ||
t.Errorf("Error parsing embedded templates: %v", err) | ||
} | ||
} |
Oops, something went wrong.