Skip to content

Commit

Permalink
Allow non-root build to write go code.
Browse files Browse the repository at this point in the history
  • Loading branch information
thockin committed Jul 3, 2016
1 parent c3f4fb5 commit 55eaa38
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
31 changes: 21 additions & 10 deletions build/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,21 @@ readonly KUBE_BUILD_IMAGE_CROSS_TAG="v1.6.2-2"
readonly LOCAL_OUTPUT_ROOT="${KUBE_ROOT}/${OUT_DIR:-_output}"
readonly LOCAL_OUTPUT_SUBPATH="${LOCAL_OUTPUT_ROOT}/dockerized"
readonly LOCAL_OUTPUT_BINPATH="${LOCAL_OUTPUT_SUBPATH}/bin"
readonly LOCAL_OUTPUT_GOPATH="${LOCAL_OUTPUT_SUBPATH}/go"
readonly LOCAL_OUTPUT_IMAGE_STAGING="${LOCAL_OUTPUT_ROOT}/images"

readonly OUTPUT_BINPATH="${CUSTOM_OUTPUT_BINPATH:-$LOCAL_OUTPUT_BINPATH}"

readonly REMOTE_OUTPUT_ROOT="/go/src/${KUBE_GO_PACKAGE}/_output"
readonly REMOTE_OUTPUT_SUBPATH="${REMOTE_OUTPUT_ROOT}/dockerized"
readonly REMOTE_OUTPUT_BINPATH="${REMOTE_OUTPUT_SUBPATH}/bin"
readonly REMOTE_OUTPUT_GOPATH="${REMOTE_OUTPUT_SUBPATH}/go"

readonly DOCKER_MOUNT_ARGS_BASE=(
--volume "${OUTPUT_BINPATH}:${REMOTE_OUTPUT_BINPATH}"
--volume /etc/localtime:/etc/localtime:ro
)

# We create a Docker data container to cache incremental build artifacts.
readonly REMOTE_OUTPUT_GOPATH="${REMOTE_OUTPUT_SUBPATH}/go"
readonly DOCKER_DATA_MOUNT_ARGS=(
--volume "${REMOTE_OUTPUT_GOPATH}"
)

# This is where the final release artifacts are created locally
readonly RELEASE_STAGE="${LOCAL_OUTPUT_ROOT}/release-stage"
readonly RELEASE_DIR="${LOCAL_OUTPUT_ROOT}/release-tars"
Expand Down Expand Up @@ -559,16 +555,31 @@ function kube::build::clean_images() {
}

function kube::build::ensure_data_container() {
if ! "${DOCKER[@]}" inspect "${KUBE_BUILD_DATA_CONTAINER_NAME}" >/dev/null 2>&1; then
# If the data container exists AND exited successfully, we can use it.
# Otherwise nuke it and start over.
local ret=0
local code=$(docker inspect \
-f '{{.State.ExitCode}}' \
"${KUBE_BUILD_DATA_CONTAINER_NAME}" 2>/dev/null || ret=$?)
if [[ "${ret}" == 0 && "${code}" != 0 ]]; then
kube::build::destroy_container "${KUBE_BUILD_DATA_CONTAINER_NAME}"
ret=1
fi
if [[ "${ret}" != 0 ]]; then
kube::log::status "Creating data container ${KUBE_BUILD_DATA_CONTAINER_NAME}"
# We have to ensure the directory exists, or else the docker run will
# create it as root.
mkdir -p "${LOCAL_OUTPUT_GOPATH}"
# We want this to run as root to be able to chown, so non-root users can
# later use the result as a data container. This run both creates the data
# container and chowns the GOPATH.
local -ra docker_cmd=(
"${DOCKER[@]}" run
"${DOCKER_DATA_MOUNT_ARGS[@]}"
--volume "${REMOTE_OUTPUT_GOPATH}"
--name "${KUBE_BUILD_DATA_CONTAINER_NAME}"
--user "$(id -u):$(id -g)"
--hostname "${HOSTNAME}"
"${KUBE_BUILD_IMAGE}"
true
chown -R $(id -u).$(id -g) "${REMOTE_OUTPUT_GOPATH}"
)
"${docker_cmd[@]}"
fi
Expand Down
2 changes: 1 addition & 1 deletion hack/update-generated-protobuf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function prereqs() {
fi
kube::build::ensure_docker_daemon_connectivity || return 1

KUBE_ROOT_HASH=$(kube::build::short_hash "${HOSTNAME:-}:${REPO_DIR:-${KUBE_ROOT}}/go-to-protobuf")
KUBE_ROOT_HASH=$(kube::build::short_hash "${HOSTNAME:-}:${REPO_DIR:-${KUBE_ROOT}}")
KUBE_BUILD_IMAGE_TAG="build-${KUBE_ROOT_HASH}"
KUBE_BUILD_IMAGE="${KUBE_BUILD_IMAGE_REPO}:${KUBE_BUILD_IMAGE_TAG}"
KUBE_BUILD_CONTAINER_NAME="kube-build-${KUBE_ROOT_HASH}"
Expand Down

0 comments on commit 55eaa38

Please sign in to comment.