Skip to content

Commit

Permalink
Porter triggers autobuild when detecting that invocation image does n…
Browse files Browse the repository at this point in the history
…ot exist

Signed-off-by: joshuabezaleel <joshua.bezaleel@gmail.com>
  • Loading branch information
joshuabezaleel committed Nov 17, 2021
1 parent ca49edf commit dc5e6c9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
15 changes: 12 additions & 3 deletions pkg/cnab/cnab-to-oci/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
var _ RegistryProvider = &TestRegistry{}

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)
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)
MockIsInvocationImageExists 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) IsInvocationImageExists(invocationImage string) (bool, error) {
if t.MockIsInvocationImageExists != nil {
return t.MockIsInvocationImageExists(invocationImage)
}

return true, nil
}
2 changes: 2 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,6 @@ 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)

IsInvocationImageExists(invocationImage string) (bool, error)
}
28 changes: 28 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,30 @@ func (r *Registry) getDockerClient() (*command.DockerCli, error) {
}
return cli, nil
}

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

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

ref, err := cnab.ParseOCIReference(invocationImage)
if err != nil {
return false, err
}

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

imageSummaries, err := cli.Client().ImageList(ctx, imageListOpts)
if err != nil {
return false, err
}

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 {
isInvocationImageExists, err := p.Registry.IsInvocationImageExists(invocationImage.Image)
if err != nil {
return false, errors.Wrapf(err, "error while checking for existing invocation image")
}

if !isInvocationImageExists {
if p.Debug {
fmt.Fprintln(p.Err, errors.New(fmt.Sprintf("Invocation image %s doesn't exist in host registry, 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

0 comments on commit dc5e6c9

Please sign in to comment.