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

Porter triggers autobuild when detecting that invocation image does not exist #1828

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
9 changes: 9 additions & 0 deletions pkg/cnab/cnab-to-oci/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type TestRegistry struct {
MockPullBundle func(ref cnab.OCIReference, insecureRegistry bool) (cnab.BundleReference, error)
MockPushBundle func(bundleRef cnab.BundleReference, insecureRegistry bool) (bundleReference cnab.BundleReference, err error)
MockPushInvocationImage func(invocationImage string) (imageDigest digest.Digest, err error)
MockIsImageCached func(invocationImage string) (bool, error)
}

func NewTestRegistry() *TestRegistry {
Expand Down Expand Up @@ -39,3 +40,11 @@ func (t TestRegistry) PushInvocationImage(invocationImage string) (digest.Digest
}
return "", nil
}

func (t TestRegistry) IsImageCached(invocationImage string) (bool, error) {
if t.MockIsImageCached != nil {
return t.MockIsImageCached(invocationImage)
}

return true, nil
}
3 changes: 3 additions & 0 deletions pkg/cnab/cnab-to-oci/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ type RegistryProvider interface {
// the expected format of the invocationImage is REGISTRY/NAME:TAG.
// Returns the image digest from the registry.
PushInvocationImage(invocationImage string) (digest.Digest, error)

// IsImageCached checks whether a particular invocation image exists in the local image cache.
IsImageCached(invocationImage string) (bool, error)
}
23 changes: 23 additions & 0 deletions pkg/cnab/cnab-to-oci/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
dockerconfig "github.com/docker/cli/cli/config"
cliflags "github.com/docker/cli/cli/flags"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/term"
"github.com/opencontainers/go-digest"
Expand Down Expand Up @@ -188,3 +189,25 @@ func (r *Registry) getDockerClient() (*command.DockerCli, error) {
}
return cli, nil
}

func (r *Registry) IsImageCached(invocationImage string) (bool, error) {
ctx := context.Background()

cli, err := r.getDockerClient()
if err != nil {
return false, err
}

imageListOpts := types.ImageListOptions{All: true, Filters: filters.NewArgs(filters.KeyValuePair{Key: "reference", Value: invocationImage})}

imageSummaries, err := cli.Client().ImageList(ctx, imageListOpts)
if err != nil {
return false, errors.Wrapf(err, "could not list images")
}

if len(imageSummaries) == 0 {
return false, nil
}

return true, nil
}
15 changes: 15 additions & 0 deletions pkg/porter/stamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ func (p *Porter) IsBundleUpToDate(opts bundleFileOptions) (bool, error) {
return false, errors.Wrapf(err, "could not marshal data from %s", opts.CNABFile)
}

// Check whether invocation images exist in host registry.
for _, invocationImage := range bun.InvocationImages {
isImageCached, err := p.Registry.IsImageCached(invocationImage.Image)
if err != nil {
return false, err
}

if !isImageCached {
if p.Debug {
fmt.Fprintln(p.Err, errors.New(fmt.Sprintf("Invocation image %s doesn't exist in the local image cache, will need to build first", invocationImage.Image)))
}
return false, nil
}
}

oldStamp, err := configadapter.LoadStamp(bun)
if err != nil {
return false, errors.Wrapf(err, "could not load stamp from %s", opts.CNABFile)
Expand Down