From 9a9c7a5c6fac09c4d9bfdda8016df7493a00d704 Mon Sep 17 00:00:00 2001 From: Wainer dos Santos Moschetta Date: Thu, 19 Oct 2023 17:04:17 -0300 Subject: [PATCH] tests/k8s: add set_metadata_annotation() to lib.sh This new function allow to the annotations to metadata section in a yaml configuration file. Co-authored-by: Ryan Savino Signed-off-by: Wainer dos Santos Moschetta --- tests/integration/kubernetes/lib.sh | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/integration/kubernetes/lib.sh b/tests/integration/kubernetes/lib.sh index 133170041e..4c14a5d1b9 100644 --- a/tests/integration/kubernetes/lib.sh +++ b/tests/integration/kubernetes/lib.sh @@ -98,4 +98,35 @@ new_pod_config() { new_config=$(mktemp "${BATS_FILE_TMPDIR}/$(basename "${base_config}").XXX") IMAGE="$image" RUNTIMECLASS="$runtimeclass" envsubst < "$base_config" > "$new_config" echo "$new_config" +} + +# Set an annotation on configuration metadata. +# +# Usually you will pass a pod configuration file where the 'metadata' +# is relative to the 'root' path. Other configuration files like deployments, +# the annotation should be set on 'spec.template.metadata', so use the 4th +# parameter of this function to pass the base metadata path (for deployments +# cases, it will be 'spec.template' for example). +# +# Parameters: +# $1 - the yaml file +# $2 - the annotation key +# $3 - the annotation value +# $4 - (optional) base metadata path +set_metadata_annotation() { + local yaml="${1}" + local key="${2}" + local value="${3}" + local metadata_path="${4:-}" + local annotation_key="" + + [ -n "$metadata_path" ] && annotation_key+="${metadata_path}." + + # yaml annotation key name. + annotation_key+="metadata.annotations.\"${key}\"" + + echo "$annotation_key" + # yq set annotations in yaml. Quoting the key because it can have + # dots. + yq w -i --style=double "${yaml}" "${annotation_key}" "${value}" } \ No newline at end of file