mirror of
https://github.com/containrrr/watchtower.git
synced 2025-09-22 05:40:50 +02:00
Prometheus support (#450)
Co-authored-by: nils måsén <nils@piksel.se> Co-authored-by: MihailITPlace <ya.halo-halo@yandex.ru> Co-authored-by: Sebastiaan Tammer <sebastiaantammer@gmail.com>
This commit is contained in:
parent
35490c853d
commit
d7d5b25882
23 changed files with 819 additions and 106 deletions
|
@ -5,6 +5,7 @@ import (
|
|||
"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/sorter"
|
||||
"github.com/containrrr/watchtower/pkg/types"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
@ -14,8 +15,10 @@ 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) error {
|
||||
func Update(client container.Client, params types.UpdateParams) (*metrics2.Metric, error) {
|
||||
log.Debug("Checking containers for updated images")
|
||||
metric := &metrics2.Metric{}
|
||||
staleCount := 0
|
||||
|
||||
if params.LifecycleHooks {
|
||||
lifecycle.ExecutePreChecks(client, params)
|
||||
|
@ -23,9 +26,11 @@ func Update(client container.Client, params types.UpdateParams) error {
|
|||
|
||||
containers, err := client.ListContainers(params.Filter)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
staleCheckFailed := 0
|
||||
|
||||
for i, targetContainer := range containers {
|
||||
stale, err := client.IsContainerStale(targetContainer)
|
||||
if stale && !params.NoRestart && !params.MonitorOnly && !targetContainer.IsMonitorOnly() && !targetContainer.HasImageInfo() {
|
||||
|
@ -34,13 +39,20 @@ func Update(client container.Client, params types.UpdateParams) error {
|
|||
if err != nil {
|
||||
log.Infof("Unable to update container %q: %v. Proceeding to next.", containers[i].Name(), err)
|
||||
stale = false
|
||||
staleCheckFailed++
|
||||
metric.Failed++
|
||||
}
|
||||
containers[i].Stale = stale
|
||||
|
||||
if stale {
|
||||
staleCount++
|
||||
}
|
||||
}
|
||||
|
||||
containers, err = sorter.SortByDependencies(containers)
|
||||
metric.Scanned = len(containers)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
checkDependencies(containers)
|
||||
|
@ -55,24 +67,32 @@ func Update(client container.Client, params types.UpdateParams) error {
|
|||
}
|
||||
|
||||
if params.RollingRestart {
|
||||
performRollingRestart(containersToUpdate, client, params)
|
||||
metric.Failed += performRollingRestart(containersToUpdate, client, params)
|
||||
} else {
|
||||
stopContainersInReversedOrder(containersToUpdate, client, params)
|
||||
restartContainersInSortedOrder(containersToUpdate, client, params)
|
||||
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 nil
|
||||
return metric, nil
|
||||
}
|
||||
|
||||
func performRollingRestart(containers []container.Container, client container.Client, params types.UpdateParams) {
|
||||
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].Stale {
|
||||
stopStaleContainer(containers[i], client, params)
|
||||
restartStaleContainer(containers[i], client, params)
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -80,50 +100,63 @@ func performRollingRestart(containers []container.Container, client container.Cl
|
|||
if params.Cleanup {
|
||||
cleanupImages(client, cleanupImageIDs)
|
||||
}
|
||||
return failed
|
||||
}
|
||||
|
||||
func stopContainersInReversedOrder(containers []container.Container, client container.Client, params types.UpdateParams) {
|
||||
func stopContainersInReversedOrder(containers []container.Container, client container.Client, params types.UpdateParams) int {
|
||||
failed := 0
|
||||
for i := len(containers) - 1; i >= 0; i-- {
|
||||
stopStaleContainer(containers[i], client, params)
|
||||
if err := stopStaleContainer(containers[i], client, params); err != nil {
|
||||
failed++
|
||||
}
|
||||
}
|
||||
return failed
|
||||
}
|
||||
|
||||
func stopStaleContainer(container container.Container, client container.Client, params types.UpdateParams) {
|
||||
func stopStaleContainer(container container.Container, client container.Client, params types.UpdateParams) error {
|
||||
if container.IsWatchtower() {
|
||||
log.Debugf("This is the watchtower container %s", container.Name())
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
if !container.Stale {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
if params.LifecycleHooks {
|
||||
if err := lifecycle.ExecutePreUpdateCommand(client, container); err != nil {
|
||||
log.Error(err)
|
||||
log.Info("Skipping container as the pre-update command failed")
|
||||
return
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := client.StopContainer(container, params.Timeout); err != nil {
|
||||
log.Error(err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func restartContainersInSortedOrder(containers []container.Container, client container.Client, params types.UpdateParams) {
|
||||
func restartContainersInSortedOrder(containers []container.Container, client container.Client, params types.UpdateParams) int {
|
||||
imageIDs := make(map[string]bool)
|
||||
|
||||
for _, staleContainer := range containers {
|
||||
if !staleContainer.Stale {
|
||||
failed := 0
|
||||
|
||||
for _, c := range containers {
|
||||
if !c.Stale {
|
||||
continue
|
||||
}
|
||||
restartStaleContainer(staleContainer, client, params)
|
||||
imageIDs[staleContainer.ImageID()] = true
|
||||
if err := restartStaleContainer(c, client, params); err != nil {
|
||||
failed++
|
||||
}
|
||||
imageIDs[c.ImageID()] = true
|
||||
}
|
||||
|
||||
if params.Cleanup {
|
||||
cleanupImages(client, imageIDs)
|
||||
}
|
||||
|
||||
return failed
|
||||
}
|
||||
|
||||
func cleanupImages(client container.Client, imageIDs map[string]bool) {
|
||||
|
@ -134,7 +167,7 @@ func cleanupImages(client container.Client, imageIDs map[string]bool) {
|
|||
}
|
||||
}
|
||||
|
||||
func restartStaleContainer(container container.Container, client container.Client, params types.UpdateParams) {
|
||||
func restartStaleContainer(container container.Container, client container.Client, params types.UpdateParams) error {
|
||||
// Since we can't shutdown a watchtower container immediately, we need to
|
||||
// start the new one while the old one is still running. This prevents us
|
||||
// from re-using the same container name so we first rename the current
|
||||
|
@ -142,17 +175,19 @@ func restartStaleContainer(container container.Container, client container.Clien
|
|||
if container.IsWatchtower() {
|
||||
if err := client.RenameContainer(container, util.RandName()); err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
if !params.NoRestart {
|
||||
if newContainerID, err := client.StartContainer(container); err != nil {
|
||||
log.Error(err)
|
||||
return err
|
||||
} else if container.Stale && params.LifecycleHooks {
|
||||
lifecycle.ExecutePostUpdateCommand(client, newContainerID)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkDependencies(containers []container.Container) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue