Skip to content

Commit

Permalink
Merge pull request #1091 from nestoracunablanco/feat/multiarchDockerfile
Browse files Browse the repository at this point in the history
s390x enablement. Update Makefile and Dockerfiles
  • Loading branch information
kubevirt-bot authored Oct 17, 2024
2 parents fc6b617 + de58f9f commit 347b29e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 37 deletions.
26 changes: 15 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
# Build the manager binary
# Build this Dockerfile using the command: make container-build
#
# This multi-stage image approach prevents issues related to cached builder images,
# which may be incompatible due to different architectures, potentially slowing down or breaking the build process.
#
# By utilizing Go cross-compilation, we can build the target Go binary from the host architecture
# and then copy it to the target image with the desired architecture.

ARG TARGET_ARCH=amd64
FROM registry.access.redhat.com/ubi9/ubi-minimal as builder
ARG TARGET_ARCH

RUN microdnf install -y make tar gzip which && microdnf clean all

RUN curl -L https://go.dev/dl/go1.22.4.linux-amd64.tar.gz | tar -C /usr/local -xzf -
RUN export ARCH=$(uname -m | sed 's/x86_64/amd64/'); curl -L https://go.dev/dl/go1.22.4.linux-${ARCH}.tar.gz | tar -C /usr/local -xzf -
ENV PATH=$PATH:/usr/local/go/bin

# Consume required variables so we can work with make
ARG IMG_REPOSITORY
ARG IMG_TAG
ARG IMG
ARG VALIDATOR_REPOSITORY
ARG VALIDATOR_IMG_TAG
ARG VALIDATOR_IMG

WORKDIR /workspace
Expand All @@ -34,12 +38,12 @@ COPY hack/csv-generator.go hack/csv-generator.go
# Copy .golangci.yaml so we can run lint as part of the build process
COPY .golangci.yaml .golangci.yaml

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on make manager
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on make csv-generator
# Build the manager binary
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${TARGET_ARCH} GO111MODULE=on make manager
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${TARGET_ARCH} GO111MODULE=on make csv-generator


FROM registry.access.redhat.com/ubi9/ubi-minimal
FROM --platform=linux/${TARGET_ARCH} registry.access.redhat.com/ubi9/ubi-micro
LABEL org.kubevirt.hco.csv-generator.v1="/csv-generator"

WORKDIR /
Expand Down
37 changes: 13 additions & 24 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ else
OC = oc
endif

# Default to podman
SSP_BUILD_RUNTIME ?= podman

ifndef ignore-not-found
ignore-not-found = false
endif
Expand Down Expand Up @@ -199,38 +196,28 @@ container-build: unittest bundle
mkdir -p data/crd
cp bundle/manifests/ssp-operator.clusterserviceversion.yaml data/olm-catalog/ssp-operator.clusterserviceversion.yaml
cp bundle/manifests/ssp.kubevirt.io_ssps.yaml data/crd/ssp.kubevirt.io_ssps.yaml
${SSP_BUILD_RUNTIME} build -t ${IMG} \
--build-arg IMG_REPOSITORY=${IMG_REPOSITORY} \
--build-arg IMG_TAG=${IMG_TAG} \
--build-arg IMG=${IMG} \
--build-arg VALIDATOR_REPOSITORY=${VALIDATOR_REPOSITORY} \
--build-arg VALIDATOR_IMG_TAG=${VALIDATOR_IMG_TAG} \
--build-arg VALIDATOR_IMG=${VALIDATOR_IMG} \
.
podman manifest rm ${IMG} || true
podman build --build-arg TARGET_ARCH=amd64 --build-arg VALIDATOR_IMG=${VALIDATOR_IMG} --manifest=${IMG} . && \
podman build --build-arg TARGET_ARCH=s390x --build-arg VALIDATOR_IMG=${VALIDATOR_IMG} --manifest=${IMG} .

# Push the container image
.PHONY: container-push
container-push:
${SSP_BUILD_RUNTIME} push ${IMG}
podman manifest push ${IMG}

.PHONY: build-template-validator
build-template-validator:
./hack/build-template-validator.sh ${VERSION}

.PHONY: build-template-validator-container
build-template-validator-container:
${SSP_BUILD_RUNTIME} build -t ${VALIDATOR_IMG} \
--build-arg IMG_REPOSITORY=${IMG_REPOSITORY} \
--build-arg IMG_TAG=${IMG_TAG} \
--build-arg IMG=${IMG} \
--build-arg VALIDATOR_REPOSITORY=${VALIDATOR_REPOSITORY} \
--build-arg VALIDATOR_IMG_TAG=${VALIDATOR_IMG_TAG} \
--build-arg VALIDATOR_IMG=${VALIDATOR_IMG} \
. -f validator.Dockerfile
podman manifest rm ${VALIDATOR_IMG} || true && \
podman build --build-arg TARGET_ARCH=amd64 --manifest=${VALIDATOR_IMG} . -f validator.Dockerfile && \
podman build --build-arg TARGET_ARCH=s390x --manifest=${VALIDATOR_IMG} . -f validator.Dockerfile

.PHONY: push-template-validator-container
push-template-validator-container:
${SSP_BUILD_RUNTIME} push ${VALIDATOR_IMG}
podman manifest push ${VALIDATOR_IMG}


##@ Build Dependencies
Expand All @@ -257,14 +244,16 @@ kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
$(KUSTOMIZE): $(LOCALBIN)
test -s $(LOCALBIN)/kustomize || curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN)

# The final line allows for downloading and copying into the LOCALBIN folder when cross-compiling, as GOBIN is not compatible with setting a different GOARCH
.PHONY: controller-gen
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
$(CONTROLLER_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
test -s $(LOCALBIN)/controller-gen || \
GOBIN=$(LOCALBIN) GOARCH=$(ARCH) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)

# Download operator-sdk locally if necessary
$(OPERATOR_SDK): $(LOCALBIN)
curl --create-dirs -JL https://github.com/operator-framework/operator-sdk/releases/download/$(OPERATOR_SDK_VERSION)/operator-sdk_linux_amd64 -o $(OPERATOR_SDK)
curl --create-dirs -JL https://github.com/operator-framework/operator-sdk/releases/download/$(OPERATOR_SDK_VERSION)/operator-sdk_linux_$(ARCH) -o $(OPERATOR_SDK)
chmod 0755 $(OPERATOR_SDK)

.PHONY: operator-sdk
Expand All @@ -289,7 +278,7 @@ bundle: operator-sdk manifests kustomize csv-generator manager-envsubst
# Build the bundle image.
.PHONY: bundle-build
bundle-build:
${SSP_BUILD_RUNTIME} build -f bundle.Dockerfile -t $(BUNDLE_IMG) .
podman build -f bundle.Dockerfile -t $(BUNDLE_IMG) .

.PHONY: release
release: container-build container-push build-template-validator-container push-template-validator-container bundle build-functests
Expand Down
16 changes: 14 additions & 2 deletions validator.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
# Build this Dockerfile using the command: make build-template-validator-container
#
# This multi-stage image approach prevents issues related to cached builder images,
# which may be incompatible due to different architectures, potentially slowing down or breaking the build process.
#
# By utilizing Go cross-compilation, we can build the target Go binary from the host architecture
# and then copy it to the target image with the desired architecture.

ARG TARGET_ARCH=amd64
FROM registry.access.redhat.com/ubi9/ubi-minimal as builder
ARG TARGET_ARCH

RUN microdnf install -y make tar gzip which && microdnf clean all
RUN export ARCH=$(uname -m | sed 's/x86_64/amd64/'); curl -L https://go.dev/dl/go1.22.4.linux-${ARCH}.tar.gz | tar -C /usr/local -xzf -
Expand All @@ -22,15 +32,17 @@ COPY api/ api/
COPY internal/ internal/
COPY pkg/ pkg/

RUN CGO_ENABLED=0 GOOS=linux GO111MODULE=on go build -a -ldflags="-X 'kubevirt.io/ssp-operator/internal/template-validator/version.COMPONENT=$COMPONENT'\
# Compile for the TARGET_ARCH
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${TARGET_ARCH} GO111MODULE=on go build -a -ldflags="-X 'kubevirt.io/ssp-operator/internal/template-validator/version.COMPONENT=$COMPONENT'\
-X 'kubevirt.io/ssp-operator/internal/template-validator/version.VERSION=$VERSION'\
-X 'kubevirt.io/ssp-operator/internal/template-validator/version.BRANCH=$BRANCH'\
-X 'kubevirt.io/ssp-operator/internal/template-validator/version.REVISION=$REVISION'" -o kubevirt-template-validator internal/template-validator/main.go

# Hack: Create an empty directory in the builder image and copy it to the target image to avoid triggering any architecture-specific commands
RUN mkdir emptydir

FROM registry.access.redhat.com/ubi9/ubi-micro

FROM --platform=linux/${TARGET_ARCH} registry.access.redhat.com/ubi9/ubi-micro

# Hack: Refer to the last comment in the builder image.
COPY --from=builder /workspace/emptydir /etc/webhook/certs
Expand Down

0 comments on commit 347b29e

Please sign in to comment.