Skip to content

Commit

Permalink
Improve build to allow local cross platform builds (#16)
Browse files Browse the repository at this point in the history
* Improve build to allow local cross platform builds

* fix make docker-build

* install go in the scan CI check

* update go to fix CVE-2024-24791

* update controller tools
  • Loading branch information
sergicastro authored Aug 9, 2024
1 parent b515325 commit 62adea8
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
docker-hub:
runs-on: ubuntu-latest
env:
IMG: tetrate/kubegres:${{ github.ref_name }}
IMG: tetrate/kubegres:${{ github.ref_name }}
PLATFORMS: linux/amd64,linux/arm64
steps:
- uses: docker/setup-qemu-action@v3
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/scan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ jobs:
platforms: amd64
- uses: docker/setup-buildx-action@v3
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- run: make docker-build
- uses: aquasecurity/trivy-action@master
with:
Expand Down
39 changes: 6 additions & 33 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,41 +1,14 @@
# Build the manager binary
FROM golang:1.22 as builder

ARG TARGETPLATFORM
ARG BUILDPLATFORM

WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# 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 api/ api/
COPY controllers/ controllers/

# Build
ENV CGO_ENABLED=0 \
GO111MODULE=on

SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN \
export GOOS \
&& GOOS=$(echo ${TARGETPLATFORM} | cut -d / -f1) \
&& export GOARCH \
&& GOARCH=$(echo ${TARGETPLATFORM} | cut -d / -f2) \
&& export GOARM \
&& GOARM=$(echo ${TARGETPLATFORM} | cut -d / -f3 | cut -c2-) \
&& go build -a -o manager main.go
## Build the manager binary

# 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

ARG TARGETOS
ARG TARGETARCH

WORKDIR /
COPY --from=builder /workspace/manager .
COPY build/bin/manager-${TARGETOS}-${TARGETARCH} manager
USER 65532:65532

ENTRYPOINT ["/manager"]
21 changes: 15 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,14 @@ test: build envtest kind ## Run tests.
##@ Build

.PHONY: build
build: manifests generate fmt vet ## Build manager binary.
build: manifests generate fmt vet $(addprefix build/,$(subst $(comma),$(space),$(PLATFORMS))) ## Build manager binary.
go generate
go build -o bin/manager main.go

build/%: PLATFORM=$(*)
build/%: GOARCH=$(notdir $(PLATFORM))
build/%: GOOS=$(subst /,,$(dir $(PLATFORM)))
build/%: ## Build manager binary for a specific platform.
GOOS=${GOOS} GOARCH=${GOARCH} go build -o build/bin/manager-$(GOOS)-$(GOARCH) main.go

.PHONY: run
run: install ## Run a controller from your host.
Expand All @@ -77,22 +82,26 @@ DOCKER_BUILDER_NAME=kubegres
.PHONY: run
docker-buildx:
docker buildx inspect $(DOCKER_BUILDER_NAME) || \
docker buildx create --name $(DOCKER_BUILDER_NAME) --driver docker-container --driver-opt network=host --buildkitd-flags '--allow-insecure-entitlement network.host'
docker buildx create --name $(DOCKER_BUILDER_NAME) --driver docker-container --driver-opt network=host \
--buildkitd-flags '--allow-insecure-entitlement network.host' --platform linux/amd64,linux/arm64

#docker-build: test ## Build docker image with the manager.
.PHONY: docker-build-push
docker-build-push: build docker-buildx ## Build docker image with the manager.
docker buildx build --builder $(DOCKER_BUILDER_NAME) --platform ${PLATFORMS} -t ${IMG} --push .

.PHONY: docker-build
docker-build: $(addprefix docker-build/,$(subst $(comma),$(space),$(PLATFORMS))) ## Build docker images for all platforms.
docker-build: build $(addprefix docker-build/,$(subst $(comma),$(space),$(PLATFORMS))) ## Build docker images for all platforms.

# Intentionally build the image for a specific platform, using arch as the image tag suffix so we avoid overwriting the multi-arch images.
.PHONY: docker-build/%
docker-build/%: PLATFORM=$(*)
docker-build/%: DOCKER_OS=$(subst /,,$(dir $(PLATFORM)))
docker-build/%: DOCKER_ARCH=$(notdir $(PLATFORM))
docker-build/%: docker-buildx ## Build docker image with ARCH as image tag suffix.
docker buildx build --builder $(DOCKER_BUILDER_NAME) --platform ${PLATFORM} -t ${IMG}-${DOCKER_ARCH} --load .
docker buildx build --builder $(DOCKER_BUILDER_NAME) --platform ${PLATFORM} \
--build-arg TARGETOS=$(DOCKER_OS) --build-arg TARGETARCH=$(DOCKER_ARCH) \
-t ${IMG}-${DOCKER_ARCH} --load .

##@ Deployment

Expand Down Expand Up @@ -145,7 +154,7 @@ KIND ?= $(LOCALBIN)/kind

## Tool Versions
KUSTOMIZE_VERSION ?= v3.8.7
CONTROLLER_TOOLS_VERSION ?= v0.9.2
CONTROLLER_TOOLS_VERSION ?= v0.15.0
KIND_VERSION ?= v0.19.0
KUBEBUILDER_TOOLS_VERSION := 1.24.2

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module reactive-tech.io/kubegres

go 1.22.4
go 1.22.5

require (
github.com/go-logr/logr v1.2.3
Expand Down

0 comments on commit 62adea8

Please sign in to comment.