|
|
|
@ -1,10 +1,11 @@
|
|
|
|
|
package actions
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/containrrr/watchtower/internal/util"
|
|
|
|
|
"github.com/containrrr/watchtower/pkg/container"
|
|
|
|
|
"github.com/containrrr/watchtower/pkg/lifecycle"
|
|
|
|
|
metrics2 "github.com/containrrr/watchtower/pkg/metrics"
|
|
|
|
|
"github.com/containrrr/watchtower/pkg/metrics"
|
|
|
|
|
"github.com/containrrr/watchtower/pkg/sorter"
|
|
|
|
|
"github.com/containrrr/watchtower/pkg/types"
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
@ -14,21 +15,63 @@ import (
|
|
|
|
|
// used to start those containers have been updated. If a change is detected in
|
|
|
|
|
// any of the images, the associated containers are stopped and restarted with
|
|
|
|
|
// the new image.
|
|
|
|
|
func Update(client container.Client, params types.UpdateParams) (*metrics2.Metric, error) {
|
|
|
|
|
func Update(client container.Client, params types.UpdateParams) (*metrics.Metric, error) {
|
|
|
|
|
log.Debug("Checking containers for updated images")
|
|
|
|
|
metric := &metrics2.Metric{}
|
|
|
|
|
staleCount := 0
|
|
|
|
|
metric := &metrics.Metric{}
|
|
|
|
|
|
|
|
|
|
if params.LifecycleHooks {
|
|
|
|
|
lifecycle.ExecutePreChecks(client, params)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
containers, err := client.ListContainers(params.Filter)
|
|
|
|
|
containers, err := ScanForContainerUpdates(client, params, metric)
|
|
|
|
|
|
|
|
|
|
// Link all containers that are depended upon
|
|
|
|
|
checkDependencies(containers)
|
|
|
|
|
|
|
|
|
|
var containersToUpdate []container.Container
|
|
|
|
|
if !params.MonitorOnly {
|
|
|
|
|
for _, c := range containers {
|
|
|
|
|
if !c.IsMonitorOnly() {
|
|
|
|
|
containersToUpdate = append(containersToUpdate, c)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
undirectedNodes := CreateUndirectedLinks(containersToUpdate)
|
|
|
|
|
updateGraphs, err := sorter.SortByDependencies(containersToUpdate, undirectedNodes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
staleCheckFailed := 0
|
|
|
|
|
imageIDs := make(map[string]bool)
|
|
|
|
|
for _, graphContainers := range updateGraphs {
|
|
|
|
|
if err := ensureUpdateAllowedByLabels(graphContainers); err != nil {
|
|
|
|
|
log.Error(err)
|
|
|
|
|
metric.Failed += len(graphContainers)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
metric.Failed += stopContainersInReversedOrder(graphContainers, client, params)
|
|
|
|
|
metric.Failed += restartContainersInSortedOrder(graphContainers, client, params, imageIDs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
metric.Updated = metric.StaleCount - (metric.Failed - metric.StaleCheckFailed)
|
|
|
|
|
|
|
|
|
|
if params.Cleanup {
|
|
|
|
|
cleanupImages(client, imageIDs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if params.LifecycleHooks {
|
|
|
|
|
lifecycle.ExecutePostChecks(client, params)
|
|
|
|
|
}
|
|
|
|
|
return metric, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ScanForContainerUpdates(client container.Client, params types.UpdateParams, metric *metrics.Metric) ([]container.Container, error) {
|
|
|
|
|
|
|
|
|
|
containers, err := client.ListContainers(params.Filter)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i, targetContainer := range containers {
|
|
|
|
|
stale, err := client.IsContainerStale(targetContainer)
|
|
|
|
@ -50,69 +93,22 @@ func Update(client container.Client, params types.UpdateParams) (*metrics2.Metri
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Infof("Unable to update container %q: %v. Proceeding to next.", targetContainer.Name(), err)
|
|
|
|
|
stale = false
|
|
|
|
|
staleCheckFailed++
|
|
|
|
|
metric.StaleCheckFailed++
|
|
|
|
|
metric.Failed++
|
|
|
|
|
}
|
|
|
|
|
containers[i].Stale = stale
|
|
|
|
|
|
|
|
|
|
if stale {
|
|
|
|
|
staleCount++
|
|
|
|
|
metric.StaleCount++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
containers, err = sorter.SortByDependencies(containers)
|
|
|
|
|
|
|
|
|
|
metric.Scanned = len(containers)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update linkedToRestarting property for dependent containers
|
|
|
|
|
checkDependencies(containers)
|
|
|
|
|
|
|
|
|
|
var containersToUpdate []container.Container
|
|
|
|
|
if !params.MonitorOnly {
|
|
|
|
|
for _, c := range containers {
|
|
|
|
|
if !c.IsMonitorOnly() {
|
|
|
|
|
containersToUpdate = append(containersToUpdate, c)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if params.RollingRestart {
|
|
|
|
|
metric.Failed += performRollingRestart(containersToUpdate, client, params)
|
|
|
|
|
} else {
|
|
|
|
|
metric.Failed += stopContainersInReversedOrder(containersToUpdate, client, params)
|
|
|
|
|
metric.Failed += restartContainersInSortedOrder(containersToUpdate, client, params)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
metric.Updated = staleCount - (metric.Failed - staleCheckFailed)
|
|
|
|
|
|
|
|
|
|
if params.LifecycleHooks {
|
|
|
|
|
lifecycle.ExecutePostChecks(client, params)
|
|
|
|
|
}
|
|
|
|
|
return metric, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func performRollingRestart(containers []container.Container, client container.Client, params types.UpdateParams) int {
|
|
|
|
|
cleanupImageIDs := make(map[string]bool)
|
|
|
|
|
failed := 0
|
|
|
|
|
|
|
|
|
|
for i := len(containers) - 1; i >= 0; i-- {
|
|
|
|
|
if containers[i].ToRestart() {
|
|
|
|
|
if err := stopStaleContainer(containers[i], client, params); err != nil {
|
|
|
|
|
failed++
|
|
|
|
|
}
|
|
|
|
|
if err := restartStaleContainer(containers[i], client, params); err != nil {
|
|
|
|
|
failed++
|
|
|
|
|
}
|
|
|
|
|
cleanupImageIDs[containers[i].ImageID()] = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if params.Cleanup {
|
|
|
|
|
cleanupImages(client, cleanupImageIDs)
|
|
|
|
|
}
|
|
|
|
|
return failed
|
|
|
|
|
return containers, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func stopContainersInReversedOrder(containers []container.Container, client container.Client, params types.UpdateParams) int {
|
|
|
|
@ -149,8 +145,7 @@ func stopStaleContainer(container container.Container, client container.Client,
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func restartContainersInSortedOrder(containers []container.Container, client container.Client, params types.UpdateParams) int {
|
|
|
|
|
imageIDs := make(map[string]bool)
|
|
|
|
|
func restartContainersInSortedOrder(containers []container.Container, client container.Client, params types.UpdateParams, imageIDs map[string]bool) int {
|
|
|
|
|
|
|
|
|
|
failed := 0
|
|
|
|
|
|
|
|
|
@ -164,10 +159,6 @@ func restartContainersInSortedOrder(containers []container.Container, client con
|
|
|
|
|
imageIDs[c.ImageID()] = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if params.Cleanup {
|
|
|
|
|
cleanupImages(client, imageIDs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return failed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -204,22 +195,54 @@ func restartStaleContainer(container container.Container, client container.Clien
|
|
|
|
|
|
|
|
|
|
func checkDependencies(containers []container.Container) {
|
|
|
|
|
|
|
|
|
|
// Build hash lookup map
|
|
|
|
|
lookup := make(map[string]*container.Container, len(containers))
|
|
|
|
|
for _, c := range containers {
|
|
|
|
|
lookup[c.Name()] = &c
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, c := range containers {
|
|
|
|
|
if c.ToRestart() {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LinkLoop:
|
|
|
|
|
for _, linkName := range c.Links() {
|
|
|
|
|
for _, candidate := range containers {
|
|
|
|
|
if candidate.Name() != linkName {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if candidate.ToRestart() {
|
|
|
|
|
c.LinkedToRestarting = true
|
|
|
|
|
break LinkLoop
|
|
|
|
|
}
|
|
|
|
|
if lookup[linkName].ToRestart() {
|
|
|
|
|
c.LinkedToRestarting = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ensureUpdateAllowedByLabels(updateGraph []container.Container) error {
|
|
|
|
|
for _, c := range updateGraph {
|
|
|
|
|
if c.ToRestart() && c.IsMonitorOnly() {
|
|
|
|
|
if c.LinkedToRestarting {
|
|
|
|
|
return fmt.Errorf("container %q needs to be restarted to satisfy a linked dependency, but it is set to monitor-only", c.Name())
|
|
|
|
|
}
|
|
|
|
|
// return fmt.Errorf("container %q needs to be restarted to satisfy a linked dependency, but it is set to monitor-only", c.Name())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CreateUndirectedLinks creates a map of undirected links
|
|
|
|
|
// Key: Name of a container
|
|
|
|
|
// Value: List of containers that are linked to the container
|
|
|
|
|
// i.e if Container A depends on B, undirectedNodes['A'] will initially contain B.
|
|
|
|
|
// This function adds 'A' into undirectedNodes['B'] to make the link undirected.
|
|
|
|
|
func CreateUndirectedLinks(containers []container.Container) map[string][]string {
|
|
|
|
|
|
|
|
|
|
undirectedNodes := make(map[string][]string)
|
|
|
|
|
for i := 0; i < len(containers); i++ {
|
|
|
|
|
undirectedNodes[containers[i].Name()] = containers[i].Links()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i := 0; i < len(containers); i++ {
|
|
|
|
|
for j := 0; j < len(containers[i].Links()); j++ {
|
|
|
|
|
undirectedNodes[containers[i].Links()[j]] = append(undirectedNodes[containers[i].Links()[j]], containers[i].Name())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return undirectedNodes
|
|
|
|
|
}
|
|
|
|
|