mirror of
https://github.com/containrrr/watchtower.git
synced 2025-12-16 15:10:12 +01:00
Add a label take precedence argument
This commit is contained in:
parent
36391b0ae7
commit
82f5472fd6
6 changed files with 85 additions and 30 deletions
|
|
@ -39,6 +39,7 @@ var (
|
|||
lifecycleHooks bool
|
||||
rollingRestart bool
|
||||
scope string
|
||||
labelPrecedence bool
|
||||
)
|
||||
|
||||
var rootCmd = NewRootCommand()
|
||||
|
|
@ -109,6 +110,7 @@ func PreRun(cmd *cobra.Command, _ []string) {
|
|||
lifecycleHooks, _ = f.GetBool("enable-lifecycle-hooks")
|
||||
rollingRestart, _ = f.GetBool("rolling-restart")
|
||||
scope, _ = f.GetString("scope")
|
||||
labelPrecedence, _ = f.GetBool("label-take-precedence")
|
||||
|
||||
if scope != "" {
|
||||
log.Debugf(`Using scope %q`, scope)
|
||||
|
|
@ -366,6 +368,7 @@ func runUpdatesWithNotifications(filter t.Filter) *metrics.Metric {
|
|||
MonitorOnly: monitorOnly,
|
||||
LifecycleHooks: lifecycleHooks,
|
||||
RollingRestart: rollingRestart,
|
||||
LabelPrecedence: labelPrecedence,
|
||||
}
|
||||
result, err := actions.Update(client, updateParams)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -205,7 +205,7 @@ Environment Variable: WATCHTOWER_POLL_INTERVAL
|
|||
```
|
||||
|
||||
## Filter by enable label
|
||||
Update containers that have a `com.centurylinklabs.watchtower.enable` label set to true.
|
||||
Monitor and update containers that have a `com.centurylinklabs.watchtower.enable` label set to true.
|
||||
|
||||
```text
|
||||
Argument: --label-enable
|
||||
|
|
@ -215,7 +215,7 @@ Environment Variable: WATCHTOWER_LABEL_ENABLE
|
|||
```
|
||||
|
||||
## Filter by disable label
|
||||
__Do not__ update containers that have `com.centurylinklabs.watchtower.enable` label set to false and
|
||||
__Do not__ Monitor and update containers that have `com.centurylinklabs.watchtower.enable` label set to false and
|
||||
no `--label-enable` argument is passed. Note that only one or the other (targeting by enable label) can be
|
||||
used at the same time to target containers.
|
||||
|
||||
|
|
@ -238,6 +238,18 @@ Environment Variable: WATCHTOWER_MONITOR_ONLY
|
|||
|
||||
Note that monitor-only can also be specified on a per-container basis with the `com.centurylinklabs.watchtower.monitor-only` label set on those containers.
|
||||
|
||||
## With label taking precedence over environment variables
|
||||
|
||||
By default, environement variables will take precedence over labels. This means that if you set `WATCHTOWER_MONITOR_ONLY` to true, a container with `com.centurylinklabs.watchtower.monitor-only` set to false will not be updated. If you set `WATCHTOWER_LABEL_TAKE_PRECEDENCE` to true, then the container will also be updated
|
||||
|
||||
|
||||
```text
|
||||
Argument: --label-take-precedence
|
||||
Environment Variable: WATCHTOWER_LABEL_TAKE_PRECEDENCE
|
||||
Type: Boolean
|
||||
Default: false
|
||||
```
|
||||
|
||||
## Without restarting containers
|
||||
Do not restart containers after updating. This option can be useful when the start of the containers
|
||||
is managed by an external system such as systemd.
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ func Update(client container.Client, params types.UpdateParams) (types.Report, e
|
|||
|
||||
for i, targetContainer := range containers {
|
||||
stale, newestImage, err := client.IsContainerStale(targetContainer)
|
||||
shouldUpdate := stale && !params.NoRestart && !params.MonitorOnly && !targetContainer.IsMonitorOnly()
|
||||
shouldUpdate := stale && !params.NoRestart && (( !params.MonitorOnly && !targetContainer.IsMonitorOnly() ) || ( params.LabelPrecedence && !targetContainer.IsMonitorOnly() ))
|
||||
if err == nil && shouldUpdate {
|
||||
// Check to make sure we have all the necessary information for recreating the container
|
||||
err = targetContainer.VerifyConfiguration()
|
||||
|
|
@ -72,7 +72,7 @@ func Update(client container.Client, params types.UpdateParams) (types.Report, e
|
|||
UpdateImplicitRestart(containers)
|
||||
|
||||
var containersToUpdate []types.Container
|
||||
if !params.MonitorOnly {
|
||||
if ( !params.MonitorOnly || params.LabelPrecedence ) {
|
||||
for _, c := range containers {
|
||||
if !c.IsMonitorOnly() {
|
||||
containersToUpdate = append(containersToUpdate, c)
|
||||
|
|
|
|||
|
|
@ -182,8 +182,41 @@ var _ = Describe("the update action", func() {
|
|||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(client.TestData.TriedToRemoveImageCount).To(Equal(0))
|
||||
})
|
||||
})
|
||||
|
||||
When("watchtower has been instructed to have label take precedence", func() {
|
||||
It("it should update containers with monitor only set to false", func() {
|
||||
client := CreateMockClient(
|
||||
&TestData{
|
||||
NameOfContainerToKeep: "test-container-02",
|
||||
Containers: []types.Container{
|
||||
CreateMockContainer(
|
||||
"test-container-01",
|
||||
"test-container-01",
|
||||
"fake-image1:latest",
|
||||
time.Now()),
|
||||
CreateMockContainerWithConfig(
|
||||
"test-container-02",
|
||||
"test-container-02",
|
||||
"fake-image2:latest",
|
||||
false,
|
||||
false,
|
||||
time.Now(),
|
||||
&dockerContainer.Config{
|
||||
Labels: map[string]string{
|
||||
"com.centurylinklabs.watchtower.monitor-only": "false",
|
||||
},
|
||||
}),
|
||||
},
|
||||
},
|
||||
false,
|
||||
false,
|
||||
)
|
||||
_, err := actions.Update(client, types.UpdateParams{MonitorOnly: true, LabelPrecedence: true})
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(client.TestData.TriedToRemoveImageCount).To(Equal(1))
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
When("watchtower has been instructed to run lifecycle hooks", func() {
|
||||
|
|
|
|||
|
|
@ -185,6 +185,12 @@ func RegisterSystemFlags(rootCmd *cobra.Command) {
|
|||
"log-level",
|
||||
viper.GetString("WATCHTOWER_LOG_LEVEL"),
|
||||
"The maximum log level that will be written to STDERR. Possible values: panic, fatal, error, warn, info, debug or trace")
|
||||
|
||||
flags.BoolP(
|
||||
"label-take-precedence",
|
||||
"",
|
||||
viper.GetBool("WATCHTOWER_LABEL_TAKE_PRECEDENCE"),
|
||||
"Label applied to containers take precedence over environement variable")
|
||||
}
|
||||
|
||||
// RegisterNotificationFlags that are used by watchtower to send notifications
|
||||
|
|
|
|||
|
|
@ -13,4 +13,5 @@ type UpdateParams struct {
|
|||
MonitorOnly bool
|
||||
LifecycleHooks bool
|
||||
RollingRestart bool
|
||||
LabelPrecedence bool
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue