k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/hack/lint-dependencies.sh (about) 1 #!/usr/bin/env bash 2 3 # Copyright 2019 The Kubernetes Authors. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 # This script checks version dependencies of modules. It checks whether all 18 # pinned versions of checked dependencies match their preferred version or not. 19 # Usage: `hack/lint-dependencies.sh`. 20 21 set -o errexit 22 set -o nounset 23 set -o pipefail 24 25 KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. 26 source "${KUBE_ROOT}/hack/lib/init.sh" 27 28 # Detect problematic GOPROXY settings that prevent lookup of dependencies 29 if [[ "${GOPROXY:-}" == "off" ]]; then 30 kube::log::error "Cannot run with \$GOPROXY=off" 31 exit 1 32 fi 33 34 kube::golang::setup_env 35 kube::util::require-jq 36 37 # Set the Go environment, otherwise we get "can't compute 'all' using the 38 # vendor directory". 39 export GOWORK=off 40 export GOFLAGS=-mod=mod 41 42 # let us log all errors before we exit 43 rc=0 44 45 # List of dependencies we need to avoid dragging back into kubernetes/kubernetes 46 # Check if unwanted dependencies are removed 47 # The array and map in `unwanted-dependencies.json` are in alphabetical order. 48 go run k8s.io/kubernetes/cmd/dependencyverifier "${KUBE_ROOT}/hack/unwanted-dependencies.json" 49 50 outdated=$(go list -m -json all | jq -r " 51 select(.Replace.Version != null) | 52 select(.Version != .Replace.Version) | 53 select(.Path) | 54 \"\(.Path) 55 pinned: \(.Replace.Version) 56 preferred: \(.Version) 57 hack/pin-dependency.sh \(.Path) \(.Version)\" 58 ") 59 if [[ -n "${outdated}" ]]; then 60 echo "These modules are pinned to versions different than the minimal preferred version." 61 echo "That means that without replace directives, a different version would be selected," 62 echo "which breaks consumers of our published modules." 63 echo "1. Use hack/pin-dependency.sh to switch to the preferred version for each module" 64 echo "2. Run hack/update-vendor.sh to rebuild the vendor directory" 65 echo "3. Run hack/lint-dependencies.sh to verify no additional changes are required" 66 echo "" 67 echo "${outdated}" 68 fi 69 70 noncanonical=$(go list -m -json all | jq -r " 71 select(.Replace.Version != null) | 72 select(.Path != .Replace.Path) | 73 select(.Path) | 74 \" \(.Path) is replaced with \(.Replace.Path)\" 75 ") 76 if [[ -n "${noncanonical}" ]]; then 77 echo "" 78 echo "These modules are pinned to non-canonical repos." 79 echo "Revert to using the canonical repo for these modules before merge" 80 echo "" 81 echo "${noncanonical}" 82 fi 83 84 unused=$(comm -23 \ 85 <(go mod edit -json | jq -r '.Replace[] | select(.New.Version != null) | .Old.Path' | sort) \ 86 <(go list -m -json all | jq -r .Path | sort)) 87 if [[ -n "${unused}" ]]; then 88 echo "" 89 echo "Use the given commands to remove pinned module versions that aren't actually used:" 90 echo "${unused}" | xargs -L 1 echo 'go mod edit -dropreplace' 91 fi 92 93 if [[ -n "${unused}${outdated}${noncanonical}" ]]; then 94 rc=1 95 else 96 echo "All pinned versions of checked dependencies match their preferred version." 97 fi 98 99 exit $rc