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 (#10)
This commit is contained in:
parent
b288673a33
commit
7a8b4677fc
10 changed files with 51 additions and 9 deletions
doc
internal
pkg/docker/registry
|
@ -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
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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{
|
||||
|
|
|
@ -4,6 +4,7 @@ db:
|
|||
watch:
|
||||
workers: 100
|
||||
schedule: "*/30 * * * *"
|
||||
first_check_notif: false
|
||||
|
||||
notif:
|
||||
mail:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -11,4 +11,5 @@ type Job struct {
|
|||
Image Image
|
||||
RegImage registry.Image
|
||||
Registry *docker.RegistryClient
|
||||
FirstCheck bool
|
||||
}
|
||||
|
|
|
@ -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"`
|
||||
}
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
This is a copy of https://github.com/genuinetools/reg/blob/master/registry/image.go as of commit f3a9b00ec86f334702381edf842f03b3a9243a0a
|
|
@ -1,3 +1,4 @@
|
|||
// Source: https://github.com/genuinetools/reg/blob/f3a9b00ec86f334702381edf842f03b3a9243a0a/registry/image.go
|
||||
package registry
|
||||
|
||||
import (
|
||||
|
|
6
pkg/docker/registry/name.go
Normal file
6
pkg/docker/registry/name.go
Normal 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()
|
||||
}
|
Loading…
Add table
Reference in a new issue