2020-04-22 19:16:01 +00:00
package kubernetes
import (
"fmt"
2020-04-23 08:34:04 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
2020-12-24 08:56:10 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/proxy"
2020-04-22 19:16:01 +00:00
)
2020-04-23 08:34:04 +00:00
// SDConfig represents kubernetes-based service discovery config.
//
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#kubernetes_sd_config
type SDConfig struct {
2020-11-13 14:17:03 +00:00
APIServer string ` yaml:"api_server,omitempty" `
2020-04-23 08:34:04 +00:00
Role string ` yaml:"role" `
2020-11-13 14:17:03 +00:00
BasicAuth * promauth . BasicAuthConfig ` yaml:"basic_auth,omitempty" `
BearerToken string ` yaml:"bearer_token,omitempty" `
BearerTokenFile string ` yaml:"bearer_token_file,omitempty" `
2020-12-24 08:56:10 +00:00
ProxyURL proxy . URL ` yaml:"proxy_url,omitempty" `
2020-11-13 14:17:03 +00:00
TLSConfig * promauth . TLSConfig ` yaml:"tls_config,omitempty" `
Namespaces Namespaces ` yaml:"namespaces,omitempty" `
Selectors [ ] Selector ` yaml:"selectors,omitempty" `
2020-04-23 08:34:04 +00:00
}
// Namespaces represents namespaces for SDConfig
type Namespaces struct {
Names [ ] string ` yaml:"names" `
}
// Selector represents kubernetes selector.
//
// See https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/
// and https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
type Selector struct {
Role string ` yaml:"role" `
Label string ` yaml:"label" `
Field string ` yaml:"field" `
}
2020-04-24 14:50:21 +00:00
2021-03-02 14:42:48 +00:00
// ScrapeWorkConstructorFunc must construct ScrapeWork object for the given metaLabels.
type ScrapeWorkConstructorFunc func ( metaLabels map [ string ] string ) interface { }
// GetScrapeWorkObjects returns ScrapeWork objects for the given sdc and baseDir.
//
// swcFunc is used for constructing such objects.
func ( sdc * SDConfig ) GetScrapeWorkObjects ( baseDir string , swcFunc ScrapeWorkConstructorFunc ) ( [ ] interface { } , error ) {
cfg , err := getAPIConfig ( sdc , baseDir , swcFunc )
2020-05-04 12:53:50 +00:00
if err != nil {
2020-06-30 19:58:18 +00:00
return nil , fmt . Errorf ( "cannot create API config: %w" , err )
2020-04-24 14:50:21 +00:00
}
2021-02-26 14:54:03 +00:00
switch sdc . Role {
2021-02-26 18:21:27 +00:00
case "node" , "pod" , "service" , "endpoints" , "endpointslices" , "ingress" :
2021-03-02 14:42:48 +00:00
return cfg . aw . getScrapeWorkObjectsForRole ( sdc . Role ) , nil
2021-02-26 14:54:03 +00:00
default :
return nil , fmt . Errorf ( "unexpected `role`: %q; must be one of `node`, `pod`, `service`, `endpoints`, `endpointslices` or `ingress`; skipping it" , sdc . Role )
}
2020-04-24 14:50:21 +00:00
}
2021-03-01 12:13:56 +00:00
// MustStop stops further usage for sdc.
func ( sdc * SDConfig ) MustStop ( ) {
v := configMap . Delete ( sdc )
if v != nil {
// v can be nil if GetLabels wasn't called yet.
cfg := v . ( * apiConfig )
cfg . mustStop ( )
}
}