Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
duffle relocate of thick bundles
Browse files Browse the repository at this point in the history
Fixes #705
  • Loading branch information
glyn committed Jun 20, 2019
1 parent f066be3 commit c4ea891
Show file tree
Hide file tree
Showing 23 changed files with 954 additions and 243 deletions.
4 changes: 2 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@

[[constraint]]
name = "github.com/pivotal/image-relocation"
revision = "532dd0b42e7a50010d7868364309cd314a2bb376"
revision = "3c9c32bb3d97fc213476f1c9846a99ca35ecba5d"

[[override]]
name = "github.com/google/go-containerregistry"
Expand Down
8 changes: 5 additions & 3 deletions cmd/duffle/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"io"

"github.com/deislabs/duffle/pkg/imagestore/builder"

"github.com/deislabs/duffle/pkg/duffle/home"
"github.com/deislabs/duffle/pkg/loader"
"github.com/deislabs/duffle/pkg/packager"
Expand Down Expand Up @@ -76,20 +78,20 @@ func (ex *exportCmd) run() error {
}

func (ex *exportCmd) Export(bundlefile string, l loader.BundleLoader) error {
is, err := packager.NewImageStore(ex.thin)
bldr, err := builder.NewBuilder(ex.thin)
if err != nil {
return err
}

exp, err := packager.NewExporter(bundlefile, ex.dest, ex.home.Logs(), l, is)
exp, err := packager.NewExporter(bundlefile, ex.dest, ex.home.Logs(), l, bldr)
if err != nil {
return fmt.Errorf("Unable to set up exporter: %s", err)
}
if err := exp.Export(); err != nil {
return err
}
if ex.verbose {
fmt.Fprintf(ex.out, "Export logs: %s\n", exp.Logs)
fmt.Fprintf(ex.out, "Export logs: %s\n", exp.Logs())
}
return nil
}
Expand Down
124 changes: 59 additions & 65 deletions cmd/duffle/relocate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/deislabs/duffle/pkg/imagestore"
"github.com/deislabs/duffle/pkg/imagestore/builder"
"github.com/deislabs/duffle/pkg/loader"
"github.com/deislabs/duffle/pkg/packager"
"github.com/deislabs/duffle/pkg/relocator"

"github.com/pivotal/image-relocation/pkg/image"
"github.com/pivotal/image-relocation/pkg/pathmapping"
"github.com/pivotal/image-relocation/pkg/registry"

"github.com/deislabs/cnab-go/bundle"

Expand Down Expand Up @@ -51,8 +58,9 @@ type relocateCmd struct {
out io.Writer

// dependencies
mapping pathmapping.PathMapping
registryClient registry.Client
mapping pathmapping.PathMapping
imageStoreBuilder imagestore.Builder
imageStore imagestore.Store
}

func newRelocateCmd(w io.Writer) *cobra.Command {
Expand Down Expand Up @@ -81,7 +89,7 @@ duffle relocate path/to/bundle.json --relocation-mapping path/to/relmap.json --r
relocate.home = home.Home(homePath())

relocate.mapping = pathmapping.FlattenRepoPathPreserveTagDigest
relocate.registryClient = registry.NewRegistryClient()
relocate.imageStoreBuilder = builder.NewLocatingBuilder()

return relocate.run()
},
Expand All @@ -98,92 +106,78 @@ duffle relocate path/to/bundle.json --relocation-mapping path/to/relmap.json --r
}

func (r *relocateCmd) run() error {
bun, err := r.setup()
relMap := make(map[string]string)

rel, _, cleanup, err := r.setup()
if err != nil {
return err
}
defer cleanup()

if err := rel.Relocate(relMap); err != nil {
return err
}

return r.relocate(bun)
return r.writeRelocationMapping(relMap)
}

func (r *relocateCmd) relocate(bun *bundle.Bundle) error {
relMap := make(map[string]string)
for i := range bun.InvocationImages {
ii := bun.InvocationImages[i]
modified, err := r.relocateImage(&ii.BaseImage, relMap)
// The caller is responsible for running the returned cleanup function, which may delete the returned bundle.
func (r *relocateCmd) setup() (*relocator.Relocator, *bundle.Bundle, func(), error) {
nop := func() {}
dest := ""
bundleFile, err := resolveBundleFilePath(r.inputBundle, r.home.String(), r.bundleIsFile)
if err != nil {
return nil, nil, nop, err
}

var bun *bundle.Bundle

if strings.HasSuffix(bundleFile, ".tgz") {
source, err := filepath.Abs(bundleFile)
if err != nil {
return err
return nil, nil, nop, err
}
if modified {
bun.InvocationImages[i] = ii

dest, err = ioutil.TempDir("", "duffle-relocate-unzip")
if err != nil {
return nil, nil, nop, err
}
}

for k := range bun.Images {
im := bun.Images[k]
modified, err := r.relocateImage(&im.BaseImage, relMap)
l := loader.NewLoader()
imp, err := packager.NewImporter(source, dest, l, false)
if err != nil {
return nil, nil, nop, err
}
dest, bun, err = imp.Unzip()
if err != nil {
return err
return nil, nil, nop, err
}
if modified {
bun.Images[k] = im
} else {
bun, err = loadBundle(bundleFile)
if err != nil {
return nil, nil, nop, err
}
}

return r.writeRelocationMapping(relMap)
}

func (r *relocateCmd) relocateImage(i *bundle.BaseImage, relMap map[string]string) (bool, error) {
if !isOCI(i.ImageType) && !isDocker(i.ImageType) {
return false, nil
}
// map the image name
n, err := image.NewName(i.Image)
if err != nil {
return false, err
if err = bun.Validate(); err != nil {
return nil, nil, nop, err
}
rn := r.mapping(r.repoPrefix, n)

// tag/push the image to its new repository
dig, err := r.registryClient.Copy(n, rn)
r.imageStore, err = r.imageStoreBuilder.ArchiveDir(dest).Build()
if err != nil {
return false, err
}
if i.Digest != "" && dig.String() != i.Digest {
// should not happen
return false, fmt.Errorf("digest of image %s not preserved: old digest %s; new digest %s", i.Image, i.Digest, dig.String())
return nil, nil, nop, err
}

// update the relocation map
relMap[i.Image] = rn.String()

return true, nil
}

func isOCI(imageType string) bool {
return imageType == "" || imageType == "oci"
}

func isDocker(imageType string) bool {
return imageType == "docker"
}

func (r *relocateCmd) setup() (*bundle.Bundle, error) {
bundleFile, err := resolveBundleFilePath(r.inputBundle, r.home.String(), r.bundleIsFile)
if err != nil {
return nil, err
mapping := func(i image.Name) image.Name {
return pathmapping.FlattenRepoPathPreserveTagDigest(r.repoPrefix, i)
}

bun, err := loadBundle(bundleFile)
reloc, err := relocator.NewRelocator(bun, mapping, r.imageStore, r.out)
if err != nil {
return nil, err
}

if err = bun.Validate(); err != nil {
return nil, err
return nil, nil, nop, err
}

return bun, nil
return reloc, bun, func() { os.RemoveAll(dest) }, nil
}

func (r *relocateCmd) writeRelocationMapping(relMap map[string]string) error {
Expand Down
Loading

0 comments on commit c4ea891

Please sign in to comment.