diff --git a/Dockerfile b/Dockerfile index 11d413d..3a6f06b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # -# Copyright 2021-2023 Hewlett Packard Enterprise Development LP +# Copyright 2021-2024 Hewlett Packard Enterprise Development LP # Other additional copyright holders may be indicated within. # # The entirety of this work is licensed under the Apache License, @@ -19,6 +19,9 @@ # Build the manager binary FROM golang:1.19 as builder +ARG TARGETARCH +ARG TARGETOS + WORKDIR /workspace # Copy the Go Modules manifests COPY go.mod go.mod @@ -30,7 +33,11 @@ COPY internal/ internal/ COPY github/cluster-api/util/conversion/ github/cluster-api/util/conversion/ # Build -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager cmd/main.go +# the GOARCH has a default value to allow the binary be built according to the host where the command +# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO +# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore, +# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform. +RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go FROM builder as testing WORKDIR /workspace diff --git a/Makefile b/Makefile index e546303..6257425 100644 --- a/Makefile +++ b/Makefile @@ -15,11 +15,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -# Default container tool to use. -# To use podman: -# $ DOCKER=podman make docker-build -DOCKER ?= docker - # VERSION defines the project version for the bundle. # Update this value when you upgrade the version of your project. # To re-generate a bundle for another specific version without changing the standard setup, you can: @@ -68,6 +63,12 @@ else GOBIN=$(shell go env GOBIN) endif +# CONTAINER_TOOL defines the container tool to be used for building images. +# Be aware that the target commands are only tested with Docker which is +# scaffolded by default. However, you might want to replace it to use other +# tools. (i.e. podman) +CONTAINER_TOOL ?= docker + # Setting SHELL to bash allows bash commands to be executed by recipes. # This is a requirement for 'setup-envtest.sh' in the test target. # Options are set to exit when a recipe line exits non-zero or a piped command fails. @@ -165,8 +166,8 @@ vet: ## Run go vet against code. container-unit-test: VERSION ?= $(shell cat .version) container-unit-test: .version ## Build docker image with the manager and execute unit tests. - ${DOCKER} build -f Dockerfile --label $(IMAGE_TAG_BASE)-$@:$(VERSION)-$@ -t $(IMAGE_TAG_BASE)-$@:$(VERSION) --target testing . - ${DOCKER} run --rm -t --name $@-lustre-fs-operator $(IMAGE_TAG_BASE)-$@:$(VERSION) + ${CONTAINER_TOOL} build -f Dockerfile --label $(IMAGE_TAG_BASE)-$@:$(VERSION)-$@ -t $(IMAGE_TAG_BASE)-$@:$(VERSION) --target testing . + ${CONTAINER_TOOL} run --rm -t --name $@-lustre-fs-operator $(IMAGE_TAG_BASE)-$@:$(VERSION) TESTDIR ?= ./internal/... ./api/... test: manifests generate fmt vet envtest ## Run tests. @@ -180,17 +181,40 @@ build: generate fmt vet ## Build manager binary. run: manifests generate fmt vet ## Run a controller from your host. go run cmd/main.go +# If you wish to build the manager image targeting other platforms you can use the --platform flag. +# (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it. +# More info: https://docs.docker.com/develop/develop-images/build_enhancements/ +.PHONY: docker-build docker-build: VERSION ?= $(shell cat .version) docker-build: .version ## Build docker image with the manager. - ${DOCKER} build -t $(IMAGE_TAG_BASE):$(VERSION) . + ${CONTAINER_TOOL} build -t $(IMAGE_TAG_BASE):$(VERSION) . edit-image: VERSION ?= $(shell cat .version) edit-image: .version kustomize ## Replace manager/kustomization.yaml image with name "controller" -> ghcr tagged container reference $(KUSTOMIZE_IMAGE_TAG) config/begin default $(IMAGE_TAG_BASE) $(VERSION) +.PHONY: docker-push docker-push: VERSION ?= $(shell cat .version) docker-push: .version ## Push docker image with the manager. - ${DOCKER} push $(IMAGE_TAG_BASE):$(VERSION) + ${CONTAINER_TOOL} push $(IMAGE_TAG_BASE):$(VERSION) + +# PLATFORMS defines the target platforms for the manager image be built to provide support to multiple +# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). 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=> 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: docker-buildx +docker-buildx: VERSION ?= $(shell cat .version) +docker-buildx: ## Build and push docker image for the manager for cross-platform support + # copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile + sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross + - $(CONTAINER_TOOL) buildx create --name project-v3-builder + $(CONTAINER_TOOL) buildx use project-v3-builder + - $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag $(IMAGE_TAG_BASE):$(VERSION) -f Dockerfile.cross . + - $(CONTAINER_TOOL) buildx rm project-v3-builder + rm Dockerfile.cross kind-push: VERSION ?= $(shell cat .version) kind-push: .version @@ -341,7 +365,7 @@ bundle: manifests kustomize edit-image ## Generate bundle manifests and metadata bundle-build: VERSION ?= $(shell cat .version) bundle-build: BUNDLE_IMG ?= $(IMAGE_TAG_BASE)-bundle:v$(VERSION) bundle-build: .version ## Build the bundle image. - ${DOCKER} build -f bundle.Dockerfile -t $(BUNDLE_IMG) . + ${CONTAINER_TOOL} build -f bundle.Dockerfile -t $(BUNDLE_IMG) . .PHONY: bundle-push bundle-push: VERSION ?= $(shell cat .version) @@ -383,7 +407,7 @@ endif # https://github.com/operator-framework/community-operators/blob/7f1438c/docs/packaging-operator.md#updating-your-existing-operator .PHONY: catalog-build catalog-build: opm ## Build a catalog image. - $(OPM) index add --container-tool ${DOCKER} --mode semver --tag $(CATALOG_IMG) --bundles $(BUNDLE_IMGS) $(FROM_INDEX_OPT) + $(OPM) index add --container-tool ${CONTAINER_TOOL} --mode semver --tag $(CATALOG_IMG) --bundles $(BUNDLE_IMGS) $(FROM_INDEX_OPT) # Push the catalog image. .PHONY: catalog-push diff --git a/config/manager/kustomization.yaml b/config/manager/kustomization.yaml index cb4975c..077e270 100644 --- a/config/manager/kustomization.yaml +++ b/config/manager/kustomization.yaml @@ -18,4 +18,4 @@ kind: Kustomization images: - name: controller newName: ghcr.io/nearnodeflash/lustre-fs-operator - newTag: 0.1.0 + newTag: 0.1.1