Skip to content

Commit

Permalink
replace bundle.invocationImage with bundle.installerImage
Browse files Browse the repository at this point in the history
Signed-off-by: Yingrong Zhao <[email protected]>
  • Loading branch information
VinozzZ committed Aug 3, 2022
1 parent afcd5de commit 975055a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
4 changes: 4 additions & 0 deletions pkg/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func (r *PorterRuntime) Execute(ctx context.Context, rm *RuntimeManifest) error
return err
}

err = r.RuntimeManifest.ResolveInvocationImage(rtb, reloMap)
if err != nil {
return fmt.Errorf("unable to resolve bundle invocation images: %w", err)
}
err = r.RuntimeManifest.ResolveImages(rtb, reloMap)
if err != nil {
return fmt.Errorf("unable to resolve bundle images: %w", err)
Expand Down
30 changes: 29 additions & 1 deletion pkg/runtime/runtime_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/cnabio/cnab-go/bundle"
"github.com/cnabio/cnab-to-oci/relocation"
"github.com/hashicorp/go-multierror"
"github.com/opencontainers/go-digest"
yaml3 "gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -254,7 +255,7 @@ func (m *RuntimeManifest) buildSourceData() (map[string]interface{}, error) {
bun["name"] = m.Name
bun["version"] = m.Version
bun["description"] = m.Description
bun["invocationImage"] = m.Image
bun["installerImage"] = m.Image
bun["custom"] = m.Custom

// Make environment variable accessible
Expand Down Expand Up @@ -714,6 +715,33 @@ func (m *RuntimeManifest) applyUnboundBundleOutputs(ctx context.Context) error {
return log.Error(bigErr.ErrorOrNil())
}

// ResolveInvocationImage updates the RuntimeManifest to properly reflect the invocation image passed to the bundle via the
// mounted bundle.json and relocation mapping
func (m *RuntimeManifest) ResolveInvocationImage(bun cnab.ExtendedBundle, reloMap relocation.ImageRelocationMap) error {
for _, image := range bun.InvocationImages {
if image.Digest == "" || image.ImageType != "docker" {
continue
}

ref, err := cnab.ParseOCIReference(image.Image)
if err != nil {
return fmt.Errorf("unable to parse invocation image reference: %w", err)
}
refWithDigest, err := ref.WithDigest(digest.Digest(image.Digest))
if err != nil {
return fmt.Errorf("unable to get invocation image reference with digest: %w", err)
}

m.Image = refWithDigest.String()
break
}
relocated, ok := reloMap[m.Image]
if ok {
m.Image = relocated
}
return nil
}

// ResolveImages updates the RuntimeManifest to properly reflect the image map passed to the bundle via the
// mounted bundle.json and relocation mapping
func (m *RuntimeManifest) ResolveImages(bun cnab.ExtendedBundle, reloMap relocation.ImageRelocationMap) error {
Expand Down
54 changes: 54 additions & 0 deletions pkg/runtime/runtime_manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1004,3 +1004,57 @@ install:
assert.Equal(t, "foo-value", mixin["someInput"], "expected lower-case foo env var was resolved")
assert.Equal(t, "bar-value", mixin["moreInput"], "expected upper-case BAR env var was resolved")
}

func TestResolveInvocationImage(t *testing.T) {
testcases := []struct {
name string
bundleInvocationImg bundle.BaseImage
relocationMap relocation.ImageRelocationMap
expectedImg string
wantErr string
}{
{name: "success with no relocation map",
bundleInvocationImg: bundle.BaseImage{Image: "blah/ghost:latest", ImageType: "docker", Digest: "sha256:75c495e5ce9c428d482973d72e3ce9925e1db304a97946c9aa0b540d7537e041"},
expectedImg: "blah/ghost:latest@sha256:75c495e5ce9c428d482973d72e3ce9925e1db304a97946c9aa0b540d7537e041",
},
{name: "success with relocation map",
bundleInvocationImg: bundle.BaseImage{Image: "blah/ghost:latest", ImageType: "docker", Digest: "sha256:75c495e5ce9c428d482973d72e3ce9925e1db304a97946c9aa0b540d7537e041"},
relocationMap: relocation.ImageRelocationMap{"blah/ghost:latest@sha256:75c495e5ce9c428d482973d72e3ce9925e1db304a97946c9aa0b540d7537e041": "relocated-ghost@sha256:75c495e5ce9c428d482973d72e3ce9925e1db304a97946c9aa0b540d7537e041"},
expectedImg: "relocated-ghost@sha256:75c495e5ce9c428d482973d72e3ce9925e1db304a97946c9aa0b540d7537e041",
},
{name: "success with no update",
expectedImg: "test/image:latest",
},
{name: "failure with invalid digest",
bundleInvocationImg: bundle.BaseImage{Image: "blah/ghost:latest", ImageType: "docker", Digest: "123"},
wantErr: "unable to get invocation image reference with digest",
},
}

pCtx := portercontext.NewTestContext(t)
cfg := NewConfigFor(pCtx.Context)

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {

bun := cnab.NewBundle(bundle.Bundle{
InvocationImages: []bundle.InvocationImage{
{BaseImage: tc.bundleInvocationImg},
},
})
m := &manifest.Manifest{
Image: "test/image:latest",
}
rm := NewRuntimeManifest(cfg, cnab.ActionInstall, m)

err := rm.ResolveInvocationImage(bun, tc.relocationMap)
if tc.wantErr != "" {
require.ErrorContains(t, err, tc.wantErr)
return
}
require.NoError(t, err)
require.Equal(t, tc.expectedImg, m.Image)
})
}

}

0 comments on commit 975055a

Please sign in to comment.