Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions config/serverless-operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ config:
- onDemand: true
version: "4.14"
promotion: {}
golangVersion: "1.25"
release-1.35:
konflux:
enabled: true
Expand Down Expand Up @@ -161,6 +162,7 @@ config:
promotion: {}
prowgen:
disabled: true
golangVersion: "1.25"
repositories:
- customConfigs:
- name: 420-aws-ovn
Expand Down
107 changes: 103 additions & 4 deletions pkg/dockerfilegen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"strings"
"text/template"

"github.com/openshift-knative/hack/config"
"github.com/openshift-knative/hack/pkg/soversion"

"github.com/coreos/go-semver/semver"
Expand Down Expand Up @@ -150,11 +151,11 @@ func generateDockerfile(params Params, mainPackagesPaths sets.Set[string]) error
if err != nil {
return err
}
goVersion := goMod.Go.Version
goModGoVersion := goMod.Go.Version

// The builder images are distinguished by golang major.minor, so we ignore the rest of the goVersion
if strings.Count(goVersion, ".") > 1 {
goVersion = strings.Join(strings.Split(goVersion, ".")[0:2], ".")
if strings.Count(goModGoVersion, ".") > 1 {
goModGoVersion = strings.Join(strings.Split(goModGoVersion, ".")[0:2], ".")
}

metadata, err := project.ReadMetadataFile(params.ProjectFilePath)
Expand All @@ -167,6 +168,15 @@ func generateDockerfile(params Params, mainPackagesPaths sets.Set[string]) error
metadata = project.DefaultMetadata()
}

// get target go version
goVersion, err := goVersionFromConfig(metadata)
if err != nil {
return fmt.Errorf("failed to get Go version from config: %w", err)
}
if goVersion == nil {
goVersion = &goModGoVersion
}

rhelVersion := RHEL9
templateFilePattern := "dockerfile-templates/rhel-9/*.tmpl"
var soVersion string
Expand Down Expand Up @@ -210,7 +220,7 @@ func generateDockerfile(params Params, mainPackagesPaths sets.Set[string]) error

builderImage := params.DockerfileImageBuilderFmt
if builderImage == "" {
builderImage = builderImageForGoVersion(goVersion, rhelVersion)
builderImage = builderImageForGoVersion(*goVersion, rhelVersion)
} else {
// Builder image might be provided without formatting '%s' string as plain value
if strings.Count(params.DockerfileImageBuilderFmt, "%s") == 1 {
Expand Down Expand Up @@ -408,6 +418,95 @@ func generateDockerfile(params Params, mainPackagesPaths sets.Set[string]) error
return nil
}

// goVersionFromConfig returns the target go version for the project.
// First it checks if a dedicated go version is defined for the repositories branch in the config.
// If no repo-branch specific config is set, it uses the one from the coresponding SO branch config.
// If SO does not have a default go version in it's config neither it returns nil to let the caller
// decide.
func goVersionFromConfig(metadata *project.Metadata) (*string, error) {
// get repo config
if imagePrefix := metadata.Project.ImagePrefix; imagePrefix != "" {

// this is a component repo
branch := ""
if metadata.Project.Tag == "knative-nightly" {
branch = "release-next"
} else {
branch = fmt.Sprintf("release-%s", strings.TrimPrefix(metadata.Project.Tag, "knative-"))
}

configFiles, err := fs.ReadDir(config.Configs, ".")
if err != nil {
return nil, fmt.Errorf("failed to read config directory: %w", err)
}

for _, configFile := range configFiles {
if configFile.IsDir() {
continue
}

content, err := fs.ReadFile(config.Configs, configFile.Name())
if err != nil {
return nil, fmt.Errorf("failed to load config from %s: %w", configFile.Name(), err)
}

cfg, err := prowgen.UnmarshalConfig(content)
if err != nil {
return nil, fmt.Errorf("failed to parse config from %s: %w", configFile.Name(), err)
}

for _, repo := range cfg.Repositories {
if repo.ImagePrefix == imagePrefix {
// this is the repo we are generating the dockerfile for

if branchConfig, ok := cfg.Config.Branches[branch]; ok {
if branchConfig.GolangVersion != nil && *branchConfig.GolangVersion != "" {
return branchConfig.GolangVersion, nil
}
// we didn't find a repo-branch specific default --> fall back to SO default
}
}
}
}
}

// check SO config
soBranch := ""
if metadata.Project.Tag != "" {
// this is a component repo (-> we falled through, as we didn't find a repo-branch specific default
if metadata.Project.Tag == "knative-nightly" || metadata.Project.Tag == "main" {
soBranch = "main"
} else {
soVersion := soversion.FromUpstreamVersion(strings.TrimPrefix(metadata.Project.Tag, "knative-"))
soBranch = soversion.BranchName(soVersion)
}
} else if metadata.Project.Version != "" {
majorMinor := strings.TrimSuffix(metadata.Project.Version, ".0")
soBranch = fmt.Sprintf("release-v%s", majorMinor)
}

if soBranch != "" {
soYaml, err := config.Configs.ReadFile("serverless-operator.yaml")
if err != nil {
return nil, fmt.Errorf("failed to load config for serverless-operator: %w", err)
}

soConfig, err := prowgen.UnmarshalConfig(soYaml)
if err != nil {
return nil, fmt.Errorf("failed to parse config for serverless-operator: %w", err)
}

if cfg, ok := soConfig.Config.Branches[soBranch]; ok {
if cfg.GolangVersion != nil && *cfg.GolangVersion != "" {
return cfg.GolangVersion, nil
}
// we didn't find a repo-branch specific default --> fall back to go.mod version
}
}

return nil, nil
}

func hasVendorFolder(dir string) (bool, error) {
info, err := os.Stat(path.Join(dir, "vendor"))
if err == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
FROM registry.ci.openshift.org/ocp/4.19:cli-artifacts as tools

# Dockerfile to bootstrap build and test in openshift-ci
FROM registry.ci.openshift.org/openshift/release:rhel-9-release-golang-1.22-openshift-4.17 as builder
FROM registry.ci.openshift.org/openshift/release:rhel-9-release-golang-1.25-openshift-4.21 as builder

ARG TARGETARCH

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# DO NOT EDIT! Generated Dockerfile for cmd/discover.
ARG GO_BUILDER=registry.ci.openshift.org/openshift/release:rhel-9-release-golang-1.22-openshift-4.17
ARG GO_BUILDER=registry.ci.openshift.org/openshift/release:rhel-9-release-golang-1.25-openshift-4.21
ARG GO_RUNTIME=registry.access.redhat.com/ubi9/ubi-minimal

FROM $GO_BUILDER as builder
Expand Down
1 change: 1 addition & 0 deletions pkg/prowgen/prowgen_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type Branch struct {
SkipE2EMatches []string `json:"skipE2EMatches,omitempty" yaml:"skipE2EMatches,omitempty"`
SkipDockerFilesMatches []string `json:"skipDockerFilesMatches,omitempty" yaml:"skipDockerFilesMatches,omitempty"`
Konflux *Konflux `json:"konflux,omitempty" yaml:"konflux,omitempty"`
GolangVersion *string `json:"golangVersion,omitempty" yaml:"golangVersion,omitempty"`

// DependabotEnabled enabled if `nil`.
DependabotEnabled *bool `json:"dependabotEnabled,omitempty" yaml:"dependabotEnabled,omitempty"`
Expand Down
Loading