Skip to content

Commit

Permalink
Use go-containerregistry image processing from container-diff (#181)
Browse files Browse the repository at this point in the history
* Update to newer container-diff release/integration go-containerregistry image processing

* deps
  • Loading branch information
nkubala committed Oct 31, 2018
1 parent 72ff0cd commit 4fe00b0
Show file tree
Hide file tree
Showing 941 changed files with 85,108 additions and 47,393 deletions.
321 changes: 197 additions & 124 deletions Gopkg.lock

Large diffs are not rendered by default.

40 changes: 17 additions & 23 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -1,33 +1,15 @@

# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"

[prune]
unused-packages = true
non-go = true
go-tests = true

[[constraint]]
name = "github.com/GoogleContainerTools/container-diff"
revision = "b2e2f7ef8897f29306877618f16f0d67d8a89094"
revision = "392203e26a619b83149a9f7deb32498ee550af43"

[[constraint]]
name = "github.com/google/go-containerregistry"
revision = "eb57122f1bf944d0584c387c62c1a759c953684d"

[[constraint]]
branch = "master"
Expand All @@ -36,3 +18,15 @@
[[override]]
revision = "583c0c0531f06d5278b7d917446061adc344b5cd"
name = "github.com/spf13/pflag"

[[override]]
name = "github.com/docker/libnetwork"
branch = "master"

[[override]]
name = "github.com/docker/docker"
branch = "master"

[[override]]
name = "github.com/docker/distribution"
revision = "master"
117 changes: 72 additions & 45 deletions pkg/drivers/tar_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ import (
"strings"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"

pkgutil "github.com/GoogleContainerTools/container-diff/pkg/util"
"github.com/GoogleContainerTools/container-structure-test/pkg/types/unversioned"
"github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/mutate"
)

type TarDriver struct {
Expand All @@ -32,33 +35,32 @@ type TarDriver struct {
}

func NewTarDriver(args DriverConfig) (Driver, error) {
var prepper pkgutil.Prepper
var image pkgutil.Image
var err error
imageName := args.Image
if pkgutil.IsTar(imageName) {
prepper = &pkgutil.TarPrepper{
Source: imageName,
}
image, err = prepper.GetImage()
} else {
// see if image exists locally first.
prepper = &pkgutil.DaemonPrepper{
Source: imageName,
}
image, err = prepper.GetImage()
if pkgutil.IsTar(args.Image) {
// tar provided, so don't provide any prefix. container-diff can figure this out.
image, err := pkgutil.GetImageForName(args.Image)
if err != nil {
// didn't find locally, try to pull from remote.
prepper = &pkgutil.CloudPrepper{
Source: imageName,
}
image, err = prepper.GetImage()
return nil, errors.Wrap(err, "processing tar image reference")
}
return &TarDriver{
Image: image,
Save: args.Save,
}, nil
}
// try the local docker daemon first
image, err := pkgutil.GetImageForName("daemon://" + args.Image)
if err == nil {
logrus.Debugf("image found in local docker daemon")
return &TarDriver{
Image: image,
Save: args.Save,
}, nil
}

// image not found in local daemon, so try remote.
logrus.Infof("unable to retrieve image locally: %s", err)
image, err = pkgutil.GetImageForName("remote://" + args.Image)
if err != nil {
// didn't find image anywhere; exit
return nil, err
return nil, errors.Wrap(err, "retrieving image")
}
return &TarDriver{
Image: image,
Expand All @@ -73,31 +75,50 @@ func (d *TarDriver) Destroy() {
}

func (d *TarDriver) SetEnv(envVars []unversioned.EnvVar) error {
config, err := d.GetConfig()
configFile, err := d.Image.Image.ConfigFile()
if err != nil {
return errors.Wrapf(err, "getting image config")
return errors.Wrap(err, "retrieving image config")
}
env := config.Env
config := configFile.Config
env := convertSliceToMap(config.Env)
for _, envVar := range envVars {
env[envVar.Key] = envVar.Value
}
newConfig := pkgutil.ConfigObject{
Entrypoint: config.Entrypoint,
Cmd: config.Cmd,
Volumes: d.Image.Config.Config.Volumes,
Workdir: config.Workdir,
ExposedPorts: d.Image.Config.Config.ExposedPorts,
Labels: config.Labels,
Env: convertMapToSlice(env),
newConfig := v1.Config{
AttachStderr: config.AttachStderr,
AttachStdin: config.AttachStdin,
AttachStdout: config.AttachStdout,
Cmd: config.Cmd,
Domainname: config.Domainname,
Entrypoint: config.Entrypoint,
Env: convertMapToSlice(env),
Hostname: config.Hostname,
Image: config.Image,
Labels: config.Labels,
OnBuild: config.OnBuild,
OpenStdin: config.OpenStdin,
StdinOnce: config.StdinOnce,
Tty: config.Tty,
User: config.User,
Volumes: config.Volumes,
WorkingDir: config.WorkingDir,
ExposedPorts: config.ExposedPorts,
ArgsEscaped: config.ArgsEscaped,
NetworkDisabled: config.NetworkDisabled,
MacAddress: config.MacAddress,
StopSignal: config.StopSignal,
Shell: config.Shell,
}
newImg, err := mutate.Config(d.Image.Image, newConfig)
if err != nil {
return errors.Wrap(err, "setting new config on image")
}
newImage := pkgutil.Image{
Image: newImg,
Source: d.Image.Source,
FSPath: d.Image.FSPath,
Type: d.Image.Type,
Config: pkgutil.ConfigSchema{
History: d.Image.Config.History,
Config: newConfig,
},
Digest: d.Image.Digest,
Layers: d.Image.Layers,
}
d.Image = newImage
return nil
Expand Down Expand Up @@ -130,26 +151,32 @@ func (d *TarDriver) ReadDir(path string) ([]os.FileInfo, error) {
}

func (d *TarDriver) GetConfig() (unversioned.Config, error) {
configFile, err := d.Image.Image.ConfigFile()
if err != nil {
return unversioned.Config{}, errors.Wrap(err, "retrieving config file")
}
config := configFile.Config

// docker provides these as maps (since they can be mapped in docker run commands)
// since this will never be the case when built through a dockerfile, we convert to list of strings
volumes := []string{}
for v := range d.Image.Config.Config.Volumes {
for v := range config.Volumes {
volumes = append(volumes, v)
}

ports := []string{}
for p := range d.Image.Config.Config.ExposedPorts {
for p := range config.ExposedPorts {
// docker always appends the protocol to the port, so this is safe
ports = append(ports, strings.Split(p, "/")[0])
}

return unversioned.Config{
Env: convertSliceToMap(d.Image.Config.Config.Env),
Entrypoint: d.Image.Config.Config.Entrypoint,
Cmd: d.Image.Config.Config.Cmd,
Env: convertSliceToMap(config.Env),
Entrypoint: config.Entrypoint,
Cmd: config.Cmd,
Volumes: volumes,
Workdir: d.Image.Config.Config.Workdir,
Workdir: config.WorkingDir,
ExposedPorts: ports,
Labels: d.Image.Config.Config.Labels,
Labels: config.Labels,
}, nil
}

This file was deleted.

Loading

0 comments on commit 4fe00b0

Please sign in to comment.