
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
```
145 lines
5.1 KiB
Bash
Executable File
145 lines
5.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# Copyright 2016 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.
|
|
|
|
# Script to fetch latest openapi spec.
|
|
# Puts the updated spec at api/openapi-spec/
|
|
|
|
set -o errexit
|
|
set -o nounset
|
|
set -o pipefail
|
|
|
|
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
|
|
DISCOVERY_ROOT_DIR="${KUBE_ROOT}/api/discovery"
|
|
OPENAPI_ROOT_DIR="${KUBE_ROOT}/api/openapi-spec"
|
|
source "${KUBE_ROOT}/hack/lib/init.sh"
|
|
|
|
kube::util::require-jq
|
|
kube::golang::old::setup_env
|
|
kube::etcd::install
|
|
|
|
make -C "${KUBE_ROOT}" WHAT=cmd/kube-apiserver
|
|
|
|
function cleanup()
|
|
{
|
|
if [[ -n ${APISERVER_PID-} ]]; then
|
|
kill "${APISERVER_PID}" 1>&2 2>/dev/null
|
|
wait "${APISERVER_PID}" || true
|
|
fi
|
|
unset APISERVER_PID
|
|
|
|
kube::etcd::cleanup
|
|
|
|
kube::log::status "Clean up complete"
|
|
}
|
|
|
|
trap cleanup EXIT SIGINT
|
|
|
|
TMP_DIR=${TMP_DIR:-$(kube::realpath "$(mktemp -d -t "$(basename "$0").XXXXXX")")}
|
|
ETCD_HOST=${ETCD_HOST:-127.0.0.1}
|
|
ETCD_PORT=${ETCD_PORT:-2379}
|
|
API_PORT=${API_PORT:-8050}
|
|
API_HOST=${API_HOST:-127.0.0.1}
|
|
API_LOGFILE=${API_LOGFILE:-${TMP_DIR}/openapi-api-server.log}
|
|
|
|
kube::etcd::start
|
|
|
|
echo "dummy_token,admin,admin" > "${TMP_DIR}/tokenauth.csv"
|
|
|
|
# setup envs for TokenRequest required flags
|
|
SERVICE_ACCOUNT_LOOKUP=${SERVICE_ACCOUNT_LOOKUP:-true}
|
|
SERVICE_ACCOUNT_KEY=${SERVICE_ACCOUNT_KEY:-${TMP_DIR}/kube-serviceaccount.key}
|
|
# Generate ServiceAccount key if needed
|
|
if [[ ! -f "${SERVICE_ACCOUNT_KEY}" ]]; then
|
|
mkdir -p "$(dirname "${SERVICE_ACCOUNT_KEY}")"
|
|
openssl genrsa -out "${SERVICE_ACCOUNT_KEY}" 2048 2>/dev/null
|
|
fi
|
|
|
|
# Start kube-apiserver
|
|
# omit enums from static openapi snapshots used to generate clients until #109177 is resolved
|
|
kube::log::status "Starting kube-apiserver"
|
|
"${KUBE_OUTPUT_HOSTBIN}/kube-apiserver" \
|
|
--bind-address="${API_HOST}" \
|
|
--secure-port="${API_PORT}" \
|
|
--etcd-servers="http://${ETCD_HOST}:${ETCD_PORT}" \
|
|
--advertise-address="10.10.10.10" \
|
|
--cert-dir="${TMP_DIR}/certs" \
|
|
--feature-gates=AllAlpha=true,OpenAPIEnums=false \
|
|
--runtime-config="api/all=true" \
|
|
--token-auth-file="${TMP_DIR}/tokenauth.csv" \
|
|
--authorization-mode=RBAC \
|
|
--service-account-key-file="${SERVICE_ACCOUNT_KEY}" \
|
|
--service-account-lookup="${SERVICE_ACCOUNT_LOOKUP}" \
|
|
--service-account-issuer="https://kubernetes.default.svc" \
|
|
--service-account-signing-key-file="${SERVICE_ACCOUNT_KEY}" \
|
|
--v=2 \
|
|
--service-cluster-ip-range="10.0.0.0/24" >"${API_LOGFILE}" 2>&1 &
|
|
APISERVER_PID=$!
|
|
|
|
if ! kube::util::wait_for_url "https://${API_HOST}:${API_PORT}/healthz" "apiserver: "; then
|
|
kube::log::error "Here are the last 10 lines from kube-apiserver (${API_LOGFILE})"
|
|
kube::log::error "=== BEGIN OF LOG ==="
|
|
tail -10 "${API_LOGFILE}" >&2 || :
|
|
kube::log::error "=== END OF LOG ==="
|
|
exit 1
|
|
fi
|
|
|
|
kube::log::status "Updating aggregated discovery"
|
|
|
|
rm -fr "${DISCOVERY_ROOT_DIR}"
|
|
mkdir -p "${DISCOVERY_ROOT_DIR}"
|
|
curl -kfsS -H 'Authorization: Bearer dummy_token' -H 'Accept: application/json;g=apidiscovery.k8s.io;v=v2beta1;as=APIGroupDiscoveryList' "https://${API_HOST}:${API_PORT}/apis" | jq -S . > "${DISCOVERY_ROOT_DIR}/aggregated_v2beta1.json"
|
|
|
|
kube::log::status "Updating " "${OPENAPI_ROOT_DIR} for OpenAPI v2"
|
|
|
|
rm -f "${OPENAPI_ROOT_DIR}/swagger.json"
|
|
curl -w "\n" -kfsS -H 'Authorization: Bearer dummy_token' \
|
|
"https://${API_HOST}:${API_PORT}/openapi/v2" \
|
|
| jq -S '.info.version="unversioned"' \
|
|
> "${OPENAPI_ROOT_DIR}/swagger.json"
|
|
|
|
kube::log::status "Updating " "${OPENAPI_ROOT_DIR}/v3 for OpenAPI v3"
|
|
|
|
mkdir -p "${OPENAPI_ROOT_DIR}/v3"
|
|
# clean up folder, note that some files start with dot like
|
|
# ".well-known__openid-configuration_openapi.json"
|
|
rm -r "${OPENAPI_ROOT_DIR}"/v3/{*,.*} || true
|
|
|
|
rm -rf "${OPENAPI_ROOT_DIR}/v3/*"
|
|
curl -w "\n" -kfsS -H 'Authorization: Bearer dummy_token' \
|
|
"https://${API_HOST}:${API_PORT}/openapi/v3" \
|
|
| jq -r '.paths | to_entries | .[].key' \
|
|
| while read -r group; do
|
|
kube::log::status "Updating OpenAPI spec and discovery for group ${group}"
|
|
OPENAPI_FILENAME="${group}_openapi.json"
|
|
OPENAPI_FILENAME_ESCAPED="${OPENAPI_FILENAME//\//__}"
|
|
OPENAPI_PATH="${OPENAPI_ROOT_DIR}/v3/${OPENAPI_FILENAME_ESCAPED}"
|
|
curl -w "\n" -kfsS -H 'Authorization: Bearer dummy_token' \
|
|
"https://${API_HOST}:${API_PORT}/openapi/v3/{$group}" \
|
|
| jq -S '.info.version="unversioned"' \
|
|
> "$OPENAPI_PATH"
|
|
|
|
if [[ "${group}" == "api"* ]]; then
|
|
DISCOVERY_FILENAME="${group}.json"
|
|
DISCOVERY_FILENAME_ESCAPED="${DISCOVERY_FILENAME//\//__}"
|
|
DISCOVERY_PATH="${DISCOVERY_ROOT_DIR}/${DISCOVERY_FILENAME_ESCAPED}"
|
|
curl -kfsS -H 'Authorization: Bearer dummy_token' "https://${API_HOST}:${API_PORT}/{$group}" | jq -S . > "$DISCOVERY_PATH"
|
|
fi
|
|
done
|
|
|
|
kube::log::status "SUCCESS"
|
|
|
|
# ex: ts=2 sw=2 et filetype=sh
|