Skip to content

Commit b7d6a9e

Browse files
authored
fix(): critical vuln fixes, optimise multi arch build (#457)
* fix(): Optimise multi arch builds Signed-off-by: gourishkb <gourish@aveshasystems.com> * fix(): critical vuln issues Signed-off-by: gourishkb <gourish@aveshasystems.com> --------- Signed-off-by: gourishkb <gourish@aveshasystems.com>
1 parent d7e1cd6 commit b7d6a9e

3 files changed

Lines changed: 28 additions & 23 deletions

File tree

Dockerfile

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# syntax=docker/dockerfile:1
12
##########################################################
23
#Dockerfile
34
#Copyright (c) 2022 Avesha, Inc. All rights reserved.
@@ -16,31 +17,32 @@
1617
#See the License for the specific language governing permissions and
1718
#limitations under the License.
1819
##########################################################
19-
20-
# Build the manager binary
21-
FROM golang:1.24 AS builder
22-
23-
WORKDIR /workspace
24-
# Copy the Go Modules manifests
25-
COPY go.mod go.mod
26-
COPY go.sum go.sum
27-
ADD vendor vendor
28-
# cache deps before building and copying source so that we don't need to re-download as much
29-
# and so that source changes don't invalidate our downloaded layer
30-
#RUN echo "[url \"git@bitbucket.org:\"]\n\tinsteadOf = https://bitbucket.org/" >> /root/.gitconfig
31-
20+
FROM --platform=$BUILDPLATFORM golang:1.24 AS builder
21+
LABEL maintainer="Avesha Systems"
3222
ARG TARGETOS
33-
ARG TARGETPLATFORM
3423
ARG TARGETARCH
24+
ARG BUILDPLATFORM
25+
WORKDIR /workspace
26+
27+
# Copy the Go Modules manifests first for better layer caching
28+
COPY go.mod go.sum ./
29+
# Copy vendor directory (required for -mod=vendor build)
30+
COPY vendor vendor/
3531

3632
# Copy the go source
37-
COPY main.go main.go
33+
COPY main.go ./
3834
COPY api/ api/
3935
COPY controllers/ controllers/
4036
COPY pkg/ pkg/
4137
COPY events/ events/
42-
# Build
43-
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} GO111MODULE=on go build -mod=vendor -a -o manager main.go
38+
39+
# Cross-compile with optimizations and caching
40+
RUN --mount=type=cache,target=/root/.cache/go-build \
41+
--mount=type=cache,target=/go/pkg \
42+
CGO_ENABLED=0 \
43+
GOOS=${TARGETOS:-linux} \
44+
GOARCH=${TARGETARCH} \
45+
go build -mod=vendor -ldflags="-w -s" -trimpath -o manager main.go
4446

4547
# Use distroless as minimal base image to package the manager binary
4648
# Refer to https://github.com/GoogleContainerTools/distroless for more details

Makefile

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,15 @@ run: manifests generate fmt vet ## Run a controller from your host.
114114
go run ./main.go
115115

116116
.PHONY: docker-build
117-
docker-build: ## Build docker image with the manager.
118-
docker buildx create --name container --driver=docker-container || true
119-
docker build --builder container --platform linux/amd64,linux/arm64 -t ${IMG} .
117+
docker-build: ## Build docker image with the manager (multi-arch, requires --push to create manifest list).
118+
docker buildx create --name container --driver=docker-container --use || docker buildx use container || true
119+
docker buildx build --builder container --platform linux/amd64,linux/arm64 -t ${IMG} .
120120

121121
.PHONY: docker-push
122-
docker-push: ## Push docker image with the manager.
123-
docker buildx create --name container --driver=docker-container || true
124-
docker build --push --builder container --platform linux/amd64,linux/arm64 -t ${IMG} .
122+
docker-push: ## Build and push docker image with the manager (multi-arch manifest list).
123+
@echo "Note: BuildKit requires network access to verify image manifests. If offline, images must be pre-pulled."
124+
docker buildx create --name container --driver=docker-container --use || docker buildx use container || true
125+
docker buildx build --push --builder container --platform linux/amd64,linux/arm64 -t ${IMG} .
125126

126127
##@ Deployment
127128

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module github.com/kubeslice/worker-operator
22

33
go 1.24.0
44

5+
toolchain go1.24.11
6+
57
// replace github.com/kubeslice/apis => ../../misc/apis
68

79
require (

0 commit comments

Comments
 (0)