Path: blob/main/component/prometheus/operator/configgen/config_gen.go
5389 views
package configgen12// SEE https://github.com/prometheus-operator/prometheus-operator/blob/aa8222d7e9b66e9293ed11c9291ea70173021029/pkg/prometheus/promcfg.go34import (5"fmt"6"regexp"78k8sConfig "github.com/grafana/agent/component/common/kubernetes"9promopv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"10commonConfig "github.com/prometheus/common/config"11"github.com/prometheus/common/model"12promk8s "github.com/prometheus/prometheus/discovery/kubernetes"13"github.com/prometheus/prometheus/model/relabel"14)1516type ConfigGenerator struct {17Client *k8sConfig.ClientArguments18}1920var (21invalidLabelCharRE = regexp.MustCompile(`[^a-zA-Z0-9_]`)22)2324// generateK8SSDConfig generates a kubernetes service discovery config based on the given namespace selector.25// The k8s sd config is mostly dependent on our local config for accessing the kubernetes cluster.26// If undefined it will default to an in-cluster config27func (cg *ConfigGenerator) generateK8SSDConfig(namespaceSelector promopv1.NamespaceSelector, namespace string, role promk8s.Role, attachMetadata *promopv1.AttachMetadata) *promk8s.SDConfig {28cfg := &promk8s.SDConfig{29Role: role,30}31namespaces := cg.getNamespacesFromNamespaceSelector(namespaceSelector, namespace)32if len(namespaces) != 0 {33cfg.NamespaceDiscovery.Names = namespaces34}35client := cg.Client36if client.KubeConfig != "" {37cfg.KubeConfig = client.KubeConfig38}39if client.APIServer.URL != nil {40hCfg := client.HTTPClientConfig41cfg.APIServer = client.APIServer.Convert()4243if hCfg.BasicAuth != nil {44cfg.HTTPClientConfig.BasicAuth = hCfg.BasicAuth.Convert()45}4647if hCfg.BearerToken != "" {48cfg.HTTPClientConfig.BearerToken = commonConfig.Secret(hCfg.BearerToken)49}50if hCfg.BearerTokenFile != "" {51cfg.HTTPClientConfig.BearerTokenFile = hCfg.BearerTokenFile52}53cfg.HTTPClientConfig.TLSConfig = *hCfg.TLSConfig.Convert()54if hCfg.Authorization != nil {55if hCfg.Authorization.Type == "" {56hCfg.Authorization.Type = "Bearer"57}58cfg.HTTPClientConfig.Authorization = hCfg.Authorization.Convert()59}60}61if attachMetadata != nil {62cfg.AttachMetadata.Node = attachMetadata.Node63}64return cfg65}6667func (cg *ConfigGenerator) generateSafeTLS(tls promopv1.SafeTLSConfig) (commonConfig.TLSConfig, error) {68tc := commonConfig.TLSConfig{}69tc.InsecureSkipVerify = tls.InsecureSkipVerify7071if tls.CA.Secret != nil || tls.CA.ConfigMap != nil {72return tc, fmt.Errorf("loading ca certs no supported yet")73}74if tls.Cert.Secret != nil || tls.Cert.ConfigMap != nil {75return tc, fmt.Errorf("loading tls certs no supported yet")76}77if tls.KeySecret != nil {78return tc, fmt.Errorf("loading tls certs no supported yet")79}80if tls.ServerName != "" {81tc.ServerName = tls.ServerName82}83return tc, nil84}8586type relabeler struct {87configs []*relabel.Config88}8990// add adds a relabel config to the relabeler. It sets defaults from prometheus defaults.91func (r *relabeler) add(cfgs ...*relabel.Config) {92for _, cfg := range cfgs {93if cfg.Action == "" {94cfg.Action = relabel.DefaultRelabelConfig.Action95}96if cfg.Separator == "" {97cfg.Separator = relabel.DefaultRelabelConfig.Separator98}99if cfg.Regex.Regexp == nil {100cfg.Regex = relabel.DefaultRelabelConfig.Regex101}102if cfg.Replacement == "" {103cfg.Replacement = relabel.DefaultRelabelConfig.Replacement104}105r.configs = append(r.configs, cfg)106}107}108109// addFromV1 converts from an externally generated monitoringv1 RelabelConfig. Used for converting relabel rules generated by external package110func (r *relabeler) addFromV1(cfgs ...*promopv1.RelabelConfig) (err error) {111for _, c := range cfgs {112cfg := &relabel.Config{}113for _, l := range c.SourceLabels {114cfg.SourceLabels = append(cfg.SourceLabels, model.LabelName(l))115}116if c.Separator != "" {117cfg.Separator = c.Separator118}119if c.TargetLabel != "" {120cfg.TargetLabel = c.TargetLabel121}122if c.Regex != "" {123cfg.Regex, err = relabel.NewRegexp(c.Regex)124if err != nil {125return err126}127}128if c.Modulus != 0 {129cfg.Modulus = c.Modulus130}131if c.Replacement != "" {132cfg.Replacement = c.Replacement133}134if c.Action != "" {135cfg.Action = relabel.Action(c.Action)136}137r.configs = append(r.configs, cfg)138}139return nil140}141142func (cg *ConfigGenerator) initRelabelings() relabeler {143r := relabeler{}144// Relabel prometheus job name into a meta label145r.add(&relabel.Config{146SourceLabels: model.LabelNames{"job"},147TargetLabel: "__tmp_prometheus_job_name",148})149return r150}151152func sanitizeLabelName(name string) model.LabelName {153return model.LabelName(invalidLabelCharRE.ReplaceAllString(name, "_"))154}155156func (cg *ConfigGenerator) getNamespacesFromNamespaceSelector(nsel promopv1.NamespaceSelector, namespace string) []string {157if nsel.Any {158return []string{}159} else if len(nsel.MatchNames) == 0 {160return []string{namespace}161}162return nsel.MatchNames163}164165166