Skip to content

Commit

Permalink
fix: fix Makefile and Dockerfile for idempotent images and to support…
Browse files Browse the repository at this point in the history
… multi-arch builds Fixes RHOAIENG-12078 (#322)

* fix: remove .git from Dockerfile

Signed-off-by: Dhiraj Bokde <[email protected]>

* fix: remove git check for model-registry.yaml in Makefile
fix gen/openapi-server make target to generate go-server if model-registry.yaml file is newer

Signed-off-by: Dhiraj Bokde <[email protected]>

* fix: commit updated type_asserts.go

Signed-off-by: Dhiraj Bokde <[email protected]>

* fix: sort assert functions in gen_type_asserts.sh before writing them to type_asserts.go

Signed-off-by: Dhiraj Bokde <[email protected]>

* feat: add 'image/buildx' target to create multi-platform docker images using docker buildx,
split 'build' target into 'build/prepare' and 'build/compile' to optimize multi-platform builds

Signed-off-by: Dhiraj Bokde <[email protected]>

* feat: add docker env variables to enable multi-platform cross compilation, optimize build steps

Signed-off-by: Dhiraj Bokde <[email protected]>

* fix: add podman support for buildx

Signed-off-by: Dhiraj Bokde <[email protected]>

* fix: remove docker builder if it already exists

Signed-off-by: Dhiraj Bokde <[email protected]>

---------

Signed-off-by: Dhiraj Bokde <[email protected]>
  • Loading branch information
dhirajsb authored Sep 19, 2024
1 parent 044edd7 commit 3b00296
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 197 deletions.
15 changes: 12 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Build the model-registry binary
FROM registry.access.redhat.com/ubi8/go-toolset:1.21 AS builder
FROM --platform=$BUILDPLATFORM registry.access.redhat.com/ubi8/go-toolset:1.21 AS builder
ARG TARGETOS
ARG TARGETARCH

WORKDIR /workspace
# Copy the Go Modules manifests
Expand All @@ -20,7 +22,6 @@ RUN yum install -y nodejs npm java-11
COPY ["Makefile", "main.go", ".openapi-generator-ignore", "openapitools.json", "./"]

# Copy rest of the source
COPY .git/ .git/
COPY cmd/ cmd/
COPY api/ api/
COPY internal/ internal/
Expand All @@ -34,7 +35,15 @@ RUN make deps

# Build
USER root
RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 make clean model-registry

# NOTE: The two instructions below are effectively equivalent to 'make clean build'
# DO NOT REMOVE THE 'build/prepare' TARGET!!!
# It ensures consitent repeatable Dockerfile builds

# prepare the build in a separate layer
RUN make clean build/prepare
# compile separately to optimize multi-platform builds
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} make build/compile

# Use distroless as minimal base image to package the model-registry binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
56 changes: 45 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,10 @@ openapi/validate: bin/openapi-generator-cli

# generate the openapi server implementation
.PHONY: gen/openapi-server
gen/openapi-server: bin/openapi-generator-cli openapi/validate
@if git diff --cached --exit-code --name-only | grep -q "api/openapi/model-registry.yaml" || \
git diff --exit-code --name-only | grep -q "api/openapi/model-registry.yaml" || \
[ -n "${FORCE_SERVER_GENERATION}" ]; then \
./scripts/gen_openapi_server.sh; \
else \
echo "INFO api/openapi/model-registry.yaml is not staged or modified, will not re-generate server"; \
fi
gen/openapi-server: bin/openapi-generator-cli openapi/validate internal/server/openapi/api_model_registry_service.go

internal/server/openapi/api_model_registry_service.go: bin/openapi-generator-cli api/openapi/model-registry.yaml
ROOT_FOLDER=${PROJECT_PATH} ./scripts/gen_openapi_server.sh

# generate the openapi schema model and client
.PHONY: gen/openapi
Expand All @@ -102,7 +98,7 @@ vet:

.PHONY: clean
clean:
rm -Rf ./model-registry internal/ml_metadata/proto/*.go internal/converter/generated/*.go pkg/openapi
rm -Rf ./model-registry internal/ml_metadata/proto/*.go internal/converter/generated/*.go pkg/openapi internal/server/openapi/api_model_registry_service.go

.PHONY: clean/odh
clean/odh:
Expand Down Expand Up @@ -157,10 +153,22 @@ deps: bin/protoc bin/go-enum bin/protoc-gen-go bin/protoc-gen-go-grpc bin/golang
vendor:
${GO} mod vendor

.PHONY: build
build: gen vet lint
# WARNING: DO NOT DELETE THIS TARGET, USED BY Dockerfile!!!
.PHONY: build/prepare
build/prepare: gen vet lint

# WARNING: DO NOT DELETE THIS TARGET, USED BY Dockerfile!!!
.PHONY: build/compile
build/compile:
${GO} build -buildvcs=false

# WARNING: DO NOT EDIT THIS TARGET DIRECTLY!!!
# Use build/prepare to add build prerequisites
# Use build/compile to add/edit go source compilation
# WARNING: Editing this target directly WILL affect the Dockerfile image build!!!
.PHONY: build
build: build/prepare build/compile

.PHONY: build/odh
build/odh: vet
${GO} build -buildvcs=false
Expand Down Expand Up @@ -210,6 +218,32 @@ endif
image/build:
${DOCKER} build . -f ${DOCKERFILE} -t ${IMG}:$(IMG_VERSION)

# build docker image using buildx
# PLATFORMS defines the target platforms for the model registry image be built to provide support to multiple
# architectures. (i.e. make docker-buildx). To use this option you need to:
# - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/
# - have enabled BuildKit. More info: https://docs.docker.com/develop/develop-images/build_enhancements/
# - be able to push the image to your registry (i.e. if you do not set a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
# To adequately provide solutions that are compatible with multiple platforms, you should consider using this option.
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
.PHONY: image/buildx
image/buildx:
ifeq ($(DOCKER),docker)
# docker uses builder containers
- $(DOCKER) buildx rm model-registry-builder
$(DOCKER) buildx create --use --name model-registry-builder --platform=$(PLATFORMS)
$(DOCKER) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f ${DOCKERFILE} .
$(DOCKER) buildx rm model-registry-builder
else ifeq ($(DOCKER),podman)
# podman uses image manifests
$(DOCKER) manifest create -a ${IMG}
$(DOCKER) buildx build --platform=$(PLATFORMS) --manifest ${IMG} -f ${DOCKERFILE} .
$(DOCKER) manifest push ${IMG}
$(DOCKER) manifest rm ${IMG}
else
$(error Unsupported container tool $(DOCKER))
endif

# push docker image
.PHONY: image/push
image/push:
Expand Down
Loading

0 comments on commit 3b00296

Please sign in to comment.