Skip to content

Commit

Permalink
Add cross-platform docker builds (#218)
Browse files Browse the repository at this point in the history
* Make nifikop docker image cross-platform

* handle PUSHLATEST flag in docker-buildx command

* update changelog

* generate manifests before building images in CI

* fixed indentation
  • Loading branch information
mh013370 authored Mar 14, 2023
1 parent 1849eae commit 0a8ce81
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
8 changes: 4 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ orbs:
- build/_output
- vendor

# Build job, which build operator docker image (with operator-sdk build)
# Build job, which builds the cross-platform operator docker image (with operator-sdk build)
build-operator:
<<: *params_operator
<<: *job_operator
Expand All @@ -97,13 +97,13 @@ orbs:
- << parameters.operatorDir >>/build/_output
- << parameters.operatorDir >>/vendor
- deploy:
name: Push image to Docker Hub
name: Build & Push Operator Image to Github Container Registry
command: |
if [[ $(echo "$CIRCLE_BRANCH" | grep -c "pull") -gt 0 ]]; then
echo "This is a PR, we don't push to Hub."
echo "This is a PR, we don't push to GitHub."
else
docker login ghcr.io -u $GH_NAME --password $GH_TOKEN
make docker-push
make docker-buildx
fi
- save_cache:
name: Save build artifacts in cache
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Added

- [PR #218](https://github.com/konpyutaika/nifikop/pull/218) - **[Operator]** Add cross-platform support to nifikop docker image.
- [PR #220](https://github.com/konpyutaika/nifikop/pull/220) - **[Operator/NifiCluster]** Made `Pod` readiness and liveness checks configurable.

### Changed
Expand Down
8 changes: 6 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ COPY controllers/ controllers/
COPY pkg/ pkg/
COPY version/ version/

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager main.go
# Build the operator
# TARGETARCH, TARGETOS are automatically set by docker.
# see: https://sdk.operatorframework.io/docs/advanced-topics/multi-arch/#manifest-lists
# see: https://www.docker.com/blog/faster-multi-platform-builds-dockerfile-cross-compilation-guide/
ARG TARGETOS TARGETARCH
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH GO111MODULE=on go build -a -o manager main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,13 @@ docker-build:
docker build -t $(REPOSITORY):$(VERSION) .

.PHONY: build
build: manager manifests docker-build
build: manager manifests

.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)


# Run go fmt against code
.PHONY: fmt
fmt:
Expand Down Expand Up @@ -256,7 +255,11 @@ docker-buildx: test ## Build and push docker image for the manager for cross-pla
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
- docker buildx create --name project-v3-builder
docker buildx use project-v3-builder
- docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross
ifdef PUSHLATEST
- docker buildx build --push --platform=$(PLATFORMS) --tag $(REPOSITORY):$(VERSION) --tag $(REPOSITORY):latest -f Dockerfile.cross .
else
- docker buildx build --push --platform=$(PLATFORMS) --tag $(REPOSITORY):$(VERSION) -f Dockerfile.cross .
endif
- docker buildx rm project-v3-builder
rm Dockerfile.cross

Expand Down

0 comments on commit 0a8ce81

Please sign in to comment.