func()

in pkg/operator/apis/monitoring/v1/node_types.go [129:212]


func (c *ClusterNodeMonitoring) endpointScrapeConfig(ep *ScrapeNodeEndpoint, projectID, location, cluster string) (*promconfig.ScrapeConfig, error) {
	// Filter targets that belong to selected nodes.
	relabelCfgs, err := relabelingsForSelector(c.Spec.Selector, c)
	if err != nil {
		return nil, err
	}

	metricsPath := "/metrics"
	if ep.Path != "" {
		metricsPath = ep.Path
	}

	relabelCfgs = append(relabelCfgs,
		&relabel.Config{
			Action:      relabel.Replace,
			Replacement: c.Name,
			TargetLabel: "job",
		},
		&relabel.Config{
			Action:       relabel.Replace,
			SourceLabels: prommodel.LabelNames{"__meta_kubernetes_node_name"},
			TargetLabel:  "node",
		},
		&relabel.Config{
			Action:       relabel.Replace,
			SourceLabels: prommodel.LabelNames{"__meta_kubernetes_node_name"},
			Replacement:  fmt.Sprintf(`$1:%s`, strings.TrimPrefix(metricsPath, "/")),
			TargetLabel:  "instance",
		},
		// Force target labels so they cannot be overwritten by metric labels.
		&relabel.Config{
			Action:      relabel.Replace,
			TargetLabel: "project_id",
			Replacement: projectID,
		},
		&relabel.Config{
			Action:      relabel.Replace,
			TargetLabel: "location",
			Replacement: location,
		},
		&relabel.Config{
			Action:      relabel.Replace,
			TargetLabel: "cluster",
			Replacement: cluster,
		},
	)

	discoveryCfgs := discovery.Configs{
		&discoverykube.SDConfig{
			HTTPClientConfig: config.DefaultHTTPClientConfig,
			Role:             discoverykube.RoleNode,
			// Drop all potential targets not the same node as the collector. The $(NODE_NAME) variable
			// is interpolated by the config reloader sidecar before the config reaches the Prometheus collector.
			// Doing it through selectors rather than relabeling should substantially reduce the client and
			// server side load.
			Selectors: []discoverykube.SelectorConfig{
				{
					Role:  discoverykube.RoleNode,
					Field: fmt.Sprintf("metadata.name=$(%s)", EnvVarNodeName),
				},
			},
		},
	}

	httpCfg := config.HTTPClientConfig{
		Authorization: &config.Authorization{
			CredentialsFile: "/var/run/secrets/kubernetes.io/serviceaccount/token",
		},
		TLSConfig: config.TLSConfig{
			CAFile: "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt",
		},
	}
	if tls := ep.TLS; tls != nil {
		httpCfg.TLSConfig.InsecureSkipVerify = tls.InsecureSkipVerify
	}

	return buildPrometheusScrapeConfig(fmt.Sprintf("%s%s", c.GetKey(), metricsPath), discoveryCfgs, httpCfg, relabelCfgs, c.Spec.Limits,
		ScrapeEndpoint{Interval: ep.Interval,
			Timeout:          ep.Timeout,
			Path:             metricsPath,
			MetricRelabeling: ep.MetricRelabeling,
			Scheme:           ep.Scheme,
			Params:           ep.Params})
}