Skip to content

Commit

Permalink
Cross-compile Go binaries outside docker builds (#247)
Browse files Browse the repository at this point in the history
Co-authored-by: Paolo Ambrosio <[email protected]>
  • Loading branch information
jmendesky and paoloambrosio-skyuk authored May 15, 2023
1 parent c7d1e7c commit f2a8ae8
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 110 deletions.
25 changes: 2 additions & 23 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,30 +1,9 @@
# Build the manager binary
FROM golang:1.18 as builder

WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
COPY go.work go.work
COPY argo/ argo/
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN go mod download

# Copy the go source
COPY main.go main.go
COPY apis/ apis/
COPY controllers/ controllers/
COPY external/ external/

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM gcr.io/distroless/static:nonroot

WORKDIR /
COPY --from=builder /workspace/manager .
COPY bin/manager .
USER 65532:65532

ENTRYPOINT ["/manager"]
9 changes: 1 addition & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ IMG ?= kfp-operator-controller
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:preserveUnknownFields=false"

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif

all: build

##@ General
Expand Down Expand Up @@ -94,7 +87,7 @@ integration-test-all: integration-test
##@ Build

build: generate fmt vet ## Build manager binary.
go build -o bin/manager main.go
GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o bin/manager main.go

run: manifests generate fmt vet ## Run a controller from your host.
go run ./main.go --zap-devel --config config/manager/controller_manager_config.yaml
Expand Down
4 changes: 2 additions & 2 deletions argo/common/test_utils.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build unit || decoupled
// +build unit decoupled
//go:build unit || decoupled || integration
// +build unit decoupled integration

package common

Expand Down
14 changes: 8 additions & 6 deletions argo/kfp-compiler/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
FROM python:3.7.12 AS PYTHON37

ARG WHEEL_VERSION

COPY argo/kfp-compiler/dist/*-${WHEEL_VERSION}-*.whl /
COPY dist/*-${WHEEL_VERSION}-*.whl /
RUN pip install /*-${WHEEL_VERSION}-*.whl --target=/kfp-compiler && \
rm /*-${WHEEL_VERSION}-*.whl

FROM python:3.9.13 AS PYTHON39
ARG WHEEL_VERSION

COPY argo/kfp-compiler/dist/*-${WHEEL_VERSION}-*.whl /
COPY dist/*-${WHEEL_VERSION}-*.whl /
RUN pip install /*-${WHEEL_VERSION}-*.whl --target=/kfp-compiler && \
rm /*-${WHEEL_VERSION}-*.whl

FROM alpine:3.14.2

COPY --from=PYTHON37 /kfp-compiler /kfp-compiler/py3.7
COPY --from=PYTHON39 /kfp-compiler /kfp-compiler/py3.9
ADD argo/kfp-compiler/resources/compile.sh /kfp-compiler/compile.sh
WORKDIR /
COPY --from=PYTHON37 /kfp-compiler kfp-compiler/py3.7
COPY --from=PYTHON39 /kfp-compiler kfp-compiler/py3.9
ADD resources/compile.sh kfp-compiler/compile.sh

ADD argo/kfp-compiler/resources/entrypoint.sh /entrypoint.sh
ADD resources/entrypoint.sh entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
18 changes: 2 additions & 16 deletions argo/providers/kfp/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
FROM golang:1.18 as builder

WORKDIR /workspace

COPY argo argo
COPY go.mod go.mod
COPY go.sum go.sum
COPY apis apis

WORKDIR /workspace/argo/providers

RUN go mod download

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o provider ./kfp/cmd

FROM gcr.io/distroless/static:nonroot

WORKDIR /
COPY --from=builder /workspace/argo/providers .
COPY bin/provider .
USER 65532:65532

ENTRYPOINT ["/provider"]
2 changes: 1 addition & 1 deletion argo/providers/kfp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ test: unit-test decoupled-test
##@ Build

build: generate
go build -o bin/provider ./cmd
GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o bin/provider ./cmd

##@ Containers

Expand Down
22 changes: 4 additions & 18 deletions argo/providers/stub/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
FROM golang:1.18 as builder

WORKDIR /workspace

COPY argo argo
COPY go.mod go.mod
COPY go.sum go.sum
COPY apis apis

WORKDIR /workspace/argo/providers

RUN go mod download

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o provider ./stub/cmd

FROM alpine:3.17.0

WORKDIR /
COPY argo/providers/stub/inject.sh /inject.sh
COPY argo/providers/stub/compile.sh /compile.sh
COPY --from=builder /workspace/argo/providers .
COPY inject.sh inject.sh
COPY compile.sh compile.sh
COPY bin/provider .
USER 65532:65532

ENTRYPOINT ["/bin/sh", "/inject.sh"]
2 changes: 1 addition & 1 deletion argo/providers/stub/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ IMG := kfp-operator-stub-provider
##@ Build

build:
go build -o bin/provider ./cmd
GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o bin/provider ./cmd

##@ Containers

Expand Down
18 changes: 2 additions & 16 deletions argo/providers/vai/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
FROM golang:1.18 as builder

WORKDIR /workspace

COPY argo argo
COPY go.mod go.mod
COPY go.sum go.sum
COPY apis apis

WORKDIR /workspace/argo/providers

RUN go mod download

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o provider ./vai/cmd

FROM gcr.io/distroless/static:nonroot

WORKDIR /
COPY --from=builder /workspace/argo/providers .
COPY bin/provider .
USER 65532:65532

ENTRYPOINT ["/provider"]
2 changes: 1 addition & 1 deletion argo/providers/vai/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ generate: mockgen
$(MOCKGEN) -destination mock_pipeline_client.go -package=vai github.com/sky-uk/kfp-operator/argo/providers/vai PipelineJobClient

build: generate
go build -o bin/provider ./cmd
GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o bin/provider ./cmd

##@ Containers

Expand Down
18 changes: 2 additions & 16 deletions argo/run-completer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
FROM golang:1.18 as builder

WORKDIR /workspace

COPY argo argo
COPY go.mod go.mod
COPY go.sum go.sum
COPY apis apis

WORKDIR /workspace/argo/run-completer

RUN go mod download

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o run-completer ./cmd

FROM gcr.io/distroless/static:nonroot

WORKDIR /
COPY --from=builder /workspace/argo/run-completer .
COPY bin/run-completer .
USER 65532:65532

ENTRYPOINT ["/run-completer"]
2 changes: 1 addition & 1 deletion argo/run-completer/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test: unit-test decoupled-test
##@ Build

build:
go build -o bin/run-completer ./cmd
GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o bin/run-completer ./cmd

fmt: ## Run go fmt against code.
go fmt ./...
Expand Down
4 changes: 3 additions & 1 deletion docker-targets.mk
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ define docker-push-to-registry
endef
endif

docker-build: GOOS=linux
docker-build: GOARCH=amd64
docker-build: build ## Build container image
docker build ${DOCKER_BUILD_EXTRA_PARAMS} -t ${IMG} -t ${IMG}:${VERSION} -f Dockerfile $(_DOCKER_TARGETS_MK_DIR)
docker build ${DOCKER_BUILD_EXTRA_PARAMS} -t ${IMG} -t ${IMG}:${VERSION} -f Dockerfile .

0 comments on commit f2a8ae8

Please sign in to comment.