Skip to content

Commit

Permalink
[feat] - add svcmonitors
Browse files Browse the repository at this point in the history
  • Loading branch information
heliapb committed Sep 24, 2024
1 parent 842655d commit c836f8e
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions internal/analyzers/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,22 @@ func RunPrometheusAnalyzer(ctx context.Context, clientSets *k8sutil.ClientSets,
"ServiceMonitorNamespaceSelector": prometheus.Spec.ServiceMonitorNamespaceSelector,
"ScrapeConfigNamespaceSelector": prometheus.Spec.ScrapeConfigNamespaceSelector,
"ProbeNamespaceSelector": prometheus.Spec.ProbeNamespaceSelector,
"RuleNamespaceSelector": prometheus.Spec.RuleNamespaceSelector,
//"RuleNamespaceSelector": prometheus.Spec.RuleNamespaceSelector,
}

if !checkPrometheusNamespaceSelectorsStatus(namespaceSelectors) {
return fmt.Errorf("Validation failed")
return fmt.Errorf("Namespace Selectors Validation failed")
}

serviceSelectors := map[string]interface{}{
"ServiceMonitorSelector": prometheus.Spec.ServiceMonitorSelector,
"PodMonitorSelector": prometheus.Spec.PodMonitorSelector,
"ProbeSelector": prometheus.Spec.ProbeSelector,
"ScrapeConfigSelector": prometheus.Spec.ScrapeConfigSelector,
}

if !checkPrometheusServiceSelectorsStatus(serviceSelectors) {
return fmt.Errorf("Service Selectors Validation failed")
}

slog.Info("Prometheus is compliant, no issues found", "name", name, "namespace", namespace)
Expand Down Expand Up @@ -186,3 +197,23 @@ func checkPrometheusNamespaceSelectorsStatus(namespaceSelectors map[string]inter

return false
}

func checkPrometheusServiceSelectorsStatus(serviceSelectors map[string]interface{}) bool {
//var emptServiceSelectors []string
selectorCount := 0

for selectorName, selector := range serviceSelectors {
if selector == nil {
fmt.Printf("No %s is defined, defaulting to the same namespace of Prometheus\n", selectorName)
selectorCount++
return false
}
}
if selectorCount == 4{
fmt.Printf("Neither serviceMonitorSelector nor podMonitorSelector, nor probeSelector, not scrapeConfigSelector specified - use additionalScrapeConfigs instead \n")
return false
}
// TODO, add validations for when selectors != nil

return false
}

0 comments on commit c836f8e

Please sign in to comment.