Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

add docker image build support #588

Merged
merged 5 commits into from
Jun 6, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM golang:alpine as build
ARG LD_FLAGS
WORKDIR /go/src/github.com/ksonnet/ksonnet
COPY . /go/src/github.com/ksonnet/ksonnet
RUN CGO_ENABLED=0 GOOS=linux go build -o ks -ldflags="${LD_FLAGS}" ./cmd/ks

FROM alpine:latest as certs
RUN apk --update add ca-certificates

FROM scratch
COPY --from=certs /etc/ssl/certs/* /etc/ssl/certs/
COPY --from=build /go/src/github.com/ksonnet/ksonnet/ks /bin/ks
ENTRYPOINT ["/bin/ks"]
16 changes: 13 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
VERSION?=dev-$(shell date +%FT%T%z)
KS_BIN?=ks

APIMACHINERY_VER := $(shell dep status | grep k8s.io/apimachinery | awk '{print $$3}')
APIMACHINERY_VER := $(shell grep -B1 k8s.io/apimachinery Gopkg.lock | head -n1 | cut -d'"' -f2)
REVISION=$(shell git rev-parse HEAD)
GIT_TAG=$(shell git describe --tags)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to only set GIT_TAG if is needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so in Makefiles variables set with = are lazily evaluated ... so it really doesn't hurt anything to make it available here as it won't run git describe unless you are building a docker image. but i don't mind moving this into the target if that seems cleaner?


GO = go
EXTRA_GO_FLAGS =
GO_FLAGS = -ldflags="-X main.version=$(VERSION) -X main.apimachineryVersion=$(APIMACHINERY_VER) -X generator.revision=$(REVISION) $(GO_LDFLAGS) " $(EXTRA_GO_FLAGS)
LD_FLAGS = -X main.version=$(VERSION) -X main.apimachineryVersion=$(APIMACHINERY_VER) -X generator.revision=$(REVISION) $(GO_LDFLAGS)
GO_FLAGS = -ldflags="$(LD_FLAGS) " $(EXTRA_GO_FLAGS)
GOFMT = gofmt
# GINKGO = "go test" also works if you want to avoid ginkgo tool
GINKGO = ginkgo
Expand All @@ -39,12 +41,20 @@ INTEGRATION_TEST_FIXTURES = ./fixtures

all: ks docs

ks:
Gopkg.lock: Gopkg.toml
dep ensure
touch Gopkg.lock
$(eval APIMACHINERY_VER := $(shell grep -B1 k8s.io/apimachinery Gopkg.lock | head -n1 | cut -d'"' -f2))

ks: Gopkg.lock
$(GO) build -o $(KS_BIN) $(GO_FLAGS) ./cmd/ks

docs:
$(DOC_GEN_FILE)

docker-image: Gopkg.lock
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be be added to .PHONY at the bottom of the file? (and also install should be as well)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

docker build -t ks:$(GIT_TAG) --build-arg LD_FLAGS="$(LD_FLAGS)" .

install:
$(GO) build -o $(GOPATH)/bin/ks $(GO_FLAGS) ./cmd/ks

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ brew install ksonnet/tap/ks

See the [releases page](https://github.com/ksonnet/ksonnet/releases) to download the latest released binary.

### Build a docker image

A docker image can be built and used similarly to our manual build [as seen here](/docs/docker-image-build.md)

### Manually build and install

You can download and manually build from source by following [these instructions](/docs/build-install.md).
Expand Down
30 changes: 30 additions & 0 deletions docs/docker-image-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Build a `ks` docker image
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GuessWhoSamFoo What do you think about this text?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Referencing Docker outside the context of a CLI should be upper case.


The intended use case for building a docker images is to run `ks` from CI/CD. As a development tool it is recommended to install `ks` locally, though it is still possible to use the docker image for development as well.

Copy and paste the following commands:
```bash
# Clone the ksonnet repo into your GOPATH
go get github.com/ksonnet/ksonnet

# Build and install binary under shortname `ks` into $GOPATH/bin
cd "${GOPATH}/src/github.com/ksonnet/ksonnet"
make docker-image
```

# Running `ks` via docker

In order to run via docker, the `ks` process needs access to a kubernetes config file, certificates, and the current working directory. We can set these up using mounts. Here's what it looks like running on a local cluster with minikube:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of "We can set these up using mounts"

Pass the --mount flag to capture the path of these files.


```bash
docker run -e KUBECONFIG="~/.kube/config" --mount type=bind,source="~/.kube/conifg",target="~/.kube/config" --mount type=bind,source="~/.minikube",target="~/.minikube" --mount type=bind,source="$(pwd)",target="$(pwd)" -w "$(pwd)" ks --help
```

This sets the $KUBECONFIG environment variable inside the container, mounts the config and the directory holding certificates (which can be found inside the kubeconfig), and mounts the current working directory so that the `ks` binary knows where to work.

It is also possible to use aliases, though keep in mind that this sets the working directory statically to the directory of the app you are working on:

```bash
alias ks="docker run -e KUBECONFIG=/path/to/kube/config --mount type=bind,source=/path/to/kube/config,target=/path/to/kube/config --mount type=bind,source=/path/to/cert/dir,target=/path/to/cert/dir --mount type=bind,source=/path/to/app/dir,target=/path/to/app/dir -w /path/to/app/dir ks"
ks env list
```