Skip to content

Commit 6abd262

Browse files
committed
fix: rule name patterns
In a rule, the Record and Alert fields were validated incorrectly. Record must be a valid metric name, which allows colons in additional to valid label name characters. Alert must be a valid label value, which allows any UTF-8 character.
1 parent 358cec2 commit 6abd262

File tree

6 files changed

+69
-16
lines changed

6 files changed

+69
-16
lines changed

charts/operator/crds/monitoring.googleapis.com_clusterrules.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ spec:
8484
description: |-
8585
Name of the alert to evaluate the expression as.
8686
Only one of `record` and `alert` must be set.
87-
pattern: ^[a-zA-Z_][a-zA-Z0-9_]*$
8887
type: string
8988
annotations:
9089
additionalProperties:
@@ -112,7 +111,7 @@ spec:
112111
description: |-
113112
Record the result of the expression to this metric name.
114113
Only one of `record` and `alert` must be set.
115-
pattern: ^[a-zA-Z_][a-zA-Z0-9_]*$
114+
pattern: ^[a-zA-Z_:][a-zA-Z0-9_:]*$
116115
type: string
117116
required:
118117
- expr

charts/operator/crds/monitoring.googleapis.com_globalrules.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ spec:
8383
description: |-
8484
Name of the alert to evaluate the expression as.
8585
Only one of `record` and `alert` must be set.
86-
pattern: ^[a-zA-Z_][a-zA-Z0-9_]*$
8786
type: string
8887
annotations:
8988
additionalProperties:
@@ -111,7 +110,7 @@ spec:
111110
description: |-
112111
Record the result of the expression to this metric name.
113112
Only one of `record` and `alert` must be set.
114-
pattern: ^[a-zA-Z_][a-zA-Z0-9_]*$
113+
pattern: ^[a-zA-Z_:][a-zA-Z0-9_:]*$
115114
type: string
116115
required:
117116
- expr

charts/operator/crds/monitoring.googleapis.com_rules.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ spec:
8484
description: |-
8585
Name of the alert to evaluate the expression as.
8686
Only one of `record` and `alert` must be set.
87-
pattern: ^[a-zA-Z_][a-zA-Z0-9_]*$
8887
type: string
8988
annotations:
9089
additionalProperties:
@@ -112,7 +111,7 @@ spec:
112111
description: |-
113112
Record the result of the expression to this metric name.
114113
Only one of `record` and `alert` must be set.
115-
pattern: ^[a-zA-Z_][a-zA-Z0-9_]*$
114+
pattern: ^[a-zA-Z_:][a-zA-Z0-9_:]*$
116115
type: string
117116
required:
118117
- expr

e2e/crd_validation_test.go

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -867,10 +867,30 @@ func TestCRDValidation(t *testing.T) {
867867
})
868868
t.Run("Rules", func(t *testing.T) {
869869
tests := map[string]test{
870-
"minimal": {
870+
"minimal-alerting": {
871871
obj: &monitoringv1.Rules{
872872
ObjectMeta: metav1.ObjectMeta{
873-
Name: "minimal",
873+
Name: "minimal-alerting",
874+
Namespace: "default",
875+
},
876+
Spec: monitoringv1.RulesSpec{
877+
Groups: []monitoringv1.RuleGroup{
878+
{
879+
Rules: []monitoringv1.Rule{
880+
{
881+
Alert: "Any-characters:Allowed!?#@",
882+
},
883+
},
884+
},
885+
},
886+
},
887+
},
888+
wantErr: false,
889+
},
890+
"minimal-recording": {
891+
obj: &monitoringv1.Rules{
892+
ObjectMeta: metav1.ObjectMeta{
893+
Name: "minimal-recording",
874894
Namespace: "default",
875895
},
876896
Spec: monitoringv1.RulesSpec{
@@ -928,6 +948,46 @@ func TestCRDValidation(t *testing.T) {
928948
},
929949
wantErr: true,
930950
},
951+
"invalid-rule-name-dash": {
952+
obj: &monitoringv1.Rules{
953+
ObjectMeta: metav1.ObjectMeta{
954+
Name: "invalid-rule-name-dash",
955+
Namespace: "default",
956+
},
957+
Spec: monitoringv1.RulesSpec{
958+
Groups: []monitoringv1.RuleGroup{
959+
{
960+
Rules: []monitoringv1.Rule{
961+
{
962+
Record: "dashes-not-allowed",
963+
},
964+
},
965+
},
966+
},
967+
},
968+
},
969+
wantErr: true,
970+
},
971+
"valid-rule-name-colon": {
972+
obj: &monitoringv1.Rules{
973+
ObjectMeta: metav1.ObjectMeta{
974+
Name: "valid-rule-name-colon",
975+
Namespace: "default",
976+
},
977+
Spec: monitoringv1.RulesSpec{
978+
Groups: []monitoringv1.RuleGroup{
979+
{
980+
Rules: []monitoringv1.Rule{
981+
{
982+
Record: "colon:allowed",
983+
},
984+
},
985+
},
986+
},
987+
},
988+
},
989+
wantErr: false,
990+
},
931991
"invalid-annotation": {
932992
obj: &monitoringv1.Rules{
933993
ObjectMeta: metav1.ObjectMeta{

manifests/setup.yaml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,6 @@ spec:
13921392
description: |-
13931393
Name of the alert to evaluate the expression as.
13941394
Only one of `record` and `alert` must be set.
1395-
pattern: ^[a-zA-Z_][a-zA-Z0-9_]*$
13961395
type: string
13971396
annotations:
13981397
additionalProperties:
@@ -1419,7 +1418,7 @@ spec:
14191418
description: |-
14201419
Record the result of the expression to this metric name.
14211420
Only one of `record` and `alert` must be set.
1422-
pattern: ^[a-zA-Z_][a-zA-Z0-9_]*$
1421+
pattern: ^[a-zA-Z_:][a-zA-Z0-9_:]*$
14231422
type: string
14241423
required:
14251424
- expr
@@ -1659,7 +1658,6 @@ spec:
16591658
description: |-
16601659
Name of the alert to evaluate the expression as.
16611660
Only one of `record` and `alert` must be set.
1662-
pattern: ^[a-zA-Z_][a-zA-Z0-9_]*$
16631661
type: string
16641662
annotations:
16651663
additionalProperties:
@@ -1686,7 +1684,7 @@ spec:
16861684
description: |-
16871685
Record the result of the expression to this metric name.
16881686
Only one of `record` and `alert` must be set.
1689-
pattern: ^[a-zA-Z_][a-zA-Z0-9_]*$
1687+
pattern: ^[a-zA-Z_:][a-zA-Z0-9_:]*$
16901688
type: string
16911689
required:
16921690
- expr
@@ -3769,7 +3767,6 @@ spec:
37693767
description: |-
37703768
Name of the alert to evaluate the expression as.
37713769
Only one of `record` and `alert` must be set.
3772-
pattern: ^[a-zA-Z_][a-zA-Z0-9_]*$
37733770
type: string
37743771
annotations:
37753772
additionalProperties:
@@ -3796,7 +3793,7 @@ spec:
37963793
description: |-
37973794
Record the result of the expression to this metric name.
37983795
Only one of `record` and `alert` must be set.
3799-
pattern: ^[a-zA-Z_][a-zA-Z0-9_]*$
3796+
pattern: ^[a-zA-Z_:][a-zA-Z0-9_:]*$
38003797
type: string
38013798
required:
38023799
- expr

pkg/operator/apis/monitoring/v1/rules_types.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,10 @@ type RuleGroup struct {
191191
type Rule struct {
192192
// Record the result of the expression to this metric name.
193193
// Only one of `record` and `alert` must be set.
194-
// +kubebuilder:validation:Pattern=^[a-zA-Z_][a-zA-Z0-9_]*$
194+
// +kubebuilder:validation:Pattern="^[a-zA-Z_:][a-zA-Z0-9_:]*$"
195195
Record string `json:"record,omitempty"`
196196
// Name of the alert to evaluate the expression as.
197197
// Only one of `record` and `alert` must be set.
198-
// +kubebuilder:validation:Pattern=^[a-zA-Z_][a-zA-Z0-9_]*$
199198
Alert string `json:"alert,omitempty"`
200199
// The PromQL expression to evaluate.
201200
Expr string `json:"expr"`

0 commit comments

Comments
 (0)