Fix container order in update method

This commit is contained in:
DarkAEther 2020-11-27 17:25:27 +05:30
parent 4fb0c8ed0e
commit 78a4d63088
2 changed files with 36 additions and 36 deletions

View file

@ -35,7 +35,7 @@ func CreateUndirectedLinks(containers []container.Container) map[string][]string
// Each list inside the outer list contains containers that are related by links
// This method checks for staleness, checks dependencies, sorts the containers and returns the final
// [][]container.Container
func PrepareContainerList(client container.Client, params types.UpdateParams) ([][]container.Container, error) {
func PrepareContainerList(client container.Client, params types.UpdateParams) ([]container.Container, error) {
containers, err := client.ListContainers(params.Filter)
if err != nil {
@ -56,16 +56,7 @@ func PrepareContainerList(client container.Client, params types.UpdateParams) ([
checkDependencies(containers)
var dependencySortedGraphs [][]container.Container
undirectedNodes := CreateUndirectedLinks(containers)
dependencySortedGraphs, err = sorter.SortByDependencies(containers,undirectedNodes)
if err != nil {
return nil, err
}
return dependencySortedGraphs, nil
return containers, nil
}
// Update looks at the running Docker containers to see if any of the images
@ -79,14 +70,14 @@ func Update(client container.Client, params types.UpdateParams) error {
lifecycle.ExecutePreChecks(client, params)
}
containers, err := client.ListContainers(params.Filter)
containers, err := PrepareContainerList(client, params)
if err != nil {
return err
}
containersToUpdate := []container.Container{}
if !params.MonitorOnly {
for i := len(containers) - 1; i >= 0; i-- {
for i := 0; i < len(containers); i++ {
if !containers[i].IsMonitorOnly() {
containersToUpdate = append(containersToUpdate, containers[i])
}
@ -99,7 +90,11 @@ func Update(client container.Client, params types.UpdateParams) error {
if params.RollingRestart {
performRollingRestart(containersToUpdate, client, params)
} else {
dependencySortedGraphs, err := PrepareContainerList(client, params)
var dependencySortedGraphs [][]container.Container
undirectedNodes := CreateUndirectedLinks(containersToUpdate)
dependencySortedGraphs, err := sorter.SortByDependencies(containersToUpdate,undirectedNodes)
if err != nil {
return err
}
@ -108,11 +103,11 @@ func Update(client container.Client, params types.UpdateParams) error {
for _, dependencyGraph:= range dependencySortedGraphs {
stopContainersInReversedOrder(dependencyGraph, client, params)
restartContainersInSortedOrder(dependencyGraph, client, params, imageIDs)
}
//clean up after containers updated
if params.Cleanup {
cleanupImages(client,imageIDs)
}
//clean up after containers updated
if params.Cleanup {
cleanupImages(client,imageIDs)
}
}

View file

@ -8,7 +8,7 @@ import (
container2 "github.com/docker/docker/api/types/container"
cli "github.com/docker/docker/client"
"time"
"github.com/containrrr/watchtower/pkg/sorter"
. "github.com/containrrr/watchtower/internal/actions/mocks"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
@ -38,19 +38,19 @@ var _ = Describe("the update action", func() {
"test-container-01",
"fake-image:latest",
time.Now().AddDate(0, 0, -1),
make([]string, 0)),
nil),
CreateMockContainer(
"test-container-02",
"test-container-02",
"fake-image:latest",
time.Now(),
make([]string, 0)),
nil),
CreateMockContainer(
"test-container-02",
"test-container-02",
"fake-image:latest",
time.Now(),
make([]string, 0)),
nil),
},
},
dockerClient,
@ -77,7 +77,7 @@ var _ = Describe("the update action", func() {
"unique-test-container",
"unique-fake-image:latest",
time.Now(),
make([]string, 0),
nil,
),
)
err := actions.Update(client, types.UpdateParams{Cleanup: true})
@ -109,57 +109,57 @@ var _ = Describe("the update action", func() {
"k-container-03",
"fake-image:latest",
time.Now().Add(time.Second * 4),
links[2],),
links[2]),
CreateMockContainer(
"k-container-02",
"k-container-02",
"fake-image:latest",
time.Now().Add(time.Second * 2),
links[1],),
links[1]),
CreateMockContainer(
"k-container-01",
"k-container-01",
"fake-image:latest",
time.Now(),
links[0],),
links[0]),
CreateMockContainer(
"t-container-03",
"t-container-03",
"fake-image-2:latest",
time.Now().Add(time.Second * 4),
links[5],),
links[5]),
CreateMockContainer(
"t-container-02",
"t-container-02",
"fake-image-2:latest",
time.Now().Add(time.Second * 2),
links[4],),
links[4]),
CreateMockContainer(
"t-container-01",
"t-container-01",
"fake-image-2:latest",
time.Now(),
links[3],),
links[3]),
CreateMockContainer(
"x-container-01",
"x-container-01",
"fake-image-1:latest",
time.Now(),
links[6],),
links[6]),
CreateMockContainer(
"x-container-02",
"x-container-02",
"fake-image-1:latest",
time.Now().Add(time.Second * 2),
links[6],),
links[6]),
CreateMockContainer(
"x-container-03",
"x-container-03",
"fake-image-1:latest",
time.Now().Add(time.Second * 4),
links[6],),
links[6]),
},
},
dockerClient,
@ -170,7 +170,9 @@ var _ = Describe("the update action", func() {
When("there are multiple containers with links", func() {
It("should create appropriate dependency sorted lists", func() {
dependencySortedGraphs, err := actions.PrepareContainerList(client, types.UpdateParams{Cleanup: true})
containers, err := actions.PrepareContainerList(client, types.UpdateParams{Cleanup: true})
undirectedNodes := actions.CreateUndirectedLinks(containers)
dependencySortedGraphs, err := sorter.SortByDependencies(containers,undirectedNodes)
Expect(err).NotTo(HaveOccurred())
var output [][]string
@ -241,7 +243,8 @@ var _ = Describe("the update action", func() {
"test-container-01",
"test-container-01",
"fake-image1:latest",
time.Now()),
time.Now(),
nil),
CreateMockContainerWithConfig(
"test-container-02",
"test-container-02",
@ -276,12 +279,14 @@ var _ = Describe("the update action", func() {
"test-container-01",
"test-container-01",
"fake-image:latest",
time.Now()),
time.Now(),
nil),
CreateMockContainer(
"test-container-02",
"test-container-02",
"fake-image:latest",
time.Now()),
time.Now(),
nil),
},
},
dockerClient,