-
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
369 additions
and
17 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
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,56 @@ | ||
/* | ||
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"` | ||
Templates []Template `json:"templates"` | ||
VersionsMapper VersionsMapper `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"` | ||
} | ||
|
||
// Template refers a template file and defines the target file name format. | ||
type Template struct { | ||
Format string `json:"format"` | ||
Name string `json:"name"` | ||
} | ||
|
||
// Upgrade describes a kubernetes upgrade. | ||
type Upgrade struct { | ||
From string `json:"from"` | ||
To string `json:"to"` | ||
} | ||
|
||
// VersionsMapper provides key value pairs for a parent key. | ||
type VersionsMapper map[string]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,163 @@ | ||
/* | ||
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 ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/Masterminds/sprig" | ||
"github.com/pkg/errors" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
const generatedFileHeader = "# Code generated by cluster-api's prowjob-gen. DO NOT EDIT.\n" | ||
|
||
// newGenerator initializes a generator which includes parsing the configured templates. | ||
func newGenerator(config Config, templatesDir, outputDir string) (*generator, error) { | ||
g := &generator{ | ||
config: config, | ||
outputDir: outputDir, | ||
createdFiles: map[string]bool{}, | ||
} | ||
|
||
var err error | ||
g.templates, err = template.New(""). | ||
Funcs(g.templateFunctions()). | ||
ParseGlob(templatesDir + "/*.yaml.tpl") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return g, err | ||
} | ||
|
||
type generator struct { | ||
templates *template.Template | ||
config Config | ||
outputDir string | ||
createdFiles map[string]bool | ||
} | ||
|
||
// generate executes every template for every branch and writes the result to a | ||
// file in outputDir. | ||
func (g *generator) generate() error { | ||
for _, tpl := range g.config.Templates { | ||
for branch := range g.config.Branches { | ||
out, err := g.executeTemplate(branch, tpl.Name) | ||
if err != nil && !errors.Is(err, os.ErrNotExist) { | ||
return errors.Wrapf(err, "Generating prowjobs for template %s", tpl.Name) | ||
} | ||
|
||
fileName := fmt.Sprintf(tpl.Format, strings.ReplaceAll(branch, ".", "-")) | ||
filePath := filepath.Clean(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) | ||
} | ||
|
||
g.createdFiles[fileName] = true | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// cleanup deletes files which have the generatedFileHeader and had not been updated | ||
// during generate. | ||
func (g *generator) cleanup() error { | ||
entries, err := os.ReadDir(g.outputDir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, entry := range entries { | ||
if _, ok := g.createdFiles[entry.Name()]; ok { | ||
continue | ||
} | ||
|
||
if entry.IsDir() { | ||
continue | ||
} | ||
|
||
path := filepath.Clean(path.Join(g.outputDir, entry.Name())) | ||
data, err := os.ReadFile(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if strings.HasPrefix(string(data), generatedFileHeader) { | ||
klog.Infof("Deleting file %s", entry.Name()) | ||
if err := os.Remove(path); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// executeTemplate executes a previously parsed template with the data for a specific branch. | ||
func (g *generator) executeTemplate(branch, templateName string) (*bytes.Buffer, error) { | ||
klog.Infof("executing template %q for branch %q", templateName, branch) | ||
|
||
data := map[string]interface{}{ | ||
"branch": branch, | ||
"config": g.config.Branches[branch], | ||
} | ||
|
||
var out bytes.Buffer | ||
|
||
// Write yaml comment as header to indicate this file got generated. | ||
out.WriteString(generatedFileHeader) | ||
|
||
if err := g.templates.ExecuteTemplate(&out, templateName, data); err != nil { | ||
return nil, errors.Wrapf(err, "Executing template %q for branch %q", templateName, branch) | ||
} | ||
|
||
return &out, nil | ||
} | ||
|
||
// templateFunctions returns the functions available inside of templates. | ||
func (g *generator) templateFunctions() template.FuncMap { | ||
funcs := sprig.HermeticTxtFuncMap() | ||
funcs["versionMapperLookup"] = g.versionMapperLookup | ||
funcs["lastUpgradeVersion"] = g.lastUpgradeVersion | ||
return funcs | ||
} | ||
|
||
// versionMapperLookup returns a value from the versions mapper for a given version and key. | ||
func (g *generator) versionMapperLookup(version, key string) string { | ||
v, ok := g.config.VersionsMapper[version] | ||
if !ok { | ||
klog.Fatalf("Failed to lookup version (%q) in config", version) | ||
} | ||
c, ok := v[key] | ||
if !ok { | ||
klog.Fatalf("Failed to lookup component version (%q) for version %q in config", key, version) | ||
} | ||
return c | ||
} | ||
|
||
// lastUpgradeVersion returns the last Upgrade entry in the Upgrades slice for a given branch. | ||
func (g *generator) lastUpgradeVersion(branch string) Upgrade { | ||
upgrades := g.config.Branches[branch].Upgrades | ||
return upgrades[len(upgrades)-1] | ||
} |
Oops, something went wrong.