mirror of
https://github.com/containrrr/watchtower.git
synced 2025-12-16 15:10:12 +01:00
generalize label override logic
This commit is contained in:
parent
0baaf647f1
commit
2d1d8e70a4
3 changed files with 24 additions and 61 deletions
|
|
@ -2,12 +2,14 @@
|
||||||
package container
|
package container
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/containrrr/watchtower/internal/util"
|
"github.com/containrrr/watchtower/internal/util"
|
||||||
wt "github.com/containrrr/watchtower/pkg/types"
|
wt "github.com/containrrr/watchtower/pkg/types"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
dockercontainer "github.com/docker/docker/api/types/container"
|
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
|
// 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.
|
// the monitor-only label, the monitor-only argument and the label-take-precedence argument.
|
||||||
func (c Container) IsMonitorOnly(params wt.UpdateParams) bool {
|
func (c Container) IsMonitorOnly(params wt.UpdateParams) bool {
|
||||||
var containerMonitorOnlyLabel bool
|
return c.getContainerOrGlobalBool(params.MonitorOnly, monitorOnlyLabel, params.LabelPrecedence)
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsNoPull returns whether the image should be pulled based on values of
|
// 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.
|
// the no-pull label, the no-pull argument and the label-take-precedence argument.
|
||||||
func (c Container) IsNoPull(params wt.UpdateParams) bool {
|
func (c Container) IsNoPull(params wt.UpdateParams) bool {
|
||||||
var containerNoPullLabel bool
|
return c.getContainerOrGlobalBool(params.NoPull, noPullLabel, params.LabelPrecedence)
|
||||||
|
}
|
||||||
|
|
||||||
LabelIsDefined := false
|
func (c Container) getContainerOrGlobalBool(globalVal bool, label string, contPrecedence bool) bool {
|
||||||
|
if contVal, err := c.getBoolLabelValue(label); err != nil {
|
||||||
rawBool, ok := c.getLabelValue(noPullLabel)
|
if !errors.Is(err, errorLabelNotFound) {
|
||||||
if ok {
|
logrus.WithField("error", err).WithField("label", label).Warn("Failed to parse label value")
|
||||||
parsedBool, err := strconv.ParseBool(rawBool)
|
|
||||||
if err == nil {
|
|
||||||
LabelIsDefined = true
|
|
||||||
containerNoPullLabel = parsedBool
|
|
||||||
} else {
|
|
||||||
// Defaulting to false
|
|
||||||
containerNoPullLabel = false
|
|
||||||
}
|
}
|
||||||
|
return globalVal
|
||||||
} else {
|
} else {
|
||||||
// Defaulting to false
|
if contPrecedence {
|
||||||
containerNoPullLabel = false
|
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,3 +5,4 @@ import "errors"
|
||||||
var errorNoImageInfo = errors.New("no available image info")
|
var errorNoImageInfo = errors.New("no available image info")
|
||||||
var errorNoContainerInfo = errors.New("no available container info")
|
var errorNoContainerInfo = errors.New("no available container info")
|
||||||
var errorInvalidConfig = errors.New("container configuration missing or invalid")
|
var errorInvalidConfig = errors.New("container configuration missing or invalid")
|
||||||
|
var errorLabelNotFound = errors.New("label was not found in container")
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
package container
|
package container
|
||||||
|
|
||||||
|
import "strconv"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
watchtowerLabel = "com.centurylinklabs.watchtower"
|
watchtowerLabel = "com.centurylinklabs.watchtower"
|
||||||
signalLabel = "com.centurylinklabs.watchtower.stop-signal"
|
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]
|
val, ok := c.containerInfo.Config.Labels[label]
|
||||||
return val, ok
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue