Skip to content

Commit

Permalink
refactor: default podmonitoring with CRDs
Browse files Browse the repository at this point in the history
  • Loading branch information
bernot-dev committed Jan 17, 2025
1 parent 1b2cbb5 commit 5de2c50
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 290 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,13 @@ spec:
type: object
x-kubernetes-map-type: atomic
targetLabels:
default:
metadata:
- namespace
- pod
- container
- top_level_controller_name
- top_level_controller_type
description: |-
Labels to add to the Prometheus target for discovered endpoints.
The `instance` label is always set to `<pod_name>:<port>` or `<node_name>:<port>`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,12 @@ spec:
type: object
x-kubernetes-map-type: atomic
targetLabels:
default:
metadata:
- pod
- container
- top_level_controller_name
- top_level_controller_type
description: |-
Labels to add to the Prometheus target for discovered endpoints.
The `instance` label is always set to `<pod_name>:<port>` or `<node_name>:<port>`
Expand Down
44 changes: 0 additions & 44 deletions charts/operator/templates/mutatingwebhookconfiguration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,50 +22,6 @@ metadata:
{{- include "prometheus-engine.labels" . | nindent 4 }}
{{- end }}
webhooks:
- name: default.podmonitorings.gmp-operator.gmp-system.monitoring.googleapis.com
admissionReviewVersions:
- v1
clientConfig:
# caBundle populated by operator.
service:
name: gmp-operator
namespace: {{.Values.namespace.system}}
port: 443
path: /default/monitoring.googleapis.com/v1/podmonitorings
failurePolicy: Fail
rules:
- resources:
- podmonitorings
apiGroups:
- monitoring.googleapis.com
apiVersions:
- v1
operations:
- CREATE
- UPDATE
sideEffects: None
- name: default.clusterpodmonitorings.gmp-operator.gmp-system.monitoring.googleapis.com
admissionReviewVersions:
- v1
clientConfig:
# caBundle populated by operator.
service:
name: gmp-operator
namespace: {{.Values.namespace.system}}
port: 443
path: /default/monitoring.googleapis.com/v1/clusterpodmonitorings
failurePolicy: Fail
rules:
- resources:
- clusterpodmonitorings
apiGroups:
- monitoring.googleapis.com
apiVersions:
- v1
operations:
- CREATE
- UPDATE
sideEffects: None
- name: default.operatorconfigs.gmp-operator.gmp-system.monitoring.googleapis.com
admissionReviewVersions:
- v1
Expand Down
44 changes: 0 additions & 44 deletions manifests/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -964,50 +964,6 @@ kind: MutatingWebhookConfiguration
metadata:
name: gmp-operator.gmp-system.monitoring.googleapis.com
webhooks:
- name: default.podmonitorings.gmp-operator.gmp-system.monitoring.googleapis.com
admissionReviewVersions:
- v1
clientConfig:
# caBundle populated by operator.
service:
name: gmp-operator
namespace: gmp-system
port: 443
path: /default/monitoring.googleapis.com/v1/podmonitorings
failurePolicy: Fail
rules:
- resources:
- podmonitorings
apiGroups:
- monitoring.googleapis.com
apiVersions:
- v1
operations:
- CREATE
- UPDATE
sideEffects: None
- name: default.clusterpodmonitorings.gmp-operator.gmp-system.monitoring.googleapis.com
admissionReviewVersions:
- v1
clientConfig:
# caBundle populated by operator.
service:
name: gmp-operator
namespace: gmp-system
port: 443
path: /default/monitoring.googleapis.com/v1/clusterpodmonitorings
failurePolicy: Fail
rules:
- resources:
- clusterpodmonitorings
apiGroups:
- monitoring.googleapis.com
apiVersions:
- v1
operations:
- CREATE
- UPDATE
sideEffects: None
- name: default.operatorconfigs.gmp-operator.gmp-system.monitoring.googleapis.com
admissionReviewVersions:
- v1
Expand Down
13 changes: 13 additions & 0 deletions manifests/setup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,13 @@ spec:
type: object
x-kubernetes-map-type: atomic
targetLabels:
default:
metadata:
- namespace
- pod
- container
- top_level_controller_name
- top_level_controller_type
description: |-
Labels to add to the Prometheus target for discovered endpoints.
The `instance` label is always set to `<pod_name>:<port>` or `<node_name>:<port>`
Expand Down Expand Up @@ -2952,6 +2959,12 @@ spec:
type: object
x-kubernetes-map-type: atomic
targetLabels:
default:
metadata:
- pod
- container
- top_level_controller_name
- top_level_controller_type
description: |-
Labels to add to the Prometheus target for discovered endpoints.
The `instance` label is always set to `<pod_name>:<port>` or `<node_name>:<port>`
Expand Down
32 changes: 2 additions & 30 deletions pkg/operator/apis/monitoring/v1/pod_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,6 @@ func (p *PodMonitoring) ValidateDelete() (admission.Warnings, error) {
return nil, nil
}

