mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
600ad5c1b7
Steps to reproduce: ``` # docker run -tid --name aaa ubuntu 57bfd00ac5559f72eec8c1b32a01fe38427d66687940f74611e65137414f0ada # docker run -tid --name bbb --link aaa ubuntu 23ad18362950f39b638206ab4d1885fd4f50cbd1d16aac9cab8e97e0c8363471 # docker ps --no-trunc CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 23ad18362950f39b638206ab4d1885fd4f50cbd1d16aac9cab8e97e0c8363471 ubuntu "/bin/bash" 4 seconds ago Up 3 seconds bbb 57bfd00ac5559f72eec8c1b32a01fe38427d66687940f74611e65137414f0ada ubuntu "/bin/bash" 14 seconds ago Up 14 seconds aaa,bbb/aaa # docker rm -f bbb bbb # docker ps --no-trunc CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 57bfd00ac5559f72eec8c1b32a01fe38427d66687940f74611e65137414f0ada ubuntu "/bin/bash" 29 seconds ago Up 28 seconds aaa,bbb/aaa # docker rm --link bbb/aaa Error response from daemon: Cannot get parent /bbb for name /bbb/aaa ``` When we rm container `bbb`, we can still see `bbb/aaa` in `docker ps --no-trunc`. And this link cannot be deleted since container `bbb` has already been removed. We should remove links of a container when it is deleted. Signed-off-by: Yuanhong Peng <pengyuanhong@huawei.com>
91 lines
2.6 KiB
Go
91 lines
2.6 KiB
Go
package daemon
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/docker/docker/container"
|
|
)
|
|
|
|
// linkIndex stores link relationships between containers, including their specified alias
|
|
// The alias is the name the parent uses to reference the child
|
|
type linkIndex struct {
|
|
// idx maps a parent->alias->child relationship
|
|
idx map[*container.Container]map[string]*container.Container
|
|
// childIdx maps child->parent->aliases
|
|
childIdx map[*container.Container]map[*container.Container]map[string]struct{}
|
|
mu sync.Mutex
|
|
}
|
|
|
|
func newLinkIndex() *linkIndex {
|
|
return &linkIndex{
|
|
idx: make(map[*container.Container]map[string]*container.Container),
|
|
childIdx: make(map[*container.Container]map[*container.Container]map[string]struct{}),
|
|
}
|
|
}
|
|
|
|
// link adds indexes for the passed in parent/child/alias relationships
|
|
func (l *linkIndex) link(parent, child *container.Container, alias string) {
|
|
l.mu.Lock()
|
|
|
|
if l.idx[parent] == nil {
|
|
l.idx[parent] = make(map[string]*container.Container)
|
|
}
|
|
l.idx[parent][alias] = child
|
|
if l.childIdx[child] == nil {
|
|
l.childIdx[child] = make(map[*container.Container]map[string]struct{})
|
|
}
|
|
if l.childIdx[child][parent] == nil {
|
|
l.childIdx[child][parent] = make(map[string]struct{})
|
|
}
|
|
l.childIdx[child][parent][alias] = struct{}{}
|
|
|
|
l.mu.Unlock()
|
|
}
|
|
|
|
// unlink removes the requested alias for the given parent/child
|
|
func (l *linkIndex) unlink(alias string, child, parent *container.Container) {
|
|
l.mu.Lock()
|
|
delete(l.idx[parent], alias)
|
|
delete(l.childIdx[child], parent)
|
|
l.mu.Unlock()
|
|
}
|
|
|
|
// children maps all the aliases-> children for the passed in parent
|
|
// aliases here are the aliases the parent uses to refer to the child
|
|
func (l *linkIndex) children(parent *container.Container) map[string]*container.Container {
|
|
l.mu.Lock()
|
|
children := l.idx[parent]
|
|
l.mu.Unlock()
|
|
return children
|
|
}
|
|
|
|
// parents maps all the aliases->parent for the passed in child
|
|
// aliases here are the aliases the parents use to refer to the child
|
|
func (l *linkIndex) parents(child *container.Container) map[string]*container.Container {
|
|
l.mu.Lock()
|
|
|
|
parents := make(map[string]*container.Container)
|
|
for parent, aliases := range l.childIdx[child] {
|
|
for alias := range aliases {
|
|
parents[alias] = parent
|
|
}
|
|
}
|
|
|
|
l.mu.Unlock()
|
|
return parents
|
|
}
|
|
|
|
// delete deletes all link relationships referencing this container
|
|
func (l *linkIndex) delete(container *container.Container) []string {
|
|
l.mu.Lock()
|
|
|
|
var aliases []string
|
|
for alias, child := range l.idx[container] {
|
|
aliases = append(aliases, alias)
|
|
delete(l.childIdx[child], container)
|
|
}
|
|
delete(l.idx, container)
|
|
delete(l.childIdx, container)
|
|
l.mu.Unlock()
|
|
return aliases
|
|
}
|