mirror of
https://github.com/prometheus-operator/prometheus-operator.git
synced 2025-04-21 11:48:53 +00:00
PrometheusSpec: add ObjectMeta field
ObjectMeta field in PromSpec will propagate labels and annotations to prometheus pods created by the operator. Fixes #580
This commit is contained in:
parent
5e35bfef0b
commit
ad0319104c
5 changed files with 51 additions and 10 deletions
Documentation
pkg
|
@ -134,6 +134,7 @@ Specification of the desired behavior of the Prometheus cluster. More info: http
|
|||
|
||||
| Field | Description | Scheme | Required |
|
||||
| ----- | ----------- | ------ | -------- |
|
||||
| podMetadata | Standard object’s metadata. More info: http://releases.k8s.io/HEAD/docs/devel/api-conventions.md#metadata Metadata Labels and Annotations gets propagated to the prometheus pods. | [metav1.ObjectMeta](https://kubernetes.io/docs/api-reference/v1.6/#objectmeta-v1-meta) | false |
|
||||
| serviceMonitorSelector | ServiceMonitors to be selected for target discovery. | *[metav1.LabelSelector](https://kubernetes.io/docs/api-reference/v1.6/#labelselector-v1-meta) | false |
|
||||
| version | Version of Prometheus to be deployed. | string | false |
|
||||
| paused | When a Prometheus deployment is paused, no actions except for deletion will be performed on the underlying objects. | bool | false |
|
||||
|
|
|
@ -49,6 +49,10 @@ type PrometheusList struct {
|
|||
// Specification of the desired behavior of the Prometheus cluster. More info:
|
||||
// http://releases.k8s.io/HEAD/docs/devel/api-conventions.md#spec-and-status
|
||||
type PrometheusSpec struct {
|
||||
// Standard object’s metadata. More info:
|
||||
// http://releases.k8s.io/HEAD/docs/devel/api-conventions.md#metadata
|
||||
// Metadata Labels and Annotations gets propagated to the prometheus pods.
|
||||
PodMetadata metav1.ObjectMeta `json:"podMetadata,omitempty"`
|
||||
// ServiceMonitors to be selected for target discovery.
|
||||
ServiceMonitorSelector *metav1.LabelSelector `json:"serviceMonitorSelector,omitempty"`
|
||||
// Version of Prometheus to be deployed.
|
||||
|
|
|
@ -49,6 +49,10 @@ type PrometheusList struct {
|
|||
// Specification of the desired behavior of the Prometheus cluster. More info:
|
||||
// http://releases.k8s.io/HEAD/docs/devel/api-conventions.md#spec-and-status
|
||||
type PrometheusSpec struct {
|
||||
// Standard object’s metadata. More info:
|
||||
// http://releases.k8s.io/HEAD/docs/devel/api-conventions.md#metadata
|
||||
// Metadata Labels and Annotations gets propagated to the prometheus pods.
|
||||
PodMetadata metav1.ObjectMeta `json:"podMetadata,omitempty"`
|
||||
// ServiceMonitors to be selected for target discovery.
|
||||
ServiceMonitorSelector *metav1.LabelSelector `json:"serviceMonitorSelector,omitempty"`
|
||||
// Version of Prometheus to be deployed.
|
||||
|
|
|
@ -39,8 +39,7 @@ const (
|
|||
governingServiceName = "prometheus-operated"
|
||||
DefaultVersion = "v1.7.1"
|
||||
defaultRetention = "24h"
|
||||
|
||||
configMapsFilename = "configmaps.json"
|
||||
configMapsFilename = "configmaps.json"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -440,7 +439,18 @@ func makeStatefulSetSpec(p monitoringv1.Prometheus, c *Config, ruleConfigMaps []
|
|||
Port: intstr.FromString("web"),
|
||||
},
|
||||
}
|
||||
|
||||
podAnnotations := map[string]string{}
|
||||
podLabels := map[string]string{}
|
||||
for k, v := range p.Spec.PodMetadata.Labels {
|
||||
// ToDo: define which labels can be set and sanitize them.
|
||||
podLabels[k] = v
|
||||
}
|
||||
podLabels["app"] = "prometheus"
|
||||
podLabels["prometheus"] = p.Name
|
||||
for k, v := range p.Spec.PodMetadata.Annotations {
|
||||
// ToDo: define which annotations can be set and sanitize them.
|
||||
podAnnotations[k] = v
|
||||
}
|
||||
return &v1beta1.StatefulSetSpec{
|
||||
ServiceName: governingServiceName,
|
||||
Replicas: p.Spec.Replicas,
|
||||
|
@ -449,10 +459,8 @@ func makeStatefulSetSpec(p monitoringv1.Prometheus, c *Config, ruleConfigMaps []
|
|||
},
|
||||
Template: v1.PodTemplateSpec{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Labels: map[string]string{
|
||||
"app": "prometheus",
|
||||
"prometheus": p.Name,
|
||||
},
|
||||
Labels: podLabels,
|
||||
Annotations: podAnnotations,
|
||||
},
|
||||
Spec: v1.PodSpec{
|
||||
Containers: []v1.Container{
|
||||
|
|
|
@ -15,14 +15,13 @@
|
|||
package prometheus
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
monitoringv1 "github.com/coreos/prometheus-operator/pkg/client/monitoring/v1"
|
||||
"github.com/stretchr/testify/require"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/pkg/api/v1"
|
||||
"k8s.io/client-go/pkg/apis/apps/v1beta1"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -53,6 +52,31 @@ func TestStatefulSetLabelingAndAnnotations(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestPodLabelsAnnotations(t *testing.T) {
|
||||
annotations := map[string]string{
|
||||
"testannotation": "testvalue",
|
||||
}
|
||||
labels := map[string]string{
|
||||
"testlabel": "testvalue",
|
||||
}
|
||||
sset, err := makeStatefulSet(monitoringv1.Prometheus{
|
||||
ObjectMeta: metav1.ObjectMeta{},
|
||||
Spec: monitoringv1.PrometheusSpec{
|
||||
PodMetadata: metav1.ObjectMeta{
|
||||
Annotations: annotations,
|
||||
Labels: labels,
|
||||
},
|
||||
},
|
||||
}, nil, defaultTestConfig, []*v1.ConfigMap{})
|
||||
require.NoError(t, err)
|
||||
if _, ok := sset.Spec.Template.ObjectMeta.Labels["testlabel"]; !ok {
|
||||
t.Fatal("Pod labes are not properly propagated")
|
||||
}
|
||||
if !reflect.DeepEqual(annotations, sset.Spec.Template.ObjectMeta.Annotations) {
|
||||
t.Fatal("Pod annotaitons are not properly propagated")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatefulSetPVC(t *testing.T) {
|
||||
labels := map[string]string{
|
||||
"testlabel": "testlabelvalue",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue