Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
Prometheus AnalyzeKind = "prometheus"
Alertmanager AnalyzeKind = "alertmanager"
PrometheusAgent AnalyzeKind = "prometheusagent"
Overlapping AnalyzeKind = "overlapping"
)

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 Overlapping:
return analyzers.RunOverlappingAnalyzer(cmd.Context(), clientSets, analyzerFlags.Name, analyzerFlags.Namespace)
default:
return fmt.Errorf("kind %s not supported", analyzerFlags.Kind)
}
Expand Down
125 changes: 125 additions & 0 deletions internal/analyzers/overlapping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// 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"
"strings"

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

func RunOverlappingAnalyzer(ctx context.Context, clientSets *k8sutil.ClientSets, _, namespace string) error {
serviceMonitors, err := clientSets.MClient.MonitoringV1().ServiceMonitors(namespace).List(ctx, metav1.ListOptions{})
if err != nil && !errors.IsNotFound(err) {
return err
}

podMonitors, err := clientSets.MClient.MonitoringV1().PodMonitors(namespace).List(ctx, metav1.ListOptions{})
if err != nil && !errors.IsNotFound(err) {
return err
}

if (serviceMonitors == nil || len(serviceMonitors.Items) == 0) && (podMonitors == nil || len(podMonitors.Items) == 0) {
return nil
}

var overlapErrs []string

for _, servicemonitor := range serviceMonitors.Items {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's required for a first version, but would be nice if we can also compare pod monitors with service monitors.

Imagine a use case where you deploy one pod monitor and one service monitor to collect metrics from the same service.

if err := checkOverlappingServiceMonitors(ctx, clientSets, servicemonitor); err != nil {
overlapErrs = append(overlapErrs, err.Error())
}
}
for _, podmonitor := range podMonitors.Items {
if err := checkOverlappingPodMonitors(ctx, clientSets, podmonitor); err != nil {
overlapErrs = append(overlapErrs, err.Error())
}
}

if len(overlapErrs) > 0 {
return fmt.Errorf("multiple issues found:\n%s", strings.Join(overlapErrs, "\n"))
}

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

func checkOverlappingServiceMonitors(ctx context.Context, clientSets *k8sutil.ClientSets, servicemonitor *monitoringv1.ServiceMonitor) error {
selector, err := metav1.LabelSelectorAsSelector(&servicemonitor.Spec.Selector)
if err != nil {
return fmt.Errorf("invalid selector in ServiceMonitor %s/%s: %v", servicemonitor.Namespace, servicemonitor.Name, err)
}

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

serviceOverlaps := make(map[string][]string)
var overlapErrs []string
for _, service := range services.Items {
for _, svcPort := range service.Spec.Ports {
serviceKey := fmt.Sprintf("%s/%s:%d", service.Namespace, service.Name, svcPort.Port)
serviceOverlaps[serviceKey] = append(serviceOverlaps[serviceKey], servicemonitor.Name)

if len(serviceOverlaps[serviceKey]) > 1 {
overlapErrs = append(overlapErrs, fmt.Sprintf("Overlapping ServiceMonitors found for service/port %s: %v", serviceKey, serviceOverlaps[serviceKey]))
}
}
}

if len(overlapErrs) > 0 {
return fmt.Errorf("%s", strings.Join(overlapErrs, "\n"))
}

return nil
}

func checkOverlappingPodMonitors(ctx context.Context, clientSets *k8sutil.ClientSets, podmonitor *monitoringv1.PodMonitor) error {
selector, err := metav1.LabelSelectorAsSelector(&podmonitor.Spec.Selector)
if err != nil {
return fmt.Errorf("invalid selector in PodMonitor %s/%s: %v", podmonitor.Namespace, podmonitor.Name, err)
}

pods, err := clientSets.KClient.CoreV1().Pods(podmonitor.Namespace).List(ctx, metav1.ListOptions{LabelSelector: selector.String()})
if err != nil {
return fmt.Errorf("error listing pods for PodMonitor %s/%s: %v", podmonitor.Namespace, podmonitor.Name, err)
}

podOverlaps := make(map[string][]string)
var overlapErrs []string
for _, pod := range pods.Items {
for _, podPort := range podmonitor.Spec.PodMetricsEndpoints {
podKey := fmt.Sprintf("%s/%s:%s", pod.Namespace, pod.Name, podPort.Port)
podOverlaps[podKey] = append(podOverlaps[podKey], podmonitor.Name)

if len(podOverlaps[podKey]) > 1 {
overlapErrs = append(overlapErrs, fmt.Sprintf("Overlapping ServiceMonitors found for service/port %s: %v", podKey, podOverlaps[podKey]))
}
}
}

if len(overlapErrs) > 0 {
return fmt.Errorf("%s", strings.Join(overlapErrs, "\n"))
}

return nil
}
230 changes: 230 additions & 0 deletions internal/analyzers/overlapping_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
// 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.
// 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"
"testing"

"github.com/prometheus-operator/poctl/internal/k8sutil"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
monitoringclient "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned/fake"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/fake"
clienttesting "k8s.io/client-go/testing"
)

