diff --git a/internal/analyzers/prometheus.go b/internal/analyzers/prometheus.go index b7e723d..3dbd4c9 100644 --- a/internal/analyzers/prometheus.go +++ b/internal/analyzers/prometheus.go @@ -59,43 +59,35 @@ func RunPrometheusAnalyzer(ctx context.Context, clientSets *k8sutil.ClientSets, } } - validPodNS, err := checkPrometheusNamespaceSelectorsStatus(ctx, clientSets, prometheus.Spec.PodMonitorNamespaceSelector) - if err != nil { + if err := checkPrometheusNamespaceSelectors(ctx, clientSets, prometheus.Spec.PodMonitorNamespaceSelector); err != nil { return fmt.Errorf("PodMonitorNamespaceSelector is not properly defined: %s", err) } - validProbeNS, err := checkPrometheusNamespaceSelectorsStatus(ctx, clientSets, prometheus.Spec.ProbeNamespaceSelector) - if err != nil { + if err := checkPrometheusNamespaceSelectors(ctx, clientSets, prometheus.Spec.ProbeNamespaceSelector); err != nil { return fmt.Errorf("ProbeNamespaceSelector is not properly defined: %s", err) } - validServiceNS, err := checkPrometheusNamespaceSelectorsStatus(ctx, clientSets, prometheus.Spec.ServiceMonitorNamespaceSelector) - if err != nil { + if err := checkPrometheusNamespaceSelectors(ctx, clientSets, prometheus.Spec.ServiceMonitorNamespaceSelector); err != nil { return fmt.Errorf("ServiceMonitorNamespaceSelector is not properly defined: %s", err) } - validScrapeNS, err := checkPrometheusNamespaceSelectorsStatus(ctx, clientSets, prometheus.Spec.ScrapeConfigNamespaceSelector) - if err != nil { + if err := checkPrometheusNamespaceSelectors(ctx, clientSets, prometheus.Spec.ScrapeConfigNamespaceSelector); err != nil { return fmt.Errorf("ScrapeConfigNamespaceSelector is not properly defined: %s", err) } - if !validPodNS && !validProbeNS && !validServiceNS && !validScrapeNS { - fmt.Printf("Prometheus matches all namespaces\n") - } - - if err := checkPrometheusServiceSelectorsStatus(ctx, clientSets, prometheus.Spec.ServiceMonitorSelector, "ServiceMonitor", namespace); err != nil { + if err := checkPrometheusServiceSelectors(ctx, clientSets, prometheus.Spec.ServiceMonitorSelector, "ServiceMonitor", namespace); err != nil { return fmt.Errorf("ServiceMonitor is not properly defined: %s", err) } - if err := checkPrometheusServiceSelectorsStatus(ctx, clientSets, prometheus.Spec.PodMonitorSelector, "PodMonitor", namespace); err != nil { + if err := checkPrometheusServiceSelectors(ctx, clientSets, prometheus.Spec.PodMonitorSelector, "PodMonitor", namespace); err != nil { return fmt.Errorf("PodMonitor is not properly defined: %s", err) } - if err := checkPrometheusServiceSelectorsStatus(ctx, clientSets, prometheus.Spec.ProbeSelector, "Probe", namespace); err != nil { + if err := checkPrometheusServiceSelectors(ctx, clientSets, prometheus.Spec.ProbeSelector, "Probe", namespace); err != nil { return fmt.Errorf("Probe is not properly defined: %s", err) } - if err := checkPrometheusServiceSelectorsStatus(ctx, clientSets, prometheus.Spec.ScrapeConfigSelector, "ScrapeConfig", namespace); err != nil { + if err := checkPrometheusServiceSelectors(ctx, clientSets, prometheus.Spec.ScrapeConfigSelector, "ScrapeConfig", namespace); err != nil { return fmt.Errorf("ScrapeConfig is not properly defined: %s", err) } @@ -163,50 +155,30 @@ func checkClusterRoleRules(crb v1.ClusterRoleBinding, cr *v1.ClusterRole) error return nil } -func checkPrometheusNamespaceSelectorsStatus(ctx context.Context, clientSets *k8sutil.ClientSets, labelSelector *metav1.LabelSelector) (bool, error) { +func checkPrometheusNamespaceSelectors(ctx context.Context, clientSets *k8sutil.ClientSets, labelSelector *metav1.LabelSelector) error { if labelSelector == nil { - return true, nil + return nil } if len(labelSelector.MatchLabels) == 0 && len(labelSelector.MatchExpressions) == 0 { - _, err := clientSets.KClient.CoreV1().Namespaces().List(ctx, metav1.ListOptions{}) - if err != nil { - return false, fmt.Errorf("Failed to list Namespaces: %v", err) - } - return false, nil + return nil } labelMap, err := metav1.LabelSelectorAsMap(labelSelector) if err != nil { - return false, fmt.Errorf("Invalid label selector format: %v", err) + return fmt.Errorf("Invalid label selector format: %v", err) } - nsSelector := labels.SelectorFromSet(labelMap) - - options := metav1.ListOptions{ - LabelSelector: nsSelector.String(), - } - - namespaces, err := clientSets.KClient.CoreV1().Namespaces().List(ctx, options) - if err != nil { - return false, fmt.Errorf("Failed to list namespaces: %v", err) - } + namespaces, err := clientSets.KClient.CoreV1().Namespaces().List(ctx, metav1.ListOptions{LabelSelector: labels.SelectorFromSet(labelMap).String(),}) if len(namespaces.Items) == 0 { - return false, fmt.Errorf("No Namespaces match the provided selector") - } - - matchedNamespaces := []string{} - - for _, ns := range namespaces.Items { - matchedNamespaces = append(matchedNamespaces, ns.Name) + return fmt.Errorf("No Namespaces match the provided selector") } - fmt.Printf("Matched namespaces: %v\n", matchedNamespaces) - return true, nil + return nil } -func checkPrometheusServiceSelectorsStatus(ctx context.Context, clientSets *k8sutil.ClientSets, labelSelector *metav1.LabelSelector, selectorName, namespace string) error { +func checkPrometheusServiceSelectors(ctx context.Context, clientSets *k8sutil.ClientSets, labelSelector *metav1.LabelSelector, selectorName, namespace string) error { if labelSelector == nil { return fmt.Errorf("%s selector is not defined", selectorName) } @@ -219,16 +191,11 @@ func checkPrometheusServiceSelectorsStatus(ctx context.Context, clientSets *k8su if err != nil { return fmt.Errorf("Invalid label selector format in %s: %v", selectorName, err) } - nsSelector := labels.SelectorFromSet(labelMap) - - options := metav1.ListOptions{ - LabelSelector: nsSelector.String(), - } var matchedNames []string switch selectorName { case "ServiceMonitor": - serviceMonitors, err := clientSets.MClient.MonitoringV1().ServiceMonitors(namespace).List(ctx, options) + serviceMonitors, err := clientSets.MClient.MonitoringV1().ServiceMonitors(namespace).List(ctx, metav1.ListOptions{LabelSelector: labels.SelectorFromSet(labelMap).String(),}) if err != nil { return fmt.Errorf("Failed to list ServiceMonitors in %s: %v", namespace, err) } @@ -236,7 +203,7 @@ func checkPrometheusServiceSelectorsStatus(ctx context.Context, clientSets *k8su matchedNames = append(matchedNames, serviceMonitor.Name) } case "PodMonitor": - podMonitors, err := clientSets.MClient.MonitoringV1().PodMonitors(namespace).List(ctx, options) + podMonitors, err := clientSets.MClient.MonitoringV1().PodMonitors(namespace).List(ctx, metav1.ListOptions{LabelSelector: labels.SelectorFromSet(labelMap).String(),}) if err != nil { return fmt.Errorf("Failed to list PodMonitor in %s: %v", namespace, err) } @@ -244,7 +211,7 @@ func checkPrometheusServiceSelectorsStatus(ctx context.Context, clientSets *k8su matchedNames = append(matchedNames, podMonitor.Name) } case "Probe": - probes, err := clientSets.MClient.MonitoringV1().Probes(namespace).List(ctx, options) + probes, err := clientSets.MClient.MonitoringV1().Probes(namespace).List(ctx, metav1.ListOptions{LabelSelector: labels.SelectorFromSet(labelMap).String(),}) if err != nil { return fmt.Errorf("Failed to list Probes in %s: %v", namespace, err) } @@ -252,7 +219,7 @@ func checkPrometheusServiceSelectorsStatus(ctx context.Context, clientSets *k8su matchedNames = append(matchedNames, probe.Name) } case "ScrapeConfig": - scrapeConfigs, err := clientSets.MClient.MonitoringV1alpha1().ScrapeConfigs(namespace).List(ctx, options) + scrapeConfigs, err := clientSets.MClient.MonitoringV1alpha1().ScrapeConfigs(namespace).List(ctx, metav1.ListOptions{LabelSelector: labels.SelectorFromSet(labelMap).String(),}) if err != nil { return fmt.Errorf("Failed to list ScrapeConfigs in %s: %v", namespace, err) }