Skip to content

Instantly share code, notes, and snippets.

@Lumexralph
Created March 25, 2025 19:51
Show Gist options
  • Save Lumexralph/044b0af3fc4e5a3ad8b57294eb04edff to your computer and use it in GitHub Desktop.
Save Lumexralph/044b0af3fc4e5a3ad8b57294eb04edff to your computer and use it in GitHub Desktop.
#!/bin/bash
# This script is needed to help us resolve the k8s go modules that breaks:
# go list -m -json -mod=mod all
# Ref: https://github.com/kubernetes-csi/csi-driver-host-path/pull/583
# How to use: bash go_k8s_mod_update.sh <kubernetes-version>
# Strict error handling
set -euo pipefail
# Function to print usage and exit
usage() {
echo "Usage: $0 <kubernetes-version>"
echo "Example: $0 1.28.3"
exit 1
}
# Validate input
VERSION=${1#"v"} # Strip leading 'v' if present
if [ -z "$VERSION" ]; then
usage
fi
# Fetch Kubernetes modules from go.mod
echo "πŸ” Discovering Kubernetes modules..."
MODULES=$(
curl -sS "https://raw.githubusercontent.com/kubernetes/kubernetes/v${VERSION}/go.mod" |
grep -E "k8s.io/.* => ./staging/src/k8s.io" |
sed -E 's|.*k8s.io/([^ ]*) => ./staging/src/k8s.io/.*|k8s.io/\1|'
)
# Function to get module version
get_module_version() {
local module="$1"
go mod download -json "${module}@kubernetes-${VERSION}" |
grep -o '"Version": "[^"]*' |
cut -d'"' -f4
}
# Update modules
echo "πŸ”„ Updating Kubernetes modules to version v${VERSION}..."
for MODULE in $MODULES; do
echo " Processing module: $MODULE"
MODULE_VERSION=$(get_module_version "$MODULE")
if [ -n "$MODULE_VERSION" ]; then
go mod edit "-replace=${MODULE}=${MODULE}@${MODULE_VERSION}"
echo " βœ… Updated to ${MODULE_VERSION}"
else
echo " ❌ Could not fetch version for $MODULE"
fi
done
# Update Kubernetes dependency
echo "πŸš€ Finalizing Kubernetes version..."
go get "k8s.io/kubernetes@v${VERSION}"
echo "✨ Kubernetes module updates complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment