Skip to content

Commit

Permalink
add optional image tag filter
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminFaal committed Nov 21, 2024
1 parent 76f9cea commit e802e16
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
5 changes: 5 additions & 0 deletions docs/http-api-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,8 @@ In order to update only certain images, the image names can be provided as URL q
```bash
curl -H "Authorization: Bearer mytoken" localhost:8080/v1/update?image=foo/bar,foo/baz
```

tags are also supported:
```bash
curl -H "Authorization: Bearer mytoken" localhost:8080/v1/update?image=foo/baz:latest
```
10 changes: 8 additions & 2 deletions pkg/filters/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,15 @@ func FilterByImage(images []string, baseFilter t.Filter) t.Filter {
}

return func(c t.FilterableContainer) bool {
image := strings.Split(c.ImageName(), ":")[0]
imageParts := strings.Split(c.ImageName(), ":")
for _, targetImage := range images {
if image == targetImage {
targetImageParts := strings.Split(targetImage, ":")
if imageParts[0] == targetImageParts[0] {
if len(imageParts) == 2 && len(targetImageParts) == 2 {
if imageParts[1] != targetImageParts[1] {
continue
}
}
return baseFilter(c)
}
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/filters/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,32 @@ func TestFilterByImage(t *testing.T) {
assert.True(t, filterMultiple(container))
container.AssertExpectations(t)

filterEmptyTagged := FilterByImage(nil, NoFilter)
filterSingleTagged := FilterByImage([]string{"registry:develop"}, NoFilter)
filterMultipleTagged := FilterByImage([]string{"registry:develop", "registry:latest"}, NoFilter)
assert.NotNil(t, filterSingleTagged)
assert.NotNil(t, filterMultipleTagged)

container = new(mocks.FilterableContainer)
container.On("ImageName").Return("bla:latest")
assert.True(t, filterEmptyTagged(container))
assert.False(t, filterSingleTagged(container))
assert.False(t, filterMultipleTagged(container))
container.AssertExpectations(t)

container = new(mocks.FilterableContainer)
container.On("ImageName").Return("registry:latest")
assert.True(t, filterEmptyTagged(container))
assert.False(t, filterSingleTagged(container))
assert.True(t, filterMultipleTagged(container))
container.AssertExpectations(t)

container = new(mocks.FilterableContainer)
container.On("ImageName").Return("registry:develop")
assert.True(t, filterEmptyTagged(container))
assert.True(t, filterSingleTagged(container))
assert.True(t, filterMultipleTagged(container))
container.AssertExpectations(t)
}

func TestBuildFilter(t *testing.T) {
Expand Down

0 comments on commit e802e16

Please sign in to comment.