mirror of
https://github.com/containrrr/watchtower.git
synced 2025-09-22 05:40:50 +02:00
Monitor-only for individual containers (#652)
* Add monitor-only label * Add tests for monitor-only * Treat missing monitor-only label as if the option was set to false * Add docs for container-based monitor-only * Add function doc * Fix monitor-only logic
This commit is contained in:
parent
98b518612b
commit
bde421be0d
7 changed files with 147 additions and 10 deletions
|
@ -27,3 +27,22 @@ func CreateMockContainer(id string, name string, image string, created time.Time
|
|||
},
|
||||
)
|
||||
}
|
||||
|
||||
// CreateMockContainerWithConfig creates a container substitute valid for testing
|
||||
func CreateMockContainerWithConfig(id string, name string, image string, created time.Time, config *container2.Config) container.Container {
|
||||
content := types.ContainerJSON{
|
||||
ContainerJSONBase: &types.ContainerJSONBase{
|
||||
ID: id,
|
||||
Image: image,
|
||||
Name: name,
|
||||
Created: created.String(),
|
||||
},
|
||||
Config: config,
|
||||
}
|
||||
return *container.NewContainer(
|
||||
&content,
|
||||
&types.ImageInspect{
|
||||
ID: image,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ func Update(client container.Client, params types.UpdateParams) error {
|
|||
|
||||
for i, targetContainer := range containers {
|
||||
stale, err := client.IsContainerStale(targetContainer)
|
||||
if stale && !params.NoRestart && !params.MonitorOnly && !targetContainer.HasImageInfo() {
|
||||
if stale && !params.NoRestart && !params.MonitorOnly && !targetContainer.IsMonitorOnly() && !targetContainer.HasImageInfo() {
|
||||
err = errors.New("no available image info")
|
||||
}
|
||||
if err != nil {
|
||||
|
@ -45,18 +45,20 @@ func Update(client container.Client, params types.UpdateParams) error {
|
|||
|
||||
checkDependencies(containers)
|
||||
|
||||
if params.MonitorOnly {
|
||||
if params.LifecycleHooks {
|
||||
lifecycle.ExecutePostChecks(client, params)
|
||||
containersToUpdate := []container.Container{}
|
||||
if !params.MonitorOnly {
|
||||
for i := len(containers) - 1; i >= 0; i-- {
|
||||
if !containers[i].IsMonitorOnly() {
|
||||
containersToUpdate = append(containersToUpdate, containers[i])
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if params.RollingRestart {
|
||||
performRollingRestart(containers, client, params)
|
||||
performRollingRestart(containersToUpdate, client, params)
|
||||
} else {
|
||||
stopContainersInReversedOrder(containers, client, params)
|
||||
restartContainersInSortedOrder(containers, client, params)
|
||||
stopContainersInReversedOrder(containersToUpdate, client, params)
|
||||
restartContainersInSortedOrder(containersToUpdate, client, params)
|
||||
}
|
||||
if params.LifecycleHooks {
|
||||
lifecycle.ExecutePostChecks(client, params)
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"github.com/containrrr/watchtower/pkg/container"
|
||||
"github.com/containrrr/watchtower/pkg/container/mocks"
|
||||
"github.com/containrrr/watchtower/pkg/types"
|
||||
container2 "github.com/docker/docker/api/types/container"
|
||||
cli "github.com/docker/docker/client"
|
||||
"time"
|
||||
|
||||
|
@ -80,4 +81,73 @@ var _ = Describe("the update action", func() {
|
|||
})
|
||||
})
|
||||
})
|
||||
|
||||
When("watchtower has been instructed to monitor only", func() {
|
||||
When("certain containers are set to monitor only", func() {
|
||||
BeforeEach(func() {
|
||||
client = CreateMockClient(
|
||||
&TestData{
|
||||
NameOfContainerToKeep: "test-container-02",
|
||||
Containers: []container.Container{
|
||||
CreateMockContainer(
|
||||
"test-container-01",
|
||||
"test-container-01",
|
||||
"fake-image1:latest",
|
||||
time.Now()),
|
||||
CreateMockContainerWithConfig(
|
||||
"test-container-02",
|
||||
"test-container-02",
|
||||
"fake-image2:latest",
|
||||
time.Now(),
|
||||
&container2.Config{
|
||||
Labels: map[string]string{
|
||||
"com.centurylinklabs.watchtower.monitor-only": "true",
|
||||
},
|
||||
}),
|
||||
},
|
||||
},
|
||||
dockerClient,
|
||||
false,
|
||||
false,
|
||||
)
|
||||
})
|
||||
|
||||
It("should not update those containers", func() {
|
||||
err := actions.Update(client, types.UpdateParams{Cleanup: true})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(client.TestData.TriedToRemoveImageCount).To(Equal(1))
|
||||
})
|
||||
})
|
||||
|
||||
When("monitor only is set globally", func() {
|
||||
BeforeEach(func() {
|
||||
client = CreateMockClient(
|
||||
&TestData{
|
||||
Containers: []container.Container{
|
||||
CreateMockContainer(
|
||||
"test-container-01",
|
||||
"test-container-01",
|
||||
"fake-image:latest",
|
||||
time.Now()),
|
||||
CreateMockContainer(
|
||||
"test-container-02",
|
||||
"test-container-02",
|
||||
"fake-image:latest",
|
||||
time.Now()),
|
||||
},
|
||||
},
|
||||
dockerClient,
|
||||
false,
|
||||
false,
|
||||
)
|
||||
})
|
||||
|
||||
It("should not update any containers", func() {
|
||||
err := actions.Update(client, types.UpdateParams{MonitorOnly: true})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(client.TestData.TriedToRemoveImageCount).To(Equal(0))
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue