-
Notifications
You must be signed in to change notification settings - Fork 5
[feat] - Add MonitoringOverlapping #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| // 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 { | ||
| var monitorsListErrs []string | ||
|
|
||
| serviceMonitors, err := clientSets.MClient.MonitoringV1().ServiceMonitors(namespace).List(ctx, metav1.ListOptions{}) | ||
| if err != nil { | ||
| if errors.IsNotFound(err) { | ||
| monitorsListErrs = append(monitorsListErrs, fmt.Sprintf("No ServiceMonitors found in namespace %s", namespace)) | ||
| } | ||
| monitorsListErrs = append(monitorsListErrs, fmt.Sprintf("No ServiceMonitors found in namespace %s", namespace)) | ||
| } | ||
|
|
||
| podMonitors, err := clientSets.MClient.MonitoringV1().PodMonitors(namespace).List(ctx, metav1.ListOptions{}) | ||
| if err != nil { | ||
| if errors.IsNotFound(err) { | ||
| monitorsListErrs = append(monitorsListErrs, fmt.Sprintf("No PodMonitors found in namespace %s", namespace)) | ||
| } | ||
| monitorsListErrs = append(monitorsListErrs, fmt.Sprintf("No PodMonitors found in namespace %s", namespace)) | ||
| } | ||
|
|
||
| if len(monitorsListErrs) > 0 { | ||
| return fmt.Errorf("errors listing Pod/Service Monitors") | ||
| } | ||
|
|
||
| serviceOverlaps := make(map[string][]string) | ||
| podOverlaps := make(map[string][]string) | ||
| var overlapErrs []string | ||
|
|
||
| for _, servicemonitor := range serviceMonitors.Items { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, serviceOverlaps); err != nil { | ||
| overlapErrs = append(overlapErrs, err.Error()) | ||
| } | ||
| } | ||
| for _, podmonitor := range podMonitors.Items { | ||
| if err := checkOverlappingPodMonitors(ctx, clientSets, podmonitor, podOverlaps); err != nil { | ||
| overlapErrs = append(overlapErrs, err.Error()) | ||
| } | ||
| } | ||
|
|
||
| for key, svcMonitors := range serviceOverlaps { | ||
| if len(svcMonitors) > 1 { | ||
| overlapErrs = append(overlapErrs, fmt.Sprintf("Overlapping ServiceMonitors found for service/port %s: %v", key, svcMonitors)) | ||
| } | ||
| } | ||
|
|
||
| for key, pdMonitors := range podOverlaps { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we properly build the error message inside each for we don't nee to iterate it again to rebuild the error mesage, wdyt?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, mv the error inside the overlapping function, wdyt? |
||
| if len(pdMonitors) > 1 { | ||
| overlapErrs = append(overlapErrs, fmt.Sprintf("Overlapping PodMonitors found for pod/port %s: %v", key, pdMonitors)) | ||
| } | ||
| } | ||
|
|
||
| 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, serviceOverlaps map[string][]string) 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) | ||
| } | ||
|
|
||
| for _, service := range services.Items { | ||
| for _, scvPort := range service.Spec.Ports { | ||
| servicekey := fmt.Sprintf("%s/%s:%d", service.Namespace, service.Name, scvPort.Port) | ||
| serviceOverlaps[servicekey] = append(serviceOverlaps[servicekey], servicemonitor.Name) | ||
|
|
||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func checkOverlappingPodMonitors(ctx context.Context, clientSets *k8sutil.ClientSets, podmonitor *monitoringv1.PodMonitor, podOverlaps map[string][]string) 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) | ||
| } | ||
|
|
||
| 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) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
| 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) | ||
| } | ||
| }) | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.