Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark staging go module files as generated, add script to lint dependencies #76197

Merged
merged 3 commits into from Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitattributes
Expand Up @@ -7,3 +7,5 @@ test/test_owners.csv merge=union
**/generated.proto
**/types_swagger_doc_generated.go linguist-generated=true
api/openapi-spec/*.json linguist-generated=true
staging/**/go.mod linguist-generated=true
staging/**/go.sum linguist-generated=true
69 changes: 69 additions & 0 deletions hack/lint-dependencies.sh
@@ -0,0 +1,69 @@
#!/usr/bin/env bash

# Copyright 2019 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -o errexit
set -o nounset
set -o pipefail

KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"

# Explicitly opt into go modules, even though we're inside a GOPATH directory
export GO111MODULE=on
# Explicitly clear GOPATH, to ensure nothing this script calls makes use of that path info
export GOPATH=
# Explicitly clear GOFLAGS, since GOFLAGS=-mod=vendor breaks dependency resolution while rebuilding vendor
export GOFLAGS=
# Detect problematic GOPROXY settings that prevent lookup of dependencies
if [[ "${GOPROXY:-}" == "off" ]]; then
kube::log::error "Cannot run with \$GOPROXY=off"
exit 1
fi

kube::golang::verify_go_version
kube::util::require-jq

outdated=$(go list -m -json all | jq -r '
select(.Replace.Version != null) |
select(.Version != .Replace.Version) |
"\(.Path)
pinned: \(.Replace.Version)
preferred: \(.Version)
hack/pin-dependency.sh \(.Path) \(.Version)"
')
if [[ -n "${outdated}" ]]; then
echo "These modules are pinned to versions different than the minimal preferred version."
echo "That means that without require directives, a different version would be selected."
echo "The command to switch to the minimal preferred version is listed for each module."
echo ""
echo "${outdated}"
fi

unused=$(comm -23 \
<(go mod edit -json | jq -r '.Replace[] | select(.New.Version != null) | .Old.Path' | sort) \
<(go list -m -json all | jq -r .Path | sort))
if [[ -n "${unused}" ]]; then
echo ""
echo "Pinned module versions that aren't actually used:"
echo "${unused}"
fi

if [[ -n "${unused}${outdated}" ]]; then
exit 1
fi

echo "All pinned dependencies match their preferred version."
exit 0
27 changes: 26 additions & 1 deletion hack/pin-dependency.sh
Expand Up @@ -35,7 +35,7 @@ export GOPATH=
export GOFLAGS=
# Detect problematic GOPROXY settings that prevent lookup of dependencies
if [[ "${GOPROXY:-}" == "off" ]]; then
kube::log::error "Cannot run hack/pin-dependency.sh with \$GOPROXY=off"
kube::log::error "Cannot run with \$GOPROXY=off"
exit 1
fi

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

_tmp="${KUBE_ROOT}/_tmp"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay to _tmp like @cblecker asked for!

cleanup() {
rm -rf "${_tmp}"
}
trap "cleanup" EXIT SIGINT
cleanup
mkdir -p "${_tmp}"

# Add the require directive
echo "Running: go get ${dep}@${sha}"
go get -d "${dep}@${sha}"

# Find the resolved version
rev=$(go mod edit -json | jq -r ".Require[] | select(.Path == \"${dep}\") | .Version")

# No entry in go.mod, we must be using the natural version indirectly
if [[ -z "${rev}" ]]; then
# backup the go.mod file, since go list modifies it
cp go.mod "${_tmp}/go.mod.bak"
# 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
fi

echo "Resolved to ${dep}@${rev}"

# Add the replace directive
Expand Down
8 changes: 8 additions & 0 deletions hack/update-vendor.sh
Expand Up @@ -240,6 +240,14 @@ for repo in $(tsort "${TMP_DIR}/tidy_deps.txt"); do
$(go mod why ${loopback_deps})"
exit 1
fi

# prune unused pinned replace directives
comm -23 \
<(go mod edit -json | jq -r '.Replace[] | select(.Old.Path == .New.Path) | select(.New.Version != null) | .Old.Path' | sort) \
<(go list -m -json all | jq -r .Path | sort) |
xargs -L 1 -I {} echo "-dropreplace={}" |
xargs -L 100 go mod edit -fmt

popd >/dev/null 2>&1
done
echo "=== tidying root" >> "${LOG_FILE}"
Expand Down
233 changes: 0 additions & 233 deletions staging/src/k8s.io/api/go.mod

Large diffs are not rendered by default.

130 changes: 0 additions & 130 deletions staging/src/k8s.io/apiextensions-apiserver/go.mod

Large diffs are not rendered by default.

233 changes: 0 additions & 233 deletions staging/src/k8s.io/apimachinery/go.mod

Large diffs are not rendered by default.

153 changes: 0 additions & 153 deletions staging/src/k8s.io/apiserver/go.mod

Large diffs are not rendered by default.

207 changes: 0 additions & 207 deletions staging/src/k8s.io/cli-runtime/go.mod

Large diffs are not rendered by default.

220 changes: 0 additions & 220 deletions staging/src/k8s.io/client-go/go.mod

Large diffs are not rendered by default.

153 changes: 0 additions & 153 deletions staging/src/k8s.io/cloud-provider/go.mod

Large diffs are not rendered by default.

233 changes: 0 additions & 233 deletions staging/src/k8s.io/cluster-bootstrap/go.mod

Large diffs are not rendered by default.

253 changes: 0 additions & 253 deletions staging/src/k8s.io/code-generator/go.mod

Large diffs are not rendered by default.

232 changes: 0 additions & 232 deletions staging/src/k8s.io/component-base/go.mod

Large diffs are not rendered by default.

256 changes: 0 additions & 256 deletions staging/src/k8s.io/cri-api/go.mod

Large diffs are not rendered by default.

153 changes: 0 additions & 153 deletions staging/src/k8s.io/csi-translation-lib/go.mod

Large diffs are not rendered by default.

140 changes: 0 additions & 140 deletions staging/src/k8s.io/kube-aggregator/go.mod

Large diffs are not rendered by default.

232 changes: 0 additions & 232 deletions staging/src/k8s.io/kube-controller-manager/go.mod

Large diffs are not rendered by default.

232 changes: 0 additions & 232 deletions staging/src/k8s.io/kube-proxy/go.mod

Large diffs are not rendered by default.

232 changes: 0 additions & 232 deletions staging/src/k8s.io/kube-scheduler/go.mod

Large diffs are not rendered by default.

233 changes: 0 additions & 233 deletions staging/src/k8s.io/kubelet/go.mod

Large diffs are not rendered by default.

209 changes: 0 additions & 209 deletions staging/src/k8s.io/metrics/go.mod

Large diffs are not rendered by default.

209 changes: 0 additions & 209 deletions staging/src/k8s.io/node-api/go.mod

Large diffs are not rendered by default.

140 changes: 0 additions & 140 deletions staging/src/k8s.io/sample-apiserver/go.mod

Large diffs are not rendered by default.

207 changes: 0 additions & 207 deletions staging/src/k8s.io/sample-cli-plugin/go.mod

Large diffs are not rendered by default.

209 changes: 0 additions & 209 deletions staging/src/k8s.io/sample-controller/go.mod

Large diffs are not rendered by default.