Skip to content

Commit

Permalink
Merge pull request containers#371 from nalind/tarball
Browse files Browse the repository at this point in the history
Add a "tarball" transport for treating discrete tarballs as image layers
  • Loading branch information
mtrmac authored Nov 2, 2017
2 parents d2284a8 + 32b40fc commit b34d14c
Show file tree
Hide file tree
Showing 6 changed files with 459 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/policy.json.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ a directory containing one or more tags, or any of the parent directories.

*Note:* See `dir:` above for semantics and restrictions on the directory paths, they apply to `oci:` equivalently.

### `tarball:`

The `tarball:` transport refers to tarred up container root filesystems.

Scopes are ignored.

## Policy Requirements

Using the mechanisms above, a set of policy requirements is looked up. The policy requirements
Expand Down
48 changes: 48 additions & 0 deletions tarball/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Package tarball provides a way to generate images using one or more layer
// tarballs and an optional template configuration.
//
// An example:
// package main
//
// import (
// "fmt"
//
// cp "github.com/containers/image/copy"
// "github.com/containers/image/tarball"
// "github.com/containers/image/transports/alltransports"
//
// imgspecv1 "github.com/containers/image/transports/alltransports"
// )
//
// func imageFromTarball() {
// src, err := alltransports.ParseImageName("tarball:/var/cache/mock/fedora-26-x86_64/root_cache/cache.tar.gz")
// // - or -
// // src, err := tarball.Transport.ParseReference("/var/cache/mock/fedora-26-x86_64/root_cache/cache.tar.gz")
// if err != nil {
// panic(err)
// }
// updater, ok := src.(tarball.ConfigUpdater)
// if !ok {
// panic("unexpected: a tarball reference should implement tarball.ConfigUpdater")
// }
// config := imgspecv1.Image{
// Config: imgspecv1.ImageConfig{
// Cmd: []string{"/bin/bash"},
// },
// }
// annotations := make(map[string]string)
// annotations[imgspecv1.AnnotationDescription] = "test image built from a mock root cache"
// err = updater.ConfigUpdate(config, annotations)
// if err != nil {
// panic(err)
// }
// dest, err := alltransports.ParseImageName("docker-daemon:mock:latest")
// if err != nil {
// panic(err)
// }
// err = cp.Image(nil, dest, src, nil)
// if err != nil {
// panic(err)
// }
// }
package tarball
88 changes: 88 additions & 0 deletions tarball/tarball_reference.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package tarball

import (
"fmt"
"os"
"strings"

"github.com/containers/image/docker/reference"
"github.com/containers/image/image"
"github.com/containers/image/types"

imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
)

// ConfigUpdater is an interface that ImageReferences for "tarball" images also
// implement. It can be used to set values for a configuration, and to set
// image annotations which will be present in the images returned by the
// reference's NewImage() or NewImageSource() methods.
type ConfigUpdater interface {
ConfigUpdate(config imgspecv1.Image, annotations map[string]string) error
}

type tarballReference struct {
transport types.ImageTransport
config imgspecv1.Image
annotations map[string]string
filenames []string
stdin []byte
}

// ConfigUpdate updates the image's default configuration and adds annotations
// which will be visible in source images created using this reference.
func (r *tarballReference) ConfigUpdate(config imgspecv1.Image, annotations map[string]string) error {
r.config = config
if r.annotations == nil {
r.annotations = make(map[string]string)
}
for k, v := range annotations {
r.annotations[k] = v
}
return nil
}

func (r *tarballReference) Transport() types.ImageTransport {
return r.transport
}

func (r *tarballReference) StringWithinTransport() string {
return strings.Join(r.filenames, ":")
}

func (r *tarballReference) DockerReference() reference.Named {
return nil
}

func (r *tarballReference) PolicyConfigurationIdentity() string {
return ""
}

func (r *tarballReference) PolicyConfigurationNamespaces() []string {
return nil
}

func (r *tarballReference) NewImage(ctx *types.SystemContext) (types.Image, error) {
src, err := r.NewImageSource(ctx)
if err != nil {
return nil, err
}
img, err := image.FromSource(src)
if err != nil {
src.Close()
return nil, err
}
return img, nil
}

func (r *tarballReference) DeleteImage(ctx *types.SystemContext) error {
for _, filename := range r.filenames {
if err := os.Remove(filename); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("error removing %q: %v", filename, err)
}
}
return nil
}

func (r *tarballReference) NewImageDestination(ctx *types.SystemContext) (types.ImageDestination, error) {
return nil, fmt.Errorf("destination not implemented yet")
}
Loading

0 comments on commit b34d14c

Please sign in to comment.