Browse Source

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 7 years ago
parent
commit
9833332dba
1 changed files with 26 additions and 2 deletions
  1. 26 2
      daemon/list.go

+ 26 - 2
daemon/list.go

@@ -302,7 +302,7 @@ func (daemon *Daemon) foldFilter(view container.View, config *types.ContainerLis
 	var beforeContFilter, sinceContFilter *container.Snapshot
 
 	err = psFilters.WalkValues("before", func(value string) error {
-		beforeContFilter, err = view.Get(value)
+		beforeContFilter, err = idOrNameFilter(view, value)
 		return err
 	})
 	if err != nil {
@@ -310,7 +310,7 @@ func (daemon *Daemon) foldFilter(view container.View, config *types.ContainerLis
 	}
 
 	err = psFilters.WalkValues("since", func(value string) error {
-		sinceContFilter, err = view.Get(value)
+		sinceContFilter, err = idOrNameFilter(view, value)
 		return err
 	})
 	if err != nil {
@@ -364,6 +364,30 @@ func (daemon *Daemon) foldFilter(view container.View, config *types.ContainerLis
 		names:                view.GetAllNames(),
 	}, nil
 }
+
+func idOrNameFilter(view container.View, value string) (*container.Snapshot, error) {
+	filter, err := view.Get(value)
+	switch err.(type) {
+	case container.NoSuchContainerError:
+		// Try name search instead
+		found := ""
+		for id, idNames := range view.GetAllNames() {
+			for _, eachName := range idNames {
+				if strings.TrimPrefix(value, "/") == strings.TrimPrefix(eachName, "/") {
+					if found != "" && found != id {
+						return nil, err
+					}
+					found = id
+				}
+			}
+		}
+		if found != "" {
+			filter, err = view.Get(found)
+		}
+	}
+	return filter, err
+}
+
 func portOp(key string, filter map[nat.Port]bool) func(value string) error {
 	return func(value string) error {
 		if strings.Contains(value, ":") {