mirror of
https://github.com/crazy-max/diun.git
synced 2025-12-25 23:03:58 +01:00
Entry metadata field
This commit is contained in:
@@ -2,21 +2,29 @@ package provider
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/containerd/containerd/platforms"
|
||||
"github.com/crazy-max/diun/v4/internal/model"
|
||||
"github.com/crazy-max/diun/v4/pkg/registry"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var (
|
||||
metadataKeyChars = `a-zA-Z0-9_`
|
||||
metadataKeyRegexp = regexp.MustCompile(`^[` + metadataKeyChars + `]+$`)
|
||||
)
|
||||
|
||||
// ValidateImage returns a standard image through Docker labels
|
||||
func ValidateImage(image string, labels map[string]string, watchByDef bool) (img model.Image, err error) {
|
||||
func ValidateImage(image string, metadata, labels map[string]string, watchByDef bool) (img model.Image, err error) {
|
||||
if i := strings.Index(image, "@sha256:"); i > 0 {
|
||||
image = image[:i]
|
||||
}
|
||||
img = model.Image{
|
||||
Name: image,
|
||||
Metadata: metadata,
|
||||
NotifyOn: model.NotifyOnDefaults,
|
||||
SortTags: registry.SortTagReverse,
|
||||
}
|
||||
@@ -34,14 +42,14 @@ func ValidateImage(image string, labels map[string]string, watchByDef bool) (img
|
||||
}
|
||||
|
||||
for key, value := range labels {
|
||||
switch key {
|
||||
case "diun.regopt":
|
||||
switch {
|
||||
case key == "diun.regopt":
|
||||
img.RegOpt = value
|
||||
case "diun.watch_repo":
|
||||
case key == "diun.watch_repo":
|
||||
if img.WatchRepo, err = strconv.ParseBool(value); err != nil {
|
||||
return img, fmt.Errorf("cannot parse %s value of label %s", value, key)
|
||||
}
|
||||
case "diun.notify_on":
|
||||
case key == "diun.notify_on":
|
||||
if len(value) == 0 {
|
||||
break
|
||||
}
|
||||
@@ -53,7 +61,7 @@ func ValidateImage(image string, labels map[string]string, watchByDef bool) (img
|
||||
}
|
||||
img.NotifyOn = append(img.NotifyOn, notifyOn)
|
||||
}
|
||||
case "diun.sort_tags":
|
||||
case key == "diun.sort_tags":
|
||||
if value == "" {
|
||||
break
|
||||
}
|
||||
@@ -62,19 +70,19 @@ func ValidateImage(image string, labels map[string]string, watchByDef bool) (img
|
||||
return img, fmt.Errorf("unknown sort tags type %q", value)
|
||||
}
|
||||
img.SortTags = sortTags
|
||||
case "diun.max_tags":
|
||||
case key == "diun.max_tags":
|
||||
if img.MaxTags, err = strconv.Atoi(value); err != nil {
|
||||
return img, fmt.Errorf("cannot parse %s value of label %s", value, key)
|
||||
}
|
||||
case "diun.include_tags":
|
||||
case key == "diun.include_tags":
|
||||
img.IncludeTags = strings.Split(value, ";")
|
||||
case "diun.exclude_tags":
|
||||
case key == "diun.exclude_tags":
|
||||
img.ExcludeTags = strings.Split(value, ";")
|
||||
case "diun.hub_tpl":
|
||||
case key == "diun.hub_tpl":
|
||||
img.HubTpl = value
|
||||
case "diun.hub_link":
|
||||
case key == "diun.hub_link":
|
||||
img.HubLink = value
|
||||
case "diun.platform":
|
||||
case key == "diun.platform":
|
||||
platform, err := platforms.Parse(value)
|
||||
if err != nil {
|
||||
return img, fmt.Errorf("cannot parse %s platform of label %s", value, key)
|
||||
@@ -84,8 +92,27 @@ func ValidateImage(image string, labels map[string]string, watchByDef bool) (img
|
||||
Arch: platform.Architecture,
|
||||
Variant: platform.Variant,
|
||||
}
|
||||
case strings.HasPrefix(key, "diun.metadata."):
|
||||
mkey := strings.TrimPrefix(key, "diun.metadata.")
|
||||
if len(mkey) == 0 || len(value) == 0 {
|
||||
break
|
||||
}
|
||||
if err := validateMetadataKey(mkey); err != nil {
|
||||
return img, errors.Wrapf(err, "invalid metadata key %q", mkey)
|
||||
}
|
||||
if img.Metadata == nil {
|
||||
img.Metadata = map[string]string{}
|
||||
}
|
||||
img.Metadata[mkey] = value
|
||||
}
|
||||
}
|
||||
|
||||
return img, nil
|
||||
}
|
||||
|
||||
func validateMetadataKey(key string) error {
|
||||
if !metadataKeyRegexp.MatchString(key) {
|
||||
return errors.Errorf("only %q are allowed", metadataKeyChars)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user