diff --git a/build-tools/build-helm-charts.sh b/build-tools/build-helm-charts.sh index 29b1f6cc..35c5a5be 100755 --- a/build-tools/build-helm-charts.sh +++ b/build-tools/build-helm-charts.sh @@ -302,9 +302,34 @@ function build_application_tarball_armada { fi } +function compare_custom_manifests { + MANIFEST_LIST=$1 + for manifest in ${MANIFEST_LIST}; do + MAIN_MANIFESTS=$(ls usr/lib/fluxcd/${manifest}) + CUSTOM_MANIFESTS=$(ls usr/lib/fluxcd/custom-manifests/${manifest}) + + for manifest_file in ${MAIN_MANIFESTS}; do + if [ ! -f usr/lib/fluxcd/custom-manifests/${manifest}/${manifest_file} ]; then + echo "Warning: missing ${manifest_file} in custom manifests" + else + ${PYTHON_2_OR_3} $BUILD_HELM_CHARTS_DIR/merge_manifests.py usr/lib/fluxcd/${manifest}/${manifest_file} usr/lib/fluxcd/custom-manifests/${manifest}/${manifest_file} + fi + done + done +} + function build_application_tarball_fluxcd { FLUXCD_MANIFEST_DIR='fluxcd-manifests' + CUSTOM_MANIFEST_LIST=$(ls usr/lib/fluxcd/custom-manifests) + + if [ ${#CUSTOM_MANIFEST_LIST} -ne 0 ]; then + echo "Custom manifests detected. Merging custom manifests with FluxCD manifests" + compare_custom_manifests "${CUSTOM_MANIFEST_LIST}" + + # Removing custom manifests directory to prevent unnecessary files from being included + rm -rd usr/lib/fluxcd/custom-manifests + fi # Stage all the fluxcd manifests cp -R usr/lib/fluxcd staging/${FLUXCD_MANIFEST_DIR} diff --git a/build-tools/merge_manifests.py b/build-tools/merge_manifests.py new file mode 100644 index 00000000..0031f67b --- /dev/null +++ b/build-tools/merge_manifests.py @@ -0,0 +1,48 @@ +#!/usr/bin/python +# +# Copyright (c) 2024 Wind River Systems, Inc. +# +# SPDX-License-Identifier: Apache-2.0 +# + +import os +import sys +import ruamel.yaml as yaml + +def merge_dicts(main: dict, custom: dict) -> None: + for key, value in custom.items(): + if key in main and isinstance(main[key], dict) and isinstance(value, dict): + merge_dicts(main[key], value) + else: + main[key] = value + +def handle_manifests_merge(main_manifests_path: str, custom_manifests_path: str): + with open(main_manifests_path, 'r') as main_file, open(custom_manifests_path, 'r') as custom_file: + main_data = yaml.safe_load(main_file) + custom_data = yaml.safe_load(custom_file) + + # If both main file data and custom data are identical, no merge is needed + if main_data == custom_data: + return + + # Handle empty YAML files as empty dictionaries for comparison purposes + main_data = {} if main_data is None else main_data + custom_data = {} if custom_data is None else custom_data + + merged_data = main_data.copy() + + merge_dicts(merged_data, custom_data) + + with open(main_manifests_path, 'w') as main_file: + yaml.dump(merged_data, main_file, default_flow_style=False) + +if __name__ == "__main__": + + main_manifests_path = sys.argv[1] + custom_manifests_path = sys.argv[2] + + try: + handle_manifests_merge(main_manifests_path, custom_manifests_path) + except Exception as e: + print(f"Error trying to merge {main_manifests_path}: {e}") + sys.exit(1)