Skip to content

Commit

Permalink
feat: add additional labels for pod monitor in Cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
ma-ts committed May 5, 2024
1 parent ae692bf commit 1933afb
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
12 changes: 12 additions & 0 deletions api/v1/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2279,6 +2279,8 @@ type MonitoringConfiguration struct {
// +optional
EnablePodMonitor bool `json:"enablePodMonitor,omitempty"`

PodMonitorAdditionalLabels map[string]string `json:"podMonitorAdditionalLabels,omitempty"`

// The list of metric relabelings for the `PodMonitor`. Applied to samples before ingestion.
// +optional
PodMonitorMetricRelabelConfigs []*monitoringv1.RelabelConfig `json:"podMonitorMetricRelabelings,omitempty"`
Expand Down Expand Up @@ -3297,6 +3299,16 @@ func (cluster *Cluster) IsPodMonitorEnabled() bool {
return false
}

// GetPodMonitorAdditionalLabels returns the additional labels for the PodMonitor resource
func (cluster *Cluster) GetPodMonitorAdditionalLabels() map[string]string {
if cluster.Spec.Monitoring != nil {
if cluster.Spec.Monitoring.PodMonitorAdditionalLabels != nil {
return cluster.Spec.Monitoring.PodMonitorAdditionalLabels
}
}
return map[string]string{}
}

// GetEnableSuperuserAccess returns if the superuser access is enabled or not
func (cluster *Cluster) GetEnableSuperuserAccess() bool {
if cluster.Spec.EnableSuperuserAccess != nil {
Expand Down
7 changes: 7 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.

4 changes: 4 additions & 0 deletions config/crd/bases/postgresql.cnpg.io_clusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3017,6 +3017,10 @@ spec:
default: false
description: Enable or disable the `PodMonitor`
type: boolean
podMonitorAdditionalLabels:
additionalProperties:
type: string
type: object
podMonitorMetricRelabelings:
description: The list of metric relabelings for the `PodMonitor`.
Applied to samples before ingestion.
Expand Down
4 changes: 4 additions & 0 deletions pkg/specs/podmonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package specs

import (
"maps"

monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Expand All @@ -41,6 +43,8 @@ func (c ClusterPodMonitorManager) BuildPodMonitor() *monitoringv1.PodMonitor {
}
c.cluster.SetInheritedDataAndOwnership(&meta)

maps.Copy(meta.Labels, c.cluster.GetPodMonitorAdditionalLabels())

endpoint := monitoringv1.PodMetricsEndpoint{
Port: "metrics",
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/specs/podmonitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var _ = Describe("PodMonitor test", func() {
},
}

additionalPodMonitorLabels := map[string]string{"testlabel": "value"}
expectedEndpoint := monitoringv1.PodMetricsEndpoint{Port: "metrics"}
metricRelabelings := []*monitoringv1.RelabelConfig{
{
Expand Down Expand Up @@ -104,6 +105,14 @@ var _ = Describe("PodMonitor test", func() {
expectedEndpoint.RelabelConfigs = relabelings
Expect(monitor.Spec.PodMetricsEndpoints).To(ContainElement(*expectedEndpoint))
})
It("Should add the necessary labels to the monitoringv1.PodMonitoring object if they are set", func() {
relabeledCluster := cluster.DeepCopy()
relabeledCluster.Spec.Monitoring.PodMonitorAdditionalLabels = additionalPodMonitorLabels
mgr := NewClusterPodMonitorManager(relabeledCluster)
monitor := mgr.BuildPodMonitor()

Expect(monitor.Labels).To(ContainElement(additionalPodMonitorLabels["testlabel"]))
})

It("does not panic if monitoring section is not present", func() {
cluster := apiv1.Cluster{}
Expand Down

0 comments on commit 1933afb

Please sign in to comment.