Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/discovery/digitalocean/digitalocean.go
4096 views
1
package digitalocean
2
3
import (
4
"fmt"
5
"time"
6
7
"github.com/grafana/agent/component"
8
"github.com/grafana/agent/component/common/config"
9
"github.com/grafana/agent/component/discovery"
10
"github.com/grafana/agent/pkg/river/rivertypes"
11
"github.com/prometheus/common/model"
12
prom_discovery "github.com/prometheus/prometheus/discovery/digitalocean"
13
)
14
15
func init() {
16
component.Register(component.Registration{
17
Name: "discovery.digitalocean",
18
Args: Arguments{},
19
Exports: discovery.Exports{},
20
21
Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
22
return New(opts, args.(Arguments))
23
},
24
})
25
}
26
27
type Arguments struct {
28
RefreshInterval time.Duration `river:"refresh_interval,attr,optional"`
29
Port int `river:"port,attr,optional"`
30
31
BearerToken rivertypes.Secret `river:"bearer_token,attr,optional"`
32
BearerTokenFile string `river:"bearer_token_file,attr,optional"`
33
34
ProxyURL config.URL `river:"proxy_url,attr,optional"`
35
FollowRedirects bool `river:"follow_redirects,attr,optional"`
36
EnableHTTP2 bool `river:"enable_http2,attr,optional"`
37
}
38
39
var DefaultArguments = Arguments{
40
Port: 80,
41
RefreshInterval: time.Minute,
42
}
43
44
func (a *Arguments) UnmarshalRiver(f func(interface{}) error) error {
45
*a = DefaultArguments
46
type arguments Arguments
47
err := f((*arguments)(a))
48
if err != nil {
49
return err
50
}
51
return a.Validate()
52
}
53
54
// Validate validates the arguments. Specifically, it checks that a BearerToken or
55
// BearerTokenFile is specified, as the DigitalOcean API requires a Bearer Token for
56
// authentication.
57
func (a *Arguments) Validate() error {
58
if (a.BearerToken == "" && a.BearerTokenFile == "") ||
59
(len(a.BearerToken) > 0 && len(a.BearerTokenFile) > 0) {
60
61
return fmt.Errorf("exactly one of bearer_token or bearer_token_file must be specified")
62
}
63
64
return nil
65
}
66
67
func (a *Arguments) Convert() *prom_discovery.SDConfig {
68
httpClientConfig := config.DefaultHTTPClientConfig
69
httpClientConfig.BearerToken = a.BearerToken
70
httpClientConfig.BearerTokenFile = a.BearerTokenFile
71
httpClientConfig.ProxyURL = a.ProxyURL
72
httpClientConfig.FollowRedirects = a.FollowRedirects
73
httpClientConfig.EnableHTTP2 = a.EnableHTTP2
74
75
return &prom_discovery.SDConfig{
76
RefreshInterval: model.Duration(a.RefreshInterval),
77
Port: a.Port,
78
HTTPClientConfig: *httpClientConfig.Convert(),
79
}
80
}
81
82
func New(opts component.Options, args Arguments) (component.Component, error) {
83
return discovery.New(opts, args, func(args component.Arguments) (discovery.Discoverer, error) {
84
newArgs := args.(Arguments)
85
return prom_discovery.NewDiscovery(newArgs.Convert(), opts.Logger)
86
})
87
}
88
89