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

View file

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