Skip to content

Commit

Permalink
Merge pull request #96 from gman0/multiarch-builds
Browse files Browse the repository at this point in the history
build: added multi-arch support
  • Loading branch information
gman0 authored Jun 21, 2023
2 parents 57a3aed + 86a74df commit 71c1f70
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 103 deletions.
25 changes: 15 additions & 10 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,40 +1,45 @@
include:
- project: 'ci-tools/container-image-ci-templates'
file: 'kaniko-image.gitlab-ci.yml'
file: 'docker-image.gitlab-ci.yml'
ref: master
- project: 'ci-tools/container-image-ci-templates'
file: 'helm.gitlab-ci.yml'
ref: master

stages:
- build-bin
- build-image
- build-chart
- build-bin
- build-image
- build-chart

build-bin:
stage: build-bin
rules:
- if: $CI_COMMIT_BRANCH || $CI_COMMIT_TAG
- if: $CI_COMMIT_BRANCH || $CI_COMMIT_TAG
image: registry.cern.ch/docker.io/library/golang:1.20
artifacts:
expire_in: '10 minutes'
expire_in: '10 min'
paths:
- bin/
- bin/
script:
- make
- make build-cross

build-image:
rules:
- if: $CI_COMMIT_TAG
variables:
PUSH_IMAGE: "true"
IMAGE_TAG: $CI_COMMIT_TAG
- if: $CI_COMMIT_BRANCH
variables:
IMAGE_TAG: $CI_COMMIT_BRANCH
stage: build-image
extends: .build_kaniko
extends: .build_docker
variables:
REGISTRY_IMAGE_PATH: "registry.cern.ch/magnum/cvmfs-csi:$CI_COMMIT_TAG"
REGISTRY_IMAGE_PATH: "registry.cern.ch/kubernetes/cvmfs-csi:$IMAGE_TAG"
CONTEXT_DIR: "."
DOCKER_FILE_NAME: "deployments/docker/Dockerfile"
PLATFORMS: "linux/amd64,linux/arm64"
BUILD_ARGS: "RELEASE=$IMAGE_TAG GITREF=$CI_COMMIT_SHA CREATED=$CI_PIPELINE_CREATED_AT"

build-chart:
rules:
Expand Down
119 changes: 34 additions & 85 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

BINDIR := $(CURDIR)/bin
DIST_DIRS := find * -type d -exec
TARGETS ?= darwin/amd64 linux/amd64 linux/386 linux/arm linux/arm64 linux/ppc64le windows/amd64
IMAGE_BUILD_TOOL ?= docker
TARGETS ?= linux/amd64 linux/arm64
IMAGE_BUILD_TOOL ?= podman
GOX_PARALLEL ?= 3

GOPATH = $(shell go env GOPATH)
GOX = $(GOPATH)/bin/gox
Expand Down Expand Up @@ -61,14 +62,26 @@ endif
LDFLAGS += -X ${BASE_PKG}/internal/version.commit=${GIT_COMMIT}
LDFLAGS += -X ${BASE_PKG}/internal/version.treestate=${GIT_DIRTY}

IMAGE_TAG := ${GIT_BRANCH}
ifneq ($(GIT_TAG),)
IMAGE_TAG = ${GIT_TAG}
endif

ARCH := $(shell uname -m)
ifeq ($(ARCH),x86_64)
LOCAL_TARGET=linux/amd64
else ifeq ($(ARCH),arm64)
LOCAL_TARGET=linux/arm64
endif

.PHONY: all
all: build

# ------------------------------------------------------------------------------
# build

.PHONY: build
build: $(BINDIR)/csi-cvmfsplugin $(BINDIR)/automount-runner $(BINDIR)/singlemount-runner
build: TARGETS = $(LOCAL_TARGET)
build: build-cross

