Skip to content

Commit 65b841c

Browse files
pohlythockin
authored andcommitted
Generate go.work files
This creates go.work and enables Go Workspaces. This is a file that includes info on k/k and all the staging modules. This depends on go 1.22 and setting FORCE_HOST_GO=true (for kube scripts, which try to be hermetic). Make this part of the normal update/verify sequence. The top-level go.work file contains no replace statements. Instead, the replace statements in the individual go.mod files are used. For this to work, replace statements in the individual go.mod files have to be consistent. hack/tools has different dependencies and can't be in the main workspace, so this adds a go.work just for that. Without this, go tries to consider all deps in all modules and pick one that works for all. This is problematic because there are so many of them that it is difficult to manage. Likewise for k8s.io/code-generator/examples and k8s.io/kms/internal/plugins/_mock - add trivial go.work files. For example k/k depends on an older version of a lib that gloangci-lint needs (transitively) and it breaks. This also updates vendor (needed to make go happy), and removes vendor'ed symlinks. This breaks a LOT of our build tools, which will be fixed subsequently. Result: `go` commands work across modules: Before: ``` $ go list ./pkg/proxy/iptables/ ./staging/src/k8s.io/api/core/v1/ main module (k8s.io/kubernetes) does not contain package k8s.io/kubernetes/staging/src/k8s.io/api/core/v1 $ go build ./pkg/proxy/iptables/ ./staging/src/k8s.io/api main module (k8s.io/kubernetes) does not contain package k8s.io/kubernetes/staging/src/k8s.io/api $ go test ./pkg/proxy/iptables/ ./staging/src/k8s.io/api main module (k8s.io/kubernetes) does not contain package k8s.io/kubernetes/staging/src/k8s.io/api ``` After: ``` $ go list ./pkg/proxy/iptables/ ./staging/src/k8s.io/api/core/v1/ k8s.io/kubernetes/pkg/proxy/iptables k8s.io/api/core/v1 $ go build ./pkg/proxy/iptables/ ./staging/src/k8s.io/api $ go test ./pkg/proxy/iptables/ ./staging/src/k8s.io/api ok k8s.io/kubernetes/pkg/proxy/iptables 0.360s ok k8s.io/api 2.302s ``` Result: `make` fails: ``` $ make go version go1.22rc1 linux/amd64 +++ [0106 12:11:03] Building go targets for linux/amd64 k8s.io/kubernetes/cmd/kube-proxy (static) k8s.io/kubernetes/cmd/kube-apiserver (static) k8s.io/kubernetes/cmd/kube-controller-manager (static) k8s.io/kubernetes/cmd/kubelet (non-static) k8s.io/kubernetes/cmd/kubeadm (static) k8s.io/kubernetes/cmd/kube-scheduler (static) k8s.io/component-base/logs/kube-log-runner (static) k8s.io/kube-aggregator (static) k8s.io/apiextensions-apiserver (static) k8s.io/kubernetes/cluster/gce/gci/mounter (static) k8s.io/kubernetes/cmd/kubectl (static) k8s.io/kubernetes/cmd/kubectl-convert (static) github.com/onsi/ginkgo/v2/ginkgo (non-static) k8s.io/kubernetes/test/e2e/e2e.test (test) k8s.io/kubernetes/test/conformance/image/go-runner (non-static) k8s.io/kubernetes/cmd/kubemark (static) github.com/onsi/ginkgo/v2/ginkgo (non-static) k8s.io/kubernetes/test/e2e_node/e2e_node.test (test) test/e2e/e2e.go:35:2: cannot find package "k8s.io/api/apps/v1" in any of: /home/thockin/src/kubernetes/_output/local/go/src/k8s.io/kubernetes/vendor/k8s.io/api/apps/v1 (vendor tree) /home/thockin/src/kubernetes/_output/local/.gimme/versions/go1.22rc1.linux.amd64/src/k8s.io/api/apps/v1 (from $GOROOT) /home/thockin/src/kubernetes/_output/local/go/src/k8s.io/api/apps/v1 (from $GOPATH) ... more ... ... more ... ... more ... !!! [0106 12:13:41] Call tree: !!! [0106 12:13:41] 1: /home/thockin/src/kubernetes/hack/lib/golang.sh:948 kube::golang::build_binaries_for_platform(...) !!! [0106 12:13:41] 2: hack/make-rules/build.sh:27 kube::golang::build_binaries(...) !!! [0106 12:13:41] Call tree: !!! [0106 12:13:41] 1: hack/make-rules/build.sh:27 kube::golang::build_binaries(...) !!! [0106 12:13:41] Call tree: !!! [0106 12:13:41] 1: hack/make-rules/build.sh:27 kube::golang::build_binaries(...) make: *** [Makefile:96: all] Error 1 ``` Again, this requires go 1.22 (e.g. gotip), as go 1.21.x does not have `go work vendor` support. TO REPEAT: ( \ ./hack/update-go-workspace.sh; \ ./hack/update-vendor.sh; \ ./hack/update-go-workspace.sh; \ )
1 parent e54f229 commit 65b841c

File tree

107 files changed

+742
-1724
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+742
-1724
lines changed

.gitignore

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# gopls gets indigestion without go.work, CI gets indigestion WITH go.work
2-
/go.work
3-
/go.work.sum
4-
51
# OSX leaves these everywhere on SMB shares
62
._*
73

