Skip to content
Browse files

hack/run-in-docker: refactoring (#206)

- build krew binary outside container (do not duplicate build process)
  - allows binary to be refreshed without rebuilding container
    this way, we preserve container session/state
- mount KUBECONFIG inside the container (not sure if it's actually useful)
  I probably won't use it (since GKE clusters require gcloud to work, too)
- made the script a bit friendlier to follow as a user.

Signed-off-by: Ahmet Alp Balkan <>
  • Loading branch information...
ahmetb authored and k8s-ci-robot committed Jul 9, 2019
1 parent 3951c05 commit 2dab1a1455e813ca8847b276c2d0f26ec8c5404c
Showing with 45 additions and 23 deletions.
  1. +35 −3 hack/
  2. +10 −20 hack/sandboxed.Dockerfile
@@ -14,10 +14,42 @@
# See the License for the specific language governing permissions and
# limitations under the License.

set -euo pipefail
# This script starts a development container by mounting the krew binary from
# the local filesystem.

set -euo pipefail
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
log() { echo >&2 "$*"; }
log_ok() { log "$(tput setaf 2)$*$(tput sgr0)"; }
log_fail() { log "$(tput setaf 1)$*$(tput sgr0)"; }

if [[ ! -f "${krew_bin}" ]]; then
log "Building the ${krew_bin}."
env OSARCH="linux/amd64" "${SCRIPTDIR}/"
log_ok "Using existing ${krew_bin}."

docker build -f "${SCRIPTDIR}/sandboxed.Dockerfile" -q \
--tag "${image}" "${SCRIPTDIR}/.."
log_ok "Sandbox image '${image}' built successfully."

docker build -f "${SCRIPTDIR}/sandboxed.Dockerfile" -t krew:sandbox "${SCRIPTDIR}/.."
if [[ ! -f "${kubeconfig}" ]]; then
log_fail "Warning: kubeconfig not found at ${kubeconfig}, using /dev/null"

docker run --rm -ti krew:sandbox
log_ok "Starting docker container with volume mounts:"
log " kubeconfig=${kubeconfig}"
log " kubectl-krew=${krew_bin}"
log_ok "You can rebuild with without restarting the container."
exec docker run --rm --tty --interactive \
--volume "${krew_bin}:/usr/local/bin/kubectl-krew" \
--volume "${kubeconfig}:/etc/kubeconfig" \
--env KUBECONFIG=/etc/kubeconfig \
--hostname krew \
@@ -12,26 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

FROM golang:alpine as builder
RUN apt-get update -qqy # retain the apt cache
RUN apt-get install -qqy git curl wget

WORKDIR /go/src/
RUN curl -fsSLo /usr/bin/kubectl${KUBECTL_VERSION}/bin/linux/amd64/kubectl && \
chmod +x /usr/bin/kubectl

RUN apk add --no-cache curl && \
curl -Lo /usr/bin/kubectl${KUBECTL_VERSION}/bin/linux/amd64/kubectl && \
chmod +x /usr/bin/kubectl
# initialize index ahead of time
RUN mkdir -p $HOME/.krew/index && \
git clone $HOME/.krew/index

# build binary
COPY . .
RUN go build -tags netgo -ldflags "-s -w" ./cmd/krew

# production image
FROM alpine
RUN apk --no-cache add git && \
ln -s /usr/bin/krew /usr/bin/kubectl-krew

# initialize index
RUN mkdir -p /root/.krew/index && \
git clone /root/.krew/index

COPY --from=builder /go/src/ /usr/bin/kubectl /usr/bin/
ENTRYPOINT [ "/usr/bin/env", "bash" ]

0 comments on commit 2dab1a1

Please sign in to comment.
You can’t perform that action at this time.