|
| 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 | +set -o errexit |
| 18 | +set -o nounset |
| 19 | +set -o pipefail |
| 20 | + |
| 21 | +KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. |
| 22 | +source "${KUBE_ROOT}/hack/lib/init.sh" |
| 23 | + |
| 24 | +# Explicitly opt into go modules, even though we're inside a GOPATH directory |
| 25 | +export GO111MODULE=on |
| 26 | +# Explicitly clear GOPATH, to ensure nothing this script calls makes use of that path info |
| 27 | +export GOPATH= |
| 28 | +# Explicitly clear GOFLAGS, since GOFLAGS=-mod=vendor breaks dependency resolution while rebuilding vendor |
| 29 | +export GOFLAGS= |
| 30 | +# Detect problematic GOPROXY settings that prevent lookup of dependencies |
| 31 | +if [[ "${GOPROXY:-}" == "off" ]]; then |
| 32 | + kube::log::error "Cannot run with \$GOPROXY=off" |
| 33 | + exit 1 |
| 34 | +fi |
| 35 | + |
| 36 | +kube::golang::verify_go_version |
| 37 | +kube::util::require-jq |
| 38 | + |
| 39 | +outdated=$(go list -m -json all | jq -r ' |
| 40 | + select(.Replace.Version != null) | |
| 41 | + select(.Version != .Replace.Version) | |
| 42 | + "\(.Path) |
| 43 | + pinned: \(.Replace.Version) |
| 44 | + preferred: \(.Version) |
| 45 | + hack/pin-dependency.sh \(.Path) \(.Version)" |
| 46 | +') |
| 47 | +if [[ -n "${outdated}" ]]; then |
| 48 | + echo "These modules are pinned to versions different than the minimal preferred version." |
| 49 | + echo "That means that without require directives, a different version would be selected." |
| 50 | + echo "The command to switch to the minimal preferred version is listed for each module." |
| 51 | + echo "" |
| 52 | + echo "${outdated}" |
| 53 | +fi |
| 54 | + |
| 55 | +unused=$(comm -23 \ |
| 56 | + <(go mod edit -json | jq -r '.Replace[] | select(.New.Version != null) | .Old.Path' | sort) \ |
| 57 | + <(go list -m -json all | jq -r .Path | sort)) |
| 58 | +if [[ -n "${unused}" ]]; then |
| 59 | + echo "" |
| 60 | + echo "Pinned module versions that aren't actually used:" |
| 61 | + echo "${unused}" |
| 62 | +fi |
| 63 | + |
| 64 | +if [[ -n "${unused}${outdated}" ]]; then |
| 65 | + exit 1 |
| 66 | +fi |
| 67 | + |
| 68 | +echo "All pinned dependencies match their preferred version." |
| 69 | +exit 0 |
0 commit comments