Add a label take precedence argument

This commit is contained in:
Jean-Edouard Babin 2023-09-07 19:43:40 +02:00
parent 36391b0ae7
commit 82f5472fd6
6 changed files with 85 additions and 30 deletions

View file

@ -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)

View file

@ -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() {

View file

@ -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