Change the build_binaries path to use modules

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
```
This commit is contained in:
Tim Hockin 2022-03-14 12:37:48 -07:00
parent 65b841c077
commit 8b579b2347
No known key found for this signature in database
29 changed files with 99 additions and 51 deletions

View File

@ -446,20 +446,6 @@ kube::golang::set_platform_envs() {
fi
}
# Create the GOPATH tree under $KUBE_OUTPUT
kube::golang::create_gopath_tree() {
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
}
# Ensure the go tool exists and is a viable version.
# Inputs:
# env-var GO_VERSION is the desired go version to use, downloading it if needed (defaults to content of .go-version)
@ -508,7 +494,7 @@ EOF
fi
}
# kube::golang::setup_env will check that the `go` commands is available in
# kube::golang::old::setup_env will check that the `go` commands is available in
# ${PATH}. It will also check that the Go version is good enough for the
# Kubernetes build.
#
@ -517,7 +503,8 @@ EOF
# env-var GOBIN is unset (we want binaries in a predictable place)
# env-var GO15VENDOREXPERIMENT=1
# current directory is within GOPATH
kube::golang::setup_env() {
#FIXME: remove this when all callers are converted
kube::golang::old::setup_env() {
kube::golang::verify_go_version
# Set up GOPATH. We have tools which depend on being in a GOPATH (see
@ -539,7 +526,16 @@ kube::golang::setup_env() {
#
# Eventually, when we no longer rely on run-in-gopath.sh we may be able to
# simplify this some.
kube::golang::create_gopath_tree
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
@ -567,6 +563,58 @@ kube::golang::setup_env() {
# This seems to matter to some tools
export GO15VENDOREXPERIMENT=1
# Disable workspaces
export GOWORK=off
}
# kube::golang::new::setup_env will check that the `go` commands is available in
# ${PATH}. It will also check that the Go version is good enough for the
# Kubernetes build.
#
# Outputs:
# env-var GOPATH points to our local output dir
# env-var GOBIN is unset (we want binaries in a predictable place)
# env-var PATH includes the local GOPATH
#FIXME: rename this when all callers are converted
kube::golang::new::setup_env() {
kube::golang::verify_go_version
# Set up GOPATH. We have tools which depend on being in a GOPATH (see
# hack/run-in-gopath.sh).
#
# 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 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).
#
# Instead we set it to a phony local path and process the results ourselves.
# In particular, GOPATH[0]/bin will be used for `go install`, with
# cross-compiles adding an extra directory under that.
#
# Eventually, when we no longer rely on run-in-gopath.sh we may be able to
# simplify this some.
export GOPATH="${KUBE_GOPATH}"
# If these are not set, set them now. This ensures that any subsequent
# scripts we run (which may call this function again) use the same values.
export GOCACHE="${GOCACHE:-"${KUBE_GOPATH}/cache/build"}"
export GOMODCACHE="${GOMODCACHE:-"${KUBE_GOPATH}/cache/mod"}"
# Make sure our own Go binaries are in PATH.
export PATH="${KUBE_GOPATH}/bin:${PATH}"
# Unset GOBIN in case it already exists in the current session.
# Cross-compiles will not work with it set.
unset GOBIN
# Explicitly turn on modules.
export GO111MODULE=on
}
kube::golang::setup_gomaxprocs() {
@ -737,14 +785,14 @@ kube::golang::build_some_binaries() {
done
if [[ "${#uncovered[@]}" != 0 ]]; then
V=2 kube::log::info "Building ${uncovered[*]} without coverage..."
GO111MODULE=on GOPROXY=off go install "${build_args[@]}" "${uncovered[@]}"
GOPROXY=off go install "${build_args[@]}" "${uncovered[@]}"
else
V=2 kube::log::info "Nothing to build without coverage."
fi
else
fi
else
V=2 kube::log::info "Coverage is disabled."
GO111MODULE=on GOPROXY=off go install "${build_args[@]}" "$@"
fi
GOPROXY=off go install "${build_args[@]}" "$@"
fi
}
# Args:
@ -853,7 +901,7 @@ kube::golang::build_binaries() {
# Create a sub-shell so that we don't pollute the outer environment
(
# Check for `go` binary and set ${GOPATH}.
kube::golang::setup_env
kube::golang::new::setup_env
V=2 kube::log::info "Go version: $(GOFLAGS='' go version)"
local host_platform

View File

@ -31,7 +31,7 @@ PROTOC_VERSION=23.4
# and formats it correctly
# $1: Full path to the directory where the api.proto file is
function kube::protoc::generate_proto() {
kube::golang::setup_env
kube::golang::old::setup_env
GO111MODULE=on GOPROXY=off go install k8s.io/code-generator/cmd/go-to-protobuf/protoc-gen-gogo
kube::protoc::check_protoc

View File

@ -35,7 +35,7 @@ kube::verify::generated() {
kube::util::ensure_clean_working_dir
# This sets up the environment, like GOCACHE, which keeps the worktree cleaner.
kube::golang::setup_env
kube::golang::old::setup_env
_tmpdir="$(kube::realpath "$(mktemp -d -t "verify-generated-$(basename "$1").XXXXXX")")"
git worktree add -f -q "${_tmpdir}" HEAD

View File

@ -17,7 +17,7 @@
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/../..
source "${KUBE_ROOT}/hack/lib/init.sh"
kube::golang::setup_env
kube::golang::old::setup_env
kube::golang::setup_gomaxprocs
# start the cache mutation detector by default so that cache mutators will be found

View File

@ -21,7 +21,7 @@ set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/../..
source "${KUBE_ROOT}/hack/lib/init.sh"
kube::golang::setup_env
kube::golang::old::setup_env
kube::golang::setup_gomaxprocs
# start the cache mutation detector by default so that cache mutators will be found

View File

@ -28,7 +28,7 @@ KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
# This sets up a clean GOPATH and makes sure we are currently in it.
kube::golang::setup_env
kube::golang::old::setup_env
# Run the user-provided command.
"${@}"

View File

@ -27,7 +27,7 @@ source "${KUBE_ROOT}/hack/lib/init.sh"
source "${KUBE_ROOT}/hack/lib/protoc.sh"
cd "${KUBE_ROOT}"
kube::golang::setup_env
kube::golang::old::setup_env
DBG_CODEGEN="${DBG_CODEGEN:-0}"
GENERATED_FILE_PREFIX="${GENERATED_FILE_PREFIX:-zz_generated.}"

View File

@ -21,7 +21,7 @@ set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
kube::golang::setup_env
kube::golang::old::setup_env
# run in module mode to match test command in readme.md
export GO111MODULE=on

View File

@ -25,7 +25,7 @@ set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
kube::golang::setup_env
kube::golang::old::setup_env
BINS=(
cmd/gendocs

View File

@ -28,7 +28,7 @@ source "${KUBE_ROOT}/hack/lib/init.sh"
source "${KUBE_ROOT}/hack/lib/protoc.sh"
kube::protoc::check_protoc
kube::golang::setup_env
kube::golang::old::setup_env
GO111MODULE=on GOPROXY=off go install k8s.io/code-generator/cmd/go-to-protobuf
GO111MODULE=on GOPROXY=off go install k8s.io/code-generator/cmd/go-to-protobuf/protoc-gen-gogo

View File

@ -28,7 +28,7 @@ KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
# This sets up the environment, like GOCACHE, which keeps the worktree cleaner.
kube::golang::setup_env
kube::golang::new::setup_env
export GO111MODULE=on
cd "${KUBE_ROOT}"

View File

@ -26,7 +26,7 @@ KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
# This sets up the environment, like GOCACHE, which keeps the worktree cleaner.
kube::golang::setup_env
kube::golang::old::setup_env
# Remove all files, some of them might be obsolete.
rm -f hack/golangci*.yaml

View File

@ -27,7 +27,7 @@ source "${KUBE_ROOT}/hack/lib/init.sh"
export GO111MODULE=on
kube::golang::verify_go_version
kube::golang::setup_env
kube::golang::old::setup_env
echo 'installing mockgen'
pushd "${KUBE_ROOT}/hack/tools" >/dev/null

View File

@ -27,7 +27,7 @@ OPENAPI_ROOT_DIR="${KUBE_ROOT}/api/openapi-spec"
source "${KUBE_ROOT}/hack/lib/init.sh"
kube::util::require-jq
kube::golang::setup_env
kube::golang::old::setup_env
kube::etcd::install
make -C "${KUBE_ROOT}" WHAT=cmd/kube-apiserver

View File

@ -25,7 +25,7 @@ KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
# Get all the default Go environment.
kube::golang::setup_env
kube::golang::new::setup_env
kube::golang::verify_go_version
# Turn off workspaces until we are ready for them later

View File

@ -21,7 +21,7 @@ set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
kube::golang::setup_env
kube::golang::old::setup_env
cd "${KUBE_ROOT}"

View File

@ -25,7 +25,7 @@ set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
kube::golang::setup_env
kube::golang::old::setup_env
BINS=(
cmd/clicheck

View File

@ -26,7 +26,7 @@ KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
source "${KUBE_ROOT}/hack/lib/util.sh"
kube::golang::setup_env
kube::golang::old::setup_env
make -C "${KUBE_ROOT}" WHAT=cmd/genswaggertypedocs

View File

@ -26,7 +26,7 @@ KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
source "${KUBE_ROOT}/hack/lib/util.sh"
kube::golang::setup_env
kube::golang::old::setup_env
make -C "${KUBE_ROOT}" WHAT=cmd/fieldnamedocscheck

View File

@ -25,7 +25,7 @@ set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
kube::golang::setup_env
kube::golang::old::setup_env
BINS=(
cmd/gendocs

View File

@ -30,7 +30,7 @@ source "${KUBE_ROOT}/hack/lib/init.sh"
cd "${KUBE_ROOT}"
kube::golang::setup_env
kube::golang::old::setup_env
find_files() {
find . -not \( \

View File

@ -25,7 +25,7 @@ source "${KUBE_ROOT}/hack/lib/util.sh"
kube::util::ensure_clean_working_dir
# This sets up the environment, like GOCACHE, which keeps the worktree cleaner.
kube::golang::setup_env
kube::golang::old::setup_env
# Opt into using go modules
export GO111MODULE=on

View File

@ -26,7 +26,7 @@ set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
kube::golang::setup_env
kube::golang::old::setup_env
GO111MODULE=on GOPROXY=off go install k8s.io/code-generator/cmd/import-boss

View File

@ -26,7 +26,7 @@ set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
kube::golang::setup_env
kube::golang::old::setup_env
make -C "${KUBE_ROOT}" WHAT=cmd/importverifier

View File

@ -27,7 +27,7 @@ source "${KUBE_ROOT}/hack/lib/init.sh"
source "${KUBE_ROOT}/hack/lib/util.sh"
# This sets up the environment, like GOCACHE, which keeps the worktree cleaner.
kube::golang::setup_env
kube::golang::old::setup_env
kube::util::ensure-temp-dir
ARTIFACTS="${ARTIFACTS:-${PWD}/_artifacts}"

View File

@ -28,7 +28,7 @@ source "${KUBE_ROOT}/hack/lib/init.sh"
export GO111MODULE=on
kube::golang::verify_go_version
kube::golang::setup_env
kube::golang::old::setup_env
staging_repos=()
kube::util::read-array staging_repos < <(kube::util::list_staging_repos)

View File

@ -26,7 +26,7 @@ set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
kube::golang::setup_env
kube::golang::old::setup_env
kube::etcd::install
make -C "${KUBE_ROOT}" WHAT=cmd/kube-apiserver

View File

@ -26,7 +26,7 @@ stability_check_setup() {
kube::golang::verify_go_version
kube::util::ensure-temp-dir
cd "${KUBE_ROOT}"
kube::golang::setup_env
kube::golang::old::setup_env
}
function find_files_to_check() {

View File

@ -70,7 +70,7 @@ done
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/../../../
source "${KUBE_ROOT}/hack/lib/init.sh"
kube::golang::setup_env
kube::golang::old::setup_env
DIR_BASENAME=$(dirname "${BASH_SOURCE[0]}")
pushd "${DIR_BASENAME}"