mirror of
https://github.com/containrrr/watchtower.git
synced 2025-09-21 21:30:48 +02:00
feat(http): optional query parameter to update only containers of a specified image (#1289)
* feat(http): optional query parameter to update only containers of a specified image * fix style issues * comma separated image parameter * Support comma-separated query parameter as well as specifying it multiple times Co-authored-by: nils måsén <nils@piksel.se> * fixed compile error * fixed FilterByImageTag Not sure what changed in my testing setup, but Docker reports image names including the tag name now. * consistent use of image/tag (use image) * fixed multiple image queries * assuming I'm right here, only block on lock when any images are specified. * add unit tests for image filter. didn't add tests for update api because they didn't already exist * whoops. * use ImageName instead, add unit test for empty ImageName filter. Co-authored-by: nils måsén <nils@piksel.se>
This commit is contained in:
parent
33b8a9822c
commit
739f328ee5
6 changed files with 96 additions and 8 deletions
|
@ -188,7 +188,7 @@ func Run(c *cobra.Command, names []string) {
|
||||||
httpAPI := api.New(apiToken)
|
httpAPI := api.New(apiToken)
|
||||||
|
|
||||||
if enableUpdateAPI {
|
if enableUpdateAPI {
|
||||||
updateHandler := update.New(func() { runUpdatesWithNotifications(filter) }, updateLock)
|
updateHandler := update.New(func(images []string) { runUpdatesWithNotifications(filters.FilterByImage(images, filter)) }, updateLock)
|
||||||
httpAPI.RegisterFunc(updateHandler.Path, updateHandler.Handle)
|
httpAPI.RegisterFunc(updateHandler.Path, updateHandler.Handle)
|
||||||
// If polling isn't enabled the scheduler is never started and
|
// If polling isn't enabled the scheduler is never started and
|
||||||
// we need to trigger the startup messages manually.
|
// we need to trigger the startup messages manually.
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
@ -13,7 +14,7 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
// New is a factory function creating a new Handler instance
|
// New is a factory function creating a new Handler instance
|
||||||
func New(updateFn func(), updateLock chan bool) *Handler {
|
func New(updateFn func(images []string), updateLock chan bool) *Handler {
|
||||||
if updateLock != nil {
|
if updateLock != nil {
|
||||||
lock = updateLock
|
lock = updateLock
|
||||||
} else {
|
} else {
|
||||||
|
@ -29,7 +30,7 @@ func New(updateFn func(), updateLock chan bool) *Handler {
|
||||||
|
|
||||||
// Handler is an API handler used for triggering container update scans
|
// Handler is an API handler used for triggering container update scans
|
||||||
type Handler struct {
|
type Handler struct {
|
||||||
fn func()
|
fn func(images []string)
|
||||||
Path string
|
Path string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,12 +44,29 @@ func (handle *Handler) Handle(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
var images []string
|
||||||
case chanValue := <-lock:
|
imageQueries, found := r.URL.Query()["image"]
|
||||||
|
if found {
|
||||||
|
for _, image := range imageQueries {
|
||||||
|
images = append(images, strings.Split(image, ",")...)
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
images = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(images) > 0 {
|
||||||
|
chanValue := <-lock
|
||||||
defer func() { lock <- chanValue }()
|
defer func() { lock <- chanValue }()
|
||||||
handle.fn()
|
handle.fn(images)
|
||||||
default:
|
} else {
|
||||||
log.Debug("Skipped. Another update already running.")
|
select {
|
||||||
|
case chanValue := <-lock:
|
||||||
|
defer func() { lock <- chanValue }()
|
||||||
|
handle.fn(images)
|
||||||
|
default:
|
||||||
|
log.Debug("Skipped. Another update already running.")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,3 +78,17 @@ func (_m *FilterableContainer) Scope() (string, bool) {
|
||||||
|
|
||||||
return r0, r1
|
return r0, r1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ImageName provides a mock function with given fields:
|
||||||
|
func (_m *FilterableContainer) ImageName() string {
|
||||||
|
ret := _m.Called()
|
||||||
|
|
||||||
|
var r0 string
|
||||||
|
if rf, ok := ret.Get(0).(func() string); ok {
|
||||||
|
r0 = rf()
|
||||||
|
} else {
|
||||||
|
r0 = ret.Get(0).(string)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r0
|
||||||
|
}
|
||||||
|
|
|
@ -70,6 +70,24 @@ func FilterByScope(scope string, baseFilter t.Filter) t.Filter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FilterByImage returns all containers that have a specific image
|
||||||
|
func FilterByImage(images []string, baseFilter t.Filter) t.Filter {
|
||||||
|
if images == nil {
|
||||||
|
return baseFilter
|
||||||
|
}
|
||||||
|
|
||||||
|
return func(c t.FilterableContainer) bool {
|
||||||
|
image := strings.Split(c.ImageName(), ":")[0]
|
||||||
|
for _, targetImage := range images {
|
||||||
|
if image == targetImage {
|
||||||
|
return baseFilter(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// BuildFilter creates the needed filter of containers
|
// BuildFilter creates the needed filter of containers
|
||||||
func BuildFilter(names []string, enableLabel bool, scope string) (t.Filter, string) {
|
func BuildFilter(names []string, enableLabel bool, scope string) (t.Filter, string) {
|
||||||
sb := strings.Builder{}
|
sb := strings.Builder{}
|
||||||
|
|
|
@ -110,6 +110,43 @@ func TestFilterByDisabledLabel(t *testing.T) {
|
||||||
container.AssertExpectations(t)
|
container.AssertExpectations(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFilterByImage(t *testing.T) {
|
||||||
|
filterEmpty := FilterByImage(nil, NoFilter)
|
||||||
|
filterSingle := FilterByImage([]string{"registry"}, NoFilter)
|
||||||
|
filterMultiple := FilterByImage([]string{"registry", "bla"}, NoFilter)
|
||||||
|
assert.NotNil(t, filterSingle)
|
||||||
|
assert.NotNil(t, filterMultiple)
|
||||||
|
|
||||||
|
container := new(mocks.FilterableContainer)
|
||||||
|
container.On("ImageName").Return("registry:2")
|
||||||
|
assert.True(t, filterEmpty(container))
|
||||||
|
assert.True(t, filterSingle(container))
|
||||||
|
assert.True(t, filterMultiple(container))
|
||||||
|
container.AssertExpectations(t)
|
||||||
|
|
||||||
|
container = new(mocks.FilterableContainer)
|
||||||
|
container.On("ImageName").Return("registry:latest")
|
||||||
|
assert.True(t, filterEmpty(container))
|
||||||
|
assert.True(t, filterSingle(container))
|
||||||
|
assert.True(t, filterMultiple(container))
|
||||||
|
container.AssertExpectations(t)
|
||||||
|
|
||||||
|
container = new(mocks.FilterableContainer)
|
||||||
|
container.On("ImageName").Return("abcdef1234")
|
||||||
|
assert.True(t, filterEmpty(container))
|
||||||
|
assert.False(t, filterSingle(container))
|
||||||
|
assert.False(t, filterMultiple(container))
|
||||||
|
container.AssertExpectations(t)
|
||||||
|
|
||||||
|
container = new(mocks.FilterableContainer)
|
||||||
|
container.On("ImageName").Return("bla:latest")
|
||||||
|
assert.True(t, filterEmpty(container))
|
||||||
|
assert.False(t, filterSingle(container))
|
||||||
|
assert.True(t, filterMultiple(container))
|
||||||
|
container.AssertExpectations(t)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func TestBuildFilter(t *testing.T) {
|
func TestBuildFilter(t *testing.T) {
|
||||||
var names []string
|
var names []string
|
||||||
names = append(names, "test")
|
names = append(names, "test")
|
||||||
|
|
|
@ -7,4 +7,5 @@ type FilterableContainer interface {
|
||||||
IsWatchtower() bool
|
IsWatchtower() bool
|
||||||
Enabled() (bool, bool)
|
Enabled() (bool, bool)
|
||||||
Scope() (string, bool)
|
Scope() (string, bool)
|
||||||
|
ImageName() string
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue