Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/discovery/kubernetes/kubernetes.go
4096 views
1
// Package kubernetes implements a discovery.kubernetes component.
2
package kubernetes
3
4
import (
5
"github.com/grafana/agent/component"
6
"github.com/grafana/agent/component/common/config"
7
"github.com/grafana/agent/component/discovery"
8
promk8s "github.com/prometheus/prometheus/discovery/kubernetes"
9
)
10
11
func init() {
12
component.Register(component.Registration{
13
Name: "discovery.kubernetes",
14
Args: Arguments{},
15
Exports: discovery.Exports{},
16
17
Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
18
return New(opts, args.(Arguments))
19
},
20
})
21
}
22
23
// Arguments configures the discovery.kubernetes component.
24
type Arguments struct {
25
APIServer config.URL `river:"api_server,attr,optional"`
26
Role string `river:"role,attr"`
27
KubeConfig string `river:"kubeconfig_file,attr,optional"`
28
HTTPClientConfig config.HTTPClientConfig `river:",squash"`
29
NamespaceDiscovery NamespaceDiscovery `river:"namespaces,block,optional"`
30
Selectors []SelectorConfig `river:"selectors,block,optional"`
31
}
32
33
// DefaultConfig holds defaults for SDConfig.
34
var DefaultConfig = Arguments{
35
HTTPClientConfig: config.DefaultHTTPClientConfig,
36
}
37
38
// UnmarshalRiver implements river.Unmarshaler and applies default settings.
39
func (args *Arguments) UnmarshalRiver(f func(interface{}) error) error {
40
*args = DefaultConfig
41
type arguments Arguments
42
err := f((*arguments)(args))
43
if err != nil {
44
return err
45
}
46
47
// We must explicitly Validate because HTTPClientConfig is squashed and it won't run otherwise
48
return args.HTTPClientConfig.Validate()
49
}
50
51
// Convert converts Arguments to the Prometheus SD type.
52
func (args *Arguments) Convert() *promk8s.SDConfig {
53
selectors := make([]promk8s.SelectorConfig, len(args.Selectors))
54
for i, s := range args.Selectors {
55
selectors[i] = *s.convert()
56
}
57
return &promk8s.SDConfig{
58
APIServer: args.APIServer.Convert(),
59
Role: promk8s.Role(args.Role),
60
KubeConfig: args.KubeConfig,
61
HTTPClientConfig: *args.HTTPClientConfig.Convert(),
62
NamespaceDiscovery: *args.NamespaceDiscovery.convert(),
63
Selectors: selectors,
64
}
65
}
66
67
// NamespaceDiscovery configures filtering rules for which namespaces to discover.
68
type NamespaceDiscovery struct {
69
IncludeOwnNamespace bool `river:"own_namespace,attr,optional"`
70
Names []string `river:"names,attr,optional"`
71
}
72
73
func (nd *NamespaceDiscovery) convert() *promk8s.NamespaceDiscovery {
74
return &promk8s.NamespaceDiscovery{
75
IncludeOwnNamespace: nd.IncludeOwnNamespace,
76
Names: nd.Names,
77
}
78
}
79
80
// SelectorConfig configures selectors to filter resources to discover.
81
type SelectorConfig struct {
82
Role string `river:"role,attr"`
83
Label string `river:"label,attr,optional"`
84
Field string `river:"field,attr,optional"`
85
}
86
87
func (sc *SelectorConfig) convert() *promk8s.SelectorConfig {
88
return &promk8s.SelectorConfig{
89
Role: promk8s.Role(sc.Role),
90
Label: sc.Label,
91
Field: sc.Field,
92
}
93
}
94
95
// New returns a new instance of a discovery.kubernetes component.
96
func New(opts component.Options, args Arguments) (component.Component, error) {
97
return discovery.New(opts, args, func(args component.Arguments) (discovery.Discoverer, error) {
98
newArgs := args.(Arguments)
99
return promk8s.New(opts.Logger, newArgs.Convert())
100
})
101
}
102
103