From 9cecbf9a55f76d900ead143a7ecd264d780f621c Mon Sep 17 00:00:00 2001 From: Pete Wall Date: Fri, 7 Feb 2025 13:06:59 -0600 Subject: [PATCH] Add validation for otlp destination protocol (#1212) * Add validation for otlp destination protocol Signed-off-by: Pete Wall * Update charts/k8s-monitoring/tests/destination_validations_test.yaml Co-authored-by: Robert Lankford * Actually test the right error message Signed-off-by: Pete Wall --------- Signed-off-by: Pete Wall Co-authored-by: Robert Lankford --- .../schema-mods/types-and-enums.json | 3 ++- .../destinations/_destination_validations.tpl | 21 ++++++++++++++-- .../tests/destination_validations_test.yaml | 25 +++++++++++++++++-- charts/k8s-monitoring/values.schema.json | 6 ++++- 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/charts/k8s-monitoring/schema-mods/types-and-enums.json b/charts/k8s-monitoring/schema-mods/types-and-enums.json index 1134af83c2..1ff98baff8 100644 --- a/charts/k8s-monitoring/schema-mods/types-and-enums.json +++ b/charts/k8s-monitoring/schema-mods/types-and-enums.json @@ -49,7 +49,8 @@ }, "required": ["key", "action"] }} }} - }} + }}, + "protocol": {"enum": ["grpc", "http"]} }}, "prometheus-destination": {"properties": { "auth": {"properties": { diff --git a/charts/k8s-monitoring/templates/destinations/_destination_validations.tpl b/charts/k8s-monitoring/templates/destinations/_destination_validations.tpl index e99ce21ae7..f88057585b 100644 --- a/charts/k8s-monitoring/templates/destinations/_destination_validations.tpl +++ b/charts/k8s-monitoring/templates/destinations/_destination_validations.tpl @@ -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 }} @@ -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) }} diff --git a/charts/k8s-monitoring/tests/destination_validations_test.yaml b/charts/k8s-monitoring/tests/destination_validations_test.yaml index 25b1a849e9..cd8d228266 100644 --- a/charts/k8s-monitoring/tests/destination_validations_test.yaml +++ b/charts/k8s-monitoring/tests/destination_validations_test.yaml @@ -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: @@ -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: diff --git a/charts/k8s-monitoring/values.schema.json b/charts/k8s-monitoring/values.schema.json index ff02bd07c4..0425367c9c 100644 --- a/charts/k8s-monitoring/values.schema.json +++ b/charts/k8s-monitoring/values.schema.json @@ -1587,7 +1587,11 @@ } }, "protocol": { - "type": "string" + "type": "string", + "enum": [ + "grpc", + "http" + ] }, "readBufferSize": { "type": "string"