
This makes "new" and "old" setup_env functions. In subsequent commits,
all callers of the "old" form will be fixed, and the "new" will be
renamed back.
The old and new functions diff:
```diff
--- /tmp/a 2023-12-14 09:02:57.804092696 -0800
+++ /tmp/b 2023-12-14 09:03:09.679999585 -0800
@@ -1,4 +1,4 @@
-kube::golang::old::setup_env() {
+kube::golang:🆕:setup_env() {
kube::golang::verify_go_version
# Set up GOPATH. We have tools which depend on being in a GOPATH (see
@@ -7,9 +7,9 @@
# Even in module mode, we need to set GOPATH for `go build` and `go install`
# to work. We build various tools (usually via `go install`) from a lot of
# scripts.
- # * We can't set GOBIN because that does not work on cross-compiles.
- # * We could use `go build -o <something>`, but it's subtle when it comes
- # to cross-compiles and whether the <something> is a file or a directory,
+ # * We can't just set GOBIN because that does not work on cross-compiles.
+ # * We could always use `go build -o <something>`, but it's subtle wrt
+ # cross-compiles and whether the <something> is a file or a directory,
# and EVERY caller has to get it *just* right.
# * We could leave GOPATH alone and let `go install` write binaries
# wherever the user's GOPATH says (or doesn't say).
@@ -20,16 +20,6 @@
#
# Eventually, when we no longer rely on run-in-gopath.sh we may be able to
# simplify this some.
- local go_pkg_dir="${KUBE_GOPATH}/src/${KUBE_GO_PACKAGE}"
- local go_pkg_basedir
- go_pkg_basedir=$(dirname "${go_pkg_dir}")
-
- mkdir -p "${go_pkg_basedir}"
-
- # TODO: This symlink should be relative.
- if [[ ! -e "${go_pkg_dir}" || "$(readlink "${go_pkg_dir}")" != "${KUBE_ROOT}" ]]; then
- ln -snf "${KUBE_ROOT}" "${go_pkg_dir}"
- fi
export GOPATH="${KUBE_GOPATH}"
# If these are not set, set them now. This ensures that any subsequent
@@ -40,24 +30,10 @@
# Make sure our own Go binaries are in PATH.
export PATH="${KUBE_GOPATH}/bin:${PATH}"
- # Change directories so that we are within the GOPATH. Some tools get really
- # upset if this is not true. We use a whole fake GOPATH here to collect the
- # resultant binaries.
- local subdir
- subdir=$(kube::realpath . | sed "s|${KUBE_ROOT}||")
- cd "${KUBE_GOPATH}/src/${KUBE_GO_PACKAGE}/${subdir}" || return 1
-
- # Set GOROOT so binaries that parse code can work properly.
- GOROOT=$(go env GOROOT)
- export GOROOT
-
# Unset GOBIN in case it already exists in the current session.
# Cross-compiles will not work with it set.
unset GOBIN
- # This seems to matter to some tools
- export GO15VENDOREXPERIMENT=1
-
- # Disable workspaces
- export GOWORK=off
+ # Explicitly turn on modules.
+ export GO111MODULE=on
}
```
Result: `make` works for k/k:
```
$ make kubectl
+++ [1211 11:07:31] Building go targets for linux/amd64
k8s.io/kubernetes/cmd/kubectl (static)
$ make WHAT=./cmd/kubectl/
+++ [1211 11:08:19] Building go targets for linux/amd64
k8s.io/kubernetes/./cmd/kubectl/ (non-static)
$ make WHAT=k8s.io/kubernetes/cmd/kubectl
+++ [1211 11:08:52] Building go targets for linux/amd64
k8s.io/kubernetes/cmd/kubectl (static)
```
Result: `make` works for staging by package:
```
$ make WHAT=k8s.io/api
+++ [1211 11:11:37] Building go targets for linux/amd64
k8s.io/api (non-static)
```
Result: `make` fails for staging by path:
```
$ make WHAT=./staging/src/k8s.io/api
+++ [1211 11:12:44] Building go targets for linux/amd64
k8s.io/kubernetes/./staging/src/k8s.io/api (non-static)
cannot find module providing package k8s.io/kubernetes/staging/src/k8s.io/api: import lookup disabled by -mod=vendor
(Go version in go.work is at least 1.14 and vendor directory exists.)
!!! [1211 11:12:44] Call tree:
!!! [1211 11:12:44] 1: /home/thockin/src/kubernetes/hack/lib/golang.sh:850 kube::golang::build_some_binaries(...)
!!! [1211 11:12:44] 2: /home/thockin/src/kubernetes/hack/lib/golang.sh:1012 kube::golang::build_binaries_for_platform(...)
!!! [1211 11:12:44] 3: hack/make-rules/build.sh:27 kube::golang::build_binaries(...)
!!! [1211 11:12:44] Call tree:
!!! [1211 11:12:44] 1: hack/make-rules/build.sh:27 kube::golang::build_binaries(...)
!!! [1211 11:12:44] Call tree:
!!! [1211 11:12:44] 1: hack/make-rules/build.sh:27 kube::golang::build_binaries(...)
make: *** [Makefile:96: all] Error 1
```
Result: `make test` fails:
```
$ make test WHAT=./cmd/kubectl
+++ [1211 11:13:38] Set GOMAXPROCS automatically to 6
+++ [1211 11:13:38] Running tests without code coverage and with -race
cmd/kubectl/kubectl.go:25:2: cannot find package "k8s.io/client-go/plugin/pkg/client/auth" in any of:
/home/thockin/src/kubernetes/_output/local/go/src/k8s.io/kubernetes/vendor/k8s.io/client-go/plugin/pkg/client/auth (vendor tree)
/home/thockin/sdk/gotip/src/k8s.io/client-go/plugin/pkg/client/auth (from $GOROOT)
/home/thockin/src/kubernetes/_output/local/go/src/k8s.io/client-go/plugin/pkg/client/auth (from $GOPATH)
cmd/kubectl/kubectl.go:20:2: cannot find package "k8s.io/component-base/cli" in any of:
/home/thockin/src/kubernetes/_output/local/go/src/k8s.io/kubernetes/vendor/k8s.io/component-base/cli (vendor tree)
/home/thockin/sdk/gotip/src/k8s.io/component-base/cli (from $GOROOT)
/home/thockin/src/kubernetes/_output/local/go/src/k8s.io/component-base/cli (from $GOPATH)
cmd/kubectl/kubectl.go:21:2: cannot find package "k8s.io/kubectl/pkg/cmd" in any of:
/home/thockin/src/kubernetes/_output/local/go/src/k8s.io/kubernetes/vendor/k8s.io/kubectl/pkg/cmd (vendor tree)
/home/thockin/sdk/gotip/src/k8s.io/kubectl/pkg/cmd (from $GOROOT)
/home/thockin/src/kubernetes/_output/local/go/src/k8s.io/kubectl/pkg/cmd (from $GOPATH)
cmd/kubectl/kubectl.go:22:2: cannot find package "k8s.io/kubectl/pkg/cmd/util" in any of:
/home/thockin/src/kubernetes/_output/local/go/src/k8s.io/kubernetes/vendor/k8s.io/kubectl/pkg/cmd/util (vendor tree)
/home/thockin/sdk/gotip/src/k8s.io/kubectl/pkg/cmd/util (from $GOROOT)
/home/thockin/src/kubernetes/_output/local/go/src/k8s.io/kubectl/pkg/cmd/util (from $GOPATH)
make: *** [Makefile:191: test] Error 1
```
143 lines
4.8 KiB
Bash
Executable File
143 lines
4.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# Copyright 2021 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.
|
|
|
|
# This script generates mock files using mockgen.
|
|
# Usage: `hack/update-mocks.sh`.
|
|
|
|
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
|
|
|
|
kube::golang::verify_go_version
|
|
kube::golang::old::setup_env
|
|
|
|
echo 'installing mockgen'
|
|
pushd "${KUBE_ROOT}/hack/tools" >/dev/null
|
|
go install github.com/golang/mock/mockgen
|
|
popd >/dev/null
|
|
|
|
function git_find() {
|
|
# Similar to find but faster and easier to understand. We want to include
|
|
# modified and untracked files because this might be running against code
|
|
# which is not tracked by git yet.
|
|
git ls-files -cmo --exclude-standard \
|
|
':!:vendor/*' `# catches vendor/...` \
|
|
':!:*/vendor/*' `# catches any subdir/vendor/...` \
|
|
':!:third_party/*' `# catches third_party/...` \
|
|
':!:*/third_party/*' `# catches third_party/...` \
|
|
':!:*/testdata/*' \
|
|
"$@"
|
|
}
|
|
|
|
cd "${KUBE_ROOT}"
|
|
|
|
GENERATED_MOCK_FILE_REGEX="^// Code generated by MockGen. DO NOT EDIT.$"
|
|
|
|
# pick a tempfile path for writing to
|
|
tmp=$(mktemp)
|
|
kube::util::trap_add "rm -f ${tmp:?}" EXIT
|
|
|
|
# We use this pattern here rather than `git grep` because we don't really want
|
|
# to encode the pathspec list in multiple places and anything more complicated
|
|
# just isn't worth the effort.
|
|
git_find -z ':(glob)**/*.go' \
|
|
| { xargs -0 grep -l --null "${GENERATED_MOCK_FILE_REGEX}" || true; } \
|
|
| xargs -0 rm -f
|
|
|
|
echo 'executing go generate command on below files'
|
|
|
|
git_find -z ':(glob)**/*.go' | while read -r -d $'\0' file; do
|
|
test -f "$file" || continue
|
|
grep -q "//go:generate mockgen" "$file" || continue
|
|
|
|
temp_file_name="$(kube::realpath "$(mktemp -t "$(basename "$0").XXXXXX")")"
|
|
|
|
# search for build tag used in file
|
|
build_tag_string=$(grep -o '+build.*$' "$file") || true
|
|
|
|
# if the file does not have build string
|
|
if [ -n "$build_tag_string" ]; then
|
|
# write the build tag in the temp file
|
|
echo -n "$build_tag_string" > "$temp_file_name"
|
|
|
|
# if +build tag is defined in interface file
|
|
BUILD_TAG_FILE=$temp_file_name go generate -v "$file"
|
|
else
|
|
# if no +build tag is defined in interface file
|
|
go generate -v "$file"
|
|
fi
|
|
done
|
|
|
|
# get the changed or new mock files
|
|
git ls-files -mo --exclude-standard -z | while read -r -d $'\0' file; do
|
|
# only process files that appear to be mocks
|
|
test -f "$file" || continue
|
|
grep -q "${GENERATED_MOCK_FILE_REGEX}" "$file" || continue
|
|
|
|
# search for build tags used in file
|
|
# //go:build !providerless
|
|
# // +build !providerless
|
|
go_build_tag_string=$(grep -o 'go:build.*$' "$file") || true
|
|
build_tag_string=$(grep -o '+build.*$' "$file") || true
|
|
new_header=''
|
|
|
|
# if the file has both headers
|
|
if [ -n "$build_tag_string" ] && [ -n "$go_build_tag_string" ]
|
|
then
|
|
# create a new header with the build string and the copyright text
|
|
new_header=$(echo -e "//""$go_build_tag_string""\n""//" "$build_tag_string""\n" | cat - hack/boilerplate/boilerplate.generatego.txt)
|
|
|
|
# ignore the first line (build tag) from the file
|
|
tail -n +3 "$file" >"${tmp}"
|
|
fi
|
|
|
|
# if the file has only // +build !providerless header
|
|
if [ -n "$build_tag_string" ] && [ -z "$go_build_tag_string" ]
|
|
then
|
|
# create a new header with the build string and the copyright text
|
|
new_header=$(echo -e "//" "$build_tag_string""\n" | cat - hack/boilerplate/boilerplate.generatego.txt)
|
|
|
|
# ignore the first line (build tag) from the file
|
|
tail -n +2 "$file" >"${tmp}"
|
|
fi
|
|
|
|
# if the file has only //go:build !providerless header
|
|
if [ -z "$build_tag_string" ] && [ -n "$go_build_tag_string" ]
|
|
then
|
|
# create a new header with the build string and the copyright text
|
|
new_header=$(echo -e "//""$go_build_tag_string""\n" | cat - hack/boilerplate/boilerplate.generatego.txt)
|
|
|
|
# ignore the first line (build tag) from the file
|
|
tail -n +2 "$file" >"${tmp}"
|
|
fi
|
|
|
|
# if the header is generated
|
|
if [ -n "$new_header" ]
|
|
then
|
|
# write the newly generated header file to the original file
|
|
echo -e "$new_header" | cat - "${tmp}" > "$file"
|
|
else
|
|
# if no build string insert at the top
|
|
cat hack/boilerplate/boilerplate.generatego.txt "$file" >"${tmp}" && \
|
|
mv "${tmp}" "$file"
|
|
fi
|
|
done
|