Skip to content

Commit

Permalink
Add validation for otlp destination protocol (#1212)
Browse files Browse the repository at this point in the history
* Add validation for otlp destination protocol

Signed-off-by: Pete Wall <[email protected]>

* Update charts/k8s-monitoring/tests/destination_validations_test.yaml

Co-authored-by: Robert Lankford <[email protected]>

* Actually test the right error message

Signed-off-by: Pete Wall <[email protected]>

---------

Signed-off-by: Pete Wall <[email protected]>
Co-authored-by: Robert Lankford <[email protected]>
  • Loading branch information
petewall and rlankfo authored Feb 7, 2025
1 parent 1e83a41 commit 9cecbf9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
3 changes: 2 additions & 1 deletion charts/k8s-monitoring/schema-mods/types-and-enums.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
}, "required": ["key", "action"]
}}
}}
}}
}},
"protocol": {"enum": ["grpc", "http"]}
}},
"prometheus-destination": {"properties": {
"auth": {"properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
{{- define "destinations.validate" }}
{{- range $i, $destination := .Values.destinations }}
{{- if not $destination.name }}
{{ fail (printf "\nDestination #%d does not have a name.\nPlease set:\ndestinations:\n - name: my-destination-name" $i) }}
{{- $msg := list "" (printf "Destination #%d does not have a name." $i) }}
{{- $msg = append $msg "Please set:" }}
{{- $msg = append $msg "destinations:" }}
{{- $msg = append $msg " - name: my-destination-name" }}
{{- fail (join "\n" $msg) }}
{{- end }}

{{- if (regexFind "[^-_ a-zA-Z0-9]" $destination.name) }}
{{ fail (printf "\nDestination #%d (%s) has invalid characters in its name.\nPlease only use alphanumeric, underscores, dashes, or spaces" $i $destination.name) }}
{{- $msg := list "" (printf "Destination #%d (%s) invalid characters in its name." $i $destination.name) }}
{{- $msg = append $msg "Please only use alphanumeric, underscores, dashes, or spaces." }}
{{- fail (join "\n" $msg) }}
{{- end }}

{{- $types := (include "destinations.types" . ) | fromYamlArray }}
Expand All @@ -19,6 +25,17 @@
{{ fail (printf "\nDestination #%d (%s) is using an unknown type (%s).\nPlease set:\ndestinations:\n - name: %s\n type: \"[%s]\"" $i $destination.name $destination.type $destination.name (include "english_list_or" $types)) }}
{{- end }}

{{- if and (eq $destination.type "otlp") (not (has ($destination.protocol | default "grpc") (list "grpc" "http"))) }}
{{- $msg := list "" (printf "Destination #%d (%s) has an unsupported protocol: %s." $i $destination.name $destination.protocol) }}
{{- $msg = append $msg "The protocol must be either \"grpc\" or \"http\"" }}
{{- $msg = append $msg "Please set:" }}
{{- $msg = append $msg "destinations:" }}
{{- $msg = append $msg (printf " - name: %s" $destination.name) }}
{{- $msg = append $msg " type: otlp" }}
{{- $msg = append $msg " protocol: otlp / http" }}
{{- fail (join "\n" $msg) }}
{{- end }}

{{/* Check if OTLP destination using Grafana Cloud OTLP gateway has protocol set */}}
{{- if and (eq $destination.type "otlp") (ne $destination.protocol "http") (regexMatch "otlp-gateway-.+grafana\\.net" $destination.url) }}
{{ fail (printf "\nDestination #%d (%s) is using Grafana Cloud OTLP gateway but has incorrect protocol '%s'. The gateway requires 'http'.\nPlease set:\ndestinations:\n - name: %s\n type: otlp\n url: %s\n protocol: http" $i $destination.name ($destination.protocol | default "grpc (default)") $destination.name $destination.url) }}
Expand Down
25 changes: 23 additions & 2 deletions charts/k8s-monitoring/tests/destination_validations_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ tests:
- failedTemplate:
errorMessage: |-
execution error at (k8s-monitoring/templates/validations.yaml:5:4):
Destination #0 (prøm3thüs!) has invalid characters in its name.
Please only use alphanumeric, underscores, dashes, or spaces
Destination #0 (prøm3thüs!) invalid characters in its name.
Please only use alphanumeric, underscores, dashes, or spaces.
- it: requires a destination type
set:
Expand Down Expand Up @@ -76,6 +76,27 @@ tests:
asserts:
- notFailedTemplate: {}

- it: detects if you're using an otlp destination with an invalid protocol
set:
cluster:
name: test-cluster
destinations:
- name: my-destination
type: otlp
url: https://otlp.example.com
protocol: magic
asserts:
- failedTemplate:
errorMessage: |-
execution error at (k8s-monitoring/templates/validations.yaml:5:4):
Destination #0 (my-destination) has an unsupported protocol: magic.
The protocol must be either "grpc" or "http"
Please set:
destinations:
- name: my-destination
type: otlp
protocol: otlp / http
- it: detects if you're using the Grafana Cloud OTLP endpoint with the wrong protocol
set:
cluster:
Expand Down
6 changes: 5 additions & 1 deletion charts/k8s-monitoring/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1587,7 +1587,11 @@
}
},
"protocol": {
"type": "string"
"type": "string",
"enum": [
"grpc",
"http"
]
},
"readBufferSize": {
"type": "string"
Expand Down

0 comments on commit 9cecbf9

Please sign in to comment.