Skip to content

Commit

Permalink
Use default label for servicemonitor and prom rules (#146)
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha authored Oct 1, 2024
1 parent 7275dd9 commit 2e44e54
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ require (
k8s.io/klog/v2 v2.130.1
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0
kmodules.xyz/authorizer v0.29.1
kmodules.xyz/client-go v0.30.17
kmodules.xyz/client-go v0.30.18
kmodules.xyz/custom-resources v0.30.0
kmodules.xyz/monitoring-agent-api v0.30.1
moul.io/http2curl/v2 v2.3.1-0.20221024080105-10c404f653f7
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -774,8 +774,8 @@ kmodules.xyz/apiversion v0.2.0 h1:vAQYqZFm4xu4pbB1cAdHbFEPES6EQkcR4wc06xdTOWk=
kmodules.xyz/apiversion v0.2.0/go.mod h1:oPX8g8LvlPdPX3Yc5YvCzJHQnw3YF/X4/jdW0b1am80=
kmodules.xyz/authorizer v0.29.1 h1:uByGGoryKbZcfiEAhjcK/Y345I9mygNQP7DVpkMbNQQ=
kmodules.xyz/authorizer v0.29.1/go.mod h1:kZRhclL8twzyt2bQuJQJbpYww2sc+qFr8I5PPoq/sWY=
kmodules.xyz/client-go v0.30.17 h1:Ec22XIkAsN8Fwd001P0EzRTsfeibIFkzkZ6oYjLe5LQ=
kmodules.xyz/client-go v0.30.17/go.mod h1:CAu+JlA8RVGtj6LQHu0Q1w2mnFUajuti49c7T1AvGdM=
kmodules.xyz/client-go v0.30.18 h1:Ik8tyJx8AUWcaJ4+7LsOI+zGbYNlN7MCtWHybOermiE=
kmodules.xyz/client-go v0.30.18/go.mod h1:CAu+JlA8RVGtj6LQHu0Q1w2mnFUajuti49c7T1AvGdM=
kmodules.xyz/crd-schema-fuzz v0.29.1 h1:zJTlWYOrT5dsVVHW8HGcnR/vaWfxQfNh11QwTtkYpcs=
kmodules.xyz/crd-schema-fuzz v0.29.1/go.mod h1:n708z9YQqLMP2KNLQVgBcRJw1QpSWLvpNCEi+KJDOYE=
kmodules.xyz/custom-resources v0.30.0 h1:vR3CbseHMLwR4GvtcJJuRuwIV8voKqFqNii27rMcm1o=
Expand Down
35 changes: 28 additions & 7 deletions pkg/controllers/prometheus/prometheus_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,18 @@ const (
appBindingGrafana = "default-grafana"
)

var selfNamespace = meta_util.PodNamespace()

var defaultPresetsLabels = map[string]string{
"charts.x-helm.dev/is-default-preset": "true",
}
var (
selfNamespace = meta_util.PodNamespace()
defaultPresetsLabels = map[string]string{
"charts.x-helm.dev/is-default-preset": "true",
}
defaultPrometheusStackLabels = map[string]string{
"release": "kube-prometheus-stack",
}
defaultRancherMonitoringLabels = map[string]string{
"release": "rancher-monitoring",
}
)

// PrometheusReconciler reconciles a Prometheus object
type PrometheusReconciler struct {
Expand Down Expand Up @@ -414,7 +421,7 @@ func (r *PrometheusReconciler) SetupClusterForPrometheus(ctx context.Context, pr
}

func (r *PrometheusReconciler) CreatePreset(p *monitoringv1.Prometheus, isDefault bool) error {
presets := r.GeneratePresetForPrometheus(*p)
presets := r.GeneratePresetForPrometheus(*p, isDefault)
presetBytes, err := json.Marshal(presets)
if err != nil {
return err
Expand Down Expand Up @@ -547,21 +554,35 @@ func (r *PrometheusReconciler) CreateProjectPreset(p *monitoringv1.Prometheus, p
return nil
}

func (r *PrometheusReconciler) GeneratePresetForPrometheus(p monitoringv1.Prometheus) mona.MonitoringPresets {
func (r *PrometheusReconciler) GeneratePresetForPrometheus(p monitoringv1.Prometheus, isDefault bool) mona.MonitoringPresets {
var preset mona.MonitoringPresets

preset.Spec.Monitoring.Agent = string(mona.AgentPrometheusOperator)
svcmonLabels, ok := meta_util.LabelsForLabelSelector(p.Spec.ServiceMonitorSelector)
if !ok {
klog.Warningf("Prometheus %s/%s uses match expressions in ServiceMonitorSelector", p.Namespace, p.Name)
}
if len(svcmonLabels) == 0 {
if isDefault && r.d.RancherManaged() {
svcmonLabels = defaultRancherMonitoringLabels
} else {
svcmonLabels = defaultPrometheusStackLabels
}
}
preset.Spec.Monitoring.ServiceMonitor.Labels = svcmonLabels

preset.Form.Alert.Enabled = mona.SeverityFlagCritical
ruleLabels, ok := meta_util.LabelsForLabelSelector(p.Spec.RuleSelector)
if !ok {
klog.Warningf("Prometheus %s/%s uses match expressions in RuleSelector", p.Namespace, p.Name)
}
if len(ruleLabels) == 0 {
if isDefault && r.d.RancherManaged() {
ruleLabels = defaultRancherMonitoringLabels
} else {
ruleLabels = defaultPrometheusStackLabels
}
}
preset.Form.Alert.Labels = ruleLabels

return preset
Expand Down
4 changes: 2 additions & 2 deletions vendor/kmodules.xyz/client-go/cluster/ocm.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func IsOpenClusterHub(mapper meta.RESTMapper) bool {

func IsOpenClusterSpoke(mapper meta.RESTMapper) bool {
if _, err := mapper.RESTMappings(schema.GroupKind{
Group: "work.open-cluster-management.io",
Kind: "AppliedManifestWork",
Group: "operator.open-cluster-management.io",
Kind: "Klusterlet",
}); err == nil {
return true
}
Expand Down
2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,7 @@ kmodules.xyz/authorizer/apiserver
kmodules.xyz/authorizer/rbac
kmodules.xyz/authorizer/rbac/helpers
kmodules.xyz/authorizer/rbac/validation
# kmodules.xyz/client-go v0.30.17
# kmodules.xyz/client-go v0.30.18
## explicit; go 1.22.0
kmodules.xyz/client-go
kmodules.xyz/client-go/api/v1
Expand Down

0 comments on commit 2e44e54

Please sign in to comment.