Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to inject EFFECTIVE_VERSION build-arg #190

Merged
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
17 changes: 11 additions & 6 deletions prow/cmd/image-builder/buildcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ func (r *buildReconciler) defineDestinations(target string) ([]string, error) {

var destinations []string

if r.options.addVersionTag || r.options.addVersionSHATag {
if r.options.addVersionTag || r.options.addVersionSHATag || r.options.injectEffectiveVersion {
var version string

versionFile, err := os.Open(fmt.Sprintf("/home/prow/go/src/github.com/%s/%s/VERSION", r.options.org, r.options.repo))
Expand Down Expand Up @@ -466,18 +466,23 @@ func (r *buildReconciler) defineDestinations(target string) ([]string, error) {
destinations = append(destinations, destination)
}

effectiveVersion := fmt.Sprintf("%s-%s", version, r.options.headSHA)
if r.options.addVersionSHATag {
tag := fmt.Sprintf("%s-%s", version, r.options.baseSHA)
destination := fmt.Sprintf("--destination=%s/%s:%s", r.options.registry, target, tag)
destination := fmt.Sprintf("--destination=%s/%s:%s", r.options.registry, target, effectiveVersion)
destinations = append(destinations, destination)
}

// this is a hack, inject effective version build arg here, as we already have calculated it here
if r.options.injectEffectiveVersion {
destinations = append(destinations, fmt.Sprintf("--build-arg=EFFECTIVE_VERSION=%s", effectiveVersion))
}
}

if r.options.addDateSHATag {
if len(r.options.baseSHA) < 7 {
return destinations, fmt.Errorf("baseSHA %v is it a correct SHA", r.options.baseSHA)
if len(r.options.headSHA) < 7 {
return destinations, fmt.Errorf("baseSHA %v is it a correct SHA", r.options.headSHA)
}
tag := fmt.Sprintf("v%s-%s", time.Now().Format("20060102"), r.options.baseSHA[:7])
tag := fmt.Sprintf("v%s-%s", time.Now().Format("20060102"), r.options.headSHA[:7])
destination := fmt.Sprintf("--destination=%s/%s:%s", r.options.registry, target, tag)
destinations = append(destinations, destination)
}
Expand Down
2 changes: 1 addition & 1 deletion prow/cmd/image-builder/buildcontroller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func createTestImageBuildController(t *testing.T, initObjs ...client.Object) *bu
logLevel: "debug",
targets: flagutil.NewStrings("target1", "target2", "target3"),
kanikoArgs: flagutil.NewStrings("--build-arg=buildarg1=abc", "--build-arg=buildarg1=xyz"),
baseSHA: "abcdef1234567890",
headSHA: "abcdef1234567890",
}

r := &buildReconciler{
Expand Down
50 changes: 32 additions & 18 deletions prow/cmd/image-builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
prowjobv1 "k8s.io/test-infra/prow/apis/prowjobs/v1"
"k8s.io/test-infra/prow/flagutil"
"k8s.io/test-infra/prow/interrupts"
"k8s.io/test-infra/prow/logrusutil"
Expand All @@ -36,20 +37,21 @@ import (
)

type options struct {
dockerConfigSecret string
org string
repo string
baseSHA string
dockerfile string
targets flagutil.Strings
kanikoArgs flagutil.Strings
registry string
cacheRegistry string
kanikoImage string
addVersionTag bool
addVersionSHATag bool
addDateSHATag bool
addFixedTags flagutil.Strings
dockerConfigSecret string
org string
repo string
headSHA string
dockerfile string
targets flagutil.Strings
kanikoArgs flagutil.Strings
registry string
cacheRegistry string
kanikoImage string
addVersionTag bool
addVersionSHATag bool
addDateSHATag bool
addFixedTags flagutil.Strings
injectEffectiveVersion bool

logLevel string
}
Expand All @@ -70,6 +72,9 @@ func (o *options) Validate() error {
if !o.addVersionTag && !o.addVersionSHATag && !o.addDateSHATag && len(o.addFixedTags.Strings()) == 0 {
return fmt.Errorf("please choose at least one tagging scheme")
}
if o.headSHA == "" {
return errors.New("Head SHA must not be empty")
}
for _, kanikoArg := range o.kanikoArgs.Strings() {
if strings.HasPrefix(kanikoArg, "--cache=") || strings.HasPrefix(kanikoArg, "--cache-repo=") {
return fmt.Errorf("please use --cache-registry option to enable/disable cache")
Expand Down Expand Up @@ -98,6 +103,7 @@ func gatherOptions() options {
fs.BoolVar(&o.addVersionSHATag, "add-version-sha-tag", false, "Add label from VERSION file of git root directory plus SHA from git HEAD to image tags")
fs.BoolVar(&o.addDateSHATag, "add-date-sha-tag", false, "Using vYYYYMMDD-<rev short> scheme which is compatible to autobumper")
fs.Var(&o.addFixedTags, "add-fixed-tag", "Add a fixed tag to images")
fs.BoolVar(&o.injectEffectiveVersion, "inject-effective-version", true, "Inject EFFECTIVE_VERSION build-arg")

fs.StringVar(&o.logLevel, "log-level", "info", fmt.Sprintf("Log level is one of %v.", logrus.AllLevels))

Expand All @@ -111,12 +117,20 @@ func gatherOptions() options {
logrus.Fatalf("Unable to resolve prow job spec: %v", err)
}

if jobSpec.Refs != nil {
switch jobSpec.Type {
case prowjobv1.PeriodicJob:
logrus.Fatal("Image-builder cannot be used in periodic prow jobs")
case prowjobv1.BatchJob:
logrus.Info("Skip build in batch job")
os.Exit(0)
case prowjobv1.PostsubmitJob:
o.org = jobSpec.Refs.Org
o.repo = jobSpec.Refs.Repo
o.headSHA = jobSpec.Refs.BaseSHA
case prowjobv1.PresubmitJob:
o.org = jobSpec.Refs.Org
o.repo = jobSpec.Refs.Repo
o.baseSHA = jobSpec.Refs.BaseSHA
} else {
logrus.Fatal("Unable to find a valid git ref")
o.headSHA = jobSpec.Refs.Pulls[0].SHA
}

return o
Expand Down