瀏覽代碼

Merge pull request #30061 from yongtang/30027-docker-images-v1.12

Fix `Size` in `docker images` for v1.12 client to v1.13 daemon
Sebastiaan van Stijn 8 年之前
父節點
當前提交
0b09d6bfa7
共有 3 個文件被更改,包括 40 次插入8 次删除
  1. 3 4
      cli/command/formatter/image.go
  2. 1 4
      daemon/images.go
  3. 36 0
      integration-cli/docker_api_images_test.go

+ 3 - 4
cli/command/formatter/image.go

@@ -226,8 +226,7 @@ func (c *imageContext) CreatedAt() string {
 
 
 func (c *imageContext) Size() string {
 func (c *imageContext) Size() string {
 	c.AddHeader(sizeHeader)
 	c.AddHeader(sizeHeader)
-	//NOTE: For backward compatibility we need to return VirtualSize
-	return units.HumanSizeWithPrecision(float64(c.i.VirtualSize), 3)
+	return units.HumanSizeWithPrecision(float64(c.i.Size), 3)
 }
 }
 
 
 func (c *imageContext) Containers() string {
 func (c *imageContext) Containers() string {
@@ -253,8 +252,8 @@ func (c *imageContext) SharedSize() string {
 
 
 func (c *imageContext) UniqueSize() string {
 func (c *imageContext) UniqueSize() string {
 	c.AddHeader(uniqueSizeHeader)
 	c.AddHeader(uniqueSizeHeader)
-	if c.i.Size == -1 {
+	if c.i.VirtualSize == -1 || c.i.SharedSize == -1 {
 		return "N/A"
 		return "N/A"
 	}
 	}
-	return units.HumanSize(float64(c.i.Size))
+	return units.HumanSize(float64(c.i.VirtualSize - c.i.SharedSize))
 }
 }

+ 1 - 4
daemon/images.go

@@ -210,7 +210,6 @@ func (daemon *Daemon) Images(imageFilters filters.Args, all bool, withExtraAttrs
 			rootFS := *img.RootFS
 			rootFS := *img.RootFS
 			rootFS.DiffIDs = nil
 			rootFS.DiffIDs = nil
 
 
-			newImage.Size = 0
 			newImage.SharedSize = 0
 			newImage.SharedSize = 0
 			for _, id := range img.RootFS.DiffIDs {
 			for _, id := range img.RootFS.DiffIDs {
 				rootFS.Append(id)
 				rootFS.Append(id)
@@ -223,8 +222,6 @@ func (daemon *Daemon) Images(imageFilters filters.Args, all bool, withExtraAttrs
 
 
 				if layerRefs[chid] > 1 {
 				if layerRefs[chid] > 1 {
 					newImage.SharedSize += diffSize
 					newImage.SharedSize += diffSize
-				} else {
-					newImage.Size += diffSize
 				}
 				}
 			}
 			}
 		}
 		}
@@ -323,7 +320,7 @@ func newImage(image *image.Image, virtualSize int64) *types.ImageSummary {
 	newImage.ParentID = image.Parent.String()
 	newImage.ParentID = image.Parent.String()
 	newImage.ID = image.ID().String()
 	newImage.ID = image.ID().String()
 	newImage.Created = image.Created.Unix()
 	newImage.Created = image.Created.Unix()
-	newImage.Size = -1
+	newImage.Size = virtualSize
 	newImage.VirtualSize = virtualSize
 	newImage.VirtualSize = virtualSize
 	newImage.SharedSize = -1
 	newImage.SharedSize = -1
 	newImage.Containers = -1
 	newImage.Containers = -1

+ 36 - 0
integration-cli/docker_api_images_test.go

@@ -128,3 +128,39 @@ func (s *DockerSuite) TestAPIImagesSearchJSONContentType(c *check.C) {
 	c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
 	c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
 	c.Assert(res.Header.Get("Content-Type"), checker.Equals, "application/json")
 	c.Assert(res.Header.Get("Content-Type"), checker.Equals, "application/json")
 }
 }
+
+// Test case for 30027: image size reported as -1 in v1.12 client against v1.13 daemon.
+// This test checks to make sure both v1.12 and v1.13 client against v1.13 daemon get correct `Size` after the fix.
+func (s *DockerSuite) TestAPIImagesSizeCompatibility(c *check.C) {
+	status, b, err := request.SockRequest("GET", "/images/json", nil, daemonHost())
+	c.Assert(err, checker.IsNil)
+	c.Assert(status, checker.Equals, http.StatusOK)
+	var images []types.ImageSummary
+	err = json.Unmarshal(b, &images)
+	c.Assert(err, checker.IsNil)
+	c.Assert(len(images), checker.Not(checker.Equals), 0)
+	for _, image := range images {
+		c.Assert(image.Size, checker.Not(checker.Equals), int64(-1))
+	}
+
+	type v124Image struct {
+		ID          string `json:"Id"`
+		ParentID    string `json:"ParentId"`
+		RepoTags    []string
+		RepoDigests []string
+		Created     int64
+		Size        int64
+		VirtualSize int64
+		Labels      map[string]string
+	}
+	status, b, err = request.SockRequest("GET", "/v1.24/images/json", nil, daemonHost())
+	c.Assert(err, checker.IsNil)
+	c.Assert(status, checker.Equals, http.StatusOK)
+	var v124Images []v124Image
+	err = json.Unmarshal(b, &v124Images)
+	c.Assert(err, checker.IsNil)
+	c.Assert(len(v124Images), checker.Not(checker.Equals), 0)
+	for _, image := range v124Images {
+		c.Assert(image.Size, checker.Not(checker.Equals), int64(-1))
+	}
+}