watchtower/pkg/sorter/sort.go

110 lines
3 KiB
Go
Raw Normal View History

package sorter
2015-07-21 16:04:41 +00:00
import (
"github.com/containrrr/watchtower/pkg/container"
2015-07-21 16:04:41 +00:00
"time"
)
2015-07-21 21:40:22 +00:00
// ByCreated allows a list of Container structs to be sorted by the container's
// created date.
type ByCreated []container.Container
2015-07-21 16:04:41 +00:00
func (c ByCreated) Len() int { return len(c) }
func (c ByCreated) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
2015-07-31 22:23:17 +00:00
// Less will compare two elements (identified by index) in the Container
// list by created-date.
2015-07-21 16:04:41 +00:00
func (c ByCreated) Less(i, j int) bool {
t1, err := time.Parse(time.RFC3339Nano, c[i].ContainerInfo().Created)
2015-07-21 16:04:41 +00:00
if err != nil {
t1 = time.Now()
}
t2, _ := time.Parse(time.RFC3339Nano, c[j].ContainerInfo().Created)
2015-07-21 16:04:41 +00:00
if err != nil {
t1 = time.Now()
}
return t1.Before(t2)
}
2015-07-31 22:23:17 +00:00
// SortByDependencies will sort the list of containers taking into account any
// links between containers. Container with no outgoing links will be sorted to
// the front of their dependency list while containers without links will be
// placed into their own list.This sort order ensures that linked containers can
// be started in the correct order as well as separate independent sets of linked
// containers from each other.
2020-08-17 17:47:51 +05:30
func SortByDependencies(containers []container.Container, undirectedNodes map[string][]string) ([][]container.Container, error) {
2015-07-21 16:04:41 +00:00
sorter := dependencySorter{}
2020-08-17 17:47:51 +05:30
return sorter.Sort(containers,undirectedNodes)
2015-07-21 16:04:41 +00:00
}
type dependencySorter struct {
unvisited []container.Container
2015-07-21 16:04:41 +00:00
marked map[string]bool
sorted [][]container.Container
2015-07-21 16:04:41 +00:00
}
2020-08-17 17:47:51 +05:30
func (ds *dependencySorter) Sort(containers []container.Container, undirectedNodes map[string][]string) ([][]container.Container, error) {
2015-07-21 16:04:41 +00:00
ds.unvisited = containers
ds.marked = map[string]bool{}
for len(ds.unvisited) > 0 {
2020-08-17 17:47:51 +05:30
linkedGraph := make([]container.Container,0,0)
ds.sorted = append(ds.sorted,linkedGraph)
if err := ds.visit(ds.unvisited[0],undirectedNodes); err != nil {
2015-07-21 16:04:41 +00:00
return nil, err
}
}
return ds.sorted, nil
}
2020-08-17 17:47:51 +05:30
func (ds *dependencySorter) visit(c container.Container, undirectedNodes map[string][]string) error {
2015-07-21 16:04:41 +00:00
if _, ok := ds.marked[c.Name()]; ok {
return nil
2015-07-21 16:04:41 +00:00
}
// Mark any visited node so that we don't visit it again
2015-07-21 16:04:41 +00:00
ds.marked[c.Name()] = true
defer delete(ds.marked, c.Name())
// Recursively visit links
2020-08-17 17:47:51 +05:30
for _, linkName := range undirectedNodes[c.Name()] {
2015-07-21 16:04:41 +00:00
if linkedContainer := ds.findUnvisited(linkName); linkedContainer != nil {
2020-08-17 17:47:51 +05:30
if err := ds.visit(*linkedContainer,undirectedNodes); err != nil {
2015-07-21 16:04:41 +00:00
return err
}
}
}
// Move container from unvisited to sorted
ds.removeUnvisited(c)
ds.sorted[len(ds.sorted)-1] = append(ds.sorted[len(ds.sorted)-1], c)
2015-07-21 16:04:41 +00:00
return nil
}
func (ds *dependencySorter) findUnvisited(name string) *container.Container {
2015-07-21 16:04:41 +00:00
for _, c := range ds.unvisited {
if c.Name() == name {
return &c
}
}
return nil
}
func (ds *dependencySorter) removeUnvisited(c container.Container) {
2015-07-21 16:04:41 +00:00
var idx int
for i := range ds.unvisited {
if ds.unvisited[i].Name() == c.Name() {
idx = i
break
}
}
ds.unvisited = append(ds.unvisited[0:idx], ds.unvisited[idx+1:]...)
}