Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CNF-14823: Enforce reference and source-crs synchronization via CI #2070

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions ztp/kube-compare-reference/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
17 changes: 13 additions & 4 deletions ztp/kube-compare-reference/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,24 @@ 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
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
118 changes: 106 additions & 12 deletions ztp/kube-compare-reference/compare.sh
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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
}
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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