func TestOverlappingAnalyzer(t *testing.T) {
type testCase struct {
name string
namespace string
getMockedClientSets func(tc testCase) k8sutil.ClientSets
shouldFail bool
}
tests := []testCase{
{
name: "ErrorListingServiceMonitor",
namespace: "test",
shouldFail: true,
getMockedClientSets: func(tc testCase) k8sutil.ClientSets {
mClient := monitoringclient.NewSimpleClientset(&monitoringv1.ServiceMonitorList{})
mClient.PrependReactor("list", "servicemonitors", func(_ clienttesting.Action) (bool, runtime.Object, error) {
return true, nil, errors.NewNotFound(monitoringv1.Resource("servicemonitors"), tc.name)
})

return k8sutil.ClientSets{
MClient: mClient,
}
},
},
{
name: "ErrorListingPodMonitor",
namespace: "test",
shouldFail: true,
getMockedClientSets: func(tc testCase) k8sutil.ClientSets {
mClient := monitoringclient.NewSimpleClientset(&monitoringv1.PodMonitorList{})
mClient.PrependReactor("list", "podmonitors", func(_ clienttesting.Action) (bool, runtime.Object, error) {
return true, nil, errors.NewNotFound(monitoringv1.Resource("podmonitors"), tc.name)
})

return k8sutil.ClientSets{
MClient: mClient,
}
},
},
{
name: "OverlapingPodMonitor",
namespace: "test",
shouldFail: true,
getMockedClientSets: func(tc testCase) k8sutil.ClientSets {
mClient := monitoringclient.NewSimpleClientset(&monitoringv1.PodMonitorList{})
mClient.PrependReactor("list", "podmonitors", func(_ clienttesting.Action) (bool, runtime.Object, error) {
return true, &monitoringv1.PodMonitorList{
Items: []*monitoringv1.PodMonitor{
{
ObjectMeta: metav1.ObjectMeta{
Name: "podmonitor-1",
Namespace: tc.namespace,
},
Spec: monitoringv1.PodMonitorSpec{
Selector: metav1.LabelSelector{
MatchLabels: map[string]string{
"app": "overlapping-app",
},
},
PodMetricsEndpoints: []monitoringv1.PodMetricsEndpoint{
{Port: "http-metrics"},
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "podmonitor-2",
Namespace: tc.namespace,
},
Spec: monitoringv1.PodMonitorSpec{
Selector: metav1.LabelSelector{
MatchLabels: map[string]string{
"app": "overlapping-app",
},
},
PodMetricsEndpoints: []monitoringv1.PodMetricsEndpoint{
{Port: "http-metrics"},
},
},
},
},
}, nil
})

kClient := fake.NewSimpleClientset(&corev1.PodList{})
kClient.PrependReactor("list", "pods", func(_ clienttesting.Action) (bool, runtime.Object, error) {
return true, &corev1.PodList{
Items: []corev1.Pod{
{
ObjectMeta: metav1.ObjectMeta{
Name: "overlapping-pod",
Namespace: "test",
Labels: map[string]string{
"app": "overlapping-app",
},
},
},
},
}, nil
})

return k8sutil.ClientSets{
MClient: mClient,
KClient: kClient,
}
},
},
{
name: "OverlapingServiceMonitor",
namespace: "test",
shouldFail: true,
getMockedClientSets: func(tc testCase) k8sutil.ClientSets {
mClient := monitoringclient.NewSimpleClientset(&monitoringv1.ServiceMonitorList{})
mClient.PrependReactor("list", "servicemonitors", func(_ clienttesting.Action) (bool, runtime.Object, error) {
return true, &monitoringv1.ServiceMonitorList{
Items: []*monitoringv1.ServiceMonitor{
{
ObjectMeta: metav1.ObjectMeta{
Name: "servicemonitor-1",
Namespace: tc.namespace,
},
Spec: monitoringv1.ServiceMonitorSpec{
Selector: metav1.LabelSelector{
MatchLabels: map[string]string{
"app": "overlapping-app",
},
},
Endpoints: []monitoringv1.Endpoint{
{Port: "http-metrics"},
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "servicemonitor-2",
Namespace: tc.namespace,
},
Spec: monitoringv1.ServiceMonitorSpec{
Selector: metav1.LabelSelector{
MatchLabels: map[string]string{
"app": "overlapping-app",
},
},
Endpoints: []monitoringv1.Endpoint{
{Port: "http-metrics"},
},
},
},
},
}, nil
})

kClient := fake.NewSimpleClientset(&corev1.PodList{})
kClient.PrependReactor("list", "services", func(_ clienttesting.Action) (bool, runtime.Object, error) {
return true, &corev1.ServiceList{
Items: []corev1.Service{
{
ObjectMeta: metav1.ObjectMeta{
Name: "overlapping-pod",
Namespace: "test",
Labels: map[string]string{
"app": "overlapping-app",
},
},
},
},
}, nil
})

return k8sutil.ClientSets{
MClient: mClient,
KClient: kClient,
}
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
clientSets := tc.getMockedClientSets(tc)
err := RunOverlappingAnalyzer(context.Background(), &clientSets, tc.name, tc.namespace)
if tc.shouldFail {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}
Loading