Skip to content
This repository was archived by the owner on Dec 17, 2025. It is now read-only.
Merged
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func Run(c *cobra.Command, names []string) {
httpAPI := api.New(apiToken)

if enableUpdateAPI {
updateHandler := update.New(func() { runUpdatesWithNotifications(filter) }, updateLock)
updateHandler := update.New(func(tags []string) { runUpdatesWithNotifications(filters.FilterByImageTag(tags, filter)) }, updateLock)
httpAPI.RegisterFunc(updateHandler.Path, updateHandler.Handle)
// If polling isn't enabled the scheduler is never started and
// we need to trigger the startup messages manually.
Expand Down Expand Up @@ -293,7 +293,7 @@ func writeStartupMessage(c *cobra.Command, sched time.Time, filtering string) {
startupLog.Info("Scheduling first run: " + sched.Format("2006-01-02 15:04:05 -0700 MST"))
startupLog.Info("Note that the first check will be performed in " + until)
} else if runOnce, _ := c.PersistentFlags().GetBool("run-once"); runOnce {
startupLog.Info("Running a one time update.")
startupLog.Info("Running a one time update.")
} else {
startupLog.Info("Periodic runs are not enabled.")
}
Expand Down
20 changes: 12 additions & 8 deletions pkg/api/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"net/http"
"os"
"strings"

log "github.com/sirupsen/logrus"
)
Expand All @@ -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 {
Expand All @@ -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
}

Expand All @@ -43,12 +44,15 @@ func (handle *Handler) Handle(w http.ResponseWriter, r *http.Request) {
return
}

select {
case chanValue := <-lock:
defer func() { lock <- chanValue }()
handle.fn()
default:
log.Debug("Skipped. Another update already running.")
var images []string
if r.URL.Query().Has("image") {
images = strings.Split(r.URL.Query().Get("image"), ",")
} else {
images = nil
}

chanValue := <-lock
defer func() { lock <- chanValue }()
handle.fn(images)

}
5 changes: 5 additions & 0 deletions pkg/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,8 @@ func (c Container) VerifyConfiguration() error {

return nil
}

// Image returns the image tag of the container as reported by the runtime config
func (c Container) Image() string {
return c.runtimeConfig().Image
}
14 changes: 14 additions & 0 deletions pkg/container/mocks/FilterableContainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,17 @@ func (_m *FilterableContainer) Scope() (string, bool) {

return r0, r1
}

// Image provides a mock function with given fields:
func (_m *FilterableContainer) Image() 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
}
18 changes: 18 additions & 0 deletions pkg/filters/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,24 @@ func FilterByScope(scope string, baseFilter t.Filter) t.Filter {
}
}

// FilterByImageTag returns all containers that have a specific image
func FilterByImageTag(tags []string, baseFilter t.Filter) t.Filter {
if tags == nil {
return baseFilter
}

return func(c t.FilterableContainer) bool {
image := c.Image()
for _, targetTag := range tags {
if image == targetTag {
return baseFilter(c)
}
}

return false
}
}

// BuildFilter creates the needed filter of containers
func BuildFilter(names []string, enableLabel bool, scope string) (t.Filter, string) {
sb := strings.Builder{}
Expand Down
1 change: 1 addition & 0 deletions pkg/types/filterable_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ type FilterableContainer interface {
IsWatchtower() bool
Enabled() (bool, bool)
Scope() (string, bool)
Image() string
}