diff --git a/ztp/kube-compare-reference/Makefile b/ztp/kube-compare-reference/Makefile index 4b2b921f2..c9da61778 100644 --- a/ztp/kube-compare-reference/Makefile +++ b/ztp/kube-compare-reference/Makefile @@ -7,7 +7,7 @@ HELM_URL := https://get.helm.sh/helm-v3.16.1-linux-amd64.tar.gz HELM_PKG := helm-linux-amd64.tar.gz .PHONY: check -check: metadata_lint +check: metadata_lint compare kubectl-cluster_compare: @echo "Downloading kube-compare tool" @@ -54,4 +54,8 @@ helm: .PHONY: compare compare: convert - @./compare.sh + @./compare.sh "../source-crs" renderedv1 + +.PHONY: sync +sync: convert + @./compare.sh --sync "../source-crs" renderedv1 diff --git a/ztp/kube-compare-reference/README.md b/ztp/kube-compare-reference/README.md index 9f283cdb1..8ff0308a6 100644 --- a/ztp/kube-compare-reference/README.md +++ b/ztp/kube-compare-reference/README.md @@ -11,7 +11,9 @@ Required and Optional RDS are structured based on internal document "4.16 Telco ## Developer Notes -The reference should be kept in-sync with ../source-crs +The reference must be kept in-sync with ../source-crs + +### CI Enforcement The `make compare` target provided in this directory will compare the reference, combining it with the examples in `default_value.yaml` and excluding @@ -19,7 +21,14 @@ CRs listed in `compare_ignore`, and comparing the resulting reference-rendered CRs with ../source-crs If this check fails, either the source-crs or reference must be altered until -no differences are observed. +no differences are observed by running `make compare` locally. + +### Update workflow + +There is also a target `make sync` that will copy all reference-based CRs to +the ../source-crs directory. Beware that any local edits to source-crs will be +erased by this action, so this is intended for a workflow as follows: -TODO: For now this is a manual activity, but it will soon be mandatory and -enforced by the github CI. +- Edit the reference to reflect the intended behavior +- Update `default_value.yaml` to contain appropriate placeholder data +- Run `make sync` to update source-crs to match the reference changes diff --git a/ztp/kube-compare-reference/compare.sh b/ztp/kube-compare-reference/compare.sh index 0693e688e..210075b17 100755 --- a/ztp/kube-compare-reference/compare.sh +++ b/ztp/kube-compare-reference/compare.sh @@ -1,11 +1,5 @@ #! /bin/bash -DIFF=${DIFF:-colordiff} -if ! command -v "$DIFF" >/dev/null; then - echo "Warning: Requested diff tool '$DIFF' is not found; falling back to plain old 'diff'" - DIFF="diff" -fi - trap cleanup EXIT function cleanup() { @@ -16,11 +10,11 @@ function read_dir() { local dir=$1 local file - for file in $(ls "$dir"); do - if [ -d "$dir""/""$file" ]; then - read_dir "$dir""/""$file" + for file in "$dir"/*; do + if [ -d "$file" ]; then + read_dir "$file" else - echo "$dir""/""$file" + echo "$file" fi done } @@ -31,6 +25,12 @@ function compare_cr { local exclusionfile=$3 local status=0 + local DIFF=${DIFF:-colordiff} + if ! command -v "$DIFF" >/dev/null; then + echo "Warning: Requested diff tool '$DIFF' is not found; falling back to plain old 'diff'" + DIFF="diff" + fi + read_dir "$rendered_dir" |grep yaml > rendered_file read_dir "$source_dir" |grep yaml > source_file @@ -43,7 +43,7 @@ function compare_cr { mv "$rendered.fixed" "$rendered" # Check the differences - if ! $DIFF -u "$source_cr" "$rendered"; then + if ! "$DIFF" -u "$source_cr" "$rendered"; then status=$(( status || 1 )) printf "\n\n**********************************************************************************\n\n" fi @@ -70,4 +70,98 @@ function compare_cr { return $status } -compare_cr renderedv1 ../source-crs compare_ignore +sync_cr() { + local rendered_dir=$1 + local source_dir=$2 + local exclusionfile=$3 + local status=0 + + local -a renderedFiles + readarray -t renderedFiles < <(read_dir "$rendered_dir" | grep yaml) + + local -a sourceFiles + readarray -t sourceFiles < <(read_dir "$source_dir" | grep yaml) + + local -a excludedFiles + readarray -t excludedFiles < <(grep -v '^#' "$exclusionfile" | grep -v '^$') + + local source rendered excluded found + for rendered in "${renderedFiles[@]}"; do + found=0 + for source in "${sourceFiles[@]}"; do + if [ "${source##*/}" = "${rendered##*/}" ]; then + # Match found! + found=1 + break + fi + done + if [[ $found == 0 ]]; then + source="$source_dir/${rendered##*/}" + fi + + # Replace the CR with the rendered copy (minus the helm-rendered heading) + tail -n +3 "$rendered" >"$source" + git add "$source" + done + + for source in "${sourceFiles[@]}"; do + found=0 + for rendered in "${renderedFiles[@]}"; do + if [ "${source##*/}" = "${rendered##*/}" ]; then + # Match found! + found=1 + break + fi + done + for excluded in "${excludedFiles[@]}"; do + if [ "${source##*/}" = "${excluded##*/}" ]; then + # Match found! + found=1 + break + fi + done + if [[ $found == 0 ]]; then + git rm -f "$source" + fi + done + + git diff --cached --stat --exit-code +} + +usage() { + echo "$(basename "$0") [--sync] sourceDir renderDir" + echo + echo "Compares the rendered reference-based CRs to the CRs in the compare directory" +} + +DOSYNC=0 +for arg in "$@"; do + case "$arg" in + -h | --help) + usage + exit 0 + ;; + --sync) + DOSYNC=1 + shift + ;; + esac +done +SOURCEDIR=$1 +if [[ ! -d $SOURCEDIR ]]; then + echo "No such source directory $SOURCEDIR" + usage + exit 1 +fi +RENDERDIR=$2 +if [[ ! -d $RENDERDIR ]]; then + echo "No such source directory $RENDERDIR" + usage + exit 1 +fi + +if [[ $DOSYNC == 1 ]]; then + sync_cr "$RENDERDIR" "$SOURCEDIR" compare_ignore +else + compare_cr "$RENDERDIR" "$SOURCEDIR" compare_ignore +fi