Skip to content

Commit

Permalink
[feat] - am ns selectors
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 b871bee commit b6f4bd0
Showing 1 changed file with 44 additions and 7 deletions.
51 changes: 44 additions & 7 deletions internal/analyzers/alertmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ 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 All @@ -38,14 +39,24 @@ func RunAlertmanagerAnalyzer(ctx context.Context, clientSets *k8sutil.ClientSets
return fmt.Errorf("failed to list ServiceAcounts: %w", err)
}

if alertmanager.Spec.ConfigSecret != "" {
if err := checkAlertmanagerSecret(ctx, clientSets, alertmanager.Spec.ConfigSecret, namespace, "alertmanager.yaml"); err != nil {
return fmt.Errorf("error checking Alertmanager secret: %w", err)
if alertmanager.Spec.AlertmanagerConfigSelector == nil && alertmanager.Spec.AlertmanagerConfiguration == nil {
if alertmanager.Spec.ConfigSecret != "" {
// use provided config secret
if err := checkAlertmanagerSecret(ctx, clientSets, alertmanager.Spec.ConfigSecret, namespace, "alertmanager.yaml"); err != nil {
return fmt.Errorf("error checking Alertmanager secret: %w", err)
}
} else if alertmanager.Spec.ConfigSecret == "" {
// use the default generated secret from pkg/alertmanager/statefulset.go
amConfigSecretName := fmt.Sprintf("alertmanager-%s-generated", alertmanager.Name)
if err := checkAlertmanagerSecret(ctx, clientSets, amConfigSecretName, namespace, "alertmanager.yaml.gz"); err != nil {
return fmt.Errorf("error checking Alertmanager secret: %w", err)
}
}
} else if alertmanager.Spec.ConfigSecret == "" {
amConfigSecretName := fmt.Sprintf("alertmanager-%s-generated", alertmanager.Name)
if err := checkAlertmanagerSecret(ctx, clientSets, amConfigSecretName, namespace, "alertmanager.yaml.gz"); err != nil {
return fmt.Errorf("error checking Alertmanager secret: %w", err)
}
// If 'AlertmanagerConfigNamespaceSelector' is nil, only check own namespace.
if alertmanager.Spec.AlertmanagerConfigNamespaceSelector != nil {
if err := checkAlertmanagerNamespaceSelectors(ctx, clientSets, alertmanager.Spec.AlertmanagerConfigNamespaceSelector); err != nil {
return fmt.Errorf("AlertmanagerConfigNamespaceSelector is not properly defined: %s", err)
}
}

Expand All @@ -67,3 +78,29 @@ 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
}

0 comments on commit b6f4bd0

Please sign in to comment.