Skip to content

Commit

Permalink
Make the Image reconciliation generate deterministic build names
Browse files Browse the repository at this point in the history
kpack appears to periodically reconcile the Image before its internal Build lister is updated with the previous reconcile's Build. This leads to subsequent reconciles creating duplicate builds.

By using deterministic build names the reconcile of an image will result in an error attempting to create a new build if a image reconcile starts before the informer has the previously created build.
  • Loading branch information
matthewmcnew committed Oct 4, 2021
1 parent 3e4bdcd commit e956740
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 397 deletions.
51 changes: 0 additions & 51 deletions pkg/apis/build/v1alpha1/image_builds.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package v1alpha1

import (
"fmt"
"strconv"
"time"

"github.com/google/go-containerregistry/pkg/name"
Expand Down Expand Up @@ -31,56 +30,6 @@ const (

type BuildReason string

func (im *Image) Build(sourceResolver *SourceResolver, builder BuilderResource, latestBuild *Build, reasons, changes, cacheName string, nextBuildNumber int64) *Build {
buildNumber := strconv.Itoa(int(nextBuildNumber))

return &Build{
ObjectMeta: metav1.ObjectMeta{
Namespace: im.Namespace,
GenerateName: im.generateBuildName(buildNumber),
OwnerReferences: []metav1.OwnerReference{
*kmeta.NewControllerRef(im),
},
Labels: combine(im.Labels, map[string]string{
BuildNumberLabel: buildNumber,
ImageLabel: im.Name,
ImageGenerationLabel: strconv.Itoa(int(im.Generation)),
}),
Annotations: combine(im.Annotations, map[string]string{
BuildReasonAnnotation: reasons,
BuildChangesAnnotation: changes,
}),
},
Spec: BuildSpec{
Tags: im.generateTags(buildNumber),
Builder: builder.BuildBuilderSpec(),
Bindings: im.Bindings(),
Env: im.Env(),
Resources: im.Resources(),
ServiceAccount: im.Spec.ServiceAccount,
Source: sourceResolver.SourceConfig(),
CacheName: im.Status.BuildCacheName,
LastBuild: lastBuild(latestBuild),
Notary: im.Spec.Notary,
},
}
}

func lastBuild(latestBuild *Build) *LastBuild {
if latestBuild == nil {
return nil
}

if latestBuild.IsFailure() {
return latestBuild.Spec.LastBuild
}

return &LastBuild{
Image: latestBuild.BuiltImage(),
StackId: latestBuild.Stack(),
}
}

func (im *Image) LatestForImage(build *Build) string {
if build.IsSuccess() {
return build.BuiltImage()
Expand Down
294 changes: 0 additions & 294 deletions pkg/apis/build/v1alpha1/image_builds_test.go

This file was deleted.

6 changes: 3 additions & 3 deletions pkg/apis/build/v1alpha2/image_builds.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func (im *Image) Build(sourceResolver *SourceResolver, builder BuilderResource,

return &Build{
ObjectMeta: metav1.ObjectMeta{
Namespace: im.Namespace,
GenerateName: im.generateBuildName(buildNumber),
Namespace: im.Namespace,
Name: im.generateBuildName(buildNumber),
OwnerReferences: []metav1.OwnerReference{
*kmeta.NewControllerRef(im),
},
Expand Down Expand Up @@ -255,7 +255,7 @@ func (im *Image) generateTags(buildNumber string) []string {
}

func (im *Image) generateBuildName(buildNumber string) string {
return im.Name + "-build-" + buildNumber + "-"
return kmeta.ChildName(im.Name, "-build-"+buildNumber)
}

func combine(map1, map2 map[string]string) map[string]string {
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/build/v1alpha2/image_builds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func testImageBuilds(t *testing.T, when spec.G, it spec.S) {
it("generates a build name with build number", func() {
image.Name = "imageName"
build := image.Build(sourceResolver, builder, latestBuild, "", "", 27)
assert.Contains(t, build.GenerateName, "imageName-build-27-")
assert.Contains(t, build.Name, "imageName-build-27")
})

it("sets builder to be the Builder's resolved latestImage", func() {
Expand Down
Loading

0 comments on commit e956740

Please sign in to comment.