tests:k8s: make add_kernel_initrd_anotations function generic

This PR replaces the add_kernel_initrd_annotations_to_yaml function
more generic so later can be used for other components.

Fixes #9054

Signed-off-by: Gabriela Cervantes <gabriela.cervantes.tellez@intel.com>
This commit is contained in:
Gabriela Cervantes 2024-02-08 16:29:35 +00:00
parent b99f574522
commit 0b508f301b

View File

@ -44,24 +44,22 @@ setup_policy_files() {
create_common_genpolicy_settings "${workloads_work_dir}" create_common_genpolicy_settings "${workloads_work_dir}"
} }
add_kernel_initrd_annotations_to_yaml() { add_annotations_to_yaml() {
local yaml_file="$1" local yaml_file="$1"
local mariner_kernel_path="/usr/share/cloud-hypervisor/vmlinux.bin" local annotation_name="$2"
local mariner_initrd_path="/opt/kata/share/kata-containers/kata-containers-initrd-mariner.img" local annotation_value="$3"
local resource_kind="$(yq read ${yaml_file} kind)" local resource_kind="$(yq read ${yaml_file} kind)"
case "${resource_kind}" in case "${resource_kind}" in
Pod) Pod)
echo "Adding kernel and initrd annotations to ${resource_kind} from ${yaml_file}" echo "Adding kernel and initrd annotations to ${resource_kind} from ${yaml_file}"
yq write -i "${K8S_TEST_YAML}" 'metadata.annotations[io.katacontainers.config.hypervisor.kernel]' "${mariner_kernel_path}" yq write -i "${K8S_TEST_YAML}" "metadata.annotations[${annotation_name}]" "${annotation_value}"
yq write -i "${K8S_TEST_YAML}" 'metadata.annotations[io.katacontainers.config.hypervisor.initrd]' "${mariner_initrd_path}"
;; ;;
Deployment|Job|ReplicationController) Deployment|Job|ReplicationController)
echo "Adding kernel and initrd annotations to ${resource_kind} from ${yaml_file}" echo "Adding kernel and initrd annotations to ${resource_kind} from ${yaml_file}"
yq write -i "${K8S_TEST_YAML}" 'spec.template.metadata.annotations[io.katacontainers.config.hypervisor.kernel]' "${mariner_kernel_path}" yq write -i "${K8S_TEST_YAML}" "spec.template.metadata.annotations[${annotation_name}]" "${annotation_value}"
yq write -i "${K8S_TEST_YAML}" 'spec.template.metadata.annotations[io.katacontainers.config.hypervisor.initrd]' "${mariner_initrd_path}"
;; ;;
List) List)
@ -79,12 +77,19 @@ add_kernel_initrd_annotations_to_yaml() {
esac esac
} }
add_kernel_initrd_annotations() { add_cbl_mariner_kernel_initrd_annotations() {
if [[ "${KATA_HOST_OS}" = "cbl-mariner" ]]; then if [[ "${KATA_HOST_OS}" = "cbl-mariner" ]]; then
info "Add kernel and initrd annotations" info "Add kernel and initrd path and annotations for cbl-mariner"
local mariner_annotation_kernel="io.katacontainers.config.hypervisor.kernel"
local mariner_kernel_path="/usr/share/cloud-hypervisor/vmlinux.bin"
local mariner_annotation_initrd="io.katacontainers.config.hypervisor.initrd"
local mariner_initrd_path="/opt/kata/share/kata-containers/kata-containers-initrd-mariner.img"
for K8S_TEST_YAML in runtimeclass_workloads_work/*.yaml for K8S_TEST_YAML in runtimeclass_workloads_work/*.yaml
do do
add_kernel_initrd_annotations_to_yaml "${K8S_TEST_YAML}" add_annotations_to_yaml "${K8S_TEST_YAML}" "${mariner_annotation_kernel}" "${mariner_kernel_path}"
add_annotations_to_yaml "${K8S_TEST_YAML}" "${mariner_annotation_initrd}" "${mariner_initrd_path}"
done done
fi fi
} }
@ -92,7 +97,7 @@ add_kernel_initrd_annotations() {
main() { main() {
ensure_yq ensure_yq
reset_workloads_work_dir reset_workloads_work_dir
add_kernel_initrd_annotations add_cbl_mariner_kernel_initrd_annotations
} }
main "$@" main "$@"