Skip to content

Commit 155dc39

Browse files
author
vsok
committed
RHAIENG-780: Sanity check to find the diff in Dockerfiles
Scan Jupyter image Dockerfiles to detect differences (diffs) between the upstream and downstream (Konflux) versions (i.e., between Dockerfile.x and Dockerfile.konflux.x). This script has been integrated into the make test target. This script performs the sanity check and will fail (error out) if any differences are found, requiring immediate attention to align the Konflux Dockerfiles with their upstream counterparts.
1 parent 8adf444 commit 155dc39

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,3 +516,4 @@ print-release:
516516
test:
517517
@echo "Running quick static tests"
518518
uv run pytest -m 'not buildonlytest'
519+
@./scripts/check_dockerfile_alignment.sh $*
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
#!/usr/bin/env bash
2+
#=========================================================
3+
# Script: dockerfile_diff_checker.sh
4+
# Purpose: Scan a directory tree for Dockerfile.konflux.*
5+
# and compare each with its original Dockerfile,
6+
# ignoring comments and multi-line LABEL instructions.
7+
#=========================================================
8+
9+
set -euo pipefail
10+
11+
#---------------------------------------------------------
12+
# Main script execution
13+
#---------------------------------------------------------
14+
main() {
15+
16+
# Define multiple starting directories
17+
local start_dirs=("./jupyter" "./codeserver" "./runtimes")
18+
echo "Scanning ${start_dirs[*]} for directories containing Dockerfile.konflux.*"
19+
echo "Comparing Dockerfiles, ignoring comments and LABEL blocks..."
20+
21+
# Populate array of directories
22+
local docker_dirs=()
23+
for dir in "${start_dirs[@]}"; do
24+
mapfile -t dirs < <(find_docker_dirs "$dir")
25+
docker_dirs+=("${dirs[@]}")
26+
done
27+
28+
# Process all directories and check for differences
29+
if process_dirs "${docker_dirs[@]}"; then
30+
echo "✅ All Dockerfiles are in sync."
31+
else
32+
echo "❌ Differences were found. Please inspect."
33+
exit 1
34+
fi
35+
}
36+
37+
#---------------------------------------------------------
38+
# Function: find_docker_dirs
39+
# Description:
40+
# Recursively search for directories containing files named
41+
# "Dockerfile.konflux.*". Each directory is listed only once.
42+
# Arguments:
43+
# $1 - starting directory
44+
# Returns:
45+
# Prints each directory path containing at least one
46+
# Dockerfile.konflux.* file
47+
#---------------------------------------------------------
48+
find_docker_dirs() {
49+
local start_dir="$1"
50+
51+
# Use 'find' to locate matching files, then 'dirname' to extract directories, 'sort -u' to remove duplicates
52+
find "$start_dir" -type f -name "Dockerfile.konflux.*" -exec dirname {} \; | sort -u
53+
}
54+
55+
#---------------------------------------------------------
56+
# Function: strip
57+
# Description:
58+
# Remove comments and ignore LABEL blocks from a Dockerfile.
59+
# Useful for comparing Dockerfiles while ignoring cosmetic differences.
60+
# Arguments:
61+
# Reads from standard input
62+
# Returns:
63+
# Prints stripped Dockerfile to standard output
64+
#---------------------------------------------------------
65+
strip() {
66+
awk '
67+
BEGIN { in_label = 0 }
68+
69+
# Skip full-line comments
70+
/^[[:space:]]*#/ { next }
71+
72+
# Skip lines inside multi-line LABEL blocks
73+
in_label {
74+
# End LABEL block if line does not end with backslash
75+
if ($0 !~ /\\$/) in_label = 0
76+
next
77+
}
78+
79+
# Detect start of LABEL instruction
80+
/^[[:space:]]*LABEL([ \t]|$)/ {
81+
# Multi-line LABEL block
82+
if ($0 ~ /\\$/) in_label = 1
83+
next
84+
}
85+
86+
# Print all other lines (trimmed)
87+
{
88+
# Trim leading and trailing whitespace
89+
gsub(/^[ \t]+|[ \t]+$/, "", $0)
90+
if (length($0) > 0) print $0
91+
}
92+
'
93+
}
94+
95+
#---------------------------------------------------------
96+
# Function: find_diff
97+
# Description:
98+
# Compare a Dockerfile with its corresponding konflux version,
99+
# ignoring comments and LABEL blocks. Returns 1 if differences exist.
100+
# Arguments:
101+
# $1 - Directory containing the Dockerfiles
102+
# $2 - Original Dockerfile name (basename)
103+
# $3 - Konflux Dockerfile name (basename)
104+
# Returns:
105+
# 0 if no differences, 1 if differences exist
106+
#---------------------------------------------------------
107+
find_diff() {
108+
local dir="$1"
109+
local file_orig="$2"
110+
local file_konflux="$3"
111+
112+
echo "---- diff $file_orig $file_konflux ----"
113+
114+
# Use process substitution to feed stripped files to diff
115+
local diff_output
116+
diff_output=$(diff --color=always <(strip <"$dir/$file_orig") <(strip <"$dir/$file_konflux") || true)
117+
118+
if [ -n "$diff_output" ]; then
119+
echo "❌ Differences found between $file_orig and $file_konflux"
120+
# Uncomment the next line to see detailed differences
121+
echo "$diff_output"
122+
return 1
123+
else
124+
echo "✅ No differences"
125+
return 0
126+
fi
127+
}
128+
129+
#---------------------------------------------------------
130+
# Function: process_dirs
131+
# Description:
132+
# Iterate over a list of directories, find konflux Dockerfiles
133+
# in each, and compare them with their originals.
134+
# Arguments:
135+
# Array of directories
136+
# Returns:
137+
# 0 if all Dockerfiles match, 1 if any differences exist
138+
#---------------------------------------------------------
139+
process_dirs() {
140+
local dirs=("$@")
141+
local diff_found=0
142+
143+
#dirs=("./jupyter/datascience/ubi9-python-3.12")
144+
145+
# Iterate over each directory
146+
for dir in "${dirs[@]}"; do
147+
echo "=== Processing $dir ==="
148+
149+
# Iterate over each konflux Dockerfile
150+
for konflux_file in "$dir"/Dockerfile.konflux.*; do
151+
[ -e "$konflux_file" ] || continue
152+
153+
# Derive the original Dockerfile name by removing the .konflux
154+
local dockerfile="${konflux_file/.konflux/}"
155+
156+
# Check if the original Dockerfile exists
157+
if [ ! -f "$dockerfile" ]; then
158+
echo "⚠️ $dockerfile not found in $dir" >&2
159+
continue
160+
fi
161+
162+
# Compare the files
163+
if ! find_diff "$dir" "$(basename "$dockerfile")" "$(basename "$konflux_file")"; then
164+
diff_found=1
165+
fi
166+
done
167+
done
168+
169+
return $diff_found
170+
}
171+
172+
# Call main with all script arguments
173+
main "$@"

0 commit comments

Comments
 (0)