fix: correctly handle non-stale restarts (#1220)

This commit is contained in:
nils måsén 2022-04-18 19:36:38 +02:00 committed by GitHub
parent d12ce7ce79
commit e9c83af533
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 166 additions and 93 deletions

View file

@ -2,6 +2,8 @@ package actions
import (
"errors"
"strings"
"github.com/containrrr/watchtower/internal/util"
"github.com/containrrr/watchtower/pkg/container"
"github.com/containrrr/watchtower/pkg/lifecycle"
@ -9,7 +11,6 @@ import (
"github.com/containrrr/watchtower/pkg/sorter"
"github.com/containrrr/watchtower/pkg/types"
log "github.com/sirupsen/logrus"
"strings"
)
// Update looks at the running Docker containers to see if any of the images
@ -108,8 +109,10 @@ func performRollingRestart(containers []container.Container, client container.Cl
} else {
if err := restartStaleContainer(containers[i], client, params); err != nil {
failed[containers[i].ID()] = err
} else if containers[i].Stale {
// Only add (previously) stale containers' images to cleanup
cleanupImageIDs[containers[i].ImageID()] = true
}
cleanupImageIDs[containers[i].ImageID()] = true
}
}
}
@ -127,7 +130,8 @@ func stopContainersInReversedOrder(containers []container.Container, client cont
if err := stopStaleContainer(containers[i], client, params); err != nil {
failed[containers[i].ID()] = err
} else {
stopped[containers[i].ImageID()] = true
// NOTE: If a container is restarted due to a dependency this might be empty
stopped[containers[i].SafeImageID()] = true
}
}
@ -143,6 +147,14 @@ func stopStaleContainer(container container.Container, client container.Client,
if !container.ToRestart() {
return nil
}
// Perform an additional check here to prevent us from stopping a linked container we cannot restart
if container.LinkedToRestarting {
if err := container.VerifyConfiguration(); err != nil {
return err
}
}
if params.LifecycleHooks {
skipUpdate, err := lifecycle.ExecutePreUpdateCommand(client, container)
if err != nil {
@ -171,11 +183,13 @@ func restartContainersInSortedOrder(containers []container.Container, client con
if !c.ToRestart() {
continue
}
if stoppedImages[c.ImageID()] {
if stoppedImages[c.SafeImageID()] {
if err := restartStaleContainer(c, client, params); err != nil {
failed[c.ID()] = err
} else if c.Stale {
// Only add (previously) stale containers' images to cleanup
cleanupImageIDs[c.ImageID()] = true
}
cleanupImageIDs[c.ImageID()] = true
}
}
@ -188,6 +202,9 @@ func restartContainersInSortedOrder(containers []container.Container, client con
func cleanupImages(client container.Client, imageIDs map[types.ImageID]bool) {
for imageID := range imageIDs {
if imageID == "" {
continue
}
if err := client.RemoveImageByID(imageID); err != nil {
log.Error(err)
}