Skip to content

Commit

Permalink
[feat] - mv nsselectorscheck
Browse files Browse the repository at this point in the history
Signed-off-by: Hélia Barroso <[email protected]>
  • Loading branch information
heliapb committed Nov 20, 2024
1 parent b6f4bd0 commit 883735b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 62 deletions.
33 changes: 4 additions & 29 deletions internal/analyzers/alertmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/prometheus-operator/poctl/internal/k8sutil"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)

func RunAlertmanagerAnalyzer(ctx context.Context, clientSets *k8sutil.ClientSets, name, namespace string) error {
Expand Down Expand Up @@ -55,10 +54,12 @@ func RunAlertmanagerAnalyzer(ctx context.Context, clientSets *k8sutil.ClientSets
}
// If 'AlertmanagerConfigNamespaceSelector' is nil, only check own namespace.
if alertmanager.Spec.AlertmanagerConfigNamespaceSelector != nil {
if err := checkAlertmanagerNamespaceSelectors(ctx, clientSets, alertmanager.Spec.AlertmanagerConfigNamespaceSelector); err != nil {
if err := k8sutil.CheckResourceNamespaceSelectors(ctx, *clientSets, alertmanager.Spec.AlertmanagerConfigNamespaceSelector); err != nil {
return fmt.Errorf("AlertmanagerConfigNamespaceSelector is not properly defined: %s", err)
}
}
} //else if alertmanager.Spec.AlertmanagerConfigNamespaceSelector == nil {

//}

slog.Info("Alertmanager is compliant, no issues found", "name", name, "namespace", namespace)
return nil
Expand All @@ -78,29 +79,3 @@ func checkAlertmanagerSecret(ctx context.Context, clientSets *k8sutil.ClientSets
}
return nil
}

func checkAlertmanagerNamespaceSelectors(ctx context.Context, clientSets *k8sutil.ClientSets, labelSelector *metav1.LabelSelector) error {
if labelSelector == nil {
return nil
}

if len(labelSelector.MatchLabels) == 0 && len(labelSelector.MatchExpressions) == 0 {
return nil
}

labelMap, err := metav1.LabelSelectorAsMap(labelSelector)
if err != nil {
return fmt.Errorf("invalid label selector format in %s: %v", labelSelector, err)
}
namespaces, err := clientSets.KClient.CoreV1().Namespaces().List(ctx, metav1.ListOptions{LabelSelector: labels.SelectorFromSet(labelMap).String()})

if err != nil {
return fmt.Errorf("failed to list Namespaces in %s: %v", labelSelector, err)
}

if len(namespaces.Items) == 0 {
return fmt.Errorf("no namespaces match the selector %s", labelSelector)
}

return nil
}
37 changes: 5 additions & 32 deletions internal/analyzers/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,23 @@ func RunPrometheusAnalyzer(ctx context.Context, clientSets *k8sutil.ClientSets,
}
}

if err := checkResourceNamespaceSelectors(ctx, clientSets, prometheus.Spec.PodMonitorNamespaceSelector); err != nil {
if err := k8sutil.CheckResourceNamespaceSelectors(ctx, *clientSets, prometheus.Spec.PodMonitorNamespaceSelector); err != nil {
return fmt.Errorf("podMonitorNamespaceSelector is not properly defined: %s", err)
}

if err := checkResourceNamespaceSelectors(ctx, clientSets, prometheus.Spec.ProbeNamespaceSelector); err != nil {
if err := k8sutil.CheckResourceNamespaceSelectors(ctx, *clientSets, prometheus.Spec.ProbeNamespaceSelector); err != nil {
return fmt.Errorf("probeNamespaceSelector is not properly defined: %s", err)
}

if err := checkResourceNamespaceSelectors(ctx, clientSets, prometheus.Spec.ServiceMonitorNamespaceSelector); err != nil {
if err := k8sutil.CheckResourceNamespaceSelectors(ctx, *clientSets, prometheus.Spec.ServiceMonitorNamespaceSelector); err != nil {
return fmt.Errorf("serviceMonitorNamespaceSelector is not properly defined: %s", err)
}

if err := checkResourceNamespaceSelectors(ctx, clientSets, prometheus.Spec.ScrapeConfigNamespaceSelector); err != nil {
if err := k8sutil.CheckResourceNamespaceSelectors(ctx, *clientSets, prometheus.Spec.ScrapeConfigNamespaceSelector); err != nil {
return fmt.Errorf("scrapeConfigNamespaceSelector is not properly defined: %s", err)
}

if err := checkResourceNamespaceSelectors(ctx, clientSets, prometheus.Spec.RuleNamespaceSelector); err != nil {
if err := k8sutil.CheckResourceNamespaceSelectors(ctx, *clientSets, prometheus.Spec.RuleNamespaceSelector); err != nil {
return fmt.Errorf("ruleNamespaceSelector is not properly defined: %s", err)
}

Expand Down Expand Up @@ -171,33 +171,6 @@ func checkClusterRoleRules(crb v1.ClusterRoleBinding, cr *v1.ClusterRole) error
return nil
}

func checkResourceNamespaceSelectors(ctx context.Context, clientSets *k8sutil.ClientSets, labelSelector *metav1.LabelSelector) error {
if labelSelector == nil {
return nil
}

if len(labelSelector.MatchLabels) == 0 && len(labelSelector.MatchExpressions) == 0 {
return nil
}

labelMap, err := metav1.LabelSelectorAsMap(labelSelector)
if err != nil {
return fmt.Errorf("invalid label selector format in %s: %v", labelSelector, err)
}

namespaces, err := clientSets.KClient.CoreV1().Namespaces().List(ctx, metav1.ListOptions{LabelSelector: labels.SelectorFromSet(labelMap).String()})

if err != nil {
return fmt.Errorf("failed to list Namespaces in %s: %v", labelSelector, err)
}

if len(namespaces.Items) == 0 {
return fmt.Errorf("no namespaces match the selector %s", labelSelector)
}

return nil
}

func checkResourceLabelSelectors(ctx context.Context, clientSets *k8sutil.ClientSets, labelSelector *metav1.LabelSelector, resourceName, namespace string) error {
if labelSelector == nil {
return fmt.Errorf("%s selector is not defined", resourceName)
Expand Down
31 changes: 30 additions & 1 deletion internal/k8sutil/k8sutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package k8sutil

import (
"bytes"
"context"
"fmt"
"io"
"log/slog"
Expand All @@ -26,18 +27,19 @@ import (
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
monitoringv1alpha1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1alpha1"
monitoringclient "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned"
v1 "k8s.io/api/rbac/v1"
apiv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiExtensions "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
v1 "k8s.io/api/rbac/v1"
)

var ApplyOption = metav1.ApplyOptions{
Expand Down Expand Up @@ -158,3 +160,30 @@ func IsServiceAccountBoundToRoleBindingList(clusterRoleBindings *v1.ClusterRoleB
}
return false
}

func CheckResourceNamespaceSelectors(ctx context.Context, clientSets ClientSets, labelSelector *metav1.LabelSelector) error {
if labelSelector == nil {
return nil
}

if len(labelSelector.MatchLabels) == 0 && len(labelSelector.MatchExpressions) == 0 {
return nil
}

labelMap, err := metav1.LabelSelectorAsMap(labelSelector)
if err != nil {
return fmt.Errorf("invalid label selector format in %s: %v", labelSelector, err)
}

namespaces, err := clientSets.KClient.CoreV1().Namespaces().List(ctx, metav1.ListOptions{LabelSelector: labels.SelectorFromSet(labelMap).String()})

if err != nil {
return fmt.Errorf("failed to list Namespaces in %s: %v", labelSelector, err)
}

if len(namespaces.Items) == 0 {
return fmt.Errorf("no namespaces match the selector %s", labelSelector)
}

return nil
}

0 comments on commit 883735b

Please sign in to comment.