Skip to content

Commit

Permalink
[feat] - Add MonitorsOverlapping
Browse files Browse the repository at this point in the history
Signed-off-by: Hélia Barroso <[email protected]>
  • Loading branch information
heliapb committed Dec 31, 2024
1 parent 07e44b9 commit c13e89d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
13 changes: 8 additions & 5 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ import (
type AnalyzeKind string

const (
ServiceMonitor AnalyzeKind = "servicemonitor"
Operator AnalyzeKind = "operator"
Prometheus AnalyzeKind = "prometheus"
Alertmanager AnalyzeKind = "alertmanager"
PrometheusAgent AnalyzeKind = "prometheusagent"
ServiceMonitor AnalyzeKind = "servicemonitor"
Operator AnalyzeKind = "operator"
Prometheus AnalyzeKind = "prometheus"
Alertmanager AnalyzeKind = "alertmanager"
PrometheusAgent AnalyzeKind = "prometheusagent"
MonitoringOverlapping AnalyzeKind = "monitoroverlapping"
)

type AnalyzeFlags struct {
Expand Down Expand Up @@ -87,6 +88,8 @@ func run(cmd *cobra.Command, _ []string) error {
return analyzers.RunAlertmanagerAnalyzer(cmd.Context(), clientSets, analyzerFlags.Name, analyzerFlags.Namespace)
case PrometheusAgent:
return analyzers.RunPrometheusAgentAnalyzer(cmd.Context(), clientSets, analyzerFlags.Name, analyzerFlags.Namespace)
case MonitoringOverlapping:
return analyzers.RunMonitoringOverlappingAnalyzer(cmd.Context(), clientSets, analyzerFlags.Name, analyzerFlags.Namespace)
default:
return fmt.Errorf("kind %s not supported", analyzerFlags.Kind)
}
Expand Down
67 changes: 67 additions & 0 deletions internal/analyzers/monitoroverlapping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2024 The prometheus-operator Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package analyzers

import (
"context"
"fmt"
"log/slog"

"github.com/prometheus-operator/poctl/internal/k8sutil"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func RunMonitoringOverlappingAnalyzer(ctx context.Context, clientSets *k8sutil.ClientSets, name, namespace string) error {
// list all servicemonitors
serviceMonitors, err := clientSets.MClient.MonitoringV1().ServiceMonitors(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
if errors.IsNotFound(err) {
return fmt.Errorf("ServiceMonitors %s not found in namespace %s", name, namespace)
}
return fmt.Errorf("error while listing all ServiceMonitors: %v", err)
}

overlappingMonitors := make(map[string][]string)

for _, servicemonitor := range serviceMonitors.Items {
selector, err := metav1.LabelSelectorAsSelector(&servicemonitor.Spec.Selector)
if err != nil {
return fmt.Errorf("invalid selector in ServiceMonitor %s,%s: %v", namespace, servicemonitor.Name, err)
}

services, err := clientSets.KClient.CoreV1().Services(namespace).List(ctx, metav1.ListOptions{LabelSelector: selector.String()})
if err != nil {
if errors.IsNotFound(err) {
return fmt.Errorf("service %s not found in namespace %s", name, namespace)
}
return fmt.Errorf("error listing services for ServiceMonitor %s/%s: %v", namespace, servicemonitor.Name, err)
}

for _, service := range services.Items {
key := fmt.Sprintf("%s/%s", service.Namespace, service.Name)
overlappingMonitors[key] = append(overlappingMonitors[key], servicemonitor.Name)
}
}

for services, monitors := range overlappingMonitors {
if len(monitors) > 1 {
return fmt.Errorf("service %s is selected by multiple ServiceMonitors: %v", services, monitors)
}
}

slog.Info("no overlapping monitoring confing found in", "namespace", namespace)
return nil
}

0 comments on commit c13e89d

Please sign in to comment.