Skip to content

Commit

Permalink
Merge pull request moby#15144 from aaronlehmann/graph-cleanup
Browse files Browse the repository at this point in the history
Documentation improvements and code cleanups for graph package
  • Loading branch information
calavera committed Jul 31, 2015
2 parents bb2039c + d4836cd commit 8f2dca5
Show file tree
Hide file tree
Showing 19 changed files with 168 additions and 161 deletions.
2 changes: 1 addition & 1 deletion api/client/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ func rewriteDockerfileFrom(dockerfileName string, translator func(string, regist
// Replace the line with a resolved "FROM repo@digest"
repo, tag := parsers.ParseRepositoryTag(matches[1])
if tag == "" {
tag = tags.DEFAULTTAG
tag = tags.DefaultTag
}

repoInfo, err := registry.ParseRepositoryInfo(repo)
Expand Down
4 changes: 2 additions & 2 deletions api/client/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (cli *DockerCli) pullImageCustomOut(image string, out io.Writer) error {
repos, tag := parsers.ParseRepositoryTag(image)
// pull only the image tagged 'latest' if no tag was specified
if tag == "" {
tag = tags.DEFAULTTAG
tag = tags.DefaultTag
}
v.Set("fromImage", repos)
v.Set("tag", tag)
Expand Down Expand Up @@ -96,7 +96,7 @@ func (cli *DockerCli) createContainer(config *runconfig.Config, hostConfig *runc

repo, tag := parsers.ParseRepositoryTag(config.Image)
if tag == "" {
tag = tags.DEFAULTTAG
tag = tags.DefaultTag
}

ref := registry.ParseReference(tag)
Expand Down
2 changes: 1 addition & 1 deletion api/client/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (cli *DockerCli) CmdPull(args ...string) error {

taglessRemote, tag := parsers.ParseRepositoryTag(remote)
if tag == "" && !*allTags {
tag = tags.DEFAULTTAG
tag = tags.DefaultTag
fmt.Fprintf(cli.out, "Using default tag: %s\n", tag)
} else if tag != "" && *allTags {
return fmt.Errorf("tag can't be used with --all-tags/-a")
Expand Down
28 changes: 8 additions & 20 deletions api/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,8 @@ func (s *Server) getImagesJSON(version version.Version, w http.ResponseWriter, r
return err
}

imagesConfig := graph.ImagesConfig{
Filters: r.Form.Get("filters"),
// FIXME this parameter could just be a match filter
Filter: r.Form.Get("filter"),
All: boolValue(r, "all"),
}

images, err := s.daemon.Repositories().Images(&imagesConfig)
// FIXME: The filter parameter could just be a match filter
images, err := s.daemon.Repositories().Images(r.Form.Get("filters"), r.Form.Get("filter"), boolValue(r, "all"))
if err != nil {
return err
}
Expand Down Expand Up @@ -791,23 +785,17 @@ func (s *Server) postImagesCreate(version version.Version, w http.ResponseWriter
}

src := r.Form.Get("fromSrc")
imageImportConfig := &graph.ImageImportConfig{
Changes: r.Form["changes"],
InConfig: r.Body,
OutStream: output,
}

// 'err' MUST NOT be defined within this block, we need any error
// generated from the download to be available to the output
// stream processing below
var newConfig *runconfig.Config
newConfig, err = builder.BuildFromConfig(s.daemon, &runconfig.Config{}, imageImportConfig.Changes)
newConfig, err = builder.BuildFromConfig(s.daemon, &runconfig.Config{}, r.Form["changes"])
if err != nil {
return err
}
imageImportConfig.ContainerConfig = newConfig

err = s.daemon.Repositories().Import(src, repo, tag, imageImportConfig)
err = s.daemon.Repositories().Import(src, repo, tag, r.Body, output, newConfig)
}
if err != nil {
if !output.Flushed() {
Expand Down Expand Up @@ -915,14 +903,14 @@ func (s *Server) getImagesGet(version version.Version, w http.ResponseWriter, r
w.Header().Set("Content-Type", "application/x-tar")

output := ioutils.NewWriteFlusher(w)
imageExportConfig := &graph.ImageExportConfig{Outstream: output}
var names []string
if name, ok := vars["name"]; ok {
imageExportConfig.Names = []string{name}
names = []string{name}
} else {
imageExportConfig.Names = r.Form["names"]
names = r.Form["names"]
}

if err := s.daemon.Repositories().ImageExport(imageExportConfig); err != nil {
if err := s.daemon.Repositories().ImageExport(names, output); err != nil {
if !output.Flushed() {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions daemon/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

"github.com/Sirupsen/logrus"
"github.com/docker/docker/graph"
"github.com/docker/docker/graph/tags"
"github.com/docker/docker/image"
"github.com/docker/docker/pkg/parsers"
"github.com/docker/docker/runconfig"
Expand All @@ -27,7 +27,7 @@ func (daemon *Daemon) ContainerCreate(name string, config *runconfig.Config, hos
if daemon.Graph().IsNotExist(err, config.Image) {
_, tag := parsers.ParseRepositoryTag(config.Image)
if tag == "" {
tag = graph.DefaultTag
tag = tags.DefaultTag
}
return "", warnings, fmt.Errorf("No such image: %s (tag: %s)", config.Image, tag)
}
Expand Down
11 changes: 4 additions & 7 deletions daemon/image_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/Sirupsen/logrus"
"github.com/docker/docker/api/types"
"github.com/docker/docker/graph"
"github.com/docker/docker/graph/tags"
"github.com/docker/docker/image"
"github.com/docker/docker/pkg/parsers"
"github.com/docker/docker/pkg/stringid"
Expand All @@ -27,16 +27,13 @@ func (daemon *Daemon) ImageDelete(name string, force, noprune bool) ([]types.Ima
}

func (daemon *Daemon) imgDeleteHelper(name string, list *[]types.ImageDelete, first, force, noprune bool) error {
var (
repoName, tag string
tags = []string{}
)
var repoName, tag string
repoAndTags := make(map[string][]string)

// FIXME: please respect DRY and centralize repo+tag parsing in a single central place! -- shykes
repoName, tag = parsers.ParseRepositoryTag(name)
if tag == "" {
tag = graph.DefaultTag
tag = tags.DefaultTag
}

if name == "" {
Expand Down Expand Up @@ -111,7 +108,7 @@ func (daemon *Daemon) imgDeleteHelper(name string, list *[]types.ImageDelete, fi
}
}
}
tags = daemon.Repositories().ByID()[img.ID]
tags := daemon.Repositories().ByID()[img.ID]
if (len(tags) <= 1 && repoName == "") || len(tags) == 0 {
if len(byParents[img.ID]) == 0 {
if err := daemon.Repositories().DeleteAll(img.ID); err != nil {
Expand Down
25 changes: 8 additions & 17 deletions graph/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,12 @@ import (
"github.com/docker/docker/registry"
)

// ImageExportConfig holds list of names to be exported to a output stream.
// All images with the given tag and all versions
// containing the same tag are exported. The resulting output is an
// uncompressed tar ball.
type ImageExportConfig struct {
// Names is the set of tags to export.
Names []string
// OutStream is the writer where the images are written to.
Outstream io.Writer
}

// ImageExport exports list of images to a output stream specified in the config.
// The exported images are archived into a tar when written to the output stream.
func (s *TagStore) ImageExport(imageExportConfig *ImageExportConfig) error {

// ImageExport exports list of images to a output stream specified in the
// config. The exported images are archived into a tar when written to the
// output stream. All images with the given tag and all versions containing the
// same tag are exported. names is the set of tags to export, and outStream
// is the writer which the images are written to.
func (s *TagStore) ImageExport(names []string, outStream io.Writer) error {
// get image json
tempdir, err := ioutil.TempDir("", "docker-export-")
if err != nil {
Expand All @@ -44,7 +35,7 @@ func (s *TagStore) ImageExport(imageExportConfig *ImageExportConfig) error {
repo[tag] = id
}
}
for _, name := range imageExportConfig.Names {
for _, name := range names {
name = registry.NormalizeLocalName(name)
logrus.Debugf("Serializing %s", name)
rootRepo := s.Repositories[name]
Expand Down Expand Up @@ -107,7 +98,7 @@ func (s *TagStore) ImageExport(imageExportConfig *ImageExportConfig) error {
}
defer fs.Close()

if _, err := io.Copy(imageExportConfig.Outstream, fs); err != nil {
if _, err := io.Copy(outStream, fs); err != nil {
return err
}
logrus.Debugf("End export image")
Expand Down
13 changes: 7 additions & 6 deletions graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,11 @@ func (graph *Graph) restore() error {
return nil
}

// IsNotExist detects whether an image exists by parsing the incoming error message.
// FIXME: Implement error subclass instead of looking at the error text
// Note: This is the way golang implements os.IsNotExists on Plan9
// IsNotExist detects whether an image exists by parsing the incoming error
// message.
func (graph *Graph) IsNotExist(err error, id string) bool {
// FIXME: Implement error subclass instead of looking at the error text
// Note: This is the way golang implements os.IsNotExists on Plan9
return err != nil && (strings.Contains(strings.ToLower(err.Error()), "does not exist") || strings.Contains(strings.ToLower(err.Error()), "no such")) && strings.Contains(err.Error(), id)
}

Expand Down Expand Up @@ -415,13 +416,13 @@ func (graph *Graph) ByParent() map[string][]*image.Image {
return byParent
}

// Retain keeps the images and layers that are in pulling chain so that they are not deleted.
// If not, they may be deleted by rmi with dangling condition.
// Retain keeps the images and layers that are in the pulling chain so that
// they are not deleted. If not retained, they may be deleted by rmi.
func (graph *Graph) Retain(sessionID string, layerIDs ...string) {
graph.retained.Add(sessionID, layerIDs)
}

// Release removes the referenced image id from the provided set of layers.
// Release removes the referenced image ID from the provided set of layers.
func (graph *Graph) Release(sessionID string, layerIDs ...string) {
graph.retained.Delete(sessionID, layerIDs)
}
Expand Down
15 changes: 8 additions & 7 deletions graph/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ func (graph *Graph) CheckDepth(img *image.Image) error {
return nil
}

// History returns a list of ImageHistory for the specified image name by walking the image lineage.
// History returns a slice of ImageHistory structures for the specified image
// name by walking the image lineage.
func (s *TagStore) History(name string) ([]*types.ImageHistory, error) {
foundImage, err := s.LookupImage(name)
if err != nil {
Expand Down Expand Up @@ -102,20 +103,20 @@ func (s *TagStore) History(name string) ([]*types.ImageHistory, error) {
return history, err
}

// GetParent returns the parent image.
// GetParent returns the parent image for the specified image.
func (graph *Graph) GetParent(img *image.Image) (*image.Image, error) {
if img.Parent == "" {
return nil, nil
}
return graph.Get(img.Parent)
}

// GetParentsSize returns the size of the parent.
func (graph *Graph) GetParentsSize(img *image.Image, size int64) int64 {
// GetParentsSize returns the combined size of all parent images. If there is
// no parent image or it's unavailable, it returns 0.
func (graph *Graph) GetParentsSize(img *image.Image) int64 {
parentImage, err := graph.GetParent(img)
if err != nil || parentImage == nil {
return size
return 0
}
size += parentImage.Size
return graph.GetParentsSize(parentImage, size)
return parentImage.Size + graph.GetParentsSize(parentImage)
}
31 changes: 10 additions & 21 deletions graph/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,19 @@ import (
"github.com/docker/docker/utils"
)

// ImageImportConfig holds configuration to import a image.
type ImageImportConfig struct {
// Changes are the container changes written to top layer.
Changes []string
// InConfig is the input stream containers layered data.
InConfig io.ReadCloser
// OutStream is the output stream where the image is written.
OutStream io.Writer
// ContainerConfig is the configuration of commit container.
ContainerConfig *runconfig.Config
}

// Import allows to download image from a archive.
// If the src is a URL, the content is downloaded from the archive. If the source is '-' then the imageImportConfig.InConfig
// reader will be used to load the image. Once all the layers required are loaded locally, image is then tagged using the tag specified.
func (s *TagStore) Import(src string, repo string, tag string, imageImportConfig *ImageImportConfig) error {
// Import imports an image, getting the archived layer data either from
// inConfig (if src is "-"), or from a URI specified in src. Progress output is
// written to outStream. Repository and tag names can optionally be given in
// the repo and tag arguments, respectively.
func (s *TagStore) Import(src string, repo string, tag string, inConfig io.ReadCloser, outStream io.Writer, containerConfig *runconfig.Config) error {
var (
sf = streamformatter.NewJSONStreamFormatter()
archive archive.ArchiveReader
resp *http.Response
)

if src == "-" {
archive = imageImportConfig.InConfig
archive = inConfig
} else {
u, err := url.Parse(src)
if err != nil {
Expand All @@ -47,14 +36,14 @@ func (s *TagStore) Import(src string, repo string, tag string, imageImportConfig
u.Host = src
u.Path = ""
}
imageImportConfig.OutStream.Write(sf.FormatStatus("", "Downloading from %s", u))
outStream.Write(sf.FormatStatus("", "Downloading from %s", u))
resp, err = httputils.Download(u.String())
if err != nil {
return err
}
progressReader := progressreader.New(progressreader.Config{
In: resp.Body,
Out: imageImportConfig.OutStream,
Out: outStream,
Formatter: sf,
Size: int(resp.ContentLength),
NewLines: true,
Expand All @@ -65,7 +54,7 @@ func (s *TagStore) Import(src string, repo string, tag string, imageImportConfig
archive = progressReader
}

img, err := s.graph.Create(archive, "", "", "Imported from "+src, "", nil, imageImportConfig.ContainerConfig)
img, err := s.graph.Create(archive, "", "", "Imported from "+src, "", nil, containerConfig)
if err != nil {
return err
}
Expand All @@ -75,7 +64,7 @@ func (s *TagStore) Import(src string, repo string, tag string, imageImportConfig
return err
}
}
imageImportConfig.OutStream.Write(sf.FormatStatus("", img.ID))
outStream.Write(sf.FormatStatus("", img.ID))
logID := img.ID
if tag != "" {
logID = utils.ImageReference(logID, tag)
Expand Down
Loading

0 comments on commit 8f2dca5

Please sign in to comment.