Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit d48d5cc

Browse files
committed
save relocation map from cnab-to-oci
When a relocation map is generated from `cnab-to-oci` save it within the same directory as the `bundle.json` file. More information about reloaction map can be found in corresponding `cnab-to-oci` issue: cnabio/cnab-to-oci#58 The `bundle.Bundle` struct is now wrapped in a `relocated.Bundle` that will also contain the relocation map. Methods to fetch `bundle.json` and `relocation-map.json` as well as en entire bundle with the relocation map at once are moved to a `fetch` package to avoid dependency cycle. Signed-off-by: Yves Brissaud <[email protected]>
1 parent 42218d2 commit d48d5cc

21 files changed

+274
-126
lines changed

Gopkg.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

e2e/build_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"strings"
1010
"testing"
1111

12+
"github.com/docker/app/internal/relocated"
13+
1214
"gotest.tools/fs"
1315

1416
"github.com/docker/app/internal/store"
@@ -30,11 +32,8 @@ func TestBuild(t *testing.T) {
3032

3133
cfg := getDockerConfigDir(t, cmd)
3234

33-
f := path.Join(cfg, "app", "bundles", "docker.io", "library", "single", "_tags", "1.0.0", "bundle.json")
34-
data, err := ioutil.ReadFile(f)
35-
assert.NilError(t, err)
36-
var bndl bundle.Bundle
37-
err = json.Unmarshal(data, &bndl)
35+
f := path.Join(cfg, "app", "bundles", "docker.io", "library", "single", "_tags", "1.0.0", relocated.BundleFilename)
36+
bndl, err := relocated.BundleFromFile(f)
3837
assert.NilError(t, err)
3938

4039
built := []string{bndl.InvocationImages[0].Digest, bndl.Images["web"].Digest, bndl.Images["worker"].Digest}
@@ -53,7 +52,7 @@ func TestBuild(t *testing.T) {
5352
bytes, err := ioutil.ReadFile(iidfile)
5453
assert.NilError(t, err)
5554
iid := string(bytes)
56-
actualID, err := store.FromBundle(&bndl)
55+
actualID, err := store.FromBundle(bndl)
5756
assert.NilError(t, err)
5857
assert.Equal(t, iid, fmt.Sprintf("sha256:%s", actualID.String()))
5958
})
@@ -96,7 +95,7 @@ func TestBuildWithoutTag(t *testing.T) {
9695
assert.Equal(t, len(infos), 1)
9796
id := infos[0].Name()
9897

99-
f = path.Join(cfg, "app", "bundles", "_ids", id, "bundle.json")
98+
f = path.Join(cfg, "app", "bundles", "_ids", id, relocated.BundleFilename)
10099
data, err := ioutil.ReadFile(f)
101100
assert.NilError(t, err)
102101
var bndl bundle.Bundle
@@ -127,7 +126,7 @@ func TestBuildWithArgs(t *testing.T) {
127126
assert.Equal(t, len(infos), 1)
128127
id := infos[0].Name()
129128

130-
f = path.Join(cfg, "app", "bundles", "_ids", id, "bundle.json")
129+
f = path.Join(cfg, "app", "bundles", "_ids", id, relocated.BundleFilename)
131130
data, err := ioutil.ReadFile(f)
132131
assert.NilError(t, err)
133132
var bndl bundle.Bundle

e2e/cnab_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"path"
66
"testing"
77

8+
"github.com/docker/app/internal/relocated"
89
"gotest.tools/assert"
910
is "gotest.tools/assert/cmp"
1011
"gotest.tools/icmd"
@@ -50,7 +51,7 @@ func TestCallCustomStatusAction(t *testing.T) {
5051
icmd.RunCmd(cmd).Assert(t, icmd.Success)
5152

5253
// docker app install
53-
cmd.Command = dockerCli.Command("app", "run", "--cnab-bundle-json", path.Join(testDir, "bundle.json"), "--name", testCase.name)
54+
cmd.Command = dockerCli.Command("app", "run", "--cnab-bundle-json", path.Join(testDir, relocated.BundleFilename), "--name", testCase.name)
5455
icmd.RunCmd(cmd).Assert(t, icmd.Success)
5556

5657
// docker app uninstall
@@ -78,7 +79,7 @@ func TestCnabParameters(t *testing.T) {
7879
}()
7980

8081
// docker app install
81-
cmd.Command = dockerCli.Command("app", "run", "--cnab-bundle-json", path.Join(testDir, "bundle.json"), "--name", "cnab-parameters",
82+
cmd.Command = dockerCli.Command("app", "run", "--cnab-bundle-json", path.Join(testDir, relocated.BundleFilename), "--name", "cnab-parameters",
8283
"--set", "boolParam=true",
8384
"--set", "stringParam=value",
8485
"--set", "intParam=42")

e2e/commands_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/deislabs/cnab-go/credentials"
1414
"github.com/docker/app/internal"
15+
"github.com/docker/app/internal/relocated"
1516
"github.com/docker/app/internal/yaml"
1617
"gotest.tools/assert"
1718
is "gotest.tools/assert/cmp"
@@ -205,7 +206,7 @@ func TestRunOnlyOne(t *testing.T) {
205206
Err: `"docker app run" requires exactly 1 argument.`,
206207
})
207208

208-
cmd.Command = dockerCli.Command("app", "run", "--cnab-bundle-json", "bundle.json", "myapp")
209+
cmd.Command = dockerCli.Command("app", "run", "--cnab-bundle-json", relocated.BundleFilename, "myapp")
209210
icmd.RunCmd(cmd).Assert(t, icmd.Expected{
210211
ExitCode: 1,
211212
Err: `"docker app run" cannot run a bundle and an App image`,
@@ -348,14 +349,14 @@ func TestCredentials(t *testing.T) {
348349
assert.NilError(t, err)
349350
bundleJSON := golden.Get(t, "credential-install-bundle.json")
350351
tmpDir := fs.NewDir(t, t.Name(),
351-
fs.WithFile("bundle.json", "", fs.WithBytes(bundleJSON)),
352+
fs.WithFile(relocated.BundleFilename, "", fs.WithBytes(bundleJSON)),
352353
fs.WithDir("local",
353354
fs.WithFile("test-creds.yaml", "", fs.WithBytes(buf)),
354355
),
355356
)
356357
defer tmpDir.Remove()
357358

358-
bundle := tmpDir.Join("bundle.json")
359+
bundle := tmpDir.Join(relocated.BundleFilename)
359360

360361
t.Run("missing", func(t *testing.T) {
361362
cmd.Command = dockerCli.Command(

e2e/relocation_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package e2e
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"strings"
7+
"testing"
8+
9+
"github.com/docker/app/internal/relocated"
10+
11+
"gotest.tools/assert"
12+
"gotest.tools/icmd"
13+
)
14+
15+
func TestRelocationMapCreatedOnPull(t *testing.T) {
16+
runWithDindSwarmAndRegistry(t, func(info dindSwarmAndRegistryInfo) {
17+
cmd := info.configuredCmd
18+
cfg := getDockerConfigDir(t, cmd)
19+
20+
path := filepath.Join("testdata", "local")
21+
ref := info.registryAddress + "/test/local:a-tag"
22+
bundlePath := filepath.Join(cfg, "app", "bundles", strings.Replace(info.registryAddress, ":", "_", 1), "test", "local", "_tags", "a-tag")
23+
24+
// Given a pushed application
25+
build(t, cmd, dockerCli, ref, path)
26+
cmd.Command = dockerCli.Command("app", "push", ref)
27+
icmd.RunCmd(cmd).Assert(t, icmd.Success)
28+
// And given application files are remove
29+
assert.NilError(t, os.RemoveAll(bundlePath))
30+
_, err := os.Stat(filepath.Join(bundlePath, relocated.BundleFilename))
31+
assert.Assert(t, os.IsNotExist(err))
32+
33+
// When application is pulled
34+
cmd.Command = dockerCli.Command("app", "pull", ref)
35+
icmd.RunCmd(cmd).Assert(t, icmd.Success)
36+
37+
// Then the relocation map should exist
38+
_, err = os.Stat(filepath.Join(bundlePath, relocated.RelocationMapFilename))
39+
assert.NilError(t, err)
40+
})
41+
}

internal/cnab/cnab.go

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ package cnab
33
import (
44
"context"
55
"fmt"
6-
"io/ioutil"
76
"os"
87

9-
"github.com/deislabs/cnab-go/bundle"
108
"github.com/docker/app/internal"
119
"github.com/docker/app/internal/log"
1210
"github.com/docker/app/internal/packager"
11+
"github.com/docker/app/internal/relocated"
1312
"github.com/docker/app/internal/store"
1413
appstore "github.com/docker/app/internal/store"
1514
"github.com/docker/cli/cli/command"
@@ -48,30 +47,20 @@ func getAppNameKind(name string) (string, nameKind) {
4847
return name, nameKindReference
4948
}
5049

51-
func extractAndLoadAppBasedBundle(dockerCli command.Cli, name string) (*bundle.Bundle, string, error) {
50+
func extractAndLoadAppBasedBundle(dockerCli command.Cli, name string) (*relocated.Bundle, string, error) {
5251
app, err := packager.Extract(name)
5352
if err != nil {
5453
return nil, "", err
5554
}
5655
defer app.Cleanup()
5756
bndl, err := packager.MakeBundleFromApp(dockerCli, app, nil)
58-
return bndl, "", err
59-
}
60-
61-
// LoadBundleFromFile loads a bundle from a file
62-
func LoadBundleFromFile(filename string) (*bundle.Bundle, error) {
63-
b := &bundle.Bundle{}
64-
data, err := ioutil.ReadFile(filename)
65-
if err != nil {
66-
return b, err
67-
}
68-
return bundle.Unmarshal(data)
57+
return relocated.FromBundle(bndl), "", err
6958
}
7059

7160
// ResolveBundle looks for a CNAB bundle which can be in a Docker App Package format or
7261
// a bundle stored locally or in the bundle store. It returns a built or found bundle,
7362
// a reference to the bundle if it is found in the bundlestore, and an error.
74-
func ResolveBundle(dockerCli command.Cli, bundleStore appstore.BundleStore, name string) (*bundle.Bundle, string, error) {
63+
func ResolveBundle(dockerCli command.Cli, bundleStore appstore.BundleStore, name string) (*relocated.Bundle, string, error) {
7564
// resolution logic:
7665
// - if there is a docker-app package in working directory or if a directory is given use packager.Extract
7766
// - pull the bundle from the registry and add it to the bundle store
@@ -90,7 +79,7 @@ func ResolveBundle(dockerCli command.Cli, bundleStore appstore.BundleStore, name
9079
}
9180

9281
// GetBundle searches for the bundle locally and tries to pull it if not found
93-
func GetBundle(dockerCli command.Cli, bundleStore appstore.BundleStore, name string) (*bundle.Bundle, reference.Reference, error) {
82+
func GetBundle(dockerCli command.Cli, bundleStore appstore.BundleStore, name string) (*relocated.Bundle, reference.Reference, error) {
9483
bndl, ref, err := getBundleFromStore(bundleStore, name)
9584
if err != nil {
9685
named, err := store.StringToNamedRef(name)
@@ -108,7 +97,7 @@ func GetBundle(dockerCli command.Cli, bundleStore appstore.BundleStore, name str
10897
return bndl, ref, nil
10998
}
11099

111-
func getBundleFromStore(bundleStore appstore.BundleStore, name string) (*bundle.Bundle, reference.Reference, error) {
100+
func getBundleFromStore(bundleStore appstore.BundleStore, name string) (*relocated.Bundle, reference.Reference, error) {
112101
ref, err := bundleStore.LookUp(name)
113102
if err != nil {
114103
logrus.Debugf("Unable to find reference %q in the bundle store", name)
@@ -123,18 +112,19 @@ func getBundleFromStore(bundleStore appstore.BundleStore, name string) (*bundle.
123112
}
124113

125114
// PullBundle pulls the bundle and stores it into the bundle store
126-
func PullBundle(dockerCli command.Cli, bundleStore appstore.BundleStore, tagRef reference.Named) (*bundle.Bundle, error) {
115+
func PullBundle(dockerCli command.Cli, bundleStore appstore.BundleStore, tagRef reference.Named) (*relocated.Bundle, error) {
127116
insecureRegistries, err := internal.InsecureRegistriesFromEngine(dockerCli)
128117
if err != nil {
129118
return nil, fmt.Errorf("could not retrieve insecure registries: %v", err)
130119
}
131120

132-
bndl, _, err := remotes.Pull(log.WithLogContext(context.Background()), reference.TagNameOnly(tagRef), remotes.CreateResolver(dockerCli.ConfigFile(), insecureRegistries...))
121+
bndl, relocationMap, err := remotes.Pull(log.WithLogContext(context.Background()), reference.TagNameOnly(tagRef), remotes.CreateResolver(dockerCli.ConfigFile(), insecureRegistries...))
133122
if err != nil {
134123
return nil, err
135124
}
136-
if _, err := bundleStore.Store(tagRef, bndl); err != nil {
125+
relocatedBundle := &relocated.Bundle{Bundle: bndl, RelocationMap: relocationMap}
126+
if _, err := bundleStore.Store(tagRef, relocatedBundle); err != nil {
137127
return nil, err
138128
}
139-
return bndl, nil
129+
return relocatedBundle, nil
140130
}

internal/commands/image/inspect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func runInspect(dockerCli command.Cli, appname string, opts inspectOptions) erro
7070
if err != nil {
7171
return err
7272
}
73-
installation.Bundle = bndl
73+
installation.Bundle = bndl.Bundle
7474
driverImpl, errBuf, err := cnab.SetupDriver(installation, dockerCli, opts.InstallerContextOptions, os.Stdout)
7575
if err != nil {
7676
return err

internal/commands/image/list.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77
"strings"
88
"text/tabwriter"
99

10-
"github.com/deislabs/cnab-go/bundle"
10+
"github.com/docker/app/internal/relocated"
11+
1112
"github.com/docker/app/internal/store"
1213
"github.com/docker/cli/cli/command"
1314
"github.com/docker/cli/cli/config"
@@ -182,5 +183,5 @@ func getImageListColumns(options imageListOption) []imageListColumn {
182183

183184
type pkg struct {
184185
ref reference.Reference
185-
bundle *bundle.Bundle
186+
bundle *relocated.Bundle
186187
}

internal/commands/image/list_test.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"fmt"
77
"testing"
88

9+
"github.com/docker/app/internal/relocated"
10+
911
"gotest.tools/assert"
1012

1113
"github.com/deislabs/cnab-go/bundle"
@@ -15,18 +17,18 @@ import (
1517
)
1618

1719
type bundleStoreStubForListCmd struct {
18-
refMap map[reference.Reference]*bundle.Bundle
20+
refMap map[reference.Reference]*relocated.Bundle
1921
// in order to keep the reference in the same order between tests
2022
refList []reference.Reference
2123
}
2224

23-
func (b *bundleStoreStubForListCmd) Store(ref reference.Reference, bndle *bundle.Bundle) (reference.Digested, error) {
24-
b.refMap[ref] = bndle
25+
func (b *bundleStoreStubForListCmd) Store(ref reference.Reference, bndl *relocated.Bundle) (reference.Digested, error) {
26+
b.refMap[ref] = bndl
2527
b.refList = append(b.refList, ref)
26-
return store.FromBundle(bndle)
28+
return store.FromBundle(bndl)
2729
}
2830

29-
func (b *bundleStoreStubForListCmd) Read(ref reference.Reference) (*bundle.Bundle, error) {
31+
func (b *bundleStoreStubForListCmd) Read(ref reference.Reference) (*relocated.Bundle, error) {
3032
bndl, ok := b.refMap[ref]
3133
if ok {
3234
return bndl, nil
@@ -54,17 +56,23 @@ func TestListCmd(t *testing.T) {
5456
parseReference(t, "foo/bar:1.0"),
5557
ref,
5658
}
57-
bundles := []bundle.Bundle{
59+
bundles := []relocated.Bundle{
5860
{
59-
Name: "Digested App",
61+
Bundle: &bundle.Bundle{
62+
Name: "Digested App",
63+
},
6064
},
6165
{
62-
Version: "1.0.0",
63-
SchemaVersion: "1.0.0",
64-
Name: "Foo App",
66+
Bundle: &bundle.Bundle{
67+
Version: "1.0.0",
68+
SchemaVersion: "1.0.0",
69+
Name: "Foo App",
70+
},
6571
},
6672
{
67-
Name: "Quiet App",
73+
Bundle: &bundle.Bundle{
74+
Name: "Quiet App",
75+
},
6876
},
6977
}
7078

@@ -114,13 +122,13 @@ func parseReference(t *testing.T, s string) reference.Reference {
114122
return ref
115123
}
116124

117-
func testRunList(t *testing.T, refs []reference.Reference, bundles []bundle.Bundle, options imageListOption, expectedOutput string) {
125+
func testRunList(t *testing.T, refs []reference.Reference, bundles []relocated.Bundle, options imageListOption, expectedOutput string) {
118126
var buf bytes.Buffer
119127
w := bufio.NewWriter(&buf)
120128
dockerCli, err := command.NewDockerCli(command.WithOutputStream(w))
121129
assert.NilError(t, err)
122130
bundleStore := &bundleStoreStubForListCmd{
123-
refMap: make(map[reference.Reference]*bundle.Bundle),
131+
refMap: make(map[reference.Reference]*relocated.Bundle),
124132
refList: []reference.Reference{},
125133
}
126134
for i, ref := range refs {

internal/commands/image/render.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func prepareCustomAction(actionName string, dockerCli command.Cli, appname strin
9595
if err != nil {
9696
return nil, nil, nil, err
9797
}
98-
installation.Bundle = bundle
98+
installation.Bundle = bundle.Bundle
9999

100100
if err := bdl.MergeBundleParameters(installation,
101101
bdl.WithFileParameters(opts.ParametersFiles),

0 commit comments

Comments
 (0)