mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-03 19:58:17 +00:00 
			
		
		
		
	pin-dependencies.sh: support switching repos
Sometimes it is useful to fork a dependency in a different repo and then use that forked code in Kubernetes. Normally one would do `go mod edit -replace ...=/local/path` but that has drawbacks: - repos under staging are not updated - sharing the modified Kubernetes with others is harder, for example in a WIP or RFC PR The revised pin-dependencies.sh supports this with an optional =<replacement> part in the dependency parameter. Determining the revision upfront with `go mod download` also makes the script simpler.
This commit is contained in:
		@@ -40,12 +40,31 @@ kube::util::require-jq
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
dep="${1:-}"
 | 
					dep="${1:-}"
 | 
				
			||||||
sha="${2:-}"
 | 
					sha="${2:-}"
 | 
				
			||||||
if [[ -z "${dep}" || -z "${sha}" ]]; then
 | 
					
 | 
				
			||||||
 | 
					# Specifying a different repo is optional.
 | 
				
			||||||
 | 
					replacement=
 | 
				
			||||||
 | 
					case ${dep} in
 | 
				
			||||||
 | 
					    *=*)
 | 
				
			||||||
 | 
					        # shellcheck disable=SC2001
 | 
				
			||||||
 | 
					        replacement=$(echo "${dep}" | sed -e 's/.*=//')
 | 
				
			||||||
 | 
					        # shellcheck disable=SC2001
 | 
				
			||||||
 | 
					        dep=$(echo "${dep}" | sed -e 's/=.*//')
 | 
				
			||||||
 | 
					        ;;
 | 
				
			||||||
 | 
					    *)
 | 
				
			||||||
 | 
					        replacement="${dep}"
 | 
				
			||||||
 | 
					        ;;
 | 
				
			||||||
 | 
					esac
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if [[ -z "${dep}" || -z "${replacement}" || -z "${sha}" ]]; then
 | 
				
			||||||
  echo "Usage:"
 | 
					  echo "Usage:"
 | 
				
			||||||
  echo "  hack/pin-dependency.sh \$MODULE \$SHA-OR-TAG"
 | 
					  echo "  hack/pin-dependency.sh \$MODULE[=\$REPLACEMENT] \$SHA-OR-TAG"
 | 
				
			||||||
  echo ""
 | 
					  echo ""
 | 
				
			||||||
  echo "Example:"
 | 
					  echo "Examples:"
 | 
				
			||||||
  echo "  hack/pin-dependency.sh github.com/docker/docker 501cb131a7b7"
 | 
					  echo "  hack/pin-dependency.sh github.com/docker/docker 501cb131a7b7"
 | 
				
			||||||
 | 
					  echo "  hack/pin-dependency.sh github.com/docker/docker=github.com/johndoe/docker my-experimental-branch"
 | 
				
			||||||
 | 
					  echo ""
 | 
				
			||||||
 | 
					  echo "Replacing with a different repository is useful for testing but"
 | 
				
			||||||
 | 
					  echo "the result should never be merged into Kubernetes!"
 | 
				
			||||||
  echo ""
 | 
					  echo ""
 | 
				
			||||||
  exit 1
 | 
					  exit 1
 | 
				
			||||||
fi
 | 
					fi
 | 
				
			||||||
@@ -58,41 +77,32 @@ trap "cleanup" EXIT SIGINT
 | 
				
			|||||||
cleanup
 | 
					cleanup
 | 
				
			||||||
mkdir -p "${_tmp}"
 | 
					mkdir -p "${_tmp}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Add the require directive
 | 
					 | 
				
			||||||
echo "Running: go get ${dep}@${sha}"
 | 
					 | 
				
			||||||
go get -d "${dep}@${sha}"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Find the resolved version
 | 
					# Find the resolved version before trying to use it.
 | 
				
			||||||
rev=$(go mod edit -json | jq -r ".Require[] | select(.Path == \"${dep}\") | .Version")
 | 
					echo "Running: go mod download ${replacement}@${sha}"
 | 
				
			||||||
 | 
					if meta=$(go mod download -json "${replacement}@${sha}"); then
 | 
				
			||||||
# No entry in go.mod, we must be using the natural version indirectly
 | 
					    rev=$(echo "${meta}" | jq -r ".Version")
 | 
				
			||||||
if [[ -z "${rev}" ]]; then
 | 
					else
 | 
				
			||||||
  # backup the go.mod file, since go list modifies it
 | 
					    error=$(echo "${meta}" | jq -r ".Error")
 | 
				
			||||||
  cp go.mod "${_tmp}/go.mod.bak"
 | 
					    echo "Download failed: ${error}" >&2
 | 
				
			||||||
  # find the revision
 | 
					 | 
				
			||||||
  rev=$(go list -m -json "${dep}" | jq -r .Version)
 | 
					 | 
				
			||||||
  # restore the go.mod file
 | 
					 | 
				
			||||||
  mv "${_tmp}/go.mod.bak" go.mod
 | 
					 | 
				
			||||||
fi
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# No entry found
 | 
					 | 
				
			||||||
if [[ -z "${rev}" ]]; then
 | 
					 | 
				
			||||||
  echo "Could not resolve ${sha}"
 | 
					 | 
				
			||||||
    exit 1
 | 
					    exit 1
 | 
				
			||||||
fi
 | 
					fi
 | 
				
			||||||
 | 
					echo "Resolved to ${replacement}@${rev}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
echo "Resolved to ${dep}@${rev}"
 | 
					# Add the require directive
 | 
				
			||||||
 | 
					echo "Running: go mod edit -require ${dep}@${rev}"
 | 
				
			||||||
 | 
					go mod edit -require "${dep}@${rev}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Add the replace directive
 | 
					# Add the replace directive
 | 
				
			||||||
echo "Running: go mod edit -replace ${dep}=${dep}@${rev}"
 | 
					echo "Running: go mod edit -replace ${dep}=${replacement}@${rev}"
 | 
				
			||||||
go mod edit -replace "${dep}=${dep}@${rev}"
 | 
					go mod edit -replace "${dep}=${replacement}@${rev}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Propagate pinned version to staging repos that also have that dependency
 | 
					# Propagate pinned version to staging repos that also have that dependency
 | 
				
			||||||
for repo in $(kube::util::list_staging_repos); do
 | 
					for repo in $(kube::util::list_staging_repos); do
 | 
				
			||||||
  pushd "staging/src/k8s.io/${repo}" >/dev/null 2>&1
 | 
					  pushd "staging/src/k8s.io/${repo}" >/dev/null 2>&1
 | 
				
			||||||
    if go mod edit -json | jq -e -r ".Require[] | select(.Path == \"${dep}\")" > /dev/null 2>&1; then
 | 
					    if go mod edit -json | jq -e -r ".Require[] | select(.Path == \"${dep}\")" > /dev/null 2>&1; then
 | 
				
			||||||
      go mod edit -require "${dep}@${rev}"
 | 
					      go mod edit -require "${dep}@${rev}"
 | 
				
			||||||
      go mod edit -replace "${dep}=${dep}@${rev}"
 | 
					      go mod edit -replace "${dep}=${replacement}@${rev}"
 | 
				
			||||||
    fi
 | 
					    fi
 | 
				
			||||||
  popd >/dev/null 2>&1
 | 
					  popd >/dev/null 2>&1
 | 
				
			||||||
done
 | 
					done
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user