$(BINDIR)/csi-cvmfsplugin: $(SRC)
go build $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)' -o $@ ./cmd/csi-cvmfsplugin
Expand All @@ -79,98 +92,34 @@ $(BINDIR)/automount-runner: $(SRC)
$(BINDIR)/singlemount-runner: $(SRC)
go build $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)' -o $@ ./cmd/singlemount-runner

.PHONY: build-cross
build-cross: LDFLAGS += -extldflags "-static"
build-cross: $(GOX) $(SRC)
CGO_ENABLED=0 $(GOX) -parallel=$(GOX_PARALLEL) -output="$(BINDIR)/{{.OS}}-{{.Arch}}/csi-cvmfsplugin" -osarch='$(TARGETS)' $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)' ./cmd/csi-cvmfsplugin
CGO_ENABLED=0 $(GOX) -parallel=$(GOX_PARALLEL) -output="$(BINDIR)/{{.OS}}-{{.Arch}}/automount-runner" -osarch='$(TARGETS)' $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)' ./cmd/automount-runner
CGO_ENABLED=0 $(GOX) -parallel=$(GOX_PARALLEL) -output="$(BINDIR)/{{.OS}}-{{.Arch}}/singlemount-runner" -osarch='$(TARGETS)' $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)' ./cmd/singlemount-runner

# ------------------------------------------------------------------------------
# test

.PHONY: test
test: build
test: TESTFLAGS += -race -v
test: test-style
test: test-unit

.PHONY: test-unit
test-unit:
@echo
@echo "==> Running unit tests <=="
go test $(GOFLAGS) -run $(TESTS) $(PKG) $(TESTFLAGS)

.PHONY: test-coverage
test-coverage:
@echo
@echo "==> Running unit tests with coverage <=="
@ ./scripts/coverage.sh

.PHONY: test-style
test-style:
golangci-lint run
@scripts/validate-license.sh

.PHONY: coverage
coverage:
@scripts/coverage.sh

.PHONY: format
format: $(GOIMPORTS)
go list -f '{{.Dir}}' ./... | xargs $(GOIMPORTS) -w
# image

image: build
$(IMAGE_BUILD_TOOL) build \
--build-arg RELEASE=$(IMAGE_TAG) \
--build-arg GITREF=$(GIT_COMMIT) \
--build-arg CREATED=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ") \
-t registry.cern.ch/kubernetes/cvmfs-csi:$(IMAGE_TAG) \
-f ./deployments/docker/Dockerfile .

# ------------------------------------------------------------------------------
# dependencies

$(GOX):
go install github.com/mitchellh/[email protected]

$(GOIMPORTS):
go install golang.org/x/tools/cmd/[email protected]

# ------------------------------------------------------------------------------
# release

.PHONY: build-cross
build-cross: LDFLAGS += -extldflags "-static"
build-cross: $(GOX)
CGO_ENABLED=0 $(GOX) -parallel=3 -output="_dist/{{.OS}}-{{.Arch}}/csi-cvmfsplugin_{{.OS}}_{{.Arch}}" -osarch='$(TARGETS)' $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)' ./cmd/csi-cvmfsplugin
CGO_ENABLED=0 $(GOX) -parallel=3 -output="_dist/{{.OS}}-{{.Arch}}/automount-runner_{{.OS}}_{{.Arch}}" -osarch='$(TARGETS)' $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)' ./cmd/automount-runner
CGO_ENABLED=0 $(GOX) -parallel=3 -output="_dist/{{.OS}}-{{.Arch}}/singlemount-runner_{{.OS}}_{{.Arch}}" -osarch='$(TARGETS)' $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)' ./cmd/singlemount-runner

.PHONY: dist
dist:
( \
cd _dist && \
$(DIST_DIRS) cp ../LICENSE {} \; && \
$(DIST_DIRS) cp ../README.md {} \; && \
$(DIST_DIRS) tar -zcf cvmfs-csi-${VERSION}-{}.tar.gz {} \; && \
$(DIST_DIRS) zip -r cvmfs-csi-${VERSION}-{}.zip {} \; \
)

.PHONY: checksum
checksum:
for f in _dist/*.{gz,zip} ; do \
shasum -a 256 "$${f}" | awk '{print $$1}' > "$${f}.sha256" ; \
done

.PHONY: changelog
changelog:
@./scripts/changelog.sh

# ------------------------------------------------------------------------------
# docker
DOCKER_TAG=${GIT_BRANCH}
ifneq ($(GIT_TAG),)
DOCKER_TAG = ${GIT_TAG}
endif

.PHONY: image
image: build-cross
mkdir -p bin
cp _dist/linux-amd64/csi-cvmfsplugin_linux_amd64 bin/csi-cvmfsplugin
cp _dist/linux-amd64/automount-runner_linux_amd64 bin/automount-runner
cp _dist/linux-amd64/singlemount-runner_linux_amd64 bin/singlemount-runner
sudo $(IMAGE_BUILD_TOOL) build -t cvmfs-csi:${DOCKER_TAG} -f deployments/docker/Dockerfile .

# ------------------------------------------------------------------------------
.PHONY: clean
clean:
@rm -rf $(BINDIR) ./_dist
@rm -rf $(BINDIR)

.PHONY: info
info:
Expand Down
39 changes: 31 additions & 8 deletions deployments/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
FROM registry.access.redhat.com/ubi9/ubi:latest
FROM registry.access.redhat.com/ubi9/ubi:9.2-489

LABEL description="CernVM-FS CSI Plugin"
RUN dnf install -y \
# selinux-policy is a dependency of the cvmfs package.
# When installing cvmfs on aarch64, an older version of selinux-policy
# package is selected for installation which then results in 404.
# As a temporary workaround we install it explicitly until this is fixed.
selinux-policy \
http://ecsft.cern.ch/dist/cvmfs/cvmfs-release/cvmfs-release-latest.noarch.rpm \
&& dnf --setopt=install_weak_deps=False install -y cvmfs \
&& dnf --enablerepo=* clean all && rm -rf /var/cache/yum /var/cache/dnf

RUN dnf install -y http://ecsft.cern.ch/dist/cvmfs/cvmfs-release/cvmfs-release-latest.noarch.rpm && \
dnf --setopt=install_weak_deps=False install -y cvmfs && dnf --enablerepo=* clean all && rm -rf /var/cache/yum /var/cache/dnf
ARG TARGETARCH
ARG RELEASE
ARG GITREF
ARG CREATED

COPY bin/csi-cvmfsplugin /csi-cvmfsplugin
COPY bin/automount-runner /automount-runner
COPY bin/singlemount-runner /singlemount-runner
LABEL org.opencontainers.image.title="cvmfs-csi" \
org.opencontainers.image.description="The CernVM-FS CSI image contains tools for exposing CVMFS repositories through Container Storage Interface." \
org.opencontainers.image.authors="CernVM-FS CSI authors" \
org.opencontainers.image.vendor="CernVM-FS CSI authors" \
org.opencontainers.image.licenses="Apache-2.0" \
org.opencontainers.image.documentation="https://github.com/cvmfs-contrib/cvmfs-csi" \
org.opencontainers.image.source="https://github.com/cvmfs-contrib/cvmfs-csi" \
org.opencontainers.image.url="https://github.com/cvmfs-contrib/cvmfs-csi" \
org.opencontainers.image.revision=${GITREF} \
org.opencontainers.image.version=${RELEASE} \
org.opencontainers.image.created=${CREATED} \
org.opencontainers.image.ref.name="" \
org.opencontainers.image.base.digest="" \
org.opencontainers.image.base.name=""

RUN chmod +x /csi-cvmfsplugin /automount-runner /singlemount-runner
COPY bin/linux-${TARGETARCH}/csi-cvmfsplugin /csi-cvmfsplugin
COPY bin/linux-${TARGETARCH}/automount-runner /automount-runner
COPY bin/linux-${TARGETARCH}/singlemount-runner /singlemount-runner

0 comments on commit 71c1f70

Please sign in to comment.