Skip to content

Commit

Permalink
hack: implement prowjob-generator
Browse files Browse the repository at this point in the history
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
chrischdi committed Jan 9, 2024
1 parent c8c0794 commit 2401856
Show file tree
Hide file tree
Showing 10 changed files with 1,008 additions and 0 deletions.
2 changes: 2 additions & 0 deletions hack/tools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ replace sigs.k8s.io/cluster-api/test => ../../test

require (
cloud.google.com/go/storage v1.36.0
github.com/Masterminds/sprig v2.22.0+incompatible
github.com/blang/semver/v4 v4.0.0
github.com/onsi/gomega v1.30.0
github.com/pkg/errors v0.9.1
Expand Down Expand Up @@ -37,6 +38,7 @@ require (
cloud.google.com/go/iam v1.1.5 // indirect
github.com/MakeNowJust/heredoc v1.0.0 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/semver/v3 v3.2.0 // indirect
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
Expand Down
4 changes: 4 additions & 0 deletions hack/tools/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Masterminds/semver/v3 v3.2.0 h1:3MEsd0SM6jqZojhjLWWeBY+Kcjy9i6MQAeY7YgDP83g=
github.com/Masterminds/semver/v3 v3.2.0/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/Masterminds/sprig v2.22.0+incompatible h1:z4yfnGrZ7netVz+0EDJ0Wi+5VZCSYp4Z0m2dk6cEM60=
github.com/Masterminds/sprig v2.22.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o=
github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj9n6YA=
github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM=
github.com/Microsoft/go-winio v0.5.2 h1:a9IhgEQBCUEk6QCdml9CiJGhAws+YwffDHEMp1VMrpA=
Expand Down
49 changes: 49 additions & 0 deletions hack/tools/prowjob-generator/config.go
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
175 changes: 175 additions & 0 deletions hack/tools/prowjob-generator/main.go
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"
}
30 changes: 30 additions & 0 deletions hack/tools/prowjob-generator/main_test.go
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
}
}
26 changes: 26 additions & 0 deletions hack/tools/prowjob-generator/template.go
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
)
29 changes: 29 additions & 0 deletions hack/tools/prowjob-generator/template_test.go
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)
}
}
Loading

0 comments on commit 2401856

Please sign in to comment.