generalize label override logic

This commit is contained in:
nils måsén 2023-09-16 15:41:17 +02:00
parent 0baaf647f1
commit 2d1d8e70a4
3 changed files with 24 additions and 61 deletions

View file

@ -2,12 +2,14 @@
package container
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/containrrr/watchtower/internal/util"
wt "github.com/containrrr/watchtower/pkg/types"
"github.com/sirupsen/logrus"
"github.com/docker/docker/api/types"
dockercontainer "github.com/docker/docker/api/types/container"
@ -132,77 +134,27 @@ func (c Container) Enabled() (bool, bool) {
// IsMonitorOnly returns whether the container should only be monitored based on values of
// the monitor-only label, the monitor-only argument and the label-take-precedence argument.
func (c Container) IsMonitorOnly(params wt.UpdateParams) bool {
var containerMonitorOnlyLabel bool
LabelIsDefined := false
rawBool, ok := c.getLabelValue(monitorOnlyLabel)
if ok {
parsedBool, err := strconv.ParseBool(rawBool)
if err == nil {
LabelIsDefined = true
containerMonitorOnlyLabel = parsedBool
} else {
// Defaulting to false
containerMonitorOnlyLabel = false
}
} else {
// Defaulting to false
containerMonitorOnlyLabel = false
}
// in case MonitorOnly argument is true, the results change if the container monitor-only label is explicitly set to false if the label-take-precedence is true
if params.MonitorOnly {
if LabelIsDefined {
if params.LabelPrecedence {
return containerMonitorOnlyLabel
} else {
return true
}
} else {
return true
}
} else {
return containerMonitorOnlyLabel
}
return c.getContainerOrGlobalBool(params.MonitorOnly, monitorOnlyLabel, params.LabelPrecedence)
}
// IsNoPull returns whether the image should be pulled based on values of
// the no-pull label, the no-pull argument and the label-take-precedence argument.
func (c Container) IsNoPull(params wt.UpdateParams) bool {
var containerNoPullLabel bool
return c.getContainerOrGlobalBool(params.NoPull, noPullLabel, params.LabelPrecedence)
}
LabelIsDefined := false
rawBool, ok := c.getLabelValue(noPullLabel)
if ok {
parsedBool, err := strconv.ParseBool(rawBool)
if err == nil {
LabelIsDefined = true
containerNoPullLabel = parsedBool
} else {
// Defaulting to false
containerNoPullLabel = false
func (c Container) getContainerOrGlobalBool(globalVal bool, label string, contPrecedence bool) bool {
if contVal, err := c.getBoolLabelValue(label); err != nil {
if !errors.Is(err, errorLabelNotFound) {
logrus.WithField("error", err).WithField("label", label).Warn("Failed to parse label value")
}
return globalVal
} else {
// Defaulting to false
containerNoPullLabel = false
if contPrecedence {
return contVal
} else {
return contVal || globalVal
}
// in case NoPull argument is true, the results change if the container no-pull label is explicitly set to false if the label-take-precedence is true
if params.NoPull {
if LabelIsDefined {
if params.LabelPrecedence {
return containerNoPullLabel
} else {
return true
}
} else {
return true
}
} else {
return containerNoPullLabel
}
}

View file

@ -5,3 +5,4 @@ import "errors"
var errorNoImageInfo = errors.New("no available image info")
var errorNoContainerInfo = errors.New("no available container info")
var errorInvalidConfig = errors.New("container configuration missing or invalid")
var errorLabelNotFound = errors.New("label was not found in container")

View file

@ -1,5 +1,7 @@
package container
import "strconv"
const (
watchtowerLabel = "com.centurylinklabs.watchtower"
signalLabel = "com.centurylinklabs.watchtower.stop-signal"
@ -55,3 +57,11 @@ func (c Container) getLabelValue(label string) (string, bool) {
val, ok := c.containerInfo.Config.Labels[label]
return val, ok
}
func (c Container) getBoolLabelValue(label string) (bool, error) {
if strVal, ok := c.containerInfo.Config.Labels[label]; ok {
value, err := strconv.ParseBool(strVal)
return value, err
}
return false, errorLabelNotFound
}