go.work

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
go 1.22.0
2+
3+
use (
4+
.
5+
./staging/src/k8s.io/api
6+
./staging/src/k8s.io/apiextensions-apiserver
7+
./staging/src/k8s.io/apimachinery
8+
./staging/src/k8s.io/apiserver
9+
./staging/src/k8s.io/cli-runtime
10+
./staging/src/k8s.io/client-go
11+
./staging/src/k8s.io/cloud-provider
12+
./staging/src/k8s.io/cluster-bootstrap
13+
./staging/src/k8s.io/code-generator
14+
./staging/src/k8s.io/component-base
15+
./staging/src/k8s.io/component-helpers
16+
./staging/src/k8s.io/controller-manager
17+
./staging/src/k8s.io/cri-api
18+
./staging/src/k8s.io/csi-translation-lib
19+
./staging/src/k8s.io/dynamic-resource-allocation
20+
./staging/src/k8s.io/endpointslice
21+
./staging/src/k8s.io/kms
22+
./staging/src/k8s.io/kube-aggregator
23+
./staging/src/k8s.io/kube-controller-manager
24+
./staging/src/k8s.io/kube-proxy
25+
./staging/src/k8s.io/kube-scheduler
26+
./staging/src/k8s.io/kubectl
27+
./staging/src/k8s.io/kubelet
28+
./staging/src/k8s.io/legacy-cloud-providers
29+
./staging/src/k8s.io/metrics
30+
./staging/src/k8s.io/mount-utils
31+
./staging/src/k8s.io/pod-security-admission
32+
./staging/src/k8s.io/sample-apiserver
33+
./staging/src/k8s.io/sample-cli-plugin
34+
./staging/src/k8s.io/sample-controller
35+
)

go.work.sum

Lines changed: 248 additions & 0 deletions
Large diffs are not rendered by default.

hack/make-rules/update.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ if ! ${ALL} ; then
3636
fi
3737

3838
BASH_TARGETS=(
39+
update-go-workspace
3940
update-codegen
4041
update-generated-api-compatibility-data
4142
update-generated-docs

hack/tools/go.work

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// This is a hack, but it prevents go from climbing further and trying to
2+
// reconcile the various deps across the "real" modules and this one.
3+
4+
go 1.22.0
5+
6+
use .

hack/tools/go.work.sum

Lines changed: 197 additions & 0 deletions
Large diffs are not rendered by default.

hack/update-go-workspace.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2022 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 generates go.work so that it includes all Go packages
18+
# in this repo, with a few exceptions.
19+
20+
set -o errexit
21+
set -o nounset
22+
set -o pipefail
23+
24+
# Go tools really don't like it if you have a symlink in `pwd`.
25+
cd "$(pwd -P)"
26+
27+
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
28+
source "${KUBE_ROOT}/hack/lib/init.sh"
29+
30+
# This sets up the environment, like GOCACHE, which keeps the worktree cleaner.
31+
kube::golang::setup_env
32+
export GO111MODULE=on
33+
34+
cd "${KUBE_ROOT}"
35+
36+
# Avoid issues and remove the workspace files.
37+
rm -f go.work go.work.sum
38+
39+
# Generate the workspace.
40+
go work init
41+
go work edit -use .
42+
git ls-files -z ':(glob)./staging/src/k8s.io/*/go.mod' \
43+
| xargs -0 -n1 dirname -z \
44+
| xargs -0 -n1 go work edit -use
45+
go mod download # generate go.work.sum

hack/update-vendor.sh

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ cd "$(pwd -P)"
2424
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
2525
source "${KUBE_ROOT}/hack/lib/init.sh"
2626

27+
# Get all the default Go environment.
28+
kube::golang::setup_env
29+
kube::golang::verify_go_version
30+
2731
# Turn off workspaces until we are ready for them later
2832
export GOWORK=off
2933
# Explicitly opt into go modules, even though we're inside a GOPATH directory
@@ -39,7 +43,6 @@ if [[ "${GOPROXY:-}" == "off" ]]; then
3943
exit 1
4044
fi
4145

42-
kube::golang::verify_go_version
4346
kube::util::require-jq
4447

4548
TMP_DIR="${TMP_DIR:-$(mktemp -d /tmp/update-vendor.XXXX)}"
@@ -403,15 +406,12 @@ hack/update-internal-modules.sh
403406

404407

405408
# Phase 8: rebuild vendor directory
406-
kube::log::status "vendor: running 'go mod vendor'" >&11
407-
go mod vendor
408-
409-
# create a symlink in vendor directory pointing to the staging components.
410-
# This lets other packages and tools use the local staging components as if they were vendored.
411-
for repo in $(kube::util::list_staging_repos); do
412-
rm -fr "${KUBE_ROOT}/vendor/k8s.io/${repo}"
413-
ln -s "../../staging/src/k8s.io/${repo}" "${KUBE_ROOT}/vendor/k8s.io/${repo}"
414-
done
409+
(
410+
kube::log::status "vendor: running 'go work vendor'" >&11
411+
unset GOWORK
412+
unset GOFLAGS
413+
go work vendor
414+
)
415415

416416
kube::log::status "vendor: updating vendor/LICENSES" >&11
417417
hack/update-vendor-licenses.sh

hack/verify-go-workspace.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2021 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 the OWNERS files need to be formatted or not by
18+
# `yamlfmt`. Run `hack/update-yamlfmt.sh` to actually format sources.
19+
#
20+
# Usage: `hack/verify-go-workspace.sh`.
21+
22+
23+
set -o errexit
24+
set -o nounset
25+
set -o pipefail
26+
27+
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
28+
source "${KUBE_ROOT}/hack/lib/verify-generated.sh"
29+
30+
export GO111MODULE=on # TODO(thockin): remove this when init.sh stops disabling it
31+
kube::verify::generated "Go workspace files need to be updated" "Please run 'hack/update-go-workspace.sh'" hack/update-go-workspace.sh

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module k8s.io/api
44

5-
go 1.21
5+
go 1.22.0
66

77
require (
88
github.com/gogo/protobuf v1.3.2

0 commit comments

Comments
 (0)