Skip to content

Commit

Permalink
Merge pull request kubernetes#5574 from ArtfulCoder/static_linking
Browse files Browse the repository at this point in the history
build kube-apiserver, kube-scheduler and kube-controller-manager statically
  • Loading branch information
vmarmol committed Mar 17, 2015
2 parents b84de4e + 9a901ed commit c2ba26f
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions hack/lib/golang.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ readonly KUBE_ALL_TARGETS=(
)
readonly KUBE_ALL_BINARIES=("${KUBE_ALL_TARGETS[@]##*/}")

readonly KUBE_STATIC_LIBRARIES=(
kube-apiserver
kube-controller-manager
kube-scheduler
)

kube::golang::is_statically_linked_library() {
local e
for e in "${KUBE_STATIC_LIBRARIES[@]}"; do [[ "$1" == *"/$e" ]] && return 0; done;
return 1;
}

# kube::binaries_from_targets take a list of build targets and return the
# full go package to be built
Expand Down Expand Up @@ -280,7 +291,7 @@ kube::golang::build_binaries() {

local binaries
binaries=($(kube::golang::binaries_from_targets "${targets[@]}"))

local platform
for platform in "${platforms[@]}"; do
kube::golang::set_platform_envs "${platform}"
Expand All @@ -297,15 +308,31 @@ kube::golang::build_binaries() {
if [[ ${GOOS} == "windows" ]]; then
bin="${bin}.exe"
fi
go build -o "${output_path}/${bin}" \

if kube::golang::is_statically_linked_library "${binary}"; then
CGO_ENABLED=0 go build -installsuffix cgo -o "${output_path}/${bin}" \
"${goflags[@]:+${goflags[@]}}" \
-ldflags "${version_ldflags}" \
"${binary}"
else
go build -o "${output_path}/${bin}" \
"${goflags[@]:+${goflags[@]}}" \
-ldflags "${version_ldflags}" \
"${binary}"
fi
done
else
go install "${goflags[@]:+${goflags[@]}}" \
-ldflags "${version_ldflags}" \
"${binaries[@]}"
for binary in "${binaries[@]}"; do
if kube::golang::is_statically_linked_library "${binary}"; then
CGO_ENABLED=0 go install -installsuffix cgo "${goflags[@]:+${goflags[@]}}" \
-ldflags "${version_ldflags}" \
"${binary}"
else
go install "${goflags[@]:+${goflags[@]}}" \
-ldflags "${version_ldflags}" \
"${binary}"
fi
done
fi
done
)
Expand Down

0 comments on commit c2ba26f

Please sign in to comment.