Skip to content

Commit

Permalink
ci(scripts/helm): add consolidated helm values generator
Browse files Browse the repository at this point in the history
Signed-off-by: Niladri Halder <niladri.halder26@gmail.com>
  • Loading branch information
niladrih committed Jan 9, 2024
1 parent ab96302 commit fe0b5ce
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 4 deletions.
17 changes: 13 additions & 4 deletions nix/pkgs/images/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{ dockerTools, lib, extensions, busybox, gnupg, kubernetes-helm-wrapped, semver-tool, yq-go, runCommand, img_tag ? "" }:
let
whitelistSource = extensions.project-builder.whitelistSource;
helm_chart = whitelistSource ../../.. [ "chart" "scripts/helm" ] "mayastor-extensions";
helm_chart = whitelistSource ../../.. [ "chart" "scripts/helm" "scripts/utils" ] "mayastor-extensions";
image_suffix = { "release" = ""; "debug" = "-debug"; "coverage" = "-coverage"; };
tag = if img_tag != "" then img_tag else extensions.version;
build-extensions-image = { pname, buildType, package, extraCommands ? '''', copyToRoot ? [ ], config ? { } }:
Expand Down Expand Up @@ -36,10 +36,12 @@ let
} ''
mkdir -p build && cp -drf ${helm_chart}/* build
chmod +w build/scripts/helm
chmod +w build/chart
chmod +w build/chart/*.yaml
chmod -R +w build/scripts/helm
chmod -R +w build/scripts/utils
chmod -R +w build/chart
patchShebangs build/scripts/helm/publish-chart-yaml.sh
patchShebangs build/scripts/helm/generate-consolidated-values.sh
patchShebangs build/scripts/utils/log.sh
# if tag is not semver just keep whatever is checked-in
# todo: handle this properly?
Expand All @@ -49,6 +51,13 @@ let
[[ ! ${tag} =~ ^(v?[0-9]+\.[0-9]+\.[0-9]+-0-main-unstable(-[0-9]+){6}-0)$ ]]; then
CHART_FILE=build/chart/Chart.yaml build/scripts/helm/publish-chart-yaml.sh --app-tag ${tag} --override-index ""
fi
# This modifies the build helm chart in-place with missing values of the
# dependent charts, i.e. the values from the dependent helm charts which
# are left out of the main helm chart, are set explicitly to their default
# values and are included into the main helm chart.
build/scripts/helm/generate-consolidated-values.sh -d build/chart
chmod -w build/chart
chmod -w build/chart/*.yaml
Expand Down
80 changes: 80 additions & 0 deletions scripts/helm/generate-consolidated-values.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env bash

set -o errexit

SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]:-"$0"}")")"
DEFAULT_CHART_DIR="$SCRIPT_DIR/../../chart"
CHART_DIR="$DEFAULT_CHART_DIR"

# Imports
source "$SCRIPT_DIR/../utils/log.sh"

# Print usage options for this script.
print_help() {
cat <<EOF
Usage: $(basename "${0}") [OPTIONS]
Options:
-h, --help Display this text
-d, --chart-dir <DIRECTORY> Specify the helm chart directory (default "$DEFAULT_CHART_DIR")
Examples:
$(basename "${0}") --chart-dir "./chart"
EOF
}

# Parse arguments.
parse_args() {
while test $# -gt 0; do
arg="$1"
case "$arg" in
-d | --chart-dir)
test $# -lt 2 && log_fatal "missing value for the optional argument '$arg'"
CHART_DIR="${2%/}"
shift
;;
-d=* | --chart-dir=*)
CHART_DIR="${arg#*=}"
;;
-h* | --help*)
print_help
exit 0
;;
*)
print_help
log_fatal "unexpected argument '$arg'"
;;
esac
shift
done
}

# Generate in-place consolidated values YAMLs throughout the
# helm chart hierarchy (root chart and sub-charts).
consolidate() {
local -r chart_dir="$1"
local -r chart_name="${chart_dir##*/}"

if stat "$chart_dir"/charts &> /dev/null; then
for dir in "$chart_dir"/charts/*; do
consolidate "$dir"
done
fi

if [[ $(yq ".$chart_name" "$chart_dir"/../../values.yaml) == null ]]; then
yq -i ".$chart_name = {}" "$chart_dir"/../../values.yaml
fi

yq -i ".$chart_name |= (load(\"$chart_dir/values.yaml\") * .)" "$chart_dir"/../../values.yaml
}

# Parse CLI args.
parse_args "$@"

if ! stat "$CHART_DIR"/charts &> /dev/null; then
exit 0
fi

for dir in "$CHART_DIR"/charts/*; do
consolidate "$dir"
done

0 comments on commit fe0b5ce

Please sign in to comment.