Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 32 additions & 15 deletions agents/prometheusoperator/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,33 @@ func (agent *PrometheusOperator) CreateOrUpdate(sp api.StatsAccessor, new *api.A
in.Labels = meta_util.OverwriteKeys(sp.ServiceMonitorAdditionalLabels(), new.Prometheus.ServiceMonitor.Labels)
core_util.EnsureOwnerReference(&in.ObjectMeta, owner)

in.Spec.TargetLabels = new.Prometheus.ServiceMonitor.TargetLabels
in.Spec.PodTargetLabels = new.Prometheus.ServiceMonitor.PodTargetLabels

in.Spec.NamespaceSelector = promapi.NamespaceSelector{
MatchNames: []string{sp.GetNamespace()},
}
podLabel := promapi.RelabelConfig{
SourceLabels: []promapi.LabelName{"__meta_kubernetes_endpoint_address_target_name"},
TargetLabel: "pod",
Action: "replace",
}
for _, p := range svc.Spec.Ports {
in.Spec.Endpoints = upsertMonitoringEndpoint(in.Spec.Endpoints, promapi.Endpoint{
Port: p.Name,
Interval: promapi.Duration(new.Prometheus.ServiceMonitor.Interval),
Path: sp.Path(),
HonorLabels: true,
Scheme: func() *promapi.Scheme { s := promapi.Scheme(sp.Scheme()); return &s }(),
TLSConfig: sp.TLSConfig(),
RelabelConfigs: []promapi.RelabelConfig{
{
SourceLabels: []promapi.LabelName{"__meta_kubernetes_endpoint_address_target_name"},
TargetLabel: "pod",
Action: "replace",
},
},
})
ep := promapi.Endpoint{
Port: p.Name,
Interval: promapi.Duration(new.Prometheus.ServiceMonitor.Interval),
Path: sp.Path(),
HonorLabels: true,
Scheme: func() *promapi.Scheme { s := promapi.Scheme(sp.Scheme()); return &s }(),
TLSConfig: sp.TLSConfig(),
RelabelConfigs: []promapi.RelabelConfig{podLabel},
}
mep := findEndpoint(new.Prometheus.ServiceMonitor.Endpoints, ep.Port)
if mep != nil {
ep.RelabelConfigs = append([]promapi.RelabelConfig{podLabel}, mep.RelabelConfigs...)
ep.MetricRelabelConfigs = mep.MetricRelabelConfigs
}
in.Spec.Endpoints = upsertMonitoringEndpoint(in.Spec.Endpoints, ep)
}
in.Spec.Selector = metav1.LabelSelector{
MatchLabels: svc.Labels,
Expand All @@ -105,6 +113,15 @@ func (agent *PrometheusOperator) CreateOrUpdate(sp api.StatsAccessor, new *api.A
return vt, err
}

func findEndpoint(endpoints []api.Endpoint, port string) *api.Endpoint {
for _, ep := range endpoints {
if ep.Port == port {
return &ep
}
}
return nil
}

func (agent *PrometheusOperator) Delete(sp api.StatsAccessor) (kutil.VerbType, error) {
if !discovery.ExistsGroupKind(agent.k8sClient.Discovery(), promapi.SchemeGroupVersion.Group, promapi.ServiceMonitorsKind) {
return kutil.VerbUnchanged, errors.New("cluster does not support Prometheus operator")
Expand Down
98 changes: 98 additions & 0 deletions api/v1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions api/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,53 @@ type ServiceMonitorSpec struct {
// Interval at which metrics should be scraped
// +optional
Interval string `json:"interval,omitempty"`

// targetLabels defines the labels which are transferred from the
// associated Kubernetes `Service` object onto the ingested metrics.
//
// +optional
TargetLabels []string `json:"targetLabels,omitempty"`
// podTargetLabels defines the labels which are transferred from the
// associated Kubernetes `Pod` object onto the ingested metrics.
//
// +optional
PodTargetLabels []string `json:"podTargetLabels,omitempty"`

// endpoints defines the list of endpoints part of this ServiceMonitor.
// Defines how to scrape metrics from Kubernetes [Endpoints](https://kubernetes.io/docs/concepts/services-networking/service/#endpoints) objects.
// In most cases, an Endpoints object is backed by a Kubernetes [Service](https://kubernetes.io/docs/concepts/services-networking/service/) object with the same name and labels.
// +required
Endpoints []Endpoint `json:"endpoints"`
}

// Endpoint defines an endpoint serving Prometheus metrics to be scraped by
// Prometheus.
//
// +k8s:openapi-gen=true
type Endpoint struct {
// port defines the name of the Service port which this endpoint refers to.
//
// It takes precedence over `targetPort`.
// +optional
Port string `json:"port,omitempty"`

// metricRelabelings defines the relabeling rules to apply to the
// samples before ingestion.
//
// +optional
MetricRelabelConfigs []promapi.RelabelConfig `json:"metricRelabelings,omitempty"`

// relabelings defines the relabeling rules to apply the target's
// metadata labels.
//
// The Operator automatically adds relabelings for a few standard Kubernetes fields.
//
// The original scrape job's name is available via the `__tmp_prometheus_job_name` label.
//
// More info: https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config
//
// +optional
RelabelConfigs []promapi.RelabelConfig `json:"relabelings,omitempty"`
}

type PrometheusExporterSpec struct {
Expand Down
48 changes: 48 additions & 0 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading