forked from containers/image
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request containers#371 from nalind/tarball
Add a "tarball" transport for treating discrete tarballs as image layers
- Loading branch information
Showing
6 changed files
with
459 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
Oops, something went wrong.