1
0
Fork 0
mirror of https://github.com/prometheus-operator/prometheus-operator.git synced 2025-04-21 11:48:53 +00:00

prometheus: Add ability to whitelist Kubernetes labels

This commit is contained in:
Frederic Branczyk 2018-03-23 14:45:46 +01:00
parent 0971f911e2
commit 06c8308952
No known key found for this signature in database
GPG key ID: 7741A52782A90069
8 changed files with 44 additions and 4 deletions
Documentation
contrib/kube-prometheus/manifests/examples/example-app
example/prometheus-operator-crd
pkg

View file

@ -318,6 +318,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 |

View file

@ -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

View file

@ -9,6 +9,8 @@ spec:
selector:
matchLabels:
tier: frontend
targetLabels:
- tier
endpoints:
- port: web
interval: 10s

View file

@ -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

View file

@ -1242,6 +1242,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.",

View file

@ -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.

View file

@ -732,6 +732,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))

View file

@ -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