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

chore: parallel k8sio go.mod replace update script #801

Merged
merged 2 commits into from
Oct 25, 2024
Merged
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
31 changes: 27 additions & 4 deletions hack/update-k8sio-gomod-replace.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,35 @@ MODS=($(
curl -sS https://raw.githubusercontent.com/kubernetes/kubernetes/v${VERSION}/go.mod |
sed -n 's|.*k8s.io/\(.*\) => ./staging/src/k8s.io/.*|k8s.io/\1|p'
))
for MOD in "${MODS[@]}"; do
V=$(

# Set concurrency level to the number of available CPU cores
CONCURRENCY=$(nproc)
export VERSION

# Create an empty array to store replace directives
declare -a REPLACE_COMMANDS

# Function to generate replace directive for a module
generate_replace_command() {
local MOD=$1
local V=$(
go mod download -json "${MOD}@kubernetes-${VERSION}" |
sed -n 's|.*"Version": "\(.*\)".*|\1|p'
)
echo "Updating go.mod replace directive for ${MOD}"
go mod edit "-replace=${MOD}=${MOD}@${V}"
echo "-replace=${MOD}=${MOD}@${V}"
}

# Export function for access in subshells run by xargs
export -f generate_replace_command

# Run in parallel to collect replace directives
echo "Collecting replace directives for ${#MODS[@]} modules concurrently (N=${CONCURRENCY})"
REPLACE_COMMANDS=($(printf "%s\n" "${MODS[@]}" | xargs -P "$CONCURRENCY" -n 1 -I {} bash -c 'generate_replace_command "$@"' _ {}))

# Apply each replace directive serially
for CMD in "${REPLACE_COMMANDS[@]}"; do
echo "Applying go.mod $CMD"
go mod edit "$CMD"
done

go get "k8s.io/kubernetes@v${VERSION}"
Loading