mirror of
https://github.com/containrrr/watchtower.git
synced 2025-09-22 05:40:50 +02:00

* 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>
72 lines
1.3 KiB
Go
72 lines
1.3 KiB
Go
package update
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
var (
|
|
lock chan bool
|
|
)
|
|
|
|
// New is a factory function creating a new Handler instance
|
|
func New(updateFn func(images []string), updateLock chan bool) *Handler {
|
|
if updateLock != nil {
|
|
lock = updateLock
|
|
} else {
|
|
lock = make(chan bool, 1)
|
|
lock <- true
|
|
}
|
|
|
|
return &Handler{
|
|
fn: updateFn,
|
|
Path: "/v1/update",
|
|
}
|
|
}
|
|
|
|
// Handler is an API handler used for triggering container update scans
|
|
type Handler struct {
|
|
fn func(images []string)
|
|
Path string
|
|
}
|
|
|
|
// Handle is the actual http.Handle function doing all the heavy lifting
|
|
func (handle *Handler) Handle(w http.ResponseWriter, r *http.Request) {
|
|
log.Info("Updates triggered by HTTP API request.")
|
|
|
|
_, err := io.Copy(os.Stdout, r.Body)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
|
|
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(images)
|
|
} else {
|
|
select {
|
|
case chanValue := <-lock:
|
|
defer func() { lock <- chanValue }()
|
|
handle.fn(images)
|
|
default:
|
|
log.Debug("Skipped. Another update already running.")
|
|
}
|
|
}
|
|
|
|
}
|