Skip to content
Permalink
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 <ahmetb@google.com>
  • 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/run-in-docker.sh
  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)"; }
image="krew:sandbox"

krew_bin="${SCRIPTDIR}/../out/bin/krew-linux_amd64"
if [[ ! -f "${krew_bin}" ]]; then
log "Building the ${krew_bin}."
env OSARCH="linux/amd64" "${SCRIPTDIR}/make-binaries.sh"
else
log_ok "Using existing ${krew_bin}."
fi


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}/.."
kubeconfig="${KUBECONFIG:-$HOME/.kube/config}"
if [[ ! -f "${kubeconfig}" ]]; then
log_fail "Warning: kubeconfig not found at ${kubeconfig}, using /dev/null"
kubeconfig=/dev/null
fi

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 make-binaries.sh 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 \
"${image}"
@@ -12,26 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

FROM golang:alpine as builder
FROM gcr.io/gcp-runtimes/ubuntu_16_0_4:latest
RUN apt-get update -qqy # retain the apt cache
RUN apt-get install -qqy git curl wget

WORKDIR /go/src/sigs.k8s.io/krew
ARG KUBECTL_VERSION=v1.14.2
RUN curl -fsSLo /usr/bin/kubectl https://storage.googleapis.com/kubernetes-release/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl && \
chmod +x /usr/bin/kubectl

ENV KUBECTL_VERSION v1.14.2
RUN apk add --no-cache curl && \
curl -Lo /usr/bin/kubectl https://storage.googleapis.com/kubernetes-release/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl && \
chmod +x /usr/bin/kubectl
# initialize index ahead of time
RUN mkdir -p $HOME/.krew/index && \
git clone https://github.com/kubernetes-sigs/krew-index $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 https://github.com/kubernetes-sigs/krew-index /root/.krew/index

COPY --from=builder /go/src/sigs.k8s.io/krew/krew /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.