Skip to content

Commit bc74cf4

Browse files
authored
chore(hack): avoid updating some unversioned modules (#6719)
1 parent 78d4cdf commit bc74cf4

File tree

2 files changed

+58
-17
lines changed

2 files changed

+58
-17
lines changed

hack/lib/modules.sh

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,57 +85,63 @@ modules::get_indirect_deps() {
8585

8686
# Update modules in batch
8787
# Set GOPROXY=direct to avoid https://github.com/golang/go/issues/49111
88-
# Args: dry_run patch_only modules...
88+
# Args: dry_run version_query modules...
89+
# - version_query: "latest" or "patch"
8990
modules::update_modules_batch() {
9091
local dry_run=$1
91-
local patch_only=$2
92+
local version_query=$2
9293
shift 2
9394
local modules=("$@")
9495

9596
if [[ ${#modules[@]} -eq 0 ]]; then
9697
return 0
9798
fi
9899

99-
local flag="-u"
100-
local mode="latest"
101-
if [[ "$patch_only" == "true" ]]; then
102-
flag="-u=patch"
103-
mode="patch only"
104-
fi
100+
# Build module specs with version query (e.g., module@latest or module@patch)
101+
local module_specs=()
102+
for module in "${modules[@]}"; do
103+
module_specs+=("${module}@${version_query}")
104+
done
105105

106-
modules::info " Updating ${#modules[@]} modules ($mode): ${modules[*]}"
106+
modules::info " Updating ${#modules[@]} modules (@${version_query}): ${modules[*]}"
107107

108108
if [[ "$dry_run" == "true" ]]; then
109-
echo " [DRY-RUN] GOPROXY=direct go get $flag ${modules[*]}"
109+
echo " [DRY-RUN] GOPROXY=direct go get ${module_specs[*]}"
110110
else
111-
if ! GOPROXY=direct go get "$flag" "${modules[@]}" 2>&1; then
111+
if ! GOPROXY=direct go get "${module_specs[@]}" 2>&1; then
112112
modules::error " Some modules failed to update"
113113
return 1
114114
fi
115115
fi
116116
}
117117

118118
# Update all modules in a directory
119-
# Args: dir dry_run patch_prefixes_str allowed_prefixes_str
119+
# Args: dir dry_run patch_prefixes_str allowed_prefixes_str denied_prefixes_str
120120
# - dir: directory containing go.mod
121121
# - dry_run: "true" or "false"
122122
# - patch_prefixes_str: space-separated prefixes for patch-only updates
123123
# - allowed_prefixes_str: space-separated prefixes to filter modules (only update matching modules, empty means all direct deps)
124+
# - denied_prefixes_str: space-separated prefixes to exclude modules (if a module matches both allowed and denied, it will NOT be updated)
124125
modules::update_dir() {
125126
local dir=$1
126127
local dry_run=$2
127128
local patch_prefixes_str=$3
128129
local allowed_prefixes_str=$4
130+
local denied_prefixes_str=${5:-}
129131

130132
# Convert prefix strings to arrays
131133
local patch_prefixes=()
132134
local allowed_prefixes=()
135+
local denied_prefixes=()
133136
if [[ -n "$patch_prefixes_str" ]]; then
134137
read -ra patch_prefixes <<< "$patch_prefixes_str"
135138
fi
136139
if [[ -n "$allowed_prefixes_str" ]]; then
137140
read -ra allowed_prefixes <<< "$allowed_prefixes_str"
138141
fi
142+
if [[ -n "$denied_prefixes_str" ]]; then
143+
read -ra denied_prefixes <<< "$denied_prefixes_str"
144+
fi
139145

140146
if [[ ! -f "$dir/go.mod" ]]; then
141147
modules::error "No go.mod found in $dir"
@@ -163,13 +169,22 @@ modules::update_dir() {
163169
# Helper function to categorize a module
164170
categorize_module() {
165171
local dep=$1
172+
# Skip if module matches any denied prefix
173+
if [[ ${#denied_prefixes[@]} -gt 0 ]] && modules::match_prefix "$dep" "${denied_prefixes[@]}"; then
174+
modules::info " Skipping denied module: $dep"
175+
return 0
176+
fi
166177
if [[ ${#patch_prefixes[@]} -gt 0 ]] && modules::match_prefix "$dep" "${patch_prefixes[@]}"; then
167178
patch_modules+=("$dep")
168179
else
169180
latest_modules+=("$dep")
170181
fi
171182
}
172183

184+
if [[ ${#denied_prefixes[@]} -gt 0 ]]; then
185+
modules::info " Excluding modules by denied prefixes: ${denied_prefixes[*]}"
186+
fi
187+
173188
if [[ ${#allowed_prefixes[@]} -gt 0 ]]; then
174189
# Filter mode: only update modules matching allowed prefixes (both direct and indirect)
175190
modules::info " Filtering modules by allowed prefixes: ${allowed_prefixes[*]}"
@@ -206,15 +221,15 @@ modules::update_dir() {
206221
fi
207222
fi
208223

209-
# Batch update modules
224+
# Update modules
210225
if [[ ${#latest_modules[@]} -gt 0 ]]; then
211-
if ! modules::update_modules_batch "$dry_run" "false" "${latest_modules[@]}"; then
226+
if ! modules::update_modules_batch "$dry_run" "latest" "${latest_modules[@]}"; then
212227
popd > /dev/null
213228
return 1
214229
fi
215230
fi
216231
if [[ ${#patch_modules[@]} -gt 0 ]]; then
217-
if ! modules::update_modules_batch "$dry_run" "true" "${patch_modules[@]}"; then
232+
if ! modules::update_modules_batch "$dry_run" "patch" "${patch_modules[@]}"; then
218233
popd > /dev/null
219234
return 1
220235
fi

hack/update-modules.sh

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# Options:
2222
# -p, --patch-only PREFIX Only update patch version for modules matching PREFIX (can be specified multiple times)
2323
# -a, --allowed PREFIX Only update modules matching PREFIX (can be specified multiple times, includes both direct and indirect)
24+
# -x, --denied PREFIX Exclude modules matching PREFIX (can be specified multiple times, takes precedence over allowed)
2425
# -d, --dir DIR Directory containing go.mod to update (can be specified multiple times, default: all go.mod files)
2526
# -n, --dry-run Show what would be done without making changes
2627
# -h, --help Show this help message
@@ -38,8 +39,11 @@
3839
# # Only update modules matching specific prefixes (both direct and indirect)
3940
# ./hack/update-modules.sh -a golang.org -a github.com/aws
4041
#
42+
# # Exclude specific modules from updates
43+
# ./hack/update-modules.sh -a golang.org -x golang.org/x/net
44+
#
4145
# # Combine options
42-
# ./hack/update-modules.sh -p k8s.io -p sigs.k8s.io -a golang.org -d .
46+
# ./hack/update-modules.sh -p k8s.io -p sigs.k8s.io -a golang.org -x golang.org/x/net -d .
4347

4448
set -o errexit
4549
set -o nounset
@@ -73,6 +77,12 @@ ALLOWED_PREFIXES=(
7377
"github.com/golangci/golangci-lint/v2"
7478
"github.com/apache/skywalking-eyes"
7579
)
80+
# DENIED_PREFIXES: Modules matching these prefixes will NOT be updated, even if they match ALLOWED_PREFIXES.
81+
DENIED_PREFIXES=(
82+
"k8s.io/gengo/v2"
83+
"k8s.io/kube-openapi"
84+
"k8s.io/utils"
85+
)
7686
DIRS=()
7787
DRY_RUN=false
7888

@@ -102,6 +112,14 @@ parse_args() {
102112
ALLOWED_PREFIXES+=("$2")
103113
shift 2
104114
;;
115+
-x|--denied)
116+
if [[ -z "${2:-}" ]]; then
117+
modules::error "Option -x/--denied requires a PREFIX argument"
118+
usage
119+
fi
120+
DENIED_PREFIXES+=("$2")
121+
shift 2
122+
;;
105123
-d|--dir)
106124
if [[ -z "${2:-}" ]]; then
107125
modules::error "Option -d/--dir requires a directory argument"
@@ -158,6 +176,13 @@ main() {
158176
modules::info "No allowed prefixes specified, updating all direct dependencies"
159177
fi
160178

179+
if [[ ${#DENIED_PREFIXES[@]} -gt 0 ]]; then
180+
modules::info "Denied module prefixes (excluded):"
181+
for prefix in "${DENIED_PREFIXES[@]}"; do
182+
echo " - $prefix"
183+
done
184+
fi
185+
161186
# Determine directories to update
162187
local dirs_to_update=()
163188
if [[ ${#DIRS[@]} -eq 0 ]]; then
@@ -184,11 +209,12 @@ main() {
184209
# Convert arrays to space-separated strings for passing to function
185210
local patch_prefixes_str="${PATCH_ONLY_PREFIXES[*]}"
186211
local allowed_prefixes_str="${ALLOWED_PREFIXES[*]}"
212+
local denied_prefixes_str="${DENIED_PREFIXES[*]:-}"
187213

188214
# Update each directory
189215
local failed_dirs=()
190216
for dir in "${dirs_to_update[@]}"; do
191-
if ! modules::update_dir "$dir" "$DRY_RUN" "$patch_prefixes_str" "$allowed_prefixes_str"; then
217+
if ! modules::update_dir "$dir" "$DRY_RUN" "$patch_prefixes_str" "$allowed_prefixes_str" "$denied_prefixes_str"; then
192218
failed_dirs+=("$dir")
193219
fi
194220
echo

0 commit comments

Comments
 (0)