-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathkustdiff
More file actions
executable file
·182 lines (145 loc) · 5.27 KB
/
kustdiff
File metadata and controls
executable file
·182 lines (145 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env bash
set -eux
TMP_DIR="$(mktemp -d)"
ROOT_DIR="${INPUT_ROOT_DIR:-${1:-./kustomize}}" # First check env var, then argument, then default
MAX_DEPTH="${INPUT_MAX_DEPTH:-2}" # Default to 2 if not specified
DEBUG="${DEBUG:-true}"
function debug_log() {
if [ "$DEBUG" = "true" ]; then
printf "[DEBUG] %s \n" "$1"
fi
}
function validate_root_dir() {
if [ ! -d "$ROOT_DIR" ]; then
echo "Error: Root directory '$ROOT_DIR' does not exist"
exit 1
fi
}
function validate_max_depth() {
if ! [[ "$MAX_DEPTH" =~ ^[0-9]+$ ]]; then
echo "Error: max_depth must be a positive integer, got: $MAX_DEPTH"
exit 1
fi
}
function get_targets {
find "$ROOT_DIR" -maxdepth "$MAX_DEPTH" -name kustomization.yaml -exec dirname {} \;
}
function safe_dirname() {
echo "$1" | sed 's/[^a-zA-Z0-9.]/_/g'
}
function safe_filename() {
echo "$1" | sed 's/[^a-zA-Z0-9.]/_/g'
}
function build_ref {
local ref="$1"
local output_dir="$2"
echo "Checking out ref: $ref"
git checkout "$ref" --quiet
mkdir -p "$output_dir"
for envpath in $(get_targets); do
local relative_path="${envpath#$ROOT_DIR/}"
local safe_path=$(safe_filename "$relative_path")
local output_file="$output_dir/${safe_path}.yaml"
echo "Running kustomize for $envpath"
# Check if kustomization.yaml contains helmCharts
local helm_flag=""
if grep -q "helmCharts:" "$envpath/kustomization.yaml"; then
debug_log "Helm charts detected in $envpath, enabling Helm support"
helm_flag="--enable-helm"
fi
kustomize build $helm_flag "$envpath" -o "$output_file"
if [ "$DEBUG" = "true" ]; then
debug_log "Built kustomize for $envpath to $output_file"
fi
done
}
function main {
# Validate inputs before proceeding
validate_root_dir
validate_max_depth
# Set up git identity for merge operations
git config --global user.email "github-actions@github.com" || true
git config --global user.name "GitHub Actions" || true
git config --global --add safe.directory "$GITHUB_WORKSPACE" || true
# Save current state to restore later
local current_branch
current_branch=$(git rev-parse --abbrev-ref HEAD || echo "detached")
# Check if the branch exists locally or as a remote reference
function resolve_branch_ref() {
local branch_name="$1"
# First check if it's a local branch
if git show-ref --verify --quiet "refs/heads/$branch_name"; then
echo "$branch_name"
return 0
fi
# Next check if it's a remote branch
if git show-ref --verify --quiet "refs/remotes/origin/$branch_name"; then
echo "origin/$branch_name"
return 0
fi
# Finally check if it's a valid commit SHA
if git cat-file -e "$branch_name^{commit}" 2>/dev/null; then
echo "$branch_name"
return 0
fi
# If we get here, we couldn't resolve the reference
echo "Error: Could not resolve reference: $branch_name"
return 1
}
# Resolve the BASE and HEAD references
local base_ref_resolved
base_ref_resolved=$(resolve_branch_ref "$INPUT_BASE_REF") || exit 1
debug_log "Resolved base reference: $base_ref_resolved (from $INPUT_BASE_REF)"
# Build BASE output
local safe_base_ref=$(safe_filename "$INPUT_BASE_REF")
local base_output_dir="$TMP_DIR/base"
build_ref "$base_ref_resolved" "$base_output_dir"
# Resolve HEAD reference
local head_ref_resolved
head_ref_resolved=$(resolve_branch_ref "$INPUT_HEAD_REF") || exit 1
debug_log "Resolved head reference: $head_ref_resolved (from $INPUT_HEAD_REF)"
# Create a temporary merge branch
local merge_branch="temp-merge-$RANDOM"
git checkout -b "$merge_branch" "$base_ref_resolved" --quiet
debug_log "Creating temporary merge of $head_ref_resolved into $base_ref_resolved (via $merge_branch)"
# Attempt to merge HEAD into BASE
if ! git merge "$head_ref_resolved" --quiet; then
echo "Merge conflict detected. Cannot automatically merge $INPUT_HEAD_REF into $INPUT_BASE_REF."
git merge --abort || true
git checkout "$current_branch" --quiet || git checkout "$base_ref_resolved" --quiet || true
exit 1
fi
# Build merged output
local merged_output_dir="$TMP_DIR/merged"
build_ref "$merge_branch" "$merged_output_dir"
# Compare outputs
set +e
diff=$(git diff --no-index "$base_output_dir" "$merged_output_dir" 2>&1)
local diff_exit_code=$?
debug_log "Git diff exit code: $diff_exit_code"
debug_log "Git diff output:"
debug_log "$diff"
debug_log "End of git diff output"
debug_log "------------------------------------"
# Clean up temporary branches
git checkout "$current_branch" --quiet || git checkout "$base_ref_resolved" --quiet || true
git branch -D "$merge_branch" --quiet || true
if [[ $diff_exit_code -eq 0 ]]; then
output="No differences found in kustomize output after merging $INPUT_HEAD_REF into $INPUT_BASE_REF"
else
# Just pass through the raw git diff output
output="$diff"
fi
local escaped_output=${output//$'\n'/'%0A'}
if [ ${#escaped_output} -gt 65000 ]; then
escaped_output="Output is greater than 65000 characters, and therefore too large to print as a github comment."
fi
echo "::set-output name=diff::$escaped_output"
}
# Print initial configuration
echo "Configuration:"
echo "ROOT_DIR: $ROOT_DIR"
echo "MAX_DEPTH: $MAX_DEPTH"
echo "DEBUG: $DEBUG"
debug_log "Debug mode is enabled"
main