diff --git a/docs/config/index.md b/docs/config/index.md index 4cd65c7a..cb5e7989 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -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 diff --git a/docs/config/watch.md b/docs/config/watch.md index fd2eed9b..b30fafa6 100644 --- a/docs/config/watch.md +++ b/docs/config/watch.md @@ -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 diff --git a/internal/app/diun.go b/internal/app/diun.go index 850787d1..0ea50f91 100644 --- a/internal/app/diun.go +++ b/internal/app/diun.go @@ -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 { diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 1788a08e..1a092f49 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -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 {{ .Entry.Manifest.Platform }} 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 {{ .Entry.Manifest.Platform }} 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) - } - } - } -} diff --git a/internal/config/fixtures/config.test.yml b/internal/config/fixtures/config.test.yml index a9c31851..39055cdf 100644 --- a/internal/config/fixtures/config.test.yml +++ b/internal/config/fixtures/config.test.yml @@ -6,6 +6,7 @@ watch: schedule: "*/30 * * * *" jitter: 30s firstCheckNotif: true + runOnStartup: false compareDigest: true healthchecks: baseURL: https://hc-ping.com/ diff --git a/internal/model/watch.go b/internal/model/watch.go index 05c1557e..f9239519 100644 --- a/internal/model/watch.go +++ b/internal/model/watch.go @@ -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() }