Skip to content

Commit 8a2f8e6

Browse files
authored
Merge pull request kubernetes#76197 from liggitt/generated-vendor
Mark staging go module files as generated, add script to lint dependencies
2 parents f0ac762 + ce3dad9 commit 8a2f8e6

File tree

26 files changed

+105
-4500
lines changed

26 files changed

+105
-4500
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ test/test_owners.csv merge=union
77
**/generated.proto
88
**/types_swagger_doc_generated.go linguist-generated=true
99
api/openapi-spec/*.json linguist-generated=true
10+
staging/**/go.mod linguist-generated=true
11+
staging/**/go.sum linguist-generated=true

hack/lint-dependencies.sh

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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

hack/pin-dependency.sh

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export GOPATH=
3535
export GOFLAGS=
3636
# Detect problematic GOPROXY settings that prevent lookup of dependencies
3737
if [[ "${GOPROXY:-}" == "off" ]]; then
38-
kube::log::error "Cannot run hack/pin-dependency.sh with \$GOPROXY=off"
38+
kube::log::error "Cannot run with \$GOPROXY=off"
3939
exit 1
4040
fi
4141

@@ -54,12 +54,37 @@ if [[ -z "${dep}" || -z "${sha}" ]]; then
5454
exit 1
5555
fi
5656

57+
_tmp="${KUBE_ROOT}/_tmp"
58+
cleanup() {
59+
rm -rf "${_tmp}"
60+
}
61+
trap "cleanup" EXIT SIGINT
62+
cleanup
63+
mkdir -p "${_tmp}"
64+
5765
# Add the require directive
5866
echo "Running: go get ${dep}@${sha}"
5967
go get -d "${dep}@${sha}"
6068

6169
# Find the resolved version
6270
rev=$(go mod edit -json | jq -r ".Require[] | select(.Path == \"${dep}\") | .Version")
71+
72+
# No entry in go.mod, we must be using the natural version indirectly
73+
if [[ -z "${rev}" ]]; then
74+
# backup the go.mod file, since go list modifies it
75+
cp go.mod "${_tmp}/go.mod.bak"
76+
# find the revision
77+
rev=$(go list -m -json "${dep}" | jq -r .Version)
78+
# restore the go.mod file
79+
mv "${_tmp}/go.mod.bak" go.mod
80+
fi
81+
82+
# No entry found
83+
if [[ -z "${rev}" ]]; then
84+
echo "Could not resolve ${sha}"
85+
exit 1
86+
fi
87+
6388
echo "Resolved to ${dep}@${rev}"
6489

6590
# Add the replace directive

hack/update-vendor.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,14 @@ for repo in $(tsort "${TMP_DIR}/tidy_deps.txt"); do
240240
$(go mod why ${loopback_deps})"
241241
exit 1
242242
fi
243+
244+
# prune unused pinned replace directives
245+
comm -23 \
246+
<(go mod edit -json | jq -r '.Replace[] | select(.Old.Path == .New.Path) | select(.New.Version != null) | .Old.Path' | sort) \
247+
<(go list -m -json all | jq -r .Path | sort) |
248+
xargs -L 1 -I {} echo "-dropreplace={}" |
249+
xargs -L 100 go mod edit -fmt
250+
243251
popd >/dev/null 2>&1
244252
done
245253
echo "=== tidying root" >> "${LOG_FILE}"

staging/src/k8s.io/api/go.mod

Lines changed: 0 additions & 233 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staging/src/k8s.io/apiextensions-apiserver/go.mod

Lines changed: 0 additions & 130 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staging/src/k8s.io/apimachinery/go.mod

Lines changed: 0 additions & 233 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staging/src/k8s.io/apiserver/go.mod

Lines changed: 0 additions & 153 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staging/src/k8s.io/cli-runtime/go.mod

Lines changed: 0 additions & 207 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staging/src/k8s.io/client-go/go.mod

Lines changed: 0 additions & 220 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)