⚠️ Work in Progress
You are viewing development documentation built from the latest commit on main. APIs and features are subject to change.

Prometheus Builders

Prometheus Builders - Prometheus Operator CRD Constructors

Go Reference Go Reference

The prometheus package provides strongly-typed constructor functions for creating Prometheus Operator Kubernetes resources. These are the low-level building blocks used by Kure’s higher-level stack and workflow packages.

Overview

Each config-struct builder takes a configuration struct and returns a populated Prometheus Operator custom resource. The builders handle API version and kind metadata, letting you focus on the resource specification.

Constructors

Every kind this package registers has a generated Create<Kind> wrapper in zz_generated_create.go, produced from the scheme by pkg/kubernetes/internal/gen (make gen-builders, checked by make check-builders in CI). A wrapper delegates to kubernetes.Create[T] and emits TypeMeta and identity only: no default, no label, no spec value. Namespaced kinds take (name, namespace), cluster-scoped kinds take (name). The upstream struct is the construction API; set spec fields directly or through the admissible Set*/Add* sugar below.

obj := prometheus.CreateServiceMonitor("my-app", "monitoring")

The config-struct builders (prometheus.ServiceMonitor(&prometheus.ServiceMonitorConfig{...})) are a separate, opinionated layer on top of the same upstream types; they are unchanged by the generated constructors. The hand-written Create* helpers for spec fragments that remain in this package are legacy and are removed by the prune work item of the builder-contract epic.

See the Kubernetes Builders page for the full builder contract: construction, sugar admission classes, purity and the release-1 migration ledger.

Supported Resources

ServiceMonitor

import "github.com/go-kure/kure/pkg/kubernetes/prometheus"

sm := prometheus.ServiceMonitor(&prometheus.ServiceMonitorConfig{
    Name:      "my-app",
    Namespace: "monitoring",
    Selector:  metav1.LabelSelector{
        MatchLabels: map[string]string{"app": "my-app"},
    },
    Endpoints: []monitoringv1.Endpoint{
        {Path: "/metrics", Port: "http", Interval: "30s"},
    },
    JobLabel:     "app",
    TargetLabels: []string{"app", "version"},
})
FieldTypeDescription
NamestringResource name
NamespacestringResource namespace
Selectormetav1.LabelSelectorLabel selector for target Services
Endpoints[]monitoringv1.EndpointScrape endpoint configurations
JobLabelstringLabel to use as the Prometheus job label
TargetLabels[]stringService labels to transfer to scraped metrics
NamespaceSelector*monitoringv1.NamespaceSelectorNamespaces to select Services from
SampleLimit*int64Per-scrape sample limit
Labelsmap[string]stringAdditional labels for the resource

PodMonitor

pm := prometheus.PodMonitor(&prometheus.PodMonitorConfig{
    Name:      "my-app",
    Namespace: "monitoring",
    Selector:  metav1.LabelSelector{
        MatchLabels: map[string]string{"app": "my-app"},
    },
    PodMetricsEndpoints: []monitoringv1.PodMetricsEndpoint{
        {Path: "/metrics", Port: "http", Interval: "30s"},
    },
})
FieldTypeDescription
NamestringResource name
NamespacestringResource namespace
Selectormetav1.LabelSelectorLabel selector for target Pods
PodMetricsEndpoints[]monitoringv1.PodMetricsEndpointScrape endpoint configurations
JobLabelstringLabel to use as the Prometheus job label
PodTargetLabels[]stringPod labels to transfer to scraped metrics
NamespaceSelector*monitoringv1.NamespaceSelectorNamespaces to select Pods from
SampleLimit*int64Per-scrape sample limit
Labelsmap[string]stringAdditional labels for the resource

PrometheusRule

rule := prometheus.PrometheusRule(&prometheus.PrometheusRuleConfig{
    Name:      "alerts",
    Namespace: "monitoring",
    Labels:    map[string]string{"role": "alert-rules"},
    Groups: []monitoringv1.RuleGroup{
        {
            Name: "my-app.rules",
            Rules: []monitoringv1.Rule{
                {
                    Alert: "HighErrorRate",
                    Expr:  intstr.FromString(`rate(http_requests_total{code=~"5.."}[5m]) > 0.1`),
                    For:   (*monitoringv1.Duration)(ptr("5m")),
                },
            },
        },
    },
})
FieldTypeDescription
NamestringResource name
NamespacestringResource namespace
Groups[]monitoringv1.RuleGroupRule groups containing alert and recording rules
Labelsmap[string]stringAdditional labels for the resource

Modifier Functions

Update existing resources after construction:

// ServiceMonitor modifiers
prometheus.AddServiceMonitorEndpoint(sm, monitoringv1.Endpoint{Path: "/metrics", Port: "http"})
sm.Spec.JobLabel = "app"
prometheus.AddServiceMonitorTargetLabel(sm, "version")
sm.Spec.NamespaceSelector = monitoringv1.NamespaceSelector{Any: true}
prometheus.SetServiceMonitorSampleLimit(sm, 10000)

// PodMonitor modifiers
prometheus.AddPodMonitorEndpoint(pm, monitoringv1.PodMetricsEndpoint{Path: "/metrics", Port: "http"})
pm.Spec.JobLabel = "app"
prometheus.AddPodMonitorPodTargetLabel(pm, "version")
pm.Spec.NamespaceSelector = monitoringv1.NamespaceSelector{Any: true}
prometheus.SetPodMonitorSampleLimit(pm, 10000)

// PrometheusRule modifiers
prometheus.AddPrometheusRuleGroup(rule, monitoringv1.RuleGroup{Name: "extra.rules"})