Skip to content

Commit

Permalink
Merge pull request #895 from crazy-max/runOnStartup
Browse files Browse the repository at this point in the history
runOnStartup watch option
  • Loading branch information
crazy-max authored Jun 11, 2023
2 parents 985f23d + 16e0906 commit ad4a964
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 68 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ jobs:
loglevel: info
- folder: docker3
loglevel: info
- folder: docker4
loglevel: info
- folder: dockerfile1
loglevel: debug
steps:
Expand Down
3 changes: 3 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ You can override this using the [`--config` flag or `CONFIG` env var with `serve
schedule: "0 */6 * * *"
jitter: 30s
firstCheckNotif: false
runOnStartup: true

notif:
amqp:
Expand Down Expand Up @@ -129,6 +130,7 @@ All configuration from file can be transposed into environment variables. As an
schedule: "0 */6 * * *"
jitter: 30s
firstCheckNotif: false
runOnStartup: true

notif:
gotify:
Expand Down Expand Up @@ -183,6 +185,7 @@ Can be transposed to:
DIUN_WATCH_SCHEDULE=0 */6 * * *
DIUN_WATCH_JITTER=30s
DIUN_WATCH_FIRSTCHECKNOTIF=false
DIUN_WATCH_RUNONSTARTUP=true

DIUN_NOTIF_GOTIFY_ENDPOINT=http://gotify.foo.com
DIUN_NOTIF_GOTIFY_TOKEN=Token123456
Expand Down
14 changes: 14 additions & 0 deletions docs/config/watch.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ watch:
schedule: "0 */6 * * *"
jitter: 30s
firstCheckNotif: false
runOnStartup: true
compareDigest: true
healthchecks:
baseURL: https://hc-ping.com/
Expand Down Expand Up @@ -76,6 +77,19 @@ Send notification at the very first analysis of an image. (default `false`)
!!! abstract "Environment variables"
* `DIUN_WATCH_FIRSTCHECKNOTIF`

### `runOnStartup`

Check for updates on startup. (default `true`)

!!! example "Config file"
```yaml
watch:
runOnStartup: true
```

!!! abstract "Environment variables"
* `DIUN_WATCH_RUNONSTARTUP`

### `compareDigest`

Compare the digest of an image with the registry before downloading the image manifest. It is strongly
Expand Down
5 changes: 3 additions & 2 deletions internal/app/diun.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ func (di *Diun) Start() error {
}
}()

// Run on startup
di.Run()
if *di.cfg.Watch.RunOnStartup {
di.Run()
}

// Init scheduler if defined
if len(di.cfg.Watch.Schedule) == 0 {
Expand Down
70 changes: 4 additions & 66 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package config_test

import (
"fmt"
"os"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -54,6 +52,7 @@ func TestLoadFile(t *testing.T) {
Schedule: "*/30 * * * *",
Jitter: utl.NewDuration(30 * time.Second),
FirstCheckNotif: utl.NewTrue(),
RunOnStartup: utl.NewFalse(),
CompareDigest: utl.NewTrue(),
Healthchecks: &model.Healthchecks{
BaseURL: "https://hc-ping.com/",
Expand Down Expand Up @@ -247,8 +246,6 @@ for <code>{{ .Entry.Manifest.Platform }}</code> platform.
}

func TestLoadEnv(t *testing.T) {
defer UnsetEnv("DIUN_")

testCases := []struct {
desc string
cfg string
Expand Down Expand Up @@ -372,12 +369,10 @@ func TestLoadEnv(t *testing.T) {
for _, tt := range testCases {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
UnsetEnv("DIUN_")

if tt.environ != nil {
for _, environ := range tt.environ {
n := strings.SplitN(environ, "=", 2)
os.Setenv(n[0], n[1])
t.Setenv(n[0], n[1])
}
}

Expand All @@ -394,8 +389,6 @@ func TestLoadEnv(t *testing.T) {
}

func TestLoadMixed(t *testing.T) {
defer UnsetEnv("DIUN_")

testCases := []struct {
desc string
cfg string
Expand Down Expand Up @@ -497,12 +490,10 @@ for <code>{{ .Entry.Manifest.Platform }}</code> platform.
for _, tt := range testCases {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
UnsetEnv("DIUN_")

if tt.environ != nil {
for _, environ := range tt.environ {
n := strings.SplitN(environ, "=", 2)
os.Setenv(n[0], n[1])
t.Setenv(n[0], n[1])
}
}

Expand Down Expand Up @@ -532,61 +523,8 @@ func TestValidation(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
cfg, err := config.Load(tt.cfg)
require.NoError(t, err)

dec, err := env.Encode("DIUN_", cfg)
_, err = env.Encode("DIUN_", cfg)
require.NoError(t, err)
for _, value := range dec {
fmt.Printf(`%s=%s\n`, value.Name, value.Default)
}
})
}
}

func UnsetEnv(prefix string) (restore func()) {
before := map[string]string{}

for _, e := range os.Environ() {
if !strings.HasPrefix(e, prefix) {
continue
}

parts := strings.SplitN(e, "=", 2)
before[parts[0]] = parts[1]

os.Unsetenv(parts[0])
}

return func() {
after := map[string]string{}

for _, e := range os.Environ() {
if !strings.HasPrefix(e, prefix) {
continue
}

parts := strings.SplitN(e, "=", 2)
after[parts[0]] = parts[1]

// Check if the envar previously existed
v, ok := before[parts[0]]
if !ok {
// This is a newly added envar with prefix, zap it
os.Unsetenv(parts[0])
continue
}

if parts[1] != v {
// If the envar value has changed, set it back
os.Setenv(parts[0], v)
}
}

// Still need to check if there have been any deleted envars
for k, v := range before {
if _, ok := after[k]; !ok {
// k is not present in after, so we set it.
os.Setenv(k, v)
}
}
}
}
1 change: 1 addition & 0 deletions internal/config/fixtures/config.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ watch:
schedule: "*/30 * * * *"
jitter: 30s
firstCheckNotif: true
runOnStartup: false
compareDigest: true
healthchecks:
baseURL: https://hc-ping.com/
Expand Down
2 changes: 2 additions & 0 deletions internal/model/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Watch struct {
Schedule string `yaml:"schedule,omitempty" json:"schedule,omitempty"`
Jitter *time.Duration `yaml:"jitter,omitempty" json:"jitter,omitempty" validate:"required"`
FirstCheckNotif *bool `yaml:"firstCheckNotif,omitempty" json:"firstCheckNotif,omitempty" validate:"required"`
RunOnStartup *bool `yaml:"runOnStartup,omitempty" json:"runOnStartup,omitempty" validate:"required"`
CompareDigest *bool `yaml:"compareDigest,omitempty" json:"compareDigest,omitempty" validate:"required"`
Healthchecks *Healthchecks `yaml:"healthchecks,omitempty" json:"healthchecks,omitempty"`
}
Expand All @@ -28,5 +29,6 @@ func (s *Watch) SetDefaults() {
s.Workers = 10
s.Jitter = utl.NewDuration(30 * time.Second)
s.FirstCheckNotif = utl.NewFalse()
s.RunOnStartup = utl.NewTrue()
s.CompareDigest = utl.NewTrue()
}
9 changes: 9 additions & 0 deletions test/docker4/diun.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
watch:
workers: 20
schedule: "* * * * *"
runOnStartup: false

providers:
docker:
watchByDefault: true
watchStopped: true

0 comments on commit ad4a964

Please sign in to comment.