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

Commit dc6d86d

Browse files
Merge pull request #765 from eunomie/relocation-the-right-way
Relocation the right way
2 parents 9169a3c + 39c87cb commit dc6d86d

File tree

14 files changed

+104
-125
lines changed

14 files changed

+104
-125
lines changed

cmd/cnab-run/bundle.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"github.com/deislabs/cnab-go/bundle"
5+
"github.com/docker/app/internal/relocated"
6+
"github.com/docker/cnab-to-oci/relocation"
7+
)
8+
9+
const (
10+
// bundlePath is where the CNAB runtime will put the actual Bundle definition
11+
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"
15+
)
16+
17+
func getBundle() (*bundle.Bundle, error) {
18+
return relocated.BundleJSON(bundlePath)
19+
}
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-
imageMap, err := getBundleImageMap()
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, imageMap)
24+
return appinspect.ImageInspect(os.Stdout, app, parameters, bndl.RelocatedImages())
2525
}

cmd/cnab-run/install.go

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"os"
77
"strconv"
88

9-
"github.com/deislabs/cnab-go/bundle"
109
"github.com/docker/app/internal"
1110
"github.com/docker/app/internal/packager"
1211
"github.com/docker/app/render"
@@ -19,12 +18,6 @@ import (
1918
"github.com/spf13/pflag"
2019
)
2120

