Path: blob/main/component/discovery/kubernetes/kubernetes.go
4096 views
// Package kubernetes implements a discovery.kubernetes component.1package kubernetes23import (4"github.com/grafana/agent/component"5"github.com/grafana/agent/component/common/config"6"github.com/grafana/agent/component/discovery"7promk8s "github.com/prometheus/prometheus/discovery/kubernetes"8)910func init() {11component.Register(component.Registration{12Name: "discovery.kubernetes",13Args: Arguments{},14Exports: discovery.Exports{},1516Build: func(opts component.Options, args component.Arguments) (component.Component, error) {17return New(opts, args.(Arguments))18},19})20}2122// Arguments configures the discovery.kubernetes component.23type Arguments struct {24APIServer config.URL `river:"api_server,attr,optional"`25Role string `river:"role,attr"`26KubeConfig string `river:"kubeconfig_file,attr,optional"`27HTTPClientConfig config.HTTPClientConfig `river:",squash"`28NamespaceDiscovery NamespaceDiscovery `river:"namespaces,block,optional"`29Selectors []SelectorConfig `river:"selectors,block,optional"`30}3132// DefaultConfig holds defaults for SDConfig.33var DefaultConfig = Arguments{34HTTPClientConfig: config.DefaultHTTPClientConfig,35}3637// UnmarshalRiver implements river.Unmarshaler and applies default settings.38func (args *Arguments) UnmarshalRiver(f func(interface{}) error) error {39*args = DefaultConfig40type arguments Arguments41err := f((*arguments)(args))42if err != nil {43return err44}4546// We must explicitly Validate because HTTPClientConfig is squashed and it won't run otherwise47return args.HTTPClientConfig.Validate()48}4950// Convert converts Arguments to the Prometheus SD type.51func (args *Arguments) Convert() *promk8s.SDConfig {52selectors := make([]promk8s.SelectorConfig, len(args.Selectors))53for i, s := range args.Selectors {54selectors[i] = *s.convert()55}56return &promk8s.SDConfig{57APIServer: args.APIServer.Convert(),58Role: promk8s.Role(args.Role),59KubeConfig: args.KubeConfig,60HTTPClientConfig: *args.HTTPClientConfig.Convert(),61NamespaceDiscovery: *args.NamespaceDiscovery.convert(),62Selectors: selectors,63}64}6566// NamespaceDiscovery configures filtering rules for which namespaces to discover.67type NamespaceDiscovery struct {68IncludeOwnNamespace bool `river:"own_namespace,attr,optional"`69Names []string `river:"names,attr,optional"`70}7172func (nd *NamespaceDiscovery) convert() *promk8s.NamespaceDiscovery {73return &promk8s.NamespaceDiscovery{74IncludeOwnNamespace: nd.IncludeOwnNamespace,75Names: nd.Names,76}77}7879// SelectorConfig configures selectors to filter resources to discover.80type SelectorConfig struct {81Role string `river:"role,attr"`82Label string `river:"label,attr,optional"`83Field string `river:"field,attr,optional"`84}8586func (sc *SelectorConfig) convert() *promk8s.SelectorConfig {87return &promk8s.SelectorConfig{88Role: promk8s.Role(sc.Role),89Label: sc.Label,90Field: sc.Field,91}92}9394// New returns a new instance of a discovery.kubernetes component.95func New(opts component.Options, args Arguments) (component.Component, error) {96return discovery.New(opts, args, func(args component.Arguments) (discovery.Discoverer, error) {97newArgs := args.(Arguments)98return promk8s.New(opts.Logger, newArgs.Convert())99})100}101102103