Skip to content

Commit

Permalink
[fix] reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
heliapb committed Oct 28, 2024
1 parent 0b3f5e8 commit 0df0e53
Showing 1 changed file with 20 additions and 53 deletions.
73 changes: 20 additions & 53 deletions internal/analyzers/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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(),})

Check failure on line 172 in internal/analyzers/prometheus.go

View workflow job for this annotation

GitHub Actions / Golang linter

ineffectual assignment to err (ineffassign)

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)
}
Expand All @@ -219,40 +191,35 @@ 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)
}
for _, serviceMonitor := range serviceMonitors.Items {
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)
}
for _, podMonitor := range podMonitors.Items {
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)
}
for _, probe := range probes.Items {
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)
}
Expand Down

0 comments on commit 0df0e53

Please sign in to comment.