Skip to content

Commit b9615bf

Browse files
committed
fix defaults not handled by yaml configuration
1 parent 286af41 commit b9615bf

File tree

21 files changed

+146
-282
lines changed

21 files changed

+146
-282
lines changed

.github/workflows/e2e.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ jobs:
4545
loglevel: info
4646
- folder: docker4
4747
loglevel: info
48+
- folder: docker5
49+
loglevel: info
4850
- folder: dockerfile1
4951
loglevel: debug
5052
- folder: dockerfile2

internal/app/job.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (di *Diun) createJob(job model.Job) {
118118
sublog.Error().Err(err).Msgf("Invoking job")
119119
}
120120

121-
if job.Image.WatchRepo == nil || !*job.Image.WatchRepo || len(job.RegImage.Domain) == 0 {
121+
if !*job.Image.WatchRepo || len(job.RegImage.Domain) == 0 {
122122
return
123123
}
124124

internal/config/config_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@ func TestLoadFile(t *testing.T) {
5858
BaseURL: "https://hc-ping.com/",
5959
UUID: "5bf66975-d4c7-4bf5-bcc8-b8d8a82ea278",
6060
},
61-
ImageDefaults: &model.Image{
62-
NotifyOn: model.NotifyOnDefaults,
63-
SortTags: registry.SortTagReverse,
61+
ImageDefaults: &model.ImageDefaults{
62+
WatchRepo: utl.NewFalse(),
63+
NotifyOn: []model.NotifyOn{model.NotifyOnNew},
64+
MaxTags: 5,
65+
SortTags: registry.SortTagReverse,
6466
},
6567
},
6668
Notif: &model.Notif{

internal/config/fixtures/config.test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ watch:
1111
healthchecks:
1212
baseURL: https://hc-ping.com/
1313
uuid: 5bf66975-d4c7-4bf5-bcc8-b8d8a82ea278
14+
imageDefaults:
15+
notifyOn:
16+
- new
17+
maxTags: 5
1418

1519
notif:
1620
amqp:

internal/model/watch.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package model
33
import (
44
"time"
55

6-
"github.com/crazy-max/diun/v4/pkg/registry"
76
"github.com/crazy-max/diun/v4/pkg/utl"
87
)
98

@@ -16,7 +15,7 @@ type Watch struct {
1615
RunOnStartup *bool `yaml:"runOnStartup,omitempty" json:"runOnStartup,omitempty" validate:"required"`
1716
CompareDigest *bool `yaml:"compareDigest,omitempty" json:"compareDigest,omitempty" validate:"required"`
1817
Healthchecks *Healthchecks `yaml:"healthchecks,omitempty" json:"healthchecks,omitempty"`
19-
ImageDefaults *Image `yaml:"defaults,omitempty" json:"defaults,omitempty"`
18+
ImageDefaults *ImageDefaults `yaml:"imageDefaults,omitempty" json:"imageDefaults,omitempty"`
2019
}
2120

2221
// GetDefaults gets the default values
@@ -33,8 +32,4 @@ func (s *Watch) SetDefaults() {
3332
s.FirstCheckNotif = utl.NewFalse()
3433
s.RunOnStartup = utl.NewTrue()
3534
s.CompareDigest = utl.NewTrue()
36-
s.ImageDefaults = &Image{
37-
NotifyOn: NotifyOnDefaults,
38-
SortTags: registry.SortTagReverse,
39-
}
4035
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package model
2+
3+
import (
4+
"github.com/crazy-max/diun/v4/pkg/registry"
5+
"github.com/crazy-max/diun/v4/pkg/utl"
6+
)
7+
8+
// ImageDefaults holds data necessary for image defaults configuration
9+
type ImageDefaults struct {
10+
WatchRepo *bool `yaml:"watchRepo,omitempty" json:"watchRepo,omitempty"`
11+
NotifyOn []NotifyOn `yaml:"notifyOn,omitempty" json:"notifyOn,omitempty"`
12+
MaxTags int `yaml:"maxTags,omitempty" json:"maxTags,omitempty"`
13+
SortTags registry.SortTag `yaml:"sortTags,omitempty" json:"sortTags,omitempty"`
14+
IncludeTags []string `yaml:"includeTags,omitempty" json:"includeTags,omitempty"`
15+
ExcludeTags []string `yaml:"excludeTags,omitempty" json:"excludeTags,omitempty"`
16+
Metadata map[string]string `yaml:"metadata,omitempty" json:"metadata,omitempty"`
17+
}
18+
19+
// GetDefaults gets the default values
20+
func (s *ImageDefaults) GetDefaults() *Watch {
21+
n := &Watch{}
22+
n.SetDefaults()
23+
return n
24+
}
25+
26+
// SetDefaults sets the default values
27+
func (s *ImageDefaults) SetDefaults() {
28+
s.WatchRepo = utl.NewFalse()
29+
s.NotifyOn = NotifyOnDefaults
30+
s.SortTags = registry.SortTagReverse
31+
}

internal/provider/common.go

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,25 @@ var (
1818
)
1919

2020
// ValidateImage returns a standard image through Docker labels
21-
func ValidateImage(image string, metadata, labels map[string]string, watchByDef bool, imageDefaults model.Image) (img model.Image, err error) {
21+
func ValidateImage(image string, metadata, labels map[string]string, watchByDef bool, imageDefaults *model.ImageDefaults) (img model.Image, err error) {
2222
img = model.Image{
2323
Name: image,
2424
}
2525

26-
if err := mergo.Merge(&img, imageDefaults); err != nil {
27-
return img, &invalidLabelError{errors.Wrapf(err, "failed to merge image defaults for image %s", image)}
26+
if imageDefaults != nil {
27+
img.WatchRepo = imageDefaults.WatchRepo
28+
img.NotifyOn = imageDefaults.NotifyOn
29+
img.MaxTags = imageDefaults.MaxTags
30+
img.SortTags = imageDefaults.SortTags
31+
img.IncludeTags = imageDefaults.IncludeTags
32+
img.ExcludeTags = imageDefaults.ExcludeTags
33+
img.Metadata = imageDefaults.Metadata
2834
}
2935

3036
if enableStr, ok := labels["diun.enable"]; ok {
3137
enable, err := strconv.ParseBool(enableStr)
3238
if err != nil {
33-
return img, &invalidLabelError{errors.Wrapf(err, "cannot parse %q value of label diun.enable", enableStr)}
39+
return img, errors.Wrapf(err, "cannot parse %q value of label diun.enable", enableStr)
3440
}
3541
if !enable {
3642
return model.Image{}, nil
@@ -47,7 +53,7 @@ func ValidateImage(image string, metadata, labels map[string]string, watchByDef
4753
if watchRepo, err := strconv.ParseBool(value); err == nil {
4854
img.WatchRepo = &watchRepo
4955
} else {
50-
return img, &invalidLabelError{errors.Wrapf(err, "cannot parse %q value of label %s", value, key)}
56+
return img, errors.Wrapf(err, "cannot parse %q value of label %s", value, key)
5157
}
5258
case key == "diun.notify_on":
5359
if len(value) == 0 {
@@ -57,7 +63,7 @@ func ValidateImage(image string, metadata, labels map[string]string, watchByDef
5763
for _, no := range strings.Split(value, ";") {
5864
notifyOn := model.NotifyOn(no)
5965
if !notifyOn.Valid() {
60-
return img, &invalidLabelError{errors.Errorf("unknown notify status %q", value)}
66+
return img, errors.Errorf("unknown notify status %q", value)
6167
}
6268
img.NotifyOn = append(img.NotifyOn, notifyOn)
6369
}
@@ -67,12 +73,12 @@ func ValidateImage(image string, metadata, labels map[string]string, watchByDef
6773
}
6874
sortTags := registry.SortTag(value)
6975
if !sortTags.Valid() {
70-
return img, &invalidLabelError{errors.Errorf("unknown sort tags type %q", value)}
76+
return img, errors.Errorf("unknown sort tags type %q", value)
7177
}
7278
img.SortTags = sortTags
7379
case key == "diun.max_tags":
7480
if img.MaxTags, err = strconv.Atoi(value); err != nil {
75-
return img, &invalidLabelError{errors.Wrapf(err, "cannot parse %q value of label %s", value, key)}
81+
return img, errors.Wrapf(err, "cannot parse %q value of label %s", value, key)
7682
}
7783
case key == "diun.include_tags":
7884
img.IncludeTags = strings.Split(value, ";")
@@ -85,7 +91,7 @@ func ValidateImage(image string, metadata, labels map[string]string, watchByDef
8591
case key == "diun.platform":
8692
platform, err := platforms.Parse(value)
8793
if err != nil {
88-
return img, &invalidLabelError{errors.Wrapf(err, "cannot parse %q platform of label %s", value, key)}
94+
return img, errors.Wrapf(err, "cannot parse %q platform of label %s", value, key)
8995
}
9096
img.Platform = model.ImagePlatform{
9197
OS: platform.OS,
@@ -98,7 +104,7 @@ func ValidateImage(image string, metadata, labels map[string]string, watchByDef
98104
break
99105
}
100106
if err := validateMetadataKey(mkey); err != nil {
101-
return img, &invalidLabelError{errors.Wrapf(err, "invalid metadata key %q", mkey)}
107+
return img, errors.Wrapf(err, "invalid metadata key %q", mkey)
102108
}
103109
if img.Metadata == nil {
104110
img.Metadata = map[string]string{}
@@ -121,15 +127,3 @@ func validateMetadataKey(key string) error {
121127
}
122128
return nil
123129
}
124-
125-
type invalidLabelError struct {
126-
error
127-
}
128-
129-
func (e *invalidLabelError) Error() string {
130-
return e.Error()
131-
}
132-
133-
func (e *invalidLabelError) Unwrap() error {
134-
return e
135-
}

0 commit comments

Comments
 (0)