Skip to content

Commit

Permalink
[feat] - add validation for existing selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
heliapb committed Oct 14, 2024
1 parent 4b50000 commit 1bef8bd
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions internal/analyzers/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ func RunPrometheusAnalyzer(ctx context.Context, clientSets *k8sutil.ClientSets,

if !namespaceSelectorStatus {
if len(nilNamespaceSelectors) > 0 {
fmt.Printf("No %s is defined, defaulting to the same namespace of Prometheus\n", strings.Join(nilNamespaceSelectors, ", "))
// TODO check if selectors are already created
//fmt.Printf("No %s is defined, defaulting to the same namespace of Prometheus\n", strings.Join(nilNamespaceSelectors, ", "))
checkSelectorsCreated := checkServiceDiscovery(ctx,clientSets,namespace)
if !checkSelectorsCreated{
fmt.Printf("No Service Selectors are created yet")
}
}
} else {
fmt.Println("All namespace selectors are either empty or properly defined.")
Expand All @@ -86,8 +89,11 @@ func RunPrometheusAnalyzer(ctx context.Context, clientSets *k8sutil.ClientSets,

if !serviceSelectorStatus{
if emptyServiceSelectors!= nil {
fmt.Printf("Selectors are defined: %s\n", strings.Join(emptyServiceSelectors, ", "))
// TODO check if selectors are already created
//fmt.Printf("Selectors are defined: %s\n", strings.Join(emptyServiceSelectors, ", "))
checkServiceSelectorsCreated := checkServiceDiscovery(ctx,clientSets,namespace)
if !checkServiceSelectorsCreated{
fmt.Printf("No Service Selectors are created yet")
}
}
}

Expand Down Expand Up @@ -246,3 +252,36 @@ func checkPrometheusServiceSelectorsStatus(serviceSelectors map[string]interface
}
return true, nil
}

func checkServiceDiscovery(ctx context.Context, clientSets *k8sutil.ClientSets, namespace string) bool {
var existingSelectors []string
podMonitor, err := clientSets.MClient.MonitoringV1().PodMonitors(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
fmt.Errorf("Error listing PodMonitors: %v\n", err)
}
if len(podMonitor.Items) == 0 {
existingSelectors = append(existingSelectors,"podMonitor")
}

serviceMonitor, err := clientSets.MClient.MonitoringV1().ServiceMonitors(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
fmt.Errorf("Error listing PodMonitors: %v\n", err)
}
if len(serviceMonitor.Items) == 0 {
existingSelectors = append(existingSelectors,"serviceMonitor")
}

probe, err := clientSets.MClient.MonitoringV1().Probes(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
fmt.Errorf("Error listing PodMonitors: %v\n", err)
}
if len(probe.Items) == 0 {
existingSelectors = append(existingSelectors,"probe")
}
// TODO add scrapeconfig check

if len(existingSelectors) == 0 {
return false
}
return true
}

0 comments on commit 1bef8bd

Please sign in to comment.