Skip to content

Commit

Permalink
Building and Deploying with ko
Browse files Browse the repository at this point in the history
Introduce a "build-image" and "install" targets to leverage ko deploys.
  • Loading branch information
otaviof authored and ricardomaraschini committed Feb 27, 2022
1 parent a0efb02 commit c65cf8f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .ko.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
builds:
- id: imgctrl
main: ./cmd/imgctrl
flags:
- -tags
- containers_image_openpgp
2 changes: 2 additions & 0 deletions Containerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ COPY --from=builder /src/output/bin/kubectl-image /usr/local/bin/kubectl-image
# 8083 images export/import handler.
# 8090 metrics endpoint.
EXPOSE 8080 8083 8090

ENTRYPOINT [ "/usr/local/bin/imgctrl" ]
42 changes: 37 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ PLUGIN = kubectl-image
PLUGIN_DARWIN = kubectl-image-darwin

VERSION ?= v0.0.0

REGISTRY_HOSTNAME ?= ghcr.io
REGISTRY_USERNAME ?= shipwright-io

IMAGE_BUILDER ?= podman
IMAGE ?= quay.io/shipwright/imgctrl:latest
IMAGE_TAG ?= latest
IMAGE ?= $(REGISTRY_HOSTNAME)/$(REGISTRY_USERNAME)/$(IMGCTRL)

OUTPUT_DIR ?= output
OUTPUT_BIN = $(OUTPUT_DIR)/bin
Expand All @@ -19,29 +24,38 @@ KUTTL_REPO = https://github.com/kudobuilder/kuttl
PROJECT = github.com/shipwright-io/image
GEN_OUTPUT = /tmp/$(PROJECT)/infra/images

# destination namespace to install target
NAMESPACE ?= shipwright-build

# the container image produced by ko will use this repostory, and combine with the application name
# being compiled
KO_DOCKER_REPO ?= $(REGISTRY_HOSTNAME)/$(REGISTRY_USERNAME)

# golang flags are exported through the enviroment variables, reaching all targets
GOFLAGS ?= -v -mod=vendor -ldflags='-Xmain.Version=$(VERSION)'

.EXPORT_ALL_VARIABLES:

default: build

build: $(IMGCTRL) $(PLUGIN_DARWIN) $(PLUGIN)

.PHONY: $(IMGCTRL)
$(IMGCTRL):
go build \
-ldflags="-X 'main.Version=$(VERSION)'" \
-o $(IMGCTRL_BIN) \
./cmd/$(IMGCTRL)

.PHONY: $(PLUGIN)
$(PLUGIN):
go build \
-ldflags="-X 'main.Version=$(VERSION)'" \
-o $(PLUGIN_BIN) \
./cmd/$(PLUGIN)

.PHONY: $(PLUGIN_DARWIN)
$(PLUGIN_DARWIN):
GOOS=darwin GOARCH=amd64 go build \
-tags containers_image_openpgp \
-ldflags="-X 'main.Version=$(VERSION)'" \
-o $(PLUGIN_BIN) \
./cmd/$(PLUGIN)

Expand Down Expand Up @@ -86,7 +100,7 @@ generate-k8s:

.PHONY: image
image:
VERSION=$(VERSION) $(IMAGE_BUILDER) build -f Containerfile -t $(IMAGE) .
$(IMAGE_BUILDER) build -f Containerfile -t $(IMAGE) .

.PHONY: clean
clean:
Expand All @@ -99,3 +113,21 @@ pdf:
-fmarkdown-implicit_figures \
-V geometry:margin=1in \
-o $(OUTPUT_DOC)/README.pdf

# using the environment variable directly to login against the container registry, while the username
# and container registry hostname are regular Makefile variables, please, overwrite them as needed.
registry-login:
echo "$$GITHUB_TOKEN" | \
ko login "$(REGISTRY_HOSTNAME)" --username=$(REGISTRY_USERNAME) --password-stdin

# build and push the container image with ko.
build-image:
ko publish --base-import-paths --tags="${IMAGE_TAG}" ./cmd/$(IMGCTRL)

# installs the helm rendered resources against the infomred namespace.
install:
helm template \
--namespace="$(NAMESPACE)" \
--set="image=ko://github.com/shipwright-io/image/cmd/imgctrl" \
./chart | \
ko apply --base-import-paths --filename -
2 changes: 0 additions & 2 deletions chart/templates/deploy-imgctrl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ spec:
- name: imgctrl
image: {{ .Values.image }}
imagePullPolicy: Always
command:
- /usr/local/bin/imgctrl
volumeMounts:
- mountPath: "/tmp/k8s-webhook-server/serving-certs"
name: certs
Expand Down

0 comments on commit c65cf8f

Please sign in to comment.