diff --git a/prow/cmd/image-builder/buildcontroller.go b/prow/cmd/image-builder/buildcontroller.go index 3ac4189d6..30729beac 100644 --- a/prow/cmd/image-builder/buildcontroller.go +++ b/prow/cmd/image-builder/buildcontroller.go @@ -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)) @@ -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) } diff --git a/prow/cmd/image-builder/buildcontroller_test.go b/prow/cmd/image-builder/buildcontroller_test.go index e229f5135..fa79c393f 100644 --- a/prow/cmd/image-builder/buildcontroller_test.go +++ b/prow/cmd/image-builder/buildcontroller_test.go @@ -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{ diff --git a/prow/cmd/image-builder/main.go b/prow/cmd/image-builder/main.go index 3ed5c0d91..603a3291b 100644 --- a/prow/cmd/image-builder/main.go +++ b/prow/cmd/image-builder/main.go @@ -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" @@ -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 } @@ -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") @@ -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- 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)) @@ -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