mirror of
https://github.com/crazy-max/diun.git
synced 2025-04-17 16:32:35 +00:00
Add option to set the maximum number of tags to watch for an item if watch_repo is enabled
This commit is contained in:
parent
7f87602672
commit
00ed9d24e2
6 changed files with 34 additions and 7 deletions
|
@ -1,5 +1,9 @@
|
|||
# Changelog
|
||||
|
||||
## 0.4.0 (2019/06/08)
|
||||
|
||||
* Add option to set the maximum number of tags to watch for an item if `watch_repo` is enabled
|
||||
|
||||
## 0.3.2 (2019/06/08)
|
||||
|
||||
* Fix registry client context
|
||||
|
|
|
@ -160,9 +160,10 @@ items:
|
|||
* `timeout`: Timeout is the maximum amount of time for the TCP connection to establish. 0 means no timeout (default: `10`).
|
||||
* `insecure_tls`: Allow contacting docker registry over HTTP, or HTTPS with failed TLS verification (default: `false`).
|
||||
* `items`: Slice of items to watch with the following fields:
|
||||
* `image`: Docker image to watch using `registry/path:tag` format. If registry is omitted, `docker.io` will be used. If tag is omitted, `latest` will be used. **required**
|
||||
* `image`: Docker image to watch using `registry/path:tag` format. If registry is omitted, `docker.io` will be used and if tag is omitted, `latest` will be used. **required**
|
||||
* `registry_id`: Registry ID from `registries` to use.
|
||||
* `watch_repo`: Watch all tags of this `image` repository (default: `false`).
|
||||
* `max_tags`: Maximum number of tags to watch if `watch_repo` enabled. 0 means all of them (default: `25`).
|
||||
* `include_tags`: List of regular expressions to include tags. Can be useful if you enable `watch_repo`.
|
||||
* `exclude_tags`: List of regular expressions to exclude tags. Can be useful if you enable `watch_repo`.
|
||||
|
||||
|
|
|
@ -135,12 +135,12 @@ func (di *Diun) analyzeImage(imageStr string, item model.Item, reg *docker.Regis
|
|||
}
|
||||
|
||||
func (di *Diun) analyzeRepo(image registry.Image, item model.Item, reg *docker.RegistryClient) {
|
||||
tags, err := reg.Tags(image)
|
||||
tags, tagsCount, err := reg.Tags(image, item.MaxTags)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("image", image.String()).Msg("Cannot retrieve tags")
|
||||
return
|
||||
}
|
||||
log.Debug().Str("image", image.String()).Msgf("%d tag(s) found in repository", len(tags))
|
||||
log.Debug().Str("image", image.String()).Msgf("%d tag(s) found in repository. %d will be analyzed.", tagsCount, len(tags))
|
||||
|
||||
for _, tag := range tags {
|
||||
imageStr := fmt.Sprintf("%s/%s:%s", image.Domain, image.Path, tag)
|
||||
|
|
|
@ -102,6 +102,17 @@ func (cfg *Config) Check() error {
|
|||
}
|
||||
|
||||
for key, item := range cfg.Items {
|
||||
if item.Image == "" {
|
||||
return fmt.Errorf("image is required for item %d", key)
|
||||
}
|
||||
|
||||
if err := mergo.Merge(&item, model.Item{
|
||||
WatchRepo: false,
|
||||
MaxTags: 25,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("cannot set default item values for %s: %v", item.Image, err)
|
||||
}
|
||||
|
||||
if item.RegistryID != "" {
|
||||
reg, found := cfg.Registries[item.RegistryID]
|
||||
if !found {
|
||||
|
|
|
@ -5,6 +5,7 @@ type Item struct {
|
|||
Image string `yaml:"image,omitempty" json:",omitempty"`
|
||||
RegistryID string `yaml:"registry_id,omitempty" json:",omitempty"`
|
||||
WatchRepo bool `yaml:"watch_repo,omitempty" json:",omitempty"`
|
||||
MaxTags int `yaml:"max_tags,omitempty" json:",omitempty"`
|
||||
IncludeTags []string `yaml:"include_tags,omitempty" json:",omitempty"`
|
||||
ExcludeTags []string `yaml:"exclude_tags,omitempty" json:",omitempty"`
|
||||
Registry Registry `yaml:"-" json:"-"`
|
||||
|
|
|
@ -8,20 +8,30 @@ import (
|
|||
type Tags []string
|
||||
|
||||
// Tags returns tags of a Docker repository
|
||||
func (c *RegistryClient) Tags(image registry.Image) (Tags, error) {
|
||||
func (c *RegistryClient) Tags(image registry.Image, max int) (Tags, int, error) {
|
||||
ctx, cancel := c.timeoutContext()
|
||||
defer cancel()
|
||||
|
||||
imgCls, err := c.newImage(ctx, image.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, 0, err
|
||||
}
|
||||
defer imgCls.Close()
|
||||
|
||||
tags, err := docker.GetRepositoryTags(ctx, c.sysCtx, imgCls.Reference())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return Tags(tags), err
|
||||
// Reverse order (latest tags first)
|
||||
for i := len(tags)/2 - 1; i >= 0; i-- {
|
||||
opp := len(tags) - 1 - i
|
||||
tags[i], tags[opp] = tags[opp], tags[i]
|
||||
}
|
||||
|
||||
if max > 0 && len(tags) >= max {
|
||||
return Tags(tags[:max]), len(tags), nil
|
||||
}
|
||||
|
||||
return Tags(tags), len(tags), nil
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue