Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions internal/provider/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"regexp"
"slices"
"strconv"
"strings"

Expand All @@ -17,6 +18,19 @@ var (
metadataKeyRegexp = regexp.MustCompile(`^[` + metadataKeyChars + `]+$`)
)

// MergeTags merges image-level tag filters over default tag filters.
func MergeTags(defaultTags, imageTags []string) []string {
tags := slices.Concat(defaultTags, imageTags)
seen := make(map[string]struct{}, len(tags))
return slices.DeleteFunc(tags, func(tag string) bool {
if _, ok := seen[tag]; ok {
return true
}
seen[tag] = struct{}{}
return false
})
}

// ValidateImage returns a standard image through Docker labels
func ValidateImage(image string, metadata, labels map[string]string, watchByDef bool, defaults *model.Defaults) (img model.Image, err error) {
img = model.Image{
Expand All @@ -28,8 +42,8 @@ func ValidateImage(image string, metadata, labels map[string]string, watchByDef
img.NotifyOn = defaults.NotifyOn
img.MaxTags = defaults.MaxTags
img.SortTags = defaults.SortTags
img.IncludeTags = defaults.IncludeTags
img.ExcludeTags = defaults.ExcludeTags
img.IncludeTags = MergeTags(defaults.IncludeTags, nil)
img.ExcludeTags = MergeTags(defaults.ExcludeTags, nil)
img.Metadata = defaults.Metadata
}

Expand Down Expand Up @@ -81,9 +95,9 @@ func ValidateImage(image string, metadata, labels map[string]string, watchByDef
return img, errors.Wrapf(err, "cannot parse %q value of label %s", value, key)
}
case key == "diun.include_tags":
img.IncludeTags = strings.Split(value, ";")
img.IncludeTags = MergeTags(img.IncludeTags, strings.Split(value, ";"))
case key == "diun.exclude_tags":
img.ExcludeTags = strings.Split(value, ";")
img.ExcludeTags = MergeTags(img.ExcludeTags, strings.Split(value, ";"))
case key == "diun.hub_tpl":
img.HubTpl = value
case key == "diun.hub_link":
Expand Down
8 changes: 4 additions & 4 deletions internal/provider/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func TestValidateImage(t *testing.T) {
expectedErr: nil,
},
{
name: "Override default include_tags",
name: "Merge default include_tags",
image: "myimg",
watchByDef: true,
labels: map[string]string{
Expand All @@ -417,7 +417,7 @@ func TestValidateImage(t *testing.T) {
},
expectedImage: model.Image{
Name: "myimg",
IncludeTags: []string{"ubuntu"},
IncludeTags: []string{"alpine", "ubuntu"},
},
expectedErr: nil,
},
Expand Down Expand Up @@ -462,7 +462,7 @@ func TestValidateImage(t *testing.T) {
expectedErr: nil,
},
{
name: "Override default exclude_tags",
name: "Merge default exclude_tags",
image: "myimg",
watchByDef: true,
labels: map[string]string{
Expand All @@ -473,7 +473,7 @@ func TestValidateImage(t *testing.T) {
},
expectedImage: model.Image{
Name: "myimg",
ExcludeTags: []string{"ubuntu"},
ExcludeTags: []string{"alpine", "ubuntu"},
},
expectedErr: nil,
},
Expand Down
3 changes: 3 additions & 0 deletions internal/provider/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ var (
SortTags: registry.SortTagSemver,
MaxTags: 25,
IncludeTags: []string{
`^(0|[1-9]\d*)\..*`,
`^1\.2\..*`,
},
ExcludeTags: []string{
Expand Down Expand Up @@ -120,6 +121,7 @@ var (
`^(0|[1-9]\d*)\..*`,
},
ExcludeTags: []string{
`^0\.0\..*`,
`latest`,
},
},
Expand Down Expand Up @@ -183,6 +185,7 @@ var (
SortTags: registry.SortTagReverse,
MaxTags: 25,
IncludeTags: []string{
`^(0|[1-9]\d*)\..*`,
`^1\..*`,
},
ExcludeTags: []string{
Expand Down
12 changes: 3 additions & 9 deletions internal/provider/file/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/containerd/platforms"
"github.com/crazy-max/diun/v4/internal/model"
"github.com/crazy-max/diun/v4/internal/provider"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -88,15 +89,8 @@ func (c *Client) listFileImage() []model.Image {
item.MaxTags = c.defaults.MaxTags
}

// Set default IncludeTags
if len(item.IncludeTags) == 0 {
item.IncludeTags = c.defaults.IncludeTags
}

// Set default ExcludeTags
if len(item.ExcludeTags) == 0 {
item.ExcludeTags = c.defaults.ExcludeTags
}
item.IncludeTags = provider.MergeTags(c.defaults.IncludeTags, item.IncludeTags)
item.ExcludeTags = provider.MergeTags(c.defaults.ExcludeTags, item.ExcludeTags)

images = append(images, item)
}
Expand Down