links.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package daemon
  2. import (
  3. "strings"
  4. "sync"
  5. "github.com/Sirupsen/logrus"
  6. "github.com/docker/docker/container"
  7. "github.com/docker/docker/pkg/graphdb"
  8. )
  9. // linkIndex stores link relationships between containers, including their specified alias
  10. // The alias is the name the parent uses to reference the child
  11. type linkIndex struct {
  12. // idx maps a parent->alias->child relationship
  13. idx map[*container.Container]map[string]*container.Container
  14. // childIdx maps child->parent->aliases
  15. childIdx map[*container.Container]map[*container.Container]map[string]struct{}
  16. mu sync.Mutex
  17. }
  18. func newLinkIndex() *linkIndex {
  19. return &linkIndex{
  20. idx: make(map[*container.Container]map[string]*container.Container),
  21. childIdx: make(map[*container.Container]map[*container.Container]map[string]struct{}),
  22. }
  23. }
  24. // link adds indexes for the passed in parent/child/alias relationships
  25. func (l *linkIndex) link(parent, child *container.Container, alias string) {
  26. l.mu.Lock()
  27. if l.idx[parent] == nil {
  28. l.idx[parent] = make(map[string]*container.Container)
  29. }
  30. l.idx[parent][alias] = child
  31. if l.childIdx[child] == nil {
  32. l.childIdx[child] = make(map[*container.Container]map[string]struct{})
  33. }
  34. if l.childIdx[child][parent] == nil {
  35. l.childIdx[child][parent] = make(map[string]struct{})
  36. }
  37. l.childIdx[child][parent][alias] = struct{}{}
  38. l.mu.Unlock()
  39. }
  40. // unlink removes the requested alias for the given parent/child
  41. func (l *linkIndex) unlink(alias string, child, parent *container.Container) {
  42. l.mu.Lock()
  43. delete(l.idx[parent], alias)
  44. delete(l.childIdx[child], parent)
  45. l.mu.Unlock()
  46. }
  47. // children maps all the aliases-> children for the passed in parent
  48. // aliases here are the aliases the parent uses to refer to the child
  49. func (l *linkIndex) children(parent *container.Container) map[string]*container.Container {
  50. l.mu.Lock()
  51. children := l.idx[parent]
  52. l.mu.Unlock()
  53. return children
  54. }
  55. // parents maps all the aliases->parent for the passed in child
  56. // aliases here are the aliases the parents use to refer to the child
  57. func (l *linkIndex) parents(child *container.Container) map[string]*container.Container {
  58. l.mu.Lock()
  59. parents := make(map[string]*container.Container)
  60. for parent, aliases := range l.childIdx[child] {
  61. for alias := range aliases {
  62. parents[alias] = parent
  63. }
  64. }
  65. l.mu.Unlock()
  66. return parents
  67. }
  68. // delete deletes all link relationships referencing this container
  69. func (l *linkIndex) delete(container *container.Container) {
  70. l.mu.Lock()
  71. for _, child := range l.idx[container] {
  72. delete(l.childIdx[child], container)
  73. }
  74. delete(l.idx, container)
  75. delete(l.childIdx, container)
  76. l.mu.Unlock()
  77. }
  78. // migrateLegacySqliteLinks migrates sqlite links to use links from HostConfig
  79. // when sqlite links were used, hostConfig.Links was set to nil
  80. func (daemon *Daemon) migrateLegacySqliteLinks(db *graphdb.Database, container *container.Container) error {
  81. // if links is populated (or an empty slice), then this isn't using sqlite links and can be skipped
  82. if container.HostConfig == nil || container.HostConfig.Links != nil {
  83. return nil
  84. }
  85. logrus.Debugf("migrating legacy sqlite link info for container: %s", container.ID)
  86. fullName := container.Name
  87. if fullName[0] != '/' {
  88. fullName = "/" + fullName
  89. }
  90. // don't use a nil slice, this ensures that the check above will skip once the migration has completed
  91. links := []string{}
  92. children, err := db.Children(fullName, 0)
  93. if err != nil {
  94. if !strings.Contains(err.Error(), "Cannot find child for") {
  95. return err
  96. }
  97. // else continue... it's ok if we didn't find any children, it'll just be nil and we can continue the migration
  98. }
  99. for _, child := range children {
  100. c, err := daemon.GetContainer(child.Entity.ID())
  101. if err != nil {
  102. return err
  103. }
  104. links = append(links, c.Name+":"+child.Edge.Name)
  105. }
  106. container.HostConfig.Links = links
  107. return container.WriteHostConfig()
  108. }