@@ -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.
470487func (r * PrometheusRuleReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
471488 shouldTrackPrometheusRules := func (labels map [string ]string ) bool {
0 commit comments