Skip to content

Commit

Permalink
Add built image to image status
Browse files Browse the repository at this point in the history
- Add IsSuccess, IsFailure, and BuiltImage to Build type

closes #9

Signed-off-by: Joao Pereira <jdealmeidapereira@pivotal.io>
  • Loading branch information
Danny Joyce authored and joaopapereira committed Jul 22, 2019
1 parent 6f96eec commit 7ce7c91
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 4 deletions.
25 changes: 25 additions & 0 deletions pkg/apis/build/v1alpha1/build_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,28 @@ func (b *Build) BuildRef() string {

return b.GetName()
}

func (b *Build) BuiltImage() string {
if b == nil {
return ""
}
if !b.IsSuccess() {
return ""
}

return b.Spec.Image + "@" + b.Status.SHA
}

func (b *Build) IsSuccess() bool {
if b == nil {
return false
}
return b.Status.GetCondition(duckv1alpha1.ConditionSucceeded).IsTrue()
}

func (b *Build) IsFailure() bool {
if b == nil {
return false
}
return b.Status.GetCondition(duckv1alpha1.ConditionSucceeded).IsFalse()
}
1 change: 1 addition & 0 deletions pkg/apis/build/v1alpha1/image_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type ImageBuild struct {
type ImageStatus struct {
duckv1alpha1.Status `json:",inline"`
LastBuildRef string `json:"lastBuildRef"`
LastBuiltImage string `json:"lastBuiltImage"`
BuildCounter int64 `json:"buildCounter"`
BuildCacheName string `json:"buildCacheName"`
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/reconciler/v1alpha1/image/build_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package image
import (
"sort"

duckv1alpha1 "github.com/knative/pkg/apis/duck/v1alpha1"
v1alpha1build "github.com/pivotal/build-service-system/pkg/reconciler/v1alpha1/build"

"github.com/pivotal/build-service-system/pkg/apis/build/v1alpha1"
Expand All @@ -21,9 +20,9 @@ func newBuildList(builds []*v1alpha1.Build) (buildList, error) {
buildList := buildList{}

for _, build := range builds {
if build.Status.GetCondition(duckv1alpha1.ConditionSucceeded).IsTrue() {
if build.IsSuccess() {
buildList.successfulBuilds = append(buildList.successfulBuilds, build)
} else if build.Status.GetCondition(duckv1alpha1.ConditionSucceeded).IsFalse() {
} else if build.IsFailure() {
buildList.failedBuilds = append(buildList.failedBuilds, build)
}
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/reconciler/v1alpha1/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ func (c *Reconciler) Reconcile(ctx context.Context, key string) error {

image.Status.LastBuildRef = reconciledBuild.Build.BuildRef()
image.Status.BuildCounter = reconciledBuild.BuildCounter
if lastBuild.IsSuccess() {
image.Status.LastBuiltImage = lastBuild.BuiltImage()
}

err = c.deleteOldBuilds(namespace, image)
if err != nil {
Expand Down Expand Up @@ -271,7 +274,6 @@ func (c *Reconciler) fetchLastBuild(image *v1alpha1.Image) (*v1alpha1.Build, err
if err != nil {
return nil, err
}

return builds.lastBuild, nil
}

Expand Down
45 changes: 45 additions & 0 deletions pkg/reconciler/v1alpha1/image/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,7 @@ func testImageReconciler(t *testing.T, when spec.G, it spec.S) {
},
},
Status: v1alpha1.BuildStatus{
SHA: "some-sha",
Status: duckv1alpha1.Status{
Conditions: duckv1alpha1.Conditions{
{
Expand Down Expand Up @@ -752,6 +753,7 @@ func testImageReconciler(t *testing.T, when spec.G, it spec.S) {
ObservedGeneration: originalGeneration,
},
LastBuildRef: "image-name-build-2-00001", //GenerateNameReactor
LastBuiltImage: "some/image@some-sha",
BuildCounter: 2,
BuildCacheName: "",
},
Expand Down Expand Up @@ -805,6 +807,7 @@ func testImageReconciler(t *testing.T, when spec.G, it spec.S) {
},
},
Status: v1alpha1.BuildStatus{
SHA: "some-sha",
Status: duckv1alpha1.Status{
Conditions: duckv1alpha1.Conditions{
{
Expand Down Expand Up @@ -857,6 +860,7 @@ func testImageReconciler(t *testing.T, when spec.G, it spec.S) {
ObservedGeneration: originalGeneration,
},
LastBuildRef: "image-name-build-2-00001", //GenerateNameReactor
LastBuiltImage: "some/image@some-sha",
BuildCounter: 2,
BuildCacheName: "",
},
Expand Down Expand Up @@ -917,6 +921,7 @@ func testImageReconciler(t *testing.T, when spec.G, it spec.S) {
},
},
Status: v1alpha1.BuildStatus{
SHA: "some-sha",
Status: duckv1alpha1.Status{
Conditions: duckv1alpha1.Conditions{
{
Expand Down Expand Up @@ -975,6 +980,7 @@ func testImageReconciler(t *testing.T, when spec.G, it spec.S) {
ObservedGeneration: originalGeneration,
},
LastBuildRef: "image-name-build-2-00001", //GenerateNameReactor
LastBuiltImage: "some/image@some-sha",
BuildCounter: 2,
BuildCacheName: "",
},
Expand Down Expand Up @@ -1038,6 +1044,7 @@ func testImageReconciler(t *testing.T, when spec.G, it spec.S) {
it("does not schedule a build if the previous build spec matches the current desired spec", func() {
image.Status.BuildCounter = 1
image.Status.LastBuildRef = "image-name-build-1"
image.Status.LastBuiltImage = "some/image@some-sha"

sourceResolver := resolvedSourceResolver(image)
rt.Test(rtesting.TableRow{
Expand Down Expand Up @@ -1070,6 +1077,7 @@ func testImageReconciler(t *testing.T, when spec.G, it spec.S) {
},
},
Status: v1alpha1.BuildStatus{
SHA: "some-sha",
Status: duckv1alpha1.Status{
Conditions: duckv1alpha1.Conditions{
{
Expand Down Expand Up @@ -1119,6 +1127,7 @@ func testImageReconciler(t *testing.T, when spec.G, it spec.S) {
it("deletes a successful build if more than the limit", func() {
image.Spec.SuccessBuildHistoryLimit = limit(4)
image.Status.LastBuildRef = "image-name-build-5"
image.Status.LastBuiltImage = "some/image@some-sha"
image.Status.BuildCounter = 5
sourceResolver := resolvedSourceResolver(image)

Expand All @@ -1145,6 +1154,41 @@ func testImageReconciler(t *testing.T, when spec.G, it spec.S) {
})
})
})

it("updates the last successful build on the image when the last build is successful", func() {
image.Status.BuildCounter = 1
image.Status.LastBuildRef = "image-name-build-1"
image.Status.LastBuiltImage = "some/image@some-old-sha"

sourceResolver := resolvedSourceResolver(image)
rt.Test(rtesting.TableRow{
Key: key,
Objects: runtimeObjects(
successfulBuilds(image, sourceResolver, 1),
image,
builder,
sourceResolver,
),
WantErr: false,
WantStatusUpdates: []clientgotesting.UpdateActionImpl{
{
Object: &v1alpha1.Image{
ObjectMeta: image.ObjectMeta,
Spec: image.Spec,
Status: v1alpha1.ImageStatus{
Status: duckv1alpha1.Status{
ObservedGeneration: originalGeneration,
},
LastBuildRef: "image-name-build-1",
LastBuiltImage: "some/image@some-sha",
BuildCounter: 1,
BuildCacheName: "",
},
},
},
},
})
})
})
})
}
Expand Down Expand Up @@ -1206,6 +1250,7 @@ func builds(image *v1alpha1.Image, sourceResolver *v1alpha1.SourceResolver, coun
},
},
Status: v1alpha1.BuildStatus{
SHA: "some-sha",
Status: duckv1alpha1.Status{
Conditions: duckv1alpha1.Conditions{
condition,
Expand Down

0 comments on commit 7ce7c91

Please sign in to comment.