0
0
Fork 0
mirror of https://github.com/crazy-max/diun.git synced 2025-04-25 19:53:41 +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:
workers: 10
schedule: "0 * * * *"
first_check_notif: false
notif:
mail:
@ -94,6 +95,7 @@ providers:
* `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 * * * *`).
* `first_check_notif`: Send notification at the very first analysis of an image. _Optional_. (default: `false`).
## notif

View file

@ -28,6 +28,13 @@ func (di *Diun) createJob(job model.Job) {
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
regOpts, err := di.getRegOpts(job.Image.RegOptsID)
if err != nil {
@ -166,6 +173,11 @@ func (di *Diun) runJob(job model.Job) error {
}
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{
Status: status,
Provider: job.Provider,

View file

@ -46,6 +46,7 @@ func Load(flags model.Flags, version string) (*Config, error) {
Watch: model.Watch{
Workers: 10,
Schedule: "0 * * * *",
FirstCheckNotif: false,
},
Notif: model.Notif{
Mail: model.NotifMail{

View file

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

View file

@ -1,6 +1,7 @@
package db
import (
"bytes"
"encoding/json"
"fmt"
"time"
@ -53,6 +54,23 @@ func (c *Client) Close() error {
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
func (c *Client) GetManifest(image registry.Image) (docker.Manifest, error) {
var manifest docker.Manifest

View file

@ -11,4 +11,5 @@ type Job struct {
Image Image
RegImage registry.Image
Registry *docker.RegistryClient
FirstCheck bool
}

View file

@ -4,4 +4,5 @@ package model
type Watch struct {
Workers int `yaml:"workers,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
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()
}