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

Add ExtendedBundle wrapper #1739

Merged
merged 3 commits into from
Aug 26, 2021
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
2 changes: 1 addition & 1 deletion pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (c *Cache) StoreBundle(bundleRef cnab.BundleReference) (CachedBundle, error
// cacheManifest extracts the porter.yaml from the bundle, if present and caches it
// in the same cache directory as the rest of the bundle.
func (c *Cache) cacheManifest(cb *CachedBundle) error {
if configadapter.IsPorterBundle(cb.Definition) {
if cb.Definition.IsPorterBundle() {
stamp, err := configadapter.LoadStamp(cb.Definition)
if err != nil {
fmt.Fprintf(c.Err, "WARNING: Bundle %s was created by porter but could not load the Porter stamp. This may be because it was created by an older version of Porter.\n", cb.Reference)
Expand Down
25 changes: 11 additions & 14 deletions pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cache

import (
"bytes"
"fmt"
"path"
"path/filepath"
Expand Down Expand Up @@ -89,8 +88,7 @@ func TestCacheWriteNoCacheDir(t *testing.T) {

cfg := config.NewTestConfig(t)
cfg.TestContext.AddTestFile("testdata/cnab/bundle.json", "/cnab/bundle.json")
b, err := cfg.FileSystem.ReadFile("/cnab/bundle.json")
bun, err := bundle.ParseReader(bytes.NewBuffer(b))
bun, err := cnab.LoadBundle(cfg.Context, "/cnab/bundle.json")
require.NoError(t, err, "bundle should have been valid")

c := New(cfg.Config)
Expand All @@ -116,8 +114,7 @@ func TestCacheWriteCacheDirExists(t *testing.T) {
cacheDir := filepath.Join(home, "cache")
cfg.TestContext.AddTestFile("testdata/cnab/bundle.json", "/cnab/bundle.json")
cfg.TestContext.AddTestDirectory("testdata", cacheDir)
b, err := cfg.FileSystem.ReadFile("/cnab/bundle.json")
bun, err := bundle.ParseReader(bytes.NewBuffer(b))
bun, err := cnab.LoadBundle(cfg.Context, "/cnab/bundle.json")
require.NoError(t, err, "bundle should have been valid")

c := New(cfg.Config)
Expand All @@ -137,13 +134,13 @@ func TestStoreRelocationMapping(t *testing.T) {
name string
relocationMapping relocation.ImageRelocationMap
tag cnab.OCIReference
bundle bundle.Bundle
bundle cnab.ExtendedBundle
wantedReloPath string
err error
}{
{
name: "relocation file gets a path",
bundle: bundle.Bundle{},
bundle: cnab.ExtendedBundle{},
tag: kahn1dot01,
relocationMapping: relocation.ImageRelocationMap{
"asd": "asdf",
Expand All @@ -153,7 +150,7 @@ func TestStoreRelocationMapping(t *testing.T) {
{
name: "no relocation file gets no path",
tag: kahnlatest,
bundle: bundle.Bundle{},
bundle: cnab.ExtendedBundle{},
wantedReloPath: "",
},
}
Expand All @@ -180,38 +177,38 @@ func TestStoreManifest(t *testing.T) {
tests := []struct {
name string
tag cnab.OCIReference
bundle bundle.Bundle
bundle cnab.ExtendedBundle
shouldCacheManifest bool
err error
}{
{
name: "embedded manifest",
bundle: bundle.Bundle{
bundle: cnab.ExtendedBundle{bundle.Bundle{
Custom: map[string]interface{}{
"sh.porter": map[string]interface{}{
"manifest": "bmFtZTogSEVMTE9fQ1VTVE9NCnZlcnNpb246IDAuMS4wCmRlc2NyaXB0aW9uOiAiQSBidW5kbGUgd2l0aCBhIGN1c3RvbSBhY3Rpb24iCnRhZzogZ2V0cG9ydGVyL3BvcnRlci1oZWxsbzp2MC4xLjAKaW52b2NhdGlvbkltYWdlOiBnZXRwb3J0ZXIvcG9ydGVyLWhlbGxvLWluc3RhbGxlcjowLjEuMAoKY3JlZGVudGlhbHM6CiAgLSBuYW1lOiBteS1maXJzdC1jcmVkCiAgICBlbnY6IE1ZX0ZJUlNUX0NSRUQKICAtIG5hbWU6IG15LXNlY29uZC1j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xvIFdvcmxkIgogICAgICBjb21tYW5kOiBiYXNoCiAgICAgIGZsYWdzOgogICAgICAgIGM6IGVjaG8gSGVsbG8gV29ybGQKCnVwZ3JhZGU6CiAgLSBleGVjOgogICAgICBkZXNjcmlwdGlvbjogIldvcmxkIDIuMCIKICAgICAgY29tbWFuZDogYmFzaAogICAgICBmbGFnczoKICAgICAgICBjOiBlY2hvIFdvcmxkIDIuMAoKem9tYmllczoKICAtIGV4ZWM6CiAgICAgIGRlc2NyaXB0aW9uOiAiVHJpZ2dlciB6b21iaWUgYXBvY2FseXBzZSIKICAgICAgY29tbWFuZDogYmFzaAogICAgICBmbGFnczoKICAgICAgICBjOiBlY2hvIG9oIG5vZXMgbXkgYnJhaW5zCgp1bmluc3RhbGw6CiAgLSBleGVjOgogICAgICBkZXNjcmlwdGlvbjogIlVuaW5zdGFsbCBIZWxsbyBXb3JsZCIKICAgICAgY29tbWFuZDogYmFzaAogICAgICBmbGFnczoKICAgICAgICBjOiBlY2hvIEdvb2RieWUgV29ybGQK",
},
},
},
}},
tag: kahn1dot01,
shouldCacheManifest: true,
},
{
name: "porter stamp, no manifest",
bundle: bundle.Bundle{
bundle: cnab.ExtendedBundle{bundle.Bundle{
Custom: map[string]interface{}{
"sh.porter": map[string]interface{}{
"manifestDigest": "abc123",
},
},
},
}},
tag: kahn1dot01,
shouldCacheManifest: false,
},
{
name: "no embedded manifest",
tag: kahnlatest,
bundle: bundle.Bundle{},
bundle: cnab.ExtendedBundle{},
},
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/claims/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package claims
import (
"sort"

"get.porter.sh/porter/pkg/cnab"
"get.porter.sh/porter/pkg/storage"
"github.com/cnabio/cnab-go/bundle"
"github.com/cnabio/cnab-go/bundle/definition"
"github.com/cnabio/cnab-go/schema"
)
Expand All @@ -27,7 +27,7 @@ func (o Output) DefaultDocumentFilter() interface{} {

// GetSchema returns the schema for the output from the specified bundle, or
// false if the schema is not defined.
func (o Output) GetSchema(b bundle.Bundle) (definition.Schema, bool) {
func (o Output) GetSchema(b cnab.ExtendedBundle) (definition.Schema, bool) {
if def, ok := b.Outputs[o.Name]; ok {
if schema, ok := b.Definitions[def.Definition]; ok {
return *schema, ok
Expand Down
10 changes: 5 additions & 5 deletions pkg/claims/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestRun_ShouldRecord(t *testing.T) {
b := bundle.Bundle{
Actions: map[string]bundle.Action{
"dry-run": {
Modifies: false,
Modifies: false,
Stateless: true,
},
},
Expand All @@ -52,7 +52,7 @@ func TestRun_ShouldRecord(t *testing.T) {
b := bundle.Bundle{
Actions: map[string]bundle.Action{
"audit": {
Modifies: false,
Modifies: false,
Stateless: false,
},
},
Expand All @@ -66,7 +66,7 @@ func TestRun_ShouldRecord(t *testing.T) {
b := bundle.Bundle{
Actions: map[string]bundle.Action{
"editstuff": {
Modifies: true,
Modifies: true,
Stateless: false,
},
},
Expand All @@ -77,10 +77,10 @@ func TestRun_ShouldRecord(t *testing.T) {
})

t.Run("missing definition", func(t *testing.T) {
b := bundle.Bundle{ }
b := bundle.Bundle{}

r := Run{Bundle: b, Action: "missing"}
assert.True(t, r.ShouldRecord())
})

}
}
20 changes: 1 addition & 19 deletions pkg/cnab/bundle.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,13 @@
package cnab

import (
"get.porter.sh/porter/pkg/context"
"github.com/cnabio/cnab-go/bundle"
"github.com/cnabio/cnab-to-oci/relocation"
"github.com/opencontainers/go-digest"
"github.com/pkg/errors"
)

type BundleReference struct {
Reference OCIReference
Digest digest.Digest
Definition bundle.Bundle
Definition ExtendedBundle
RelocationMap relocation.ImageRelocationMap
}

// LoadBundle from the specified filepath.
func LoadBundle(c *context.Context, bundleFile string) (bundle.Bundle, error) {
bunD, err := c.FileSystem.ReadFile(bundleFile)
if err != nil {
return bundle.Bundle{}, errors.Wrapf(err, "cannot read bundle at %s", bundleFile)
}

bun, err := bundle.Unmarshal(bunD)
if err != nil {
return bundle.Bundle{}, errors.Wrapf(err, "cannot load bundle from\n%s at %s", string(bunD), bundleFile)
}

return *bun, nil
}
6 changes: 3 additions & 3 deletions pkg/cnab/cnab-to-oci/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (r *Registry) PullBundle(ref cnab.OCIReference, insecureRegistry bool) (cna
bundleRef := cnab.BundleReference{
Reference: ref,
Digest: digest,
Definition: *bun,
Definition: cnab.ExtendedBundle{*bun},
RelocationMap: reloMap,
}

Expand All @@ -90,13 +90,13 @@ func (r *Registry) PushBundle(bundleRef cnab.BundleReference, insecureRegistry b

resolver := r.createResolver(insecureRegistries)

rm, err := remotes.FixupBundle(context.Background(), &bundleRef.Definition, bundleRef.Reference.Named, resolver, remotes.WithEventCallback(r.displayEvent), remotes.WithAutoBundleUpdate())
rm, err := remotes.FixupBundle(context.Background(), &bundleRef.Definition.Bundle, bundleRef.Reference.Named, resolver, remotes.WithEventCallback(r.displayEvent), remotes.WithAutoBundleUpdate())
if err != nil {
return cnab.BundleReference{}, errors.Wrap(err, "error preparing the bundle with cnab-to-oci before pushing")
}
bundleRef.RelocationMap = rm

d, err := remotes.Push(context.Background(), &bundleRef.Definition, rm, bundleRef.Reference.Named, resolver, true)
d, err := remotes.Push(context.Background(), &bundleRef.Definition.Bundle, rm, bundleRef.Reference.Named, resolver, true)
if err != nil {
return cnab.BundleReference{}, errors.Wrapf(err, "error pushing the bundle to %s", bundleRef.Reference)
}
Expand Down
Loading