...
1#!/usr/bin/env bash
2
3# Copyright 2017 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 whether packages under `vendor` directory have cyclic
18# dependencies on `main` or `staging` repositories.
19# Usage: `hack/verify-no-vendor-cycles.sh`.
20
21set -o errexit
22set -o nounset
23set -o pipefail
24
25KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
26source "${KUBE_ROOT}/hack/lib/init.sh"
27
28kube::golang::setup_env
29
30staging_repos=()
31kube::util::read-array staging_repos < <(kube::util::list_staging_repos)
32staging_repos_pattern=$(IFS="|"; echo "${staging_repos[*]}")
33
34cd "${KUBE_ROOT}"
35
36# Check for any module that is not main or staging and depends on main or staging
37bad_deps=$(go mod graph | grep -vE "^k8s.io/(kubernetes|${staging_repos_pattern})" | grep -E "\sk8s.io/(kubernetes|${staging_repos_pattern})" || true)
38if [[ -n "${bad_deps}" ]]; then
39 echo "Found disallowed dependencies that transitively depend on k8s.io/kubernetes or staging modules:"
40 echo "${bad_deps}"
41 exit 1
42fi
43
44kube::util::ensure-temp-dir
45
46# Get vendored packages dependencies
47# Use -deps flag to include transitive dependencies
48go list -mod=vendor -test -tags other -e -deps -json ./vendor/... > "${KUBE_TEMP}/deps_other.json"
49go list -mod=vendor -test -tags linux -e -deps -json ./vendor/... > "${KUBE_TEMP}/deps_linux.json"
50go list -mod=vendor -test -tags windows -e -deps -json ./vendor/... > "${KUBE_TEMP}/deps_windows.json"
51
52# Check for any vendored package that imports main repo
53# Staging repos are explicitly excluded even though go list does not currently consider symlinks
54go run cmd/dependencycheck/dependencycheck.go -restrict "^k8s\.io/kubernetes/" -exclude "^k8s\.io/(${staging_repos_pattern})(/|$)" "${KUBE_TEMP}/deps_other.json"
55go run cmd/dependencycheck/dependencycheck.go -restrict "^k8s\.io/kubernetes/" -exclude "^k8s\.io/(${staging_repos_pattern})(/|$)" "${KUBE_TEMP}/deps_linux.json"
56go run cmd/dependencycheck/dependencycheck.go -restrict "^k8s\.io/kubernetes/" -exclude "^k8s\.io/(${staging_repos_pattern})(/|$)" "${KUBE_TEMP}/deps_windows.json"
57
58# Check for any vendored package that imports a staging repo
59# Staging repos are explicitly excluded even though go list does not currently consider symlinks
60go run cmd/dependencycheck/dependencycheck.go -restrict "^k8s\.io/(${staging_repos_pattern})(/|$)" -exclude "^k8s\.io/(${staging_repos_pattern})(/|$)" "${KUBE_TEMP}/deps_other.json"
61go run cmd/dependencycheck/dependencycheck.go -restrict "^k8s\.io/(${staging_repos_pattern})(/|$)" -exclude "^k8s\.io/(${staging_repos_pattern})(/|$)" "${KUBE_TEMP}/deps_linux.json"
62go run cmd/dependencycheck/dependencycheck.go -restrict "^k8s\.io/(${staging_repos_pattern})(/|$)" -exclude "^k8s\.io/(${staging_repos_pattern})(/|$)" "${KUBE_TEMP}/deps_windows.json"
View as plain text