Skip to content

Commit

Permalink
feat: Multi-arch image building (#805)
Browse files Browse the repository at this point in the history
Signed-off-by: killianmuldoon <cilliancapi@gmail.com>
(cherry picked from commit 08d0048)
  • Loading branch information
e0ne authored Feb 13, 2024
2 parents 5f35deb + c428d61 commit 3d2a758
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 22 deletions.
27 changes: 14 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Ensure the final image uses the correct platform
ARG ARCH

# Build the manager binary
FROM golang:1.20 as builder

WORKDIR /workspace
# Add kubectl tool
ARG TARGETPLATFORM
ENV TARGETPLATFORM=${TARGETPLATFORM:-linux/amd64}
RUN curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/${TARGETPLATFORM}/kubectl"
RUN chmod +x ./kubectl
# Using the $ARCH in the name of the binary here ensures we don't get any cross-arch caching after this binary is downloaded.
ARG ARCH
RUN curl -L "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/${ARCH}/kubectl" -o kubectl-${ARCH}
RUN chmod +x ./kubectl-${ARCH}

# Copy the Go Modules manifests
COPY go.mod go.mod
Expand All @@ -30,7 +33,6 @@ COPY go.sum go.sum
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download -x


# Copy the go source
COPY ./ ./

Expand All @@ -41,16 +43,15 @@ RUN mkdir crds && \
cp -r deployment/network-operator/charts/node-feature-discovery/crds /workspace/crds/node-feature-discovery/

# Build
ARG ARCH ?= $(shell go env GOARCH)
ARG LDFLAGS
ARG GO_BUILD_GC_ARGS
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg/mod \
CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} go build -ldflags="${LDFLAGS}" -gcflags="${GO_BUILD_GC_ARGS}" -o manager main.go

ARG GCFLAGS
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} go build -ldflags="${LDFLAGS}" -gcflags="${GCFLAGS}" -o manager main.go

FROM registry.access.redhat.com/ubi8-micro:8.8
FROM --platform=linux/${ARCH} registry.access.redhat.com/ubi8-micro:8.8

ARG ARCH
ARG BUILD_DATE
ARG VERSION
ARG VCS_REF
Expand All @@ -74,7 +75,7 @@ LABEL org.label-schema.vcs-url="https://github.com/Mellanox/network-operator"

WORKDIR /
COPY --from=builder /workspace/manager .
COPY --from=builder /workspace/kubectl /usr/local/bin
COPY --from=builder /workspace/kubectl-${ARCH} /usr/local/bin/kubectl
COPY --from=builder /workspace/crds /crds

COPY /webhook-schemas /webhook-schemas
Expand Down
40 changes: 38 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,24 @@ VERSION?=master
DATE=`date -Iseconds`
COMMIT?=`git rev-parse --verify HEAD`
LDFLAGS="-X github.com/Mellanox/network-operator/version.Version=$(BUILD_VERSION) -X github.com/Mellanox/network-operator/version.Commit=$(COMMIT) -X github.com/Mellanox/network-operator/version.Date=$(DATE)"
GCFLAGS=""
BUILD_VERSION := $(strip $(shell [ -d .git ] && git describe --always --tags --dirty))
BUILD_TIMESTAMP := $(shell date -u +"%Y-%m-%dT%H:%M:%S%Z")
VCS_BRANCH := $(strip $(shell git rev-parse --abbrev-ref HEAD))
VCS_REF := $(strip $(shell [ -d .git ] && git rev-parse --short HEAD))

# Docker
IMAGE_BUILDER?=docker
IMAGEDIR=$(CURDIR)/images
DOCKERFILE?=$(CURDIR)/Dockerfile
TAG?=mellanox/network-operator
REGISTRY?=docker.io
IMAGE_NAME?=network-operator
CONTROLLER_IMAGE=$(REGISTRY)/$(IMAGE_NAME)
IMAGE_BUILD_OPTS?=
BUNDLE_IMG?=network-operator-bundle:$(VERSION)
# BUNDLE_GEN_FLAGS are the flags passed to the operator-sdk generate bundle command
BUNDLE_GEN_FLAGS ?= -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)
BUILD_ARCH= amd64 arm64

# USE_IMAGE_DIGESTS defines if images are resolved via tags or digests
# You can enable this value if you would like to use SHA Based Digests
Expand Down Expand Up @@ -199,7 +203,8 @@ lint: | $(GOLANGCI_LINT) ; $(info running golangci-lint...) @ ## Run golangci-l

.PHONY: lint-dockerfile
lint-dockerfile: $(HADOLINT) ; $(info running Dockerfile lint with hadolint...) @ ## Run hadolint
$Q $(HADOLINT) Dockerfile
# Ignoring warning DL3029: Do not use --platform flag with FROM
$Q $(HADOLINT) --ignore DL3029 Dockerfile

.PHONY: lint-helm
lint-helm: $(HELM) ; $(info running lint for helm charts...) @ ## Run helm lint
Expand Down Expand Up @@ -259,11 +264,42 @@ image: ; $(info Building Docker image...) @ ## Build container image
--build-arg VCS_REF="$(VCS_REF)" \
--build-arg VCS_BRANCH="$(VCS_BRANCH)" \
--build-arg LDFLAGS=$(LDFLAGS) \
--build-arg ARCH="$(ARCH)" \
--build-arg GCFLAGS="$(GCFLAGS)" \
-t $(TAG) -f $(DOCKERFILE) $(CURDIR) $(IMAGE_BUILD_OPTS)

image-push:
$(IMAGE_BUILDER) push $(TAG)

# Container image
.PHONY: image-build
image-build: ; $(info Building Docker image...) @ ## Build container image
DOCKER_BUILDKIT=1 $(IMAGE_BUILDER) build --build-arg BUILD_DATE="$(BUILD_TIMESTAMP)" \
--build-arg VERSION="$(BUILD_VERSION)" \
--build-arg VCS_REF="$(VCS_REF)" \
--build-arg VCS_BRANCH="$(VCS_BRANCH)" \
--build-arg LDFLAGS=$(LDFLAGS) \
--build-arg ARCH="$(ARCH)" \
--build-arg GCFLAGS="$(GCFLAGS)" \
-t $(CONTROLLER_IMAGE)-$(ARCH):$(VERSION) -f $(DOCKERFILE) $(CURDIR) $(IMAGE_BUILD_OPTS)

image-build-%:
make ARCH=$* image-build

.PHONY: image-build-multiarch
image-build-multiarch: $(addprefix image-build-,$(BUILD_ARCH))

image-push-for-arch:
$(IMAGE_BUILDER) push $(CONTROLLER_IMAGE)-$(ARCH):$(VERSION)

image-push-for-arch-%:
make ARCH=$* image-push-for-arch

.PHONY: image-push-multiarch
image-push-multiarch: $(addprefix image-push-for-arch-,$(BUILD_ARCH))
$(IMAGE_BUILDER) manifest create $(CONTROLLER_IMAGE):$(VERSION) $(shell echo $(BUILD_ARCH) | sed -e "s~[^ ]*~$(CONTROLLER_IMAGE)\-&:$(VERSION)~g")
$(IMAGE_BUILDER) manifest push --purge $(CONTROLLER_IMAGE):$(VERSION)

.PHONY: chart-build
chart-build: $(HELM) ; $(info Building Helm image...) @ ## Build Helm Chart
@if [ -z "$(APP_VERSION)" ]; then \
Expand Down
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -447,13 +447,17 @@ Example can be found under `./example/crs/mellanox.com_v1alpha1_macvlannetwork_c
A deployment example can be found under `example` folder [here](https://github.com/Mellanox/network-operator/blob/master/example/README.md).
## Docker image
Network operator uses `alpine` base image by default. To build Network operator with
another base image you need to pass `BASE_IMAGE` argument:
To build a container image for Network Operator use:
```bash
make image
```
docker build -t network-operator \
--build-arg BASE_IMAGE=registry.access.redhat.com/ubi8/ubi-minimal:latest \
.

To build a multi-arch image and publish to a registry use:
```bash
export REGISTRY=example.com/registry
export IMAGE_NAME=network-operator
export VERSION=v1.1.1
make image-build-multiarch image-push-multiarch
```

## Driver Containers
Expand Down
2 changes: 1 addition & 1 deletion skaffold.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ build:
docker:
buildArgs:
## Used for debugging. Disable inlining and optimizations in the go compiler. Passed to go build using `-gcflags`
GO_BUILD_GC_ARGS: "all=-N -l"
GCFLAGS: "all=-N -l"
dockerfile: Dockerfile
manifests:
## Deploy the manager.
Expand Down

0 comments on commit 3d2a758

Please sign in to comment.