Add the label take precedence check in isMonitorOnly()

This commit is contained in:
jebabin 2023-09-08 16:53:05 +00:00
parent 3d4bac9c78
commit ca227f5a57
5 changed files with 43 additions and 27 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() ) || ( params.LabelPrecedence && !targetContainer.IsMonitorOnly() ))
shouldUpdate := stale && !params.NoRestart && !targetContainer.IsMonitorOnly(params)
if err == nil && shouldUpdate {
// Check to make sure we have all the necessary information for recreating the container
err = targetContainer.VerifyConfiguration()
@ -72,12 +72,10 @@ func Update(client container.Client, params types.UpdateParams) (types.Report, e
UpdateImplicitRestart(containers)
var containersToUpdate []types.Container
if ( !params.MonitorOnly || params.LabelPrecedence ) {
for _, c := range containers {
if !c.IsMonitorOnly() {
containersToUpdate = append(containersToUpdate, c)
progress.MarkForUpdate(c.ID())
}
for _, c := range containers {
if !c.IsMonitorOnly(params) {
containersToUpdate = append(containersToUpdate, c)
progress.MarkForUpdate(c.ID())
}
}

View file

@ -178,22 +178,17 @@ var _ = Describe("the update action", func() {
false,
false,
)
_, err := actions.Update(client, types.UpdateParams{MonitorOnly: true})
_, err := actions.Update(client, types.UpdateParams{Cleanup: true, MonitorOnly: true})
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() {
It("it should update containers when monitor only is set to false", func() {
client := CreateMockClient(
&TestData{
NameOfContainerToKeep: "test-container-02",
//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",
@ -211,8 +206,8 @@ var _ = Describe("the update action", func() {
false,
false,
)
_, err := actions.Update(client, types.UpdateParams{MonitorOnly: true, LabelPrecedence: true})
Expect(err).To(HaveOccurred())
_, err := actions.Update(client, types.UpdateParams{Cleanup: true, MonitorOnly: true, LabelPrecedence: true})
Expect(err).NotTo(HaveOccurred())
Expect(client.TestData.TriedToRemoveImageCount).To(Equal(1))
})
})

BIN
oryxBuildBinary Executable file

Binary file not shown.

View file

@ -129,20 +129,43 @@ func (c Container) Enabled() (bool, bool) {
return parsedBool, true
}
// IsMonitorOnly returns the value of the monitor-only label. If the label
// is not set then false is returned.
func (c Container) IsMonitorOnly() 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
MonitorOnlyLabelIsDefined := false
rawBool, ok := c.getLabelValue(monitorOnlyLabel)
if !ok {
return false
if ok {
parsedBool, err := strconv.ParseBool(rawBool)
if err == nil {
MonitorOnlyLabelIsDefined = true
containerMonitorOnlyLabel = parsedBool
} else {
// Defaulting to false
containerMonitorOnlyLabel = false
}
} else {
// Defaulting to false
containerMonitorOnlyLabel = false
}
parsedBool, err := strconv.ParseBool(rawBool)
if err != nil {
return 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 (MonitorOnlyLabelIsDefined) {
if params.LabelPrecedence {
return containerMonitorOnlyLabel
} else {
return true
}
} else {
return true
}
} else {
return containerMonitorOnlyLabel
}
return parsedBool
}
// IsNoPull returns the value of the no-pull label. If the label is not set

View file

@ -52,7 +52,7 @@ type Container interface {
SafeImageID() ImageID
ImageName() string
Enabled() (bool, bool)
IsMonitorOnly() bool
IsMonitorOnly(UpdateParams) bool
Scope() (string, bool)
Links() []string
ToRestart() bool