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
|
@ -4,6 +4,7 @@ import (
|
|||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
@ -13,7 +14,7 @@ var (
|
|||
)
|
||||
|
||||
// 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 {
|
||||
lock = updateLock
|
||||
} else {
|
||||
|
@ -29,7 +30,7 @@ func New(updateFn func(), updateLock chan bool) *Handler {
|
|||
|
||||
// Handler is an API handler used for triggering container update scans
|
||||
type Handler struct {
|
||||
fn func()
|
||||
fn func(images []string)
|
||||
Path string
|
||||
}
|
||||
|
||||
|
@ -43,12 +44,29 @@ func (handle *Handler) Handle(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
select {
|
||||
case chanValue := <-lock:
|
||||
var images []string
|
||||
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 }()
|
||||
handle.fn()
|
||||
default:
|
||||
log.Debug("Skipped. Another update already running.")
|
||||
handle.fn(images)
|
||||
} else {
|
||||
select {
|
||||
case chanValue := <-lock:
|
||||
defer func() { lock <- chanValue }()
|
||||
handle.fn(images)
|
||||
default:
|
||||
log.Debug("Skipped. Another update already running.")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue