From e802e1642a793f951db3a7cbccd3afe72a6b4888 Mon Sep 17 00:00:00 2001 From: BenjaminFaal Date: Thu, 21 Nov 2024 20:13:28 +0100 Subject: [PATCH] add optional image tag filter --- docs/http-api-mode.md | 5 +++++ pkg/filters/filters.go | 10 ++++++++-- pkg/filters/filters_test.go | 26 ++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/docs/http-api-mode.md b/docs/http-api-mode.md index 69812bb60..05b819100 100644 --- a/docs/http-api-mode.md +++ b/docs/http-api-mode.md @@ -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 +``` diff --git a/pkg/filters/filters.go b/pkg/filters/filters.go index 4fa0bcd15..1fbe8e11d 100644 --- a/pkg/filters/filters.go +++ b/pkg/filters/filters.go @@ -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) } } diff --git a/pkg/filters/filters_test.go b/pkg/filters/filters_test.go index 2b5cb5e70..ad291e041 100644 --- a/pkg/filters/filters_test.go +++ b/pkg/filters/filters_test.go @@ -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) {