// Default implements admission.Defaulter.
func (p *PodMonitoring) Default() {
_ = p.UpdateDefault()
}

// UpdateDefault defaults any unset fields, returning true if the object was updated.
func (p *PodMonitoring) UpdateDefault() bool {
if p.Spec.TargetLabels.Metadata == nil {
md := []string{"pod", "container", "top_level_controller_name", "top_level_controller_type"}
p.Spec.TargetLabels.Metadata = &md
return true
}
return false
}

// PodMonitoringList is a list of PodMonitorings.
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type PodMonitoringList struct {
Expand Down Expand Up @@ -186,21 +171,6 @@ func (*ClusterPodMonitoring) ValidateDelete() (admission.Warnings, error) {
return nil, nil
}

// Default implements admission.Defaulter.
func (c *ClusterPodMonitoring) Default() {
_ = c.UpdateDefault()
}

// UpdateDefault defaults any unset fields, returning true if the object was updated.
func (c *ClusterPodMonitoring) UpdateDefault() bool {
if c.Spec.TargetLabels.Metadata == nil {
md := []string{"namespace", "pod", "container", "top_level_controller_name", "top_level_controller_type"}
c.Spec.TargetLabels.Metadata = &md
return true
}
return false
}

// ClusterPodMonitoringList is a list of ClusterPodMonitorings.
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type ClusterPodMonitoringList struct {
Expand All @@ -219,6 +189,7 @@ type PodMonitoringSpec struct {
// Labels to add to the Prometheus target for discovered endpoints.
// The `instance` label is always set to `<pod_name>:<port>` or `<node_name>:<port>`
// if the scraped pod is controlled by a DaemonSet.
//+kubebuilder:default={metadata:{pod,container,top_level_controller_name,top_level_controller_type}}
TargetLabels TargetLabels `json:"targetLabels,omitempty"`
// Limits to apply at scrape time.
Limits *ScrapeLimits `json:"limits,omitempty"`
Expand Down Expand Up @@ -254,6 +225,7 @@ type ClusterPodMonitoringSpec struct {
// Labels to add to the Prometheus target for discovered endpoints.
// The `instance` label is always set to `<pod_name>:<port>` or `<node_name>:<port>`
// if the scraped pod is controlled by a DaemonSet.
//+kubebuilder:default={metadata:{namespace,pod,container,top_level_controller_name,top_level_controller_type}}
TargetLabels TargetLabels `json:"targetLabels,omitempty"`
// Limits to apply at scrape time.
Limits *ScrapeLimits `json:"limits,omitempty"`
Expand Down
10 changes: 2 additions & 8 deletions pkg/operator/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,6 @@ func (r *collectionReconciler) makeCollectorConfig(ctx context.Context, spec *mo

// Mark status updates in batch with single timestamp.
for _, pmon := range podMons.Items {
updateSpec := pmon.UpdateDefault()

cond := &monitoringv1.MonitoringCondition{
Type: monitoringv1.ConfigurationCreateSuccess,
Status: corev1.ConditionTrue,
Expand All @@ -421,10 +419,9 @@ func (r *collectionReconciler) makeCollectorConfig(ctx context.Context, spec *mo
}

updateStatus := pmon.Status.SetMonitoringCondition(pmon.GetGeneration(), metav1.Now(), cond)
if updateSpec || updateStatus {
if updateStatus {
updates = append(updates, update{
object: &pmon,
spec: updateSpec,
status: updateStatus,
})
}
Expand All @@ -436,8 +433,6 @@ func (r *collectionReconciler) makeCollectorConfig(ctx context.Context, spec *mo

// Mark status updates in batch with single timestamp.
for _, cmon := range clusterPodMons.Items {
updateSpec := cmon.UpdateDefault()

cond := &monitoringv1.MonitoringCondition{
Type: monitoringv1.ConfigurationCreateSuccess,
Status: corev1.ConditionTrue,
Expand All @@ -457,10 +452,9 @@ func (r *collectionReconciler) makeCollectorConfig(ctx context.Context, spec *mo
}

updateStatus := cmon.Status.SetMonitoringCondition(cmon.GetGeneration(), metav1.Now(), cond)
if updateSpec || updateStatus {
if updateStatus {
updates = append(updates, update{
object: &cmon,
spec: updateSpec,
status: updateStatus,
})
}
Expand Down
Loading

0 comments on commit 5de2c50

Please sign in to comment.