0
0
Fork 0
mirror of https://github.com/crazy-max/diun.git synced 2025-04-25 11:49:16 +00:00

Add option to skip notification at the very first analysis of an image ()

This commit is contained in:
CrazyMax 2019-12-22 16:32:27 +01:00
parent b288673a33
commit 7a8b4677fc
No known key found for this signature in database
GPG key ID: 3248E46B6BB8C7F7
10 changed files with 51 additions and 9 deletions

View file

@ -9,6 +9,7 @@ db:
watch: watch:
workers: 10 workers: 10
schedule: "0 * * * *" schedule: "0 * * * *"
first_check_notif: false
notif: notif:
mail: mail:
@ -94,6 +95,7 @@ providers:
* `workers`: Maximum number of workers that will execute tasks concurrently. _Optional_. (default: `10`). * `workers`: Maximum number of workers that will execute tasks concurrently. _Optional_. (default: `10`).
* `schedule`: [CRON expression](https://godoc.org/github.com/robfig/cron#hdr-CRON_Expression_Format) to schedule Diun watcher. _Optional_. (default: `0 * * * *`). * `schedule`: [CRON expression](https://godoc.org/github.com/robfig/cron#hdr-CRON_Expression_Format) to schedule Diun watcher. _Optional_. (default: `0 * * * *`).
* `first_check_notif`: Send notification at the very first analysis of an image. _Optional_. (default: `false`).
## notif ## notif

View file

@ -28,6 +28,13 @@ func (di *Diun) createJob(job model.Job) {
return return
} }
// First check?
job.FirstCheck, err = di.db.First(job.RegImage)
if err != nil {
sublog.Error().Err(err).Msg("Cannot check first")
return
}
// Registry options // Registry options
regOpts, err := di.getRegOpts(job.Image.RegOptsID) regOpts, err := di.getRegOpts(job.Image.RegOptsID)
if err != nil { if err != nil {
@ -166,6 +173,11 @@ func (di *Diun) runJob(job model.Job) error {
} }
sublog.Debug().Msg("Manifest saved to database") sublog.Debug().Msg("Manifest saved to database")
if job.FirstCheck && !di.cfg.Watch.FirstCheckNotif {
sublog.Debug().Msg("Skipping notification (first check)")
return nil
}
di.notif.Send(model.NotifEntry{ di.notif.Send(model.NotifEntry{
Status: status, Status: status,
Provider: job.Provider, Provider: job.Provider,

View file

@ -44,8 +44,9 @@ func Load(flags model.Flags, version string) (*Config, error) {
Path: "diun.db", Path: "diun.db",
}, },
Watch: model.Watch{ Watch: model.Watch{
Workers: 10, Workers: 10,
Schedule: "0 * * * *", Schedule: "0 * * * *",
FirstCheckNotif: false,
}, },
Notif: model.Notif{ Notif: model.Notif{
Mail: model.NotifMail{ Mail: model.NotifMail{

View file

@ -4,6 +4,7 @@ db:
watch: watch:
workers: 100 workers: 100
schedule: "*/30 * * * *" schedule: "*/30 * * * *"
first_check_notif: false
notif: notif:
mail: mail:

View file

@ -1,6 +1,7 @@
package db package db
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"time" "time"
@ -53,6 +54,23 @@ func (c *Client) Close() error {
return c.DB.Close() return c.DB.Close()
} }
// First checks if a Docker image has ever been analyzed
func (c *Client) First(image registry.Image) (bool, error) {
found := false
err := c.View(func(tx *bolt.Tx) error {
c := tx.Bucket([]byte(bucket)).Cursor()
name := []byte(image.Name())
for k, _ := c.Seek(name); k != nil && bytes.HasPrefix(k, name); k, _ = c.Next() {
found = true
return nil
}
return nil
})
return !found, err
}
// GetManifest returns Docker image manifest // GetManifest returns Docker image manifest
func (c *Client) GetManifest(image registry.Image) (docker.Manifest, error) { func (c *Client) GetManifest(image registry.Image) (docker.Manifest, error) {
var manifest docker.Manifest var manifest docker.Manifest

View file

@ -7,8 +7,9 @@ import (
// Job holds job configuration // Job holds job configuration
type Job struct { type Job struct {
Provider string Provider string
Image Image Image Image
RegImage registry.Image RegImage registry.Image
Registry *docker.RegistryClient Registry *docker.RegistryClient
FirstCheck bool
} }

View file

@ -2,6 +2,7 @@ package model
// Watch holds data necessary for watch configuration // Watch holds data necessary for watch configuration
type Watch struct { type Watch struct {
Workers int `yaml:"workers,omitempty"` Workers int `yaml:"workers,omitempty"`
Schedule string `yaml:"schedule,omitempty"` Schedule string `yaml:"schedule,omitempty"`
FirstCheckNotif bool `yaml:"first_check_notif,omitempty"`
} }

View file

@ -1 +0,0 @@
This is a copy of https://github.com/genuinetools/reg/blob/master/registry/image.go as of commit f3a9b00ec86f334702381edf842f03b3a9243a0a

View file

@ -1,3 +1,4 @@
// Source: https://github.com/genuinetools/reg/blob/f3a9b00ec86f334702381edf842f03b3a9243a0a/registry/image.go
package registry package registry
import ( import (

View file

@ -0,0 +1,6 @@
package registry
// Name returns the full name representation of an image.
func (i Image) Name() string {
return i.named.Name()
}