22-
const (
23-
// imageMapFilePath is the path where the CNAB runtime will put the actual
24-
// service to image mapping to use
25-
imageMapFilePath = "/cnab/app/image-map.json"
26-
)
27-
2821
func installAction(instanceName string) error {
2922
cli, err := setupDockerContext()
3023
if err != nil {
@@ -42,12 +35,12 @@ func installAction(instanceName string) error {
4235
if err != nil {
4336
return err
4437
}
45-
imageMap, err := getBundleImageMap()
38+
bndl, err := getRelocatedBundle()
4639
if err != nil {
4740
return err
4841
}
4942
parameters := packager.ExtractCNABParametersValues(packager.ExtractCNABParameterMapping(app.Parameters()), os.Environ())
50-
rendered, err := render.Render(app, parameters, imageMap)
43+
rendered, err := render.Render(app, parameters, bndl.RelocatedImages())
5144
if err != nil {
5245
return err
5346
}
@@ -79,18 +72,6 @@ func getFlagset(orchestrator command.Orchestrator) *pflag.FlagSet {
7972
return result
8073
}
8174

82-
func getBundleImageMap() (map[string]bundle.Image, error) {
83-
mapJSON, err := ioutil.ReadFile(imageMapFilePath)
84-
if err != nil {
85-
return nil, err
86-
}
87-
var result map[string]bundle.Image
88-
if err := json.Unmarshal(mapJSON, &result); err != nil {
89-
return nil, err
90-
}
91-
return result, nil
92-
}
93-
9475
func addLabels(rendered *composetypes.Config) error {
9576
args, err := ioutil.ReadFile(internal.DockerArgsPath)
9677
if err != nil {

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-
imageMap, err := getBundleImageMap()
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, imageMap)
34+
rendered, err := render.Render(app, parameters, bndl.RelocatedImages())
3535
if err != nil {
3636
return err
3737
}

internal/cnab/driver.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@ package cnab
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"io"
67
"os"
78
"strings"
89

10+
"github.com/pkg/errors"
11+
912
"github.com/docker/app/internal/cliopts"
10-
store2 "github.com/docker/app/internal/store"
13+
"github.com/docker/app/internal/store"
1114

1215
"github.com/deislabs/cnab-go/claim"
1316
"github.com/deislabs/cnab-go/driver"
1417
dockerDriver "github.com/deislabs/cnab-go/driver/docker"
1518
"github.com/docker/app/internal"
1619
"github.com/docker/cli/cli/command"
1720
"github.com/docker/cli/cli/context/docker"
18-
"github.com/docker/cli/cli/context/store"
21+
cliContext "github.com/docker/cli/cli/context/store"
1922
"github.com/docker/docker/api/types/container"
2023
"github.com/docker/docker/api/types/mount"
2124
)
@@ -39,7 +42,7 @@ func RequiredClaimBindMount(c claim.Claim, dockerCli command.Cli) (BindMount, er
3942

4043
// RequiredBindMount Returns the path required to bind mount when running
4144
// the invocation image.
42-
func RequiredBindMount(targetContextName string, targetOrchestrator string, s store.Store) (BindMount, error) {
45+
func RequiredBindMount(targetContextName string, targetOrchestrator string, s cliContext.Store) (BindMount, error) {
4346
if targetOrchestrator == "kubernetes" {
4447
return BindMount{}, nil
4548
}
@@ -119,7 +122,7 @@ func prepareDriver(dockerCli command.Cli, bindMount BindMount, stdout io.Writer)
119122
return d, errBuf
120123
}
121124

122-
func SetupDriver(installation *store2.Installation, dockerCli command.Cli, opts *cliopts.InstallerContextOptions, stdout io.Writer) (driver.Driver, *bytes.Buffer, error) {
125+
func SetupDriver(installation *store.Installation, dockerCli command.Cli, opts *cliopts.InstallerContextOptions, stdout io.Writer) (driver.Driver, *bytes.Buffer, error) {
123126
dockerCli, err := opts.SetInstallerContext(dockerCli)
124127
if err != nil {
125128
return nil, nil, err
@@ -131,3 +134,31 @@ func SetupDriver(installation *store2.Installation, dockerCli command.Cli, opts
131134
driverImpl, errBuf := prepareDriver(dockerCli, bind, stdout)
132135
return driverImpl, errBuf, nil
133136
}
137+
138+
func WithRelocationMap(installation *store.Installation) func(op *driver.Operation) error {
139+
return func(op *driver.Operation) error {
140+
if err := addRelocationMapToFiles(op, installation); err != nil {
141+
return err
142+
}
143+
relocateInvocationImage(op, installation)
144+
return nil
145+
}
146+
}
147+
148+
func addRelocationMapToFiles(op *driver.Operation, installation *store.Installation) error {
149+
data, err := json.Marshal(installation.RelocationMap)
150+
if err != nil {
151+
return errors.Wrap(err, "could not marshal relocation map")
152+
}
153+
op.Files["/cnab/app/relocation-mapping.json"] = string(data)
154+
155+
return nil
156+
}
157+
158+
func relocateInvocationImage(op *driver.Operation, installation *store.Installation) {
159+
invocImage := op.Image
160+
if relocatedImage, ok := installation.RelocationMap[invocImage.Image]; ok {
161+
invocImage.Image = relocatedImage
162+
op.Image = invocImage
163+
}
164+
}

internal/commands/image/inspect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func runInspect(dockerCli command.Cli, appname string, opts inspectOptions, inst
8888
}
8989

9090
installation.SetParameter(internal.ParameterInspectFormatName, format)
91-
if err = a.Run(&installation.Claim, nil); err != nil {
91+
if err = a.Run(&installation.Claim, nil, cnab.WithRelocationMap(installation)); err != nil {
9292
return fmt.Errorf("inspect failed: %s\n%s", err, errBuf)
9393
}
9494
} else {

internal/commands/image/render.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func runRender(dockerCli command.Cli, appname string, opts renderOptions, instal
7070
}
7171
installation.Parameters[internal.ParameterRenderFormatName] = opts.formatDriver
7272

73-
if err := action.Run(&installation.Claim, nil, cfgFunc); err != nil {
73+
if err := action.Run(&installation.Claim, nil, cfgFunc, cnab.WithRelocationMap(installation)); err != nil {
7474
return fmt.Errorf("render failed: %s\n%s", err, errBuf)
7575
}
7676
return nil

internal/commands/inspect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func runInspect(dockerCli command.Cli, appName string, inspectOptions inspectOpt
7373
} else {
7474
return fmt.Errorf("inspect failed: status action is not supported by the App")
7575
}
76-
if err := a.Run(&installation.Claim, creds); err != nil {
76+
if err := a.Run(&installation.Claim, creds, cnab.WithRelocationMap(installation)); err != nil {
7777
return fmt.Errorf("inspect failed: %s\n%s", err, errBuf)
7878
}
7979

internal/commands/remove.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func runRemove(dockerCli command.Cli, installationName string, opts removeOption
8282
op.Out = dockerCli.Out()
8383
return nil
8484
}
85-
if err := uninst.Run(&installation.Claim, creds, cfgFunc); err != nil {
85+
if err := uninst.Run(&installation.Claim, creds, cfgFunc, cnab.WithRelocationMap(installation)); err != nil {
8686
if err2 := installationStore.Store(installation); err2 != nil {
8787
return fmt.Errorf("%s while %s", err2, errBuf)
8888
}

internal/commands/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func runBundle(dockerCli command.Cli, bndl *relocated.Bundle, opts runOptions, i
158158
op.Out = dockerCli.Out()
159159
return nil
160160
}
161-
err = inst.Run(&installation.Claim, creds, cfgFunc)
161+
err = inst.Run(&installation.Claim, creds, cfgFunc, cnab.WithRelocationMap(installation))
162162
}
163163
// Even if the installation failed, the installation is persisted with its failure status,
164164
// so any installation needs a clean uninstallation.

0 commit comments

Comments
 (0)