Skip to content

Commit

Permalink
Persist cache image in build
Browse files Browse the repository at this point in the history
  • Loading branch information
modulo11 committed Apr 22, 2021
1 parent 757bfbc commit 6b3e580
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 10 deletions.
11 changes: 11 additions & 0 deletions pkg/apis/build/v1alpha1/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ func (b *Build) BuiltImage() string {
return b.Status.LatestImage
}

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

return b.Status.LatestCacheImage
}

func (b *Build) IsSuccess() bool {
if b == nil {
return false
Expand Down
6 changes: 4 additions & 2 deletions pkg/apis/build/v1alpha1/build_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ type Binding struct {

// +k8s:openapi-gen=true
type LastBuild struct {
Image string `json:"image,omitempty"`
StackId string `json:"stackId,omitempty"`
Image string `json:"image,omitempty"`
CacheImage string `json:"cacheImage,omitempty"`
StackId string `json:"stackId,omitempty"`
}

// +k8s:openapi-gen=true
Expand All @@ -102,6 +103,7 @@ type BuildStatus struct {
BuildMetadata BuildpackMetadataList `json:"buildMetadata,omitempty"`
Stack BuildStack `json:"stack,omitempty"`
LatestImage string `json:"latestImage,omitempty"`
LatestCacheImage string `json:"latestCacheImage,omitempty"`
PodName string `json:"podName,omitempty"`
// +listType
StepStates []corev1.ContainerState `json:"stepStates,omitempty"`
Expand Down
5 changes: 3 additions & 2 deletions pkg/apis/build/v1alpha1/image_builds.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ func lastBuild(latestBuild *Build) *LastBuild {
}

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

Expand Down
22 changes: 22 additions & 0 deletions pkg/cnb/cnb_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ func (r *RemoteMetadataRetriever) GetBuiltImage(ctx context.Context, build *v1al
return readBuiltImage(appImage, appImageId)
}

func (r *RemoteMetadataRetriever) GetCacheImage(ctx context.Context, build *v1alpha1.Build) (string, error) {
if len(build.Spec.Cache.ImageTag) == 0 {
return "", nil
}

keychain, err := r.KeychainFactory.KeychainForSecretRef(ctx, registry.SecretRef{
ServiceAccount: build.Spec.ServiceAccount,
Namespace: build.Namespace,
})

if err != nil {
return "", err
}

_, cacheImageId, err := r.ImageFetcher.Fetch(keychain, build.Spec.Cache.ImageTag)
if err != nil {
return "", errors.Wrap(err, "unable to fetch cache image")
}

return cacheImageId, nil
}

type BuiltImage struct {
Identifier string
CompletedAt time.Time
Expand Down
7 changes: 7 additions & 0 deletions pkg/reconciler/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
//go:generate counterfeiter . MetadataRetriever
type MetadataRetriever interface {
GetBuiltImage(context.Context, *v1alpha1.Build) (cnb.BuiltImage, error)
GetCacheImage(context.Context, *v1alpha1.Build) (string, error)
}

type PodGenerator interface {
Expand Down Expand Up @@ -110,8 +111,14 @@ func (c *Reconciler) reconcile(ctx context.Context, build *v1alpha1.Build) error
return err
}

cacheImageId, err := c.MetadataRetriever.GetCacheImage(ctx, build)
if err != nil {
return err
}

build.Status.BuildMetadata = buildMetadataFromBuiltImage(image)
build.Status.LatestImage = image.Identifier
build.Status.LatestCacheImage = cacheImageId
build.Status.Stack.RunImage = image.Stack.RunImage
build.Status.Stack.ID = image.Stack.ID
}
Expand Down
81 changes: 81 additions & 0 deletions pkg/reconciler/build/buildfakes/fake_metadata_retriever.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6b3e580

Please sign in to comment.