0
0
Fork 0
mirror of https://github.com/crazy-max/diun.git synced 2025-04-11 06:01:21 +00:00

runOnStartup watch option

This commit is contained in:
CrazyMax 2023-06-12 00:04:59 +02:00
parent 352989524a
commit 780f3fb81f
No known key found for this signature in database
GPG key ID: 3248E46B6BB8C7F7
6 changed files with 27 additions and 68 deletions
docs/config
internal

View file

@ -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:
@ -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:
@ -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

View file

@ -8,6 +8,7 @@ watch:
schedule: "0 */6 * * *"
jitter: 30s
firstCheckNotif: false
runOnStartup: true
compareDigest: true
healthchecks:
baseURL: https://hc-ping.com/
@ -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

View file

@ -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 {

View file

@ -1,8 +1,6 @@
package config_test
import (
"fmt"
"os"
"strings"
"testing"
"time"
@ -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/",
@ -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
@ -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])
}
}
@ -394,8 +389,6 @@ func TestLoadEnv(t *testing.T) {
}
func TestLoadMixed(t *testing.T) {
defer UnsetEnv("DIUN_")
testCases := []struct {
desc string
cfg string
@ -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])
}
}
@ -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)
}
}
}
}

View file

@ -6,6 +6,7 @@ watch:
schedule: "*/30 * * * *"
jitter: 30s
firstCheckNotif: true
runOnStartup: false
compareDigest: true
healthchecks:
baseURL: https://hc-ping.com/

View file

@ -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"`
}
@ -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()
}