Skip to content
This repository has been archived by the owner on Jun 13, 2021. It is now read-only.

Commit

Permalink
embed relocation map instead of applying it
Browse files Browse the repository at this point in the history
Embed the relocation map inside `/cnab/app/relocation-mapping.json`
and do not touch the bundle definition.

Signed-off-by: Yves Brissaud <yves.brissaud@docker.com>
  • Loading branch information
eunomie committed Nov 27, 2019
1 parent e404042 commit 5361689
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 92 deletions.
14 changes: 14 additions & 0 deletions internal/cnab/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package cnab

import (
"bytes"
"encoding/json"
"io"
"os"
"strings"

"github.com/pkg/errors"

"github.com/docker/app/internal/cliopts"
"github.com/docker/app/internal/store"

Expand Down Expand Up @@ -131,3 +134,14 @@ func SetupDriver(installation *store.Installation, dockerCli command.Cli, opts *
driverImpl, errBuf := prepareDriver(dockerCli, bind, stdout)
return driverImpl, errBuf, nil
}

func WithRelocationMap(installation *store.Installation) func(op *driver.Operation) error {
return func(op *driver.Operation) error {
data, err := json.Marshal(installation.RelocationMap)
if err != nil {
return errors.Wrap(err, "could not marshal relocation map")
}
op.Files["/cnab/app/relocation-mapping.json"] = string(data)
return nil
}
}
2 changes: 1 addition & 1 deletion internal/commands/image/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func runInspect(dockerCli command.Cli, appname string, opts inspectOptions, inst
}

installation.SetParameter(internal.ParameterInspectFormatName, format)
if err = a.Run(&installation.Claim, nil); err != nil {
if err = a.Run(&installation.Claim, nil, cnab.WithRelocationMap(installation)); err != nil {
return fmt.Errorf("inspect failed: %s\n%s", err, errBuf)
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/image/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func runRender(dockerCli command.Cli, appname string, opts renderOptions, instal
}
installation.Parameters[internal.ParameterRenderFormatName] = opts.formatDriver

if err := action.Run(&installation.Claim, nil, cfgFunc); err != nil {
if err := action.Run(&installation.Claim, nil, cfgFunc, cnab.WithRelocationMap(installation)); err != nil {
return fmt.Errorf("render failed: %s\n%s", err, errBuf)
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func runInspect(dockerCli command.Cli, appName string, inspectOptions inspectOpt
} else {
return fmt.Errorf("inspect failed: status action is not supported by the App")
}
if err := a.Run(&installation.Claim, creds); err != nil {
if err := a.Run(&installation.Claim, creds, cnab.WithRelocationMap(installation)); err != nil {
return fmt.Errorf("inspect failed: %s\n%s", err, errBuf)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/commands/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func runRemove(dockerCli command.Cli, installationName string, opts removeOption
op.Out = dockerCli.Out()
return nil
}
if err := uninst.Run(&installation.Claim, creds, cfgFunc); err != nil {
if err := uninst.Run(&installation.Claim, creds, cfgFunc, cnab.WithRelocationMap(installation)); err != nil {
if err2 := installationStore.Store(installation); err2 != nil {
return fmt.Errorf("%s while %s", err2, errBuf)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func runBundle(dockerCli command.Cli, bndl *relocated.Bundle, opts runOptions, i
op.Out = dockerCli.Out()
return nil
}
err = inst.Run(&installation.Claim, creds, cfgFunc)
err = inst.Run(&installation.Claim, creds, cfgFunc, cnab.WithRelocationMap(installation))
}
// Even if the installation failed, the installation is persisted with its failure status,
// so any installation needs a clean uninstallation.
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func runUpdate(dockerCli command.Cli, installationName string, opts updateOption
op.Out = dockerCli.Out()
return nil
}
err = u.Run(&installation.Claim, creds, cfgFunc)
err = u.Run(&installation.Claim, creds, cfgFunc, cnab.WithRelocationMap(installation))
err2 := installationStore.Store(installation)
if err != nil {
return fmt.Errorf("Update failed: %s\n%s", err, errBuf)
Expand Down
16 changes: 0 additions & 16 deletions internal/store/installation.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func NewInstallation(name string, reference string, bndl *relocated.Bundle) (*In
Reference: reference,
RelocationMap: bndl.RelocationMap,
}
i.applyRelocationMap()

return i, nil
}
Expand All @@ -52,21 +51,6 @@ func (i Installation) SetParameter(name string, value string) {
}
}

func (i *Installation) applyRelocationMap() {
for idx, def := range i.Bundle.InvocationImages {
if img, ok := i.RelocationMap[def.Image]; ok {
def.Image = img
i.Bundle.InvocationImages[idx] = def
}
}
for name, def := range i.Bundle.Images {
if img, ok := i.RelocationMap[def.Image]; ok {
def.Image = img
i.Bundle.Images[name] = def
}
}
}

var _ InstallationStore = &installationStore{}

type installationStore struct {
Expand Down
70 changes: 0 additions & 70 deletions internal/store/installation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import (
"os"
"testing"

"github.com/docker/app/internal/relocated"

"github.com/deislabs/cnab-go/bundle"
"github.com/docker/cnab-to-oci/relocation"

"github.com/deislabs/cnab-go/claim"
"gotest.tools/assert"
"gotest.tools/fs"
Expand Down Expand Up @@ -43,68 +38,3 @@ func TestStoreAndReadInstallation(t *testing.T) {
assert.NilError(t, err)
assert.DeepEqual(t, expectedInstallation, actualInstallation)
}

func TestApplyingRelocationMap(t *testing.T) {
installation, _ := NewInstallation("name", "reference", &relocated.Bundle{
Bundle: &bundle.Bundle{
InvocationImages: []bundle.InvocationImage{
{
BaseImage: bundle.BaseImage{
Image: "localimage:1.0-invoc",
},
},
},
Images: map[string]bundle.Image{
"svc1": {
BaseImage: bundle.BaseImage{
Image: "svc-1:local",
},
},
"redis": {
BaseImage: bundle.BaseImage{
Image: "redis:latest",
},
},
"hello": {
BaseImage: bundle.BaseImage{
Image: "http-echo",
},
},
},
},
RelocationMap: relocation.ImageRelocationMap{
"localimage:1.0-invoc": "docker.io/repo/app:tag@sha256:9f9426498125d4017bdbdc861451bd447b9cb6d0c7a790093a65f508f45a1dd4",
"svc-1:local": "docker.io/repo/app:tag@sha256:d14de6677360066fcb302892bf288b8a907fddb264f587189ea17e691284e58c",
"redis:latest": "docker.io/repo/app:tag@sha256:e6e61849494c55a096cb48f7efb262271a50e2452b6a9e3553cf26f519f01d23",
},
})

expectedBundle := &bundle.Bundle{
InvocationImages: []bundle.InvocationImage{
{
BaseImage: bundle.BaseImage{
Image: "docker.io/repo/app:tag@sha256:9f9426498125d4017bdbdc861451bd447b9cb6d0c7a790093a65f508f45a1dd4",
},
},
},
Images: map[string]bundle.Image{
"svc1": {
BaseImage: bundle.BaseImage{
Image: "docker.io/repo/app:tag@sha256:d14de6677360066fcb302892bf288b8a907fddb264f587189ea17e691284e58c",
},
},
"redis": {
BaseImage: bundle.BaseImage{
Image: "docker.io/repo/app:tag@sha256:e6e61849494c55a096cb48f7efb262271a50e2452b6a9e3553cf26f519f01d23",
},
},
"hello": {
BaseImage: bundle.BaseImage{
Image: "http-echo",
},
},
},
}

assert.DeepEqual(t, expectedBundle, installation.Bundle)
}

0 comments on commit 5361689

Please sign in to comment.