-
Notifications
You must be signed in to change notification settings - Fork 175
add docker image build support #588
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
||
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 | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be be added to There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Build a `ks` docker image | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @GuessWhoSamFoo What do you think about this text? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of "We can set these up using mounts" Pass the |
||
|
||
```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 | ||
``` |
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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?