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

Commit e404042

Browse files
committed
apply relocation map at runtime
Bundle definition -with service image definitions- is mounted under `/cnab/bundle.json` in the invocation image. If a `/cnab/app/relocation-mapping.json` exists read it and apply it on top of image definitions. See https://github.com/cnabio/cnab-spec/blob/master/103-bundle-runtime.md#image-relocation Signed-off-by: Yves Brissaud <[email protected]>
1 parent f7b4189 commit e404042

File tree

5 files changed

+46
-8
lines changed

5 files changed

+46
-8
lines changed

cmd/cnab-run/bundle.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,38 @@ package main
33
import (
44
"github.com/deislabs/cnab-go/bundle"
55
"github.com/docker/app/internal/relocated"
6+
"github.com/docker/cnab-to-oci/relocation"
67
)
78

89
const (
910
// bundlePath is where the CNAB runtime will put the actual Bundle definition
1011
bundlePath = "/cnab/bundle.json"
12+
// relocationMapPath is where the CNAB runtime will put the relocation map
13+
// See https://github.com/cnabio/cnab-spec/blob/master/103-bundle-runtime.md#image-relocation
14+
relocationMapPath = "/cnab/app/relocation-mapping.json"
1115
)
1216

1317
func getBundle() (*bundle.Bundle, error) {
1418
return relocated.BundleJSON(bundlePath)
1519
}
20+
21+
func getRelocationMap() (relocation.ImageRelocationMap, error) {
22+
return relocated.RelocationMapJSON(relocationMapPath)
23+
}
24+
25+
func getRelocatedBundle() (*relocated.Bundle, error) {
26+
bndl, err := getBundle()
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
relocationMap, err := getRelocationMap()
32+
if err != nil {
33+
return nil, err
34+
}
35+
36+
return &relocated.Bundle{
37+
Bundle: bndl,
38+
RelocationMap: relocationMap,
39+
}, nil
40+
}

cmd/cnab-run/inspect.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ func inspectAction(instanceName string) error {
1515
}
1616
defer app.Cleanup()
1717

18-
bndl, err := getBundle()
18+
bndl, err := getRelocatedBundle()
1919
if err != nil {
2020
return err
2121
}
2222

2323
parameters := packager.ExtractCNABParametersValues(packager.ExtractCNABParameterMapping(app.Parameters()), os.Environ())
24-
return appinspect.ImageInspect(os.Stdout, app, parameters, bndl.Images)
24+
return appinspect.ImageInspect(os.Stdout, app, parameters, bndl.RelocatedImages())
2525
}

cmd/cnab-run/install.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ func installAction(instanceName string) error {
3535
if err != nil {
3636
return err
3737
}
38-
bndl, err := getBundle()
38+
bndl, err := getRelocatedBundle()
3939
if err != nil {
4040
return err
4141
}
4242
parameters := packager.ExtractCNABParametersValues(packager.ExtractCNABParameterMapping(app.Parameters()), os.Environ())
43-
rendered, err := render.Render(app, parameters, bndl.Images)
43+
rendered, err := render.Render(app, parameters, bndl.RelocatedImages())
4444
if err != nil {
4545
return err
4646
}

cmd/cnab-run/render.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func renderAction(instanceName string) error {
1919
}
2020
defer app.Cleanup()
2121

22-
bndl, err := getBundle()
22+
bndl, err := getRelocatedBundle()
2323
if err != nil {
2424
return err
2525
}
@@ -31,7 +31,7 @@ func renderAction(instanceName string) error {
3131

3232
parameters := packager.ExtractCNABParametersValues(packager.ExtractCNABParameterMapping(app.Parameters()), os.Environ())
3333

34-
rendered, err := render.Render(app, parameters, bndl.Images)
34+
rendered, err := render.Render(app, parameters, bndl.RelocatedImages())
3535
if err != nil {
3636
return err
3737
}

internal/relocated/bundle.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func BundleFromFile(filename string) (*Bundle, error) {
3838
}
3939

4040
relocationMapFileName := filepath.Join(filepath.Dir(filename), RelocationMapFilename)
41-
relocationMap, err := relocationMapJSON(relocationMapFileName)
41+
relocationMap, err := RelocationMapJSON(relocationMapFileName)
4242
if err != nil {
4343
return nil, errors.Wrapf(err, "failed to read relocation map")
4444
}
@@ -87,10 +87,11 @@ func BundleJSON(bundlePath string) (*bundle.Bundle, error) {
8787
return bndl, nil
8888
}
8989

90-
func relocationMapJSON(relocationMapPath string) (relocation.ImageRelocationMap, error) {
90+
func RelocationMapJSON(relocationMapPath string) (relocation.ImageRelocationMap, error) {
9191
relocationMap := relocation.ImageRelocationMap{}
9292
_, err := os.Stat(relocationMapPath)
9393
if os.IsNotExist(err) {
94+
// it's ok to not have a relocation map, just act as if the file were empty
9495
return relocationMap, nil
9596
}
9697
data, err := ioutil.ReadFile(relocationMapPath)
@@ -102,3 +103,15 @@ func relocationMapJSON(relocationMapPath string) (relocation.ImageRelocationMap,
102103
}
103104
return relocationMap, nil
104105
}
106+
107+
func (b *Bundle) RelocatedImages() map[string]bundle.Image {
108+
images := b.Images
109+
for name, def := range images {
110+
if img, ok := b.RelocationMap[def.Image]; ok {
111+
def.Image = img
112+
images[name] = def
113+
}
114+
}
115+
116+
return images
117+
}

0 commit comments

Comments
 (0)