mirror of
https://github.com/containrrr/watchtower.git
synced 2026-02-12 10:24:20 +01:00
feat(api): implement new api handler
This commit is contained in:
parent
72e437f173
commit
47091761a5
17 changed files with 571 additions and 294 deletions
22
pkg/api/updates/updates.go
Normal file
22
pkg/api/updates/updates.go
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
package updates
|
||||
|
||||
import (
|
||||
"github.com/containrrr/watchtower/pkg/types"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ModifyParamsFunc func(up *types.UpdateParams)
|
||||
type InvokedFunc func(ModifyParamsFunc) types.Report
|
||||
|
||||
func parseImages(u *url.URL) []string {
|
||||
var images []string
|
||||
imageQueries, found := u.Query()["image"]
|
||||
if found {
|
||||
for _, image := range imageQueries {
|
||||
images = append(images, strings.Split(image, ",")...)
|
||||
}
|
||||
|
||||
}
|
||||
return images
|
||||
}
|
||||
37
pkg/api/updates/updates_v1.go
Normal file
37
pkg/api/updates/updates_v1.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package updates
|
||||
|
||||
import (
|
||||
. "github.com/containrrr/watchtower/pkg/api/prelude"
|
||||
"github.com/containrrr/watchtower/pkg/filters"
|
||||
"github.com/containrrr/watchtower/pkg/types"
|
||||
"sync"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// PostV1 creates an API http.HandlerFunc for V1 of updates
|
||||
func PostV1(updateFn InvokedFunc, updateLock *sync.Mutex) HandlerFunc {
|
||||
return func(c *Context) Response {
|
||||
log.Info("Updates triggered by HTTP API request.")
|
||||
|
||||
images := parseImages(c.Request.URL)
|
||||
|
||||
if !updateLock.TryLock() {
|
||||
if len(images) > 0 {
|
||||
// If images have been passed, wait until the current updates are done
|
||||
updateLock.Lock()
|
||||
} else {
|
||||
// If a full update is running (no explicit image filter), skip this update
|
||||
log.Debug("Skipped. Another updates already running.")
|
||||
return OK(nil) // For backwards compatibility
|
||||
}
|
||||
}
|
||||
|
||||
defer updateLock.Unlock()
|
||||
_ = updateFn(func(up *types.UpdateParams) {
|
||||
up.Filter = filters.FilterByImage(images, up.Filter)
|
||||
})
|
||||
|
||||
return OK(nil)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue