Browse code

Fix `before` and `since` filter for `docker ps`

This fix tries to address the issue raised in 35931 where
`before` and `since` filter for `docker ps` does not work
and returns an error
```
Error response from daemon: no such container <container_name>
```

The issue was that `before` and `since` filter are matched
with `view.Get()` which does not take into considerations
of name match.

This fix fixes the issue by adding additional logic for name
match.

This fix fixes 35931.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

Yong Tang authored on 2018/01/05 10:47:53
Showing 1 changed files
... ...
@@ -302,7 +302,7 @@ func (daemon *Daemon) foldFilter(view container.View, config *types.ContainerLis
302 302
 	var beforeContFilter, sinceContFilter *container.Snapshot
303 303
 
304 304
 	err = psFilters.WalkValues("before", func(value string) error {
305
-		beforeContFilter, err = view.Get(value)
305
+		beforeContFilter, err = idOrNameFilter(view, value)
306 306
 		return err
307 307
 	})
308 308
 	if err != nil {
... ...
@@ -310,7 +310,7 @@ func (daemon *Daemon) foldFilter(view container.View, config *types.ContainerLis
310 310
 	}
311 311
 
312 312
 	err = psFilters.WalkValues("since", func(value string) error {
313
-		sinceContFilter, err = view.Get(value)
313
+		sinceContFilter, err = idOrNameFilter(view, value)
314 314
 		return err
315 315
 	})
316 316
 	if err != nil {
... ...
@@ -364,6 +364,30 @@ func (daemon *Daemon) foldFilter(view container.View, config *types.ContainerLis
364 364
 		names:                view.GetAllNames(),
365 365
 	}, nil
366 366
 }
367
+
368
+func idOrNameFilter(view container.View, value string) (*container.Snapshot, error) {
369
+	filter, err := view.Get(value)
370
+	switch err.(type) {
371
+	case container.NoSuchContainerError:
372
+		// Try name search instead
373
+		found := ""
374
+		for id, idNames := range view.GetAllNames() {
375
+			for _, eachName := range idNames {
376
+				if strings.TrimPrefix(value, "/") == strings.TrimPrefix(eachName, "/") {
377
+					if found != "" && found != id {
378
+						return nil, err
379
+					}
380
+					found = id
381
+				}
382
+			}
383
+		}
384
+		if found != "" {
385
+			filter, err = view.Get(found)
386
+		}
387
+	}
388
+	return filter, err
389
+}
390
+
367 391
 func portOp(key string, filter map[nat.Port]bool) func(value string) error {
368 392
 	return func(value string) error {
369 393
 		if strings.Contains(value, ":") {