Skip to content

Commit 74a7026

Browse files
authored
Merge pull request kubernetes#76974 from mtaufen/fix-golang-sh
Restrict builds to officially supported platforms
2 parents 16193d8 + dcee810 commit 74a7026

File tree

1 file changed

+142
-67
lines changed

1 file changed

+142
-67
lines changed

hack/lib/golang.sh

Lines changed: 142 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,52 @@
1818
readonly KUBE_GO_PACKAGE=k8s.io/kubernetes
1919
readonly KUBE_GOPATH="${KUBE_OUTPUT}/go"
2020

21+
# The server platform we are building on.
22+
readonly KUBE_SUPPORTED_SERVER_PLATFORMS=(
23+
linux/amd64
24+
linux/arm
25+
linux/arm64
26+
linux/s390x
27+
linux/ppc64le
28+
)
29+
30+
# The node platforms we build for
31+
readonly KUBE_SUPPORTED_NODE_PLATFORMS=(
32+
linux/amd64
33+
linux/arm
34+
linux/arm64
35+
linux/s390x
36+
linux/ppc64le
37+
windows/amd64
38+
)
39+
40+
# If we update this we should also update the set of platforms whose standard
41+
# library is precompiled for in build/build-image/cross/Dockerfile
42+
readonly KUBE_SUPPORTED_CLIENT_PLATFORMS=(
43+
linux/amd64
44+
linux/386
45+
linux/arm
46+
linux/arm64
47+
linux/s390x
48+
linux/ppc64le
49+
darwin/amd64
50+
darwin/386
51+
windows/amd64
52+
windows/386
53+
)
54+
55+
# Which platforms we should compile test targets for.
56+
# Not all client platforms need these tests
57+
readonly KUBE_SUPPORTED_TEST_PLATFORMS=(
58+
linux/amd64
59+
linux/arm
60+
linux/arm64
61+
linux/s390x
62+
linux/ppc64le
63+
darwin/amd64
64+
windows/amd64
65+
)
66+
2167
# The set of server targets that we are only building for Linux
2268
# If you update this list, please also update build/BUILD.
2369
kube::golang::server_targets() {
@@ -87,77 +133,106 @@ readonly KUBE_NODE_TARGETS
87133
readonly KUBE_NODE_BINARIES=("${KUBE_NODE_TARGETS[@]##*/}")
88134
readonly KUBE_NODE_BINARIES_WIN=("${KUBE_NODE_BINARIES[@]/%/.exe}")
89135

90-
if [[ -n "${KUBE_BUILD_PLATFORMS:-}" ]]; then
91-
IFS=" " read -ra KUBE_SERVER_PLATFORMS <<< "${KUBE_BUILD_PLATFORMS}"
92-
IFS=" " read -ra KUBE_NODE_PLATFORMS <<< "${KUBE_BUILD_PLATFORMS}"
93-
IFS=" " read -ra KUBE_TEST_PLATFORMS <<< "${KUBE_BUILD_PLATFORMS}"
94-
IFS=" " read -ra KUBE_CLIENT_PLATFORMS <<< "${KUBE_BUILD_PLATFORMS}"
95-
readonly KUBE_SERVER_PLATFORMS
96-
readonly KUBE_NODE_PLATFORMS
97-
readonly KUBE_TEST_PLATFORMS
98-
readonly KUBE_CLIENT_PLATFORMS
99-
elif [[ "${KUBE_FASTBUILD:-}" == "true" ]]; then
100-
readonly KUBE_SERVER_PLATFORMS=(linux/amd64)
101-
readonly KUBE_NODE_PLATFORMS=(linux/amd64)
102-
if [[ "${KUBE_BUILDER_OS:-}" == "darwin"* ]]; then
103-
readonly KUBE_TEST_PLATFORMS=(
104-
darwin/amd64
105-
linux/amd64
106-
)
107-
readonly KUBE_CLIENT_PLATFORMS=(
108-
darwin/amd64
109-
linux/amd64
110-
)
136+
# ------------
137+
# NOTE: All functions that return lists should use newlines.
138+
# bash functions can't return arrays, and spaces are tricky, so newline
139+
# separators are the preferred pattern.
140+
# To transform a string of newline-separated items to an array, use mapfile -t:
141+
# mapfile -t FOO <<< "$(kube::golang::dups a b c a)"
142+
#
143+
# ALWAYS remember to quote your subshells. Not doing so will break in
144+
# bash 4.3, and potentially cause other issues.
145+
# ------------
146+
147+
# Returns a sorted newline-separated list containing only duplicated items.
148+
kube::golang::dups() {
149+
# We use printf to insert newlines, which are required by sort.
150+
printf "%s\n" "$@" | sort | uniq -d
151+
}
152+
153+
# Returns a sorted newline-separated list with duplicated items removed.
154+
kube::golang::dedup() {
155+
# We use printf to insert newlines, which are required by sort.
156+
printf "%s\n" "$@" | sort -u
157+
}
158+
159+
# Depends on values of user-facing KUBE_BUILD_PLATFORMS, KUBE_FASTBUILD,
160+
# and KUBE_BUILDER_OS.
161+
# Configures KUBE_SERVER_PLATFORMS, KUBE_NODE_PLATFOMRS,
162+
# KUBE_TEST_PLATFORMS, and KUBE_CLIENT_PLATFORMS, then sets them
163+
# to readonly.
164+
# The configured vars will only contain platforms allowed by the
165+
# KUBE_SUPPORTED* vars at the top of this file.
166+
kube::golang::setup_platforms() {
167+
if [[ -n "${KUBE_BUILD_PLATFORMS:-}" ]]; then
168+
# KUBE_BUILD_PLATFORMS needs to be read into an array before the next
169+
# step, or quoting treats it all as one element.
170+
local -a platforms
171+
IFS=" " read -ra platforms <<< "${KUBE_BUILD_PLATFORMS}"
172+
173+
# Deduplicate to ensure the intersection trick with kube::golang::dups
174+
# is not defeated by duplicates in user input.
175+
mapfile -t platforms <<< "$(kube::golang::dedup "${platforms[@]}")"
176+
177+
# Use kube::golang::dups to restrict the builds to the platforms in
178+
# KUBE_SUPPORTED_*_PLATFORMS. Items should only appear at most once in each
179+
# set, so if they appear twice after the merge they are in the intersection.
180+
mapfile -t KUBE_SERVER_PLATFORMS <<< "$(kube::golang::dups \
181+
"${platforms[@]}" \
182+
"${KUBE_SUPPORTED_SERVER_PLATFORMS[@]}" \
183+
)"
184+
readonly KUBE_SERVER_PLATFORMS
185+
186+
mapfile -t KUBE_NODE_PLATFORMS <<< "$(kube::golang::dups \
187+
"${platforms[@]}" \
188+
"${KUBE_SUPPORTED_NODE_PLATFORMS[@]}" \
189+
)"
190+
readonly KUBE_NODE_PLATFORMS
191+
192+
mapfile -t KUBE_TEST_PLATFORMS <<< "$(kube::golang::dups \
193+
"${platforms[@]}" \
194+
"${KUBE_SUPPORTED_TEST_PLATFORMS[@]}" \
195+
)"
196+
readonly KUBE_TEST_PLATFORMS
197+
198+
mapfile -t KUBE_CLIENT_PLATFORMS <<< "$(kube::golang::dups \
199+
"${platforms[@]}" \
200+
"${KUBE_SUPPORTED_CLIENT_PLATFORMS[@]}" \
201+
)"
202+
readonly KUBE_CLIENT_PLATFORMS
203+
204+
elif [[ "${KUBE_FASTBUILD:-}" == "true" ]]; then
205+
readonly KUBE_SERVER_PLATFORMS=(linux/amd64)
206+
readonly KUBE_NODE_PLATFORMS=(linux/amd64)
207+
if [[ "${KUBE_BUILDER_OS:-}" == "darwin"* ]]; then
208+
readonly KUBE_TEST_PLATFORMS=(
209+
darwin/amd64
210+
linux/amd64
211+
)
212+
readonly KUBE_CLIENT_PLATFORMS=(
213+
darwin/amd64
214+
linux/amd64
215+
)
216+
else
217+
readonly KUBE_TEST_PLATFORMS=(linux/amd64)
218+
readonly KUBE_CLIENT_PLATFORMS=(linux/amd64)
219+
fi
111220
else
112-
readonly KUBE_TEST_PLATFORMS=(linux/amd64)
113-
readonly KUBE_CLIENT_PLATFORMS=(linux/amd64)
114-
fi
115-
else
116-
117-
# The server platform we are building on.
118-
readonly KUBE_SERVER_PLATFORMS=(
119-
linux/amd64
120-
linux/arm
121-
linux/arm64
122-
linux/s390x
123-
linux/ppc64le
124-
)
221+
KUBE_SERVER_PLATFORMS=("${KUBE_SUPPORTED_SERVER_PLATFORMS[@]}")
222+
readonly KUBE_SERVER_PLATFORMS
125223

126-
# The node platforms we build for
127-
readonly KUBE_NODE_PLATFORMS=(
128-
linux/amd64
129-
linux/arm
130-
linux/arm64
131-
linux/s390x
132-
linux/ppc64le
133-
windows/amd64
134-
)
224+
KUBE_NODE_PLATFORMS=("${KUBE_SUPPORTED_NODE_PLATFORMS[@]}")
225+
readonly KUBE_NODE_PLATFORMS
135226

136-
# If we update this we should also update the set of platforms whose standard library is precompiled for in build/build-image/cross/Dockerfile
137-
readonly KUBE_CLIENT_PLATFORMS=(
138-
linux/amd64
139-
linux/386
140-
linux/arm
141-
linux/arm64
142-
linux/s390x
143-
linux/ppc64le
144-
darwin/amd64
145-
darwin/386
146-
windows/amd64
147-
windows/386
148-
)
227+
KUBE_CLIENT_PLATFORMS=("${KUBE_SUPPORTED_CLIENT_PLATFORMS[@]}")
228+
readonly KUBE_CLIENT_PLATFORMS
149229

150-
# Which platforms we should compile test targets for. Not all client platforms need these tests
151-
readonly KUBE_TEST_PLATFORMS=(
152-
linux/amd64
153-
linux/arm
154-
linux/arm64
155-
linux/s390x
156-
linux/ppc64le
157-
darwin/amd64
158-
windows/amd64
159-
)
160-
fi
230+
KUBE_TEST_PLATFORMS=("${KUBE_SUPPORTED_TEST_PLATFORMS[@]}")
231+
readonly KUBE_TEST_PLATFORMS
232+
fi
233+
}
234+
235+
kube::golang::setup_platforms
161236

162237
# The set of client targets that we are building for all platforms
163238
# If you update this list, please also update build/BUILD.

0 commit comments

Comments
 (0)