Skip to content

Commit cb901de

Browse files
committed
Support spaces on prometheus alert names (#434)
1 parent c0be25d commit cb901de

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

charts/coralogix-operator/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,6 @@ Kubernetes: `>=1.16.0-0`
6161
| serviceMonitor.namespace | string | `""` | If not set, the service monitor will be created in the same namespace as the operator. |
6262
| serviceMonitor.namespaceSelector.enabled | bool | `false` | Useful when the service monitor is deployed in a different namespace than the operator. |
6363
| tolerations | list | `[]` | ref: https://kubernetes.io/docs/concepts/configuration/taint-and-toleration/ |
64+
| volumeMounts | list | `[]` | Additional volumeMounts on the output Deployment definition. |
65+
| volumes | list | `[]` | Additional volumes on the output Deployment definition. |
6466

internal/controller/prometheusrule_controller.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"errors"
2020
"fmt"
2121
"reflect"
22+
"regexp"
2223
"strings"
2324
"time"
2425

@@ -224,7 +225,11 @@ func (r *PrometheusRuleReconciler) convertPrometheusRuleAlertToCxAlert(ctx conte
224225
for alertName, rules := range alertMap {
225226
for i, rule := range rules {
226227
alert := &coralogixv1beta1.Alert{}
227-
alertName := fmt.Sprintf("%s-%s-%d", prometheusRule.Name, alertName, i)
228+
alertName := fmt.Sprintf("%s-%s-%d",
229+
prometheusRule.Name,
230+
sanitizeName(alertName),
231+
i,
232+
)
228233
alertsToKeep[alertName] = true
229234
if err := config.GetClient().Get(ctx, client.ObjectKey{Namespace: prometheusRule.Namespace, Name: alertName}, alert); err != nil {
230235
if k8serrors.IsNotFound(err) {
@@ -425,7 +430,7 @@ func prometheusAlertToMetricThreshold(rule prometheus.Rule, priority coralogixv1
425430
Threshold: resource.MustParse("0"),
426431
ForOverPct: 100,
427432
OfTheLast: coralogixv1beta1.MetricTimeWindow{
428-
DynamicDuration: ptr.To(string(ptr.Deref(rule.For, ""))),
433+
DynamicDuration: ptr.To(string(ptr.Deref(rule.For, "1m"))),
429434
},
430435
ConditionType: coralogixv1beta1.MetricThresholdConditionTypeMoreThan,
431436
},
@@ -466,6 +471,18 @@ func getOwnerReference(promRule *prometheus.PrometheusRule) metav1.OwnerReferenc
466471
}
467472
}
468473

474+
// sanitizeName converts any string into a valid Kubernetes resource name
475+
// according to RFC 1123 (lowercase alphanumeric, '-', or '.').
476+
func sanitizeName(name string) string {
477+
name = strings.ToLower(name)
478+
// Replace any invalid characters (anything not a-z, 0-9, '-', or '.') with '-'
479+
re := regexp.MustCompile(`[^a-z0-9.-]+`)
480+
name = re.ReplaceAllString(name, "-")
481+
// Trim leading/trailing non-alphanumeric characters
482+
name = strings.Trim(name, "-.")
483+
return name
484+
}
485+
469486
// SetupWithManager sets up the controller with the Manager.
470487
func (r *PrometheusRuleReconciler) SetupWithManager(mgr ctrl.Manager) error {
471488
shouldTrackPrometheusRules := func(labels map[string]string) bool {

0 commit comments

Comments
 (0)