mirror of
https://github.com/prometheus-operator/prometheus-operator.git
synced 2025-04-21 11:48:53 +00:00
Merge pull request #1145 from brancz/targetlabels
prometheus: Add ability to whitelist Kubernetes labels
This commit is contained in:
commit
f6f259a9ca
8 changed files with 44 additions and 4 deletions
Documentation
contrib/kube-prometheus/manifests/examples/example-app
example/prometheus-operator-crd
pkg
|
@ -319,6 +319,7 @@ ServiceMonitorSpec contains specification parameters for a ServiceMonitor.
|
|||
| Field | Description | Scheme | Required |
|
||||
| ----- | ----------- | ------ | -------- |
|
||||
| jobLabel | The label to use to retrieve the job name from. | string | false |
|
||||
| targetLabels | TargetLabels transfers labels on the Kubernetes Service onto the target. | []string | false |
|
||||
| endpoints | A list of endpoints allowed as part of this ServiceMonitor. | [][Endpoint](#endpoint) | true |
|
||||
| selector | Selector to select Endpoints objects. | [metav1.LabelSelector](https://v1-6.docs.kubernetes.io/docs/api-reference/v1.6/#labelselector-v1-meta) | true |
|
||||
| namespaceSelector | Selector to select which namespaces the Endpoints objects are discovered from. | [NamespaceSelector](#namespaceselector) | false |
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
kind: Service
|
||||
apiVersion: v1
|
||||
metadata:
|
||||
metadata:
|
||||
name: example-app
|
||||
labels:
|
||||
tier: frontend
|
||||
namespace: default
|
||||
spec:
|
||||
selector:
|
||||
app: example-app
|
||||
spec:
|
||||
selector:
|
||||
app: example-app
|
||||
ports:
|
||||
- name: web
|
||||
protocol: TCP
|
||||
|
|
|
@ -9,6 +9,8 @@ spec:
|
|||
selector:
|
||||
matchLabels:
|
||||
tier: frontend
|
||||
targetLabels:
|
||||
- tier
|
||||
endpoints:
|
||||
- port: web
|
||||
interval: 10s
|
||||
|
|
|
@ -221,6 +221,12 @@ spec:
|
|||
"In", and the values array contains only "value". The requirements
|
||||
are ANDed.
|
||||
type: object
|
||||
targetLabels:
|
||||
description: TargetLabels transfers labels on the Kubernetes Service
|
||||
onto the target.
|
||||
items:
|
||||
type: string
|
||||
type: array
|
||||
required:
|
||||
- endpoints
|
||||
- selector
|
||||
|
|
|
@ -1256,6 +1256,20 @@ func GetOpenAPIDefinitions(ref common.ReferenceCallback) map[string]common.OpenA
|
|||
Format: "",
|
||||
},
|
||||
},
|
||||
"targetLabels": {
|
||||
SchemaProps: spec.SchemaProps{
|
||||
Description: "TargetLabels transfers labels on the Kubernetes Service onto the target.",
|
||||
Type: []string{"array"},
|
||||
Items: &spec.SchemaOrArray{
|
||||
Schema: &spec.Schema{
|
||||
SchemaProps: spec.SchemaProps{
|
||||
Type: []string{"string"},
|
||||
Format: "",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"endpoints": {
|
||||
SchemaProps: spec.SchemaProps{
|
||||
Description: "A list of endpoints allowed as part of this ServiceMonitor.",
|
||||
|
|
|
@ -294,6 +294,8 @@ type ServiceMonitor struct {
|
|||
type ServiceMonitorSpec struct {
|
||||
// The label to use to retrieve the job name from.
|
||||
JobLabel string `json:"jobLabel,omitempty"`
|
||||
// TargetLabels transfers labels on the Kubernetes Service onto the target.
|
||||
TargetLabels []string `json:"targetLabels,omitempty"`
|
||||
// A list of endpoints allowed as part of this ServiceMonitor.
|
||||
Endpoints []Endpoint `json:"endpoints"`
|
||||
// Selector to select Endpoints objects.
|
||||
|
|
|
@ -737,6 +737,11 @@ func (in *ServiceMonitorList) DeepCopy() *ServiceMonitorList {
|
|||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *ServiceMonitorSpec) DeepCopyInto(out *ServiceMonitorSpec) {
|
||||
*out = *in
|
||||
if in.TargetLabels != nil {
|
||||
in, out := &in.TargetLabels, &out.TargetLabels
|
||||
*out = make([]string, len(*in))
|
||||
copy(*out, *in)
|
||||
}
|
||||
if in.Endpoints != nil {
|
||||
in, out := &in.Endpoints, &out.Endpoints
|
||||
*out = make([]Endpoint, len(*in))
|
||||
|
|
|
@ -345,6 +345,16 @@ func generateServiceMonitorConfig(version semver.Version, m *v1.ServiceMonitor,
|
|||
},
|
||||
}...)
|
||||
|
||||
// Relabel targetLabels from Service onto target.
|
||||
for _, l := range m.Spec.TargetLabels {
|
||||
relabelings = append(relabelings, yaml.MapSlice{
|
||||
{Key: "source_labels", Value: []string{"__meta_kubernetes_service_label_" + sanitizeLabelName(l)}},
|
||||
{Key: "target_label", Value: sanitizeLabelName(l)},
|
||||
{Key: "regex", Value: "(.+)"},
|
||||
{Key: "replacement", Value: "${1}"},
|
||||
})
|
||||
}
|
||||
|
||||
// By default, generate a safe job name from the service name. We also keep
|
||||
// this around if a jobLabel is set in case the targets don't actually have a
|
||||
// value for it. A single service may potentially have multiple metrics
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue