Browse code

Add label filter support for volume

Since we added labels for volume, it is desired to have
filter support label for volume

Closes: #21416
Signed-off-by: Kai Qiang Wu(Kennan) <wkqwu@cn.ibm.com>

Kai Qiang Wu(Kennan) authored on 2016/03/28 17:43:41
Showing 2 changed files
... ...
@@ -21,6 +21,7 @@ var acceptedVolumeFilterTags = map[string]bool{
21 21
 	"dangling": true,
22 22
 	"name":     true,
23 23
 	"driver":   true,
24
+	"label":    true,
24 25
 }
25 26
 
26 27
 var acceptedPsFilterTags = map[string]bool{
... ...
@@ -587,6 +588,21 @@ func (daemon *Daemon) filterVolumes(vols []volume.Volume, filter filters.Args) (
587 587
 				continue
588 588
 			}
589 589
 		}
590
+		if filter.Include("label") {
591
+			v, err := daemon.volumes.Get(vol.Name())
592
+			if err != nil {
593
+				return nil, err
594
+			}
595
+			if v, ok := v.(interface {
596
+				Labels() map[string]string
597
+			}); ok {
598
+				labels := v.Labels()
599
+
600
+				if !filter.MatchKVList("label", labels) {
601
+					continue
602
+				}
603
+			}
604
+		}
590 605
 		retVols = append(retVols, vol)
591 606
 	}
592 607
 	danglingOnly := false
... ...
@@ -344,3 +344,33 @@ func (s *DockerSuite) TestVolumeCliCreateLabelMultiple(c *check.C) {
344 344
 		c.Assert(strings.TrimSpace(out), check.Equals, v)
345 345
 	}
346 346
 }
347
+
348
+func (s *DockerSuite) TestVolumeCliLsFilterLabels(c *check.C) {
349
+	testVol1 := "testvolcreatelabel-1"
350
+	out, _, err := dockerCmdWithError("volume", "create", "--label", "foo=bar1", "--name", testVol1)
351
+	c.Assert(err, check.IsNil)
352
+
353
+	testVol2 := "testvolcreatelabel-2"
354
+	out, _, err = dockerCmdWithError("volume", "create", "--label", "foo=bar2", "--name", testVol2)
355
+	c.Assert(err, check.IsNil)
356
+
357
+	out, _ = dockerCmd(c, "volume", "ls", "--filter", "label=foo")
358
+
359
+	// filter with label=key
360
+	c.Assert(out, checker.Contains, "testvolcreatelabel-1\n", check.Commentf("expected volume 'testvolcreatelabel-1' in output"))
361
+	c.Assert(out, checker.Contains, "testvolcreatelabel-2\n", check.Commentf("expected volume 'testvolcreatelabel-2' in output"))
362
+
363
+	out, _ = dockerCmd(c, "volume", "ls", "--filter", "label=foo=bar1")
364
+
365
+	// filter with label=key=value
366
+	c.Assert(out, checker.Contains, "testvolcreatelabel-1\n", check.Commentf("expected volume 'testvolcreatelabel-1' in output"))
367
+	c.Assert(out, check.Not(checker.Contains), "testvolcreatelabel-2\n", check.Commentf("expected volume 'testvolcreatelabel-2 in output"))
368
+
369
+	out, _ = dockerCmd(c, "volume", "ls", "--filter", "label=non-exist")
370
+	outArr := strings.Split(strings.TrimSpace(out), "\n")
371
+	c.Assert(len(outArr), check.Equals, 1, check.Commentf("\n%s", out))
372
+
373
+	out, _ = dockerCmd(c, "volume", "ls", "--filter", "label=foo=non-exist")
374
+	outArr = strings.Split(strings.TrimSpace(out), "\n")
375
+	c.Assert(len(outArr), check.Equals, 1, check.Commentf("\n%s", out))
376
+}