Path: blob/main/component/discovery/digitalocean/digitalocean.go
4096 views
package digitalocean12import (3"fmt"4"time"56"github.com/grafana/agent/component"7"github.com/grafana/agent/component/common/config"8"github.com/grafana/agent/component/discovery"9"github.com/grafana/agent/pkg/river/rivertypes"10"github.com/prometheus/common/model"11prom_discovery "github.com/prometheus/prometheus/discovery/digitalocean"12)1314func init() {15component.Register(component.Registration{16Name: "discovery.digitalocean",17Args: Arguments{},18Exports: discovery.Exports{},1920Build: func(opts component.Options, args component.Arguments) (component.Component, error) {21return New(opts, args.(Arguments))22},23})24}2526type Arguments struct {27RefreshInterval time.Duration `river:"refresh_interval,attr,optional"`28Port int `river:"port,attr,optional"`2930BearerToken rivertypes.Secret `river:"bearer_token,attr,optional"`31BearerTokenFile string `river:"bearer_token_file,attr,optional"`3233ProxyURL config.URL `river:"proxy_url,attr,optional"`34FollowRedirects bool `river:"follow_redirects,attr,optional"`35EnableHTTP2 bool `river:"enable_http2,attr,optional"`36}3738var DefaultArguments = Arguments{39Port: 80,40RefreshInterval: time.Minute,41}4243func (a *Arguments) UnmarshalRiver(f func(interface{}) error) error {44*a = DefaultArguments45type arguments Arguments46err := f((*arguments)(a))47if err != nil {48return err49}50return a.Validate()51}5253// Validate validates the arguments. Specifically, it checks that a BearerToken or54// BearerTokenFile is specified, as the DigitalOcean API requires a Bearer Token for55// authentication.56func (a *Arguments) Validate() error {57if (a.BearerToken == "" && a.BearerTokenFile == "") ||58(len(a.BearerToken) > 0 && len(a.BearerTokenFile) > 0) {5960return fmt.Errorf("exactly one of bearer_token or bearer_token_file must be specified")61}6263return nil64}6566func (a *Arguments) Convert() *prom_discovery.SDConfig {67httpClientConfig := config.DefaultHTTPClientConfig68httpClientConfig.BearerToken = a.BearerToken69httpClientConfig.BearerTokenFile = a.BearerTokenFile70httpClientConfig.ProxyURL = a.ProxyURL71httpClientConfig.FollowRedirects = a.FollowRedirects72httpClientConfig.EnableHTTP2 = a.EnableHTTP27374return &prom_discovery.SDConfig{75RefreshInterval: model.Duration(a.RefreshInterval),76Port: a.Port,77HTTPClientConfig: *httpClientConfig.Convert(),78}79}8081func New(opts component.Options, args Arguments) (component.Component, error) {82return discovery.New(opts, args, func(args component.Arguments) (discovery.Discoverer, error) {83newArgs := args.(Arguments)84return prom_discovery.NewDiscovery(newArgs.Convert(), opts.Logger)85})86}878889