| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package dockerhub |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | _ "embed" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/netdata/netdata/go/plugins/pkg/confopt" |
| 13 | "github.com/netdata/netdata/go/plugins/pkg/web" |
| 14 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 15 | ) |
| 16 | |
| 17 | //go:embed "config_schema.json" |
| 18 | var configSchema string |
| 19 | |
| 20 | func init() { |
| 21 | collectorapi.Register("dockerhub", collectorapi.Creator{ |
| 22 | JobConfigSchema: configSchema, |
| 23 | Defaults: collectorapi.Defaults{ |
| 24 | UpdateEvery: 5, |
| 25 | }, |
| 26 | Create: func() collectorapi.CollectorV1 { return New() }, |
| 27 | Config: func() any { return &Config{} }, |
| 28 | }) |
| 29 | } |
| 30 | |
| 31 | func New() *Collector { |
| 32 | return &Collector{ |
| 33 | Config: Config{ |
| 34 | HTTPConfig: web.HTTPConfig{ |
| 35 | RequestConfig: web.RequestConfig{ |
| 36 | URL: "https://hub.docker.com/v2/repositories", |
| 37 | }, |
| 38 | ClientConfig: web.ClientConfig{ |
| 39 | Timeout: confopt.Duration(time.Second * 2), |
| 40 | }, |
| 41 | }, |
| 42 | }, |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | type Config struct { |
| 47 | Vnode string `yaml:"vnode,omitempty" json:"vnode"` |
| 48 | UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"` |
| 49 | AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"` |
| 50 | web.HTTPConfig `yaml:",inline" json:""` |
| 51 | Repositories []string `yaml:"repositories" json:"repositories"` |
| 52 | } |
| 53 | |
| 54 | type Collector struct { |
| 55 | collectorapi.Base |
| 56 | Config `yaml:",inline" json:""` |
| 57 | |
| 58 | client *apiClient |
| 59 | } |
| 60 | |
| 61 | func (c *Collector) Configuration() any { |
| 62 | return c.Config |
| 63 | } |
| 64 | |
| 65 | func (c *Collector) Init(context.Context) error { |
| 66 | if err := c.validateConfig(); err != nil { |
| 67 | return fmt.Errorf("config validation: %v", err) |
| 68 | } |
| 69 | |
| 70 | client, err := c.initApiClient() |
| 71 | if err != nil { |
| 72 | return fmt.Errorf("init api client: %v", err) |
| 73 | } |
| 74 | c.client = client |
| 75 | |
| 76 | return nil |
| 77 | } |
| 78 | |
| 79 | func (c *Collector) Check(context.Context) error { |
| 80 | mx, err := c.collect() |
| 81 | if err != nil { |
| 82 | return err |
| 83 | } |
| 84 | if len(mx) == 0 { |
| 85 | return errors.New("no metrics collected") |
| 86 | |
| 87 | } |
| 88 | return nil |
| 89 | } |
| 90 | |
| 91 | func (c *Collector) Charts() *Charts { |
| 92 | cs := charts.Copy() |
| 93 | addReposToCharts(c.Repositories, cs) |
| 94 | return cs |
| 95 | } |
| 96 | |
| 97 | func (c *Collector) Collect(context.Context) map[string]int64 { |
| 98 | mx, err := c.collect() |
| 99 | |
| 100 | if err != nil { |
| 101 | c.Error(err) |
| 102 | return nil |
| 103 | } |
| 104 | |
| 105 | return mx |
| 106 | } |
| 107 | |
| 108 | func (c *Collector) Cleanup(context.Context) { |
| 109 | if c.client != nil && c.client.httpClient != nil { |
| 110 | c.client.httpClient.CloseIdleConnections() |
| 111 | } |
| 112 | } |