Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/discovery/azure/azure.go
4095 views
1
package azure
2
3
import (
4
"fmt"
5
"time"
6
7
"github.com/Azure/go-autorest/autorest/azure"
8
"github.com/grafana/agent/component"
9
"github.com/grafana/agent/component/common/config"
10
"github.com/grafana/agent/component/discovery"
11
"github.com/grafana/agent/pkg/river/rivertypes"
12
common "github.com/prometheus/common/config"
13
"github.com/prometheus/common/model"
14
prom_discovery "github.com/prometheus/prometheus/discovery/azure"
15
)
16
17
func init() {
18
component.Register(component.Registration{
19
Name: "discovery.azure",
20
Args: Arguments{},
21
Exports: discovery.Exports{},
22
23
Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
24
return New(opts, args.(Arguments))
25
},
26
})
27
}
28
29
type Arguments struct {
30
Environment string `river:"environment,attr,optional"`
31
Port int `river:"port,attr,optional"`
32
SubscriptionID string `river:"subscription_id,attr,optional"`
33
OAuth *OAuth `river:"oauth,block,optional"`
34
ManagedIdentity *ManagedIdentity `river:"managed_identity,block,optional"`
35
RefreshInterval time.Duration `river:"refresh_interval,attr,optional"`
36
ResourceGroup string `river:"resource_group,attr,optional"`
37
38
ProxyURL config.URL `river:"proxy_url,attr,optional"`
39
FollowRedirects bool `river:"follow_redirects,attr,optional"`
40
EnableHTTP2 bool `river:"enable_http2,attr,optional"`
41
TLSConfig config.TLSConfig `river:"tls_config,block,optional"`
42
}
43
44
type OAuth struct {
45
ClientID string `river:"client_id,attr"`
46
TenantID string `river:"tenant_id,attr"`
47
ClientSecret rivertypes.Secret `river:"client_secret,attr"`
48
}
49
50
type ManagedIdentity struct {
51
ClientID string `river:"client_id,attr"`
52
}
53
54
var DefaultArguments = Arguments{
55
Environment: azure.PublicCloud.Name,
56
Port: 80,
57
RefreshInterval: 5 * time.Minute,
58
}
59
60
func (a *Arguments) UnmarshalRiver(f func(interface{}) error) error {
61
*a = DefaultArguments
62
type arguments Arguments
63
err := f((*arguments)(a))
64
if err != nil {
65
return err
66
}
67
return a.Validate()
68
}
69
70
func (a *Arguments) Validate() error {
71
if a.OAuth == nil && a.ManagedIdentity == nil || a.OAuth != nil && a.ManagedIdentity != nil {
72
return fmt.Errorf("exactly one of oauth or managed_identity must be specified")
73
}
74
return a.TLSConfig.Validate()
75
}
76
77
func (a *Arguments) Convert() *prom_discovery.SDConfig {
78
var (
79
authMethod string
80
clientID string
81
tenantID string
82
clientSecret common.Secret
83
)
84
if a.OAuth != nil {
85
authMethod = "OAuth"
86
clientID = a.OAuth.ClientID
87
tenantID = a.OAuth.TenantID
88
clientSecret = common.Secret(a.OAuth.ClientSecret)
89
} else {
90
authMethod = "ManagedIdentity"
91
clientID = a.ManagedIdentity.ClientID
92
}
93
94
httpClientConfig := config.DefaultHTTPClientConfig
95
httpClientConfig.ProxyURL = a.ProxyURL
96
httpClientConfig.FollowRedirects = a.FollowRedirects
97
httpClientConfig.EnableHTTP2 = a.EnableHTTP2
98
httpClientConfig.TLSConfig = a.TLSConfig
99
100
return &prom_discovery.SDConfig{
101
Environment: a.Environment,
102
Port: a.Port,
103
SubscriptionID: a.SubscriptionID,
104
TenantID: tenantID,
105
ClientID: clientID,
106
ClientSecret: clientSecret,
107
RefreshInterval: model.Duration(a.RefreshInterval),
108
AuthenticationMethod: authMethod,
109
ResourceGroup: a.ResourceGroup,
110
HTTPClientConfig: *httpClientConfig.Convert(),
111
}
112
}
113
114
func New(opts component.Options, args Arguments) (component.Component, error) {
115
return discovery.New(opts, args, func(args component.Arguments) (discovery.Discoverer, error) {
116
newArgs := args.(Arguments)
117
return prom_discovery.NewDiscovery(newArgs.Convert(), opts.Logger), nil
118
})
119
}
120
121