samba-in-kubernetes / samba-operator

An operator for a Samba as a service on PVCs in kubernetes
Apache License 2.0
101 stars 24 forks source link

makefile: build multi-arch image #301

Open synarete opened 1 year ago

synarete commented 1 year ago

Build multi-architecture image using buildah and qemu static utilities. Creates an image with manifest which refs amd64 (x86_64) and arm64 (aarch64) architecture-specific sub-images. Using the same Dockerfile regardless of the actual CPU architecture, and let buildah+qemu do all the low-level logic.

Note: typically, qemu would emulate arm64 on x86_64 which yields longer build time.

phlogistonjohn commented 1 year ago

Also, could we leverage make's typical target based workflows rather than an embedded shell loop? Something like:

.PHONY: image-build-multiarch
image-build-multiarch:  image-build-arch-amd64 image-build-arch-arm64
     $(BUILDAH_CMD) manifest inspect $(IMG)

image-build-arch-%:  qemu-utils image-build-multiarch-manifest
    $(BUILDAH_CMD) bud \
            --manifest $(IMG) \
            --arch $* \
            --tag "$(IMG)-$*" \
            --build-arg=GIT_VERSION="$(GIT_VERSION)" \
            --build-arg=COMMIT_ID="$(COMMIT_ID)" \
            --build-arg=ARCH="$*" . 

image-build-multiarch-manifest:
    $(BUILDAH_CMD) manifest create $(IMG)

.PHONY: image-push-multiarch
image-push-multiarch:
    $(BUILDAH_CMD) manifest push --all $(IMG) "docker://$(IMG)"

.PHONY: image-multiarch
image-multiarch: image-build-multiarch image-push-multiarch

(I left out .PHONYs on my added rule names just to be a tad shorter in a gh comment)

synarete commented 1 year ago

Looks pretty good so far, I am a bit curious why it uses buidah directly rather than docker/podman. Were there features of the process that are only supported by buildah?

docker uses the docker buildx to build multi-arch images; unfortunately, this is docker specific sub-command. I used BUILDAH_CMD instead of CONTAINER_CMD as it has the --manifest and --arch flags. With podman it would require multi-steps build.

synarete commented 1 year ago

Also, could we leverage make's typical target based workflows rather than an embedded shell loop? Something like:

.PHONY: image-build-multiarch
image-build-multiarch:  image-build-arch-amd64 image-build-arch-arm64
     $(BUILDAH_CMD) manifest inspect $(IMG)

image-build-arch-%:  qemu-utils image-build-multiarch-manifest
  $(BUILDAH_CMD) bud \
          --manifest $(IMG) \
          --arch $* \
          --tag "$(IMG)-$*" \
          --build-arg=GIT_VERSION="$(GIT_VERSION)" \
          --build-arg=COMMIT_ID="$(COMMIT_ID)" \
          --build-arg=ARCH="$*" . 

image-build-multiarch-manifest:
  $(BUILDAH_CMD) manifest create $(IMG)

.PHONY: image-push-multiarch
image-push-multiarch:
  $(BUILDAH_CMD) manifest push --all $(IMG) "docker://$(IMG)"

.PHONY: image-multiarch
image-multiarch: image-build-multiarch image-push-multiarch

(I left out .PHONYs on my added rule names just to be a tad shorter in a gh comment)

Fair enough. Will fix.

phlogistonjohn commented 11 months ago

Removed priority-review label because this is a draft.