Skip to content

Commit e637796

Browse files
committed
feat: implement CEL validations for OperatorConfig
Adds kubebuilder CEL validations to `OperatorConfig` fields based on the migration away from webhook validation. - Validates prometheus label keys in `externalLabels` - Validates `queryProjectID` constraints (length and regex pattern) - Adds `isURL` checks for `generatorUrl`, `exports.url`, `externalURL` - Implements RFC 1123 label constraints for AlertmanagerEndpoints namespace and name - Validates TLSConfig KeySecret name - Enforces HTTP/HTTPS scheme for AlertmanagerEndpoints Signed-off-by: Adam Bernot <bernot@google.com>
1 parent 9a3f24d commit e637796

5 files changed

Lines changed: 534 additions & 16 deletions

File tree

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ spec:
7878
- key
7979
type: object
8080
x-kubernetes-map-type: atomic
81+
x-kubernetes-validations:
82+
- rule: has(self.name) && self.name.matches('^[a-z0-9]([-a-z0-9.]*[a-z0-9])?$')
83+
&& size(self.name) <= 253
8184
externalLabels:
8285
additionalProperties:
8386
type: string
@@ -86,6 +89,9 @@ spec:
8689
data before being written to Google Cloud Monitoring or any other additional exports
8790
specified in the OperatorConfig. The precedence behavior matches that of Prometheus.
8891
type: object
92+
x-kubernetes-validations:
93+
- message: Invalid label key
94+
rule: self.all(key, key.matches('^[a-zA-Z_][a-zA-Z0-9_]*$'))
8995
filter:
9096
description: Filter limits which metric data is sent to Cloud Monitoring
9197
(it doesn't apply to additional exports).
@@ -157,9 +163,12 @@ spec:
157163
description: The URL of the endpoint that supports Prometheus Remote
158164
Write to export samples to.
159165
type: string
166+
x-kubernetes-validations:
167+
- rule: self == '' || isURL(self)
160168
required:
161169
- url
162170
type: object
171+
maxItems: 10
163172
type: array
164173
features:
165174
description: Features holds configuration for optional managed-collection
@@ -227,6 +236,9 @@ spec:
227236
- key
228237
type: object
229238
x-kubernetes-map-type: atomic
239+
x-kubernetes-validations:
240+
- rule: has(self.name) && self.name.matches('^[a-z0-9]([-a-z0-9.]*[a-z0-9])?$')
241+
&& size(self.name) <= 253
230242
externalURL:
231243
description: |-
232244
ExternalURL is the URL under which Alertmanager is externally reachable (for example, if
@@ -237,6 +249,8 @@ spec:
237249
238250
If no URL is provided, Alertmanager will point to the Google Cloud Metric Explorer page.
239251
type: string
252+
x-kubernetes-validations:
253+
- rule: self == '' || isURL(self)
240254
type: object
241255
metadata:
242256
type: object
@@ -288,6 +302,9 @@ spec:
288302
- key
289303
type: object
290304
x-kubernetes-map-type: atomic
305+
x-kubernetes-validations:
306+
- rule: has(self.name) && self.name.matches('^[a-z0-9]([-a-z0-9.]*[a-z0-9])?$')
307+
&& size(self.name) <= 253
291308
type:
292309
description: |-
293310
Set the authentication type. Defaults to Bearer, Basic will cause an
@@ -296,9 +313,13 @@ spec:
296313
type: object
297314
name:
298315
description: Name of Endpoints object in Namespace.
316+
maxLength: 63
317+
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?$
299318
type: string
300319
namespace:
301320
description: Namespace of Endpoints object.
321+
maxLength: 63
322+
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?$
302323
type: string
303324
pathPrefix:
304325
description: Prefix for the HTTP path alerts are pushed
@@ -312,6 +333,9 @@ spec:
312333
x-kubernetes-int-or-string: true
313334
scheme:
314335
description: Scheme to use when firing alerts.
336+
enum:
337+
- http
338+
- https
315339
type: string
316340
timeout:
317341
description: Timeout is a per-target Alertmanager timeout
@@ -373,6 +397,9 @@ spec:
373397
- key
374398
type: object
375399
x-kubernetes-map-type: atomic
400+
x-kubernetes-validations:
401+
- rule: has(self.name) && self.name.matches('^[a-z0-9]([-a-z0-9.]*[a-z0-9])?$')
402+
&& size(self.name) <= 253
376403
type: object
377404
cert:
378405
description: Struct containing the client cert file
@@ -427,6 +454,9 @@ spec:
427454
- key
428455
type: object
429456
x-kubernetes-map-type: atomic
457+
x-kubernetes-validations:
458+
- rule: has(self.name) && self.name.matches('^[a-z0-9]([-a-z0-9.]*[a-z0-9])?$')
459+
&& size(self.name) <= 253
430460
type: object
431461
insecureSkipVerify:
432462
description: Disable target certificate validation.
@@ -456,6 +486,9 @@ spec:
456486
- key
457487
type: object
458488
x-kubernetes-map-type: atomic
489+
x-kubernetes-validations:
490+
- rule: has(self.name) && self.name.matches('^[a-z0-9]([-a-z0-9.]*[a-z0-9])?$')
491+
&& size(self.name) <= 253
459492
maxVersion:
460493
description: |-
461494
Maximum TLS version. Accepted values: TLS10 (TLS 1.0), TLS11 (TLS 1.1), TLS12 (TLS 1.2), TLS13 (TLS 1.3).
@@ -477,6 +510,7 @@ spec:
477510
- namespace
478511
- port
479512
type: object
513+
maxItems: 3
480514
type: array
481515
type: object
482516
credentials:
@@ -508,6 +542,9 @@ spec:
508542
- key
509543
type: object
510544
x-kubernetes-map-type: atomic
545+
x-kubernetes-validations:
546+
- rule: has(self.name) && self.name.matches('^[a-z0-9]([-a-z0-9.]*[a-z0-9])?$')
547+
&& size(self.name) <= 253
511548
externalLabels:
512549
additionalProperties:
513550
type: string
@@ -516,17 +553,26 @@ spec:
516553
results and alerts produced by rules. The precedence behavior matches that
517554
of Prometheus.
518555
type: object
556+
x-kubernetes-validations:
557+
- message: Invalid label key
558+
rule: self.all(key, key.matches('^[a-zA-Z_][a-zA-Z0-9_]*$'))
519559
generatorUrl:
520560
description: |-
521561
The base URL used for the generator URL in the alert notification payload.
522562
Should point to an instance of a query frontend that gives access to queryProjectID.
523563
type: string
564+
x-kubernetes-validations:
565+
- rule: self == '' || isURL(self)
524566
queryProjectID:
525567
description: |-
526568
QueryProjectID is the GCP project ID to evaluate rules against.
527569
If left blank, the rule-evaluator will try attempt to infer the Project ID
528570
from the environment.
529571
type: string
572+
x-kubernetes-validations:
573+
- message: Invalid GCP project ID
574+
rule: self == '' || (self.matches('^[a-z][a-z0-9-]*[a-z0-9]$') &&
575+
size(self) >= 6 && size(self) <= 30)
530576
type: object
531577
scaling:
532578
description: Scaling contains configuration options for scaling GMP.

0 commit comments

Comments
 (0)