Skip to content

Commit

Permalink
Merge pull request juju#16380 from hpidcock/podman-building
Browse files Browse the repository at this point in the history
juju#16380

Allow building with podman using `make OCI_BUILDER=podman ...`

## QA steps

- `make microk8s-operator-update` should use docker
- `make OCI_BUILDER=podman microk8s-operator-update` should use podman

## Documentation changes

N/A

## Links

N/A
  • Loading branch information
jujubot authored Oct 6, 2023
2 parents ad91d61 + cd135a8 commit 3ffe3f7
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 52 deletions.
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ check-deps:


# CAAS related targets
export OCI_BUILDER ?= docker
DOCKER_USERNAME ?= jujusolutions
DOCKER_BUILDX_CONTEXT ?= juju-make
DOCKER_STAGING_DIR ?= ${BUILD_DIR}/docker-staging
Expand Down Expand Up @@ -487,7 +488,9 @@ image-check-build-skip:
docker-builder:
## docker-builder: Makes sure that there is a buildx context for building the
## oci images
ifeq ($(OCI_BUILDER),docker)
-@docker buildx create --name ${DOCKER_BUILDX_CONTEXT}
endif

.PHONY: image-check
operator-image: image-check docker-builder
Expand Down Expand Up @@ -526,7 +529,7 @@ host-install:
.PHONY: minikube-operator-update
minikube-operator-update: host-install operator-image
## minikube-operator-update: Push up the newly built operator image for use with minikube
docker save "$(shell ${OPERATOR_IMAGE_PATH})" | minikube image load --overwrite=true -
$(OCI_BUILDER) save "$(shell ${OPERATOR_IMAGE_PATH})" | minikube image load --overwrite=true -

.PHONY: microk8s-operator-update
microk8s-operator-update: host-install operator-image
Expand All @@ -536,7 +539,7 @@ microk8s-operator-update: host-install operator-image
.PHONY: k3s-operator-update
k3s-operator-update: host-install operator-image
## k3s-operator-update: Push up the newly built operator image for use with k3s
docker save "$(shell ${OPERATOR_IMAGE_PATH})" | sudo k3s ctr images import -
$(OCI_BUILDER) save "$(shell ${OPERATOR_IMAGE_PATH})" | sudo k3s ctr images import -

.PHONY: check-k8s-model
check-k8s-model:
Expand All @@ -548,7 +551,7 @@ check-k8s-model:
local-operator-update: check-k8s-model operator-image
## local-operator-update: Build then update local operator image
$(eval kubeworkers != juju status -m ${JUJU_K8S_MODEL} kubernetes-worker --format json | jq -c '.machines | keys' | tr -c '[:digit:]' ' ' 2>&1)
docker save "$(shell ${OPERATOR_IMAGE_PATH})" | gzip > ${DOCKER_STAGING_DIR}/jujud-operator-image.tar.gz
$(OCI_BUILDER) save "$(shell ${OPERATOR_IMAGE_PATH})" | gzip > ${DOCKER_STAGING_DIR}/jujud-operator-image.tar.gz
$(foreach wm,$(kubeworkers), juju scp -m ${JUJU_K8S_MODEL} ${DOCKER_STAGING_DIR}/jujud-operator-image.tar.gz $(wm):/tmp/jujud-operator-image.tar.gz ; )
$(foreach wm,$(kubeworkers), juju ssh -m ${JUJU_K8S_MODEL} $(wm) -- "zcat /tmp/jujud-operator-image.tar.gz | docker load" ; )

Expand Down
1 change: 0 additions & 1 deletion caas/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
FROM public.ecr.aws/ubuntu/ubuntu:20.04
ARG TARGETOS
ARG TARGETARCH
ARG BUILDOS

# Add the syslog user for audit logging.
RUN useradd --system -M syslog
Expand Down
112 changes: 64 additions & 48 deletions make_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@ JUJUD_BIN_DIR=${JUJUD_BIN_DIR:-${BUILD_DIR}/bin}
JUJU_BUILD_NUMBER=${JUJU_BUILD_NUMBER:-}

# Docker variables
OCI_BUILDER=${OCI_BUILDER:-docker}
DOCKER_USERNAME=${DOCKER_USERNAME:-jujusolutions}
DOCKER_BUILDX_CONTEXT=${DOCKER_BUILDX_CONTEXT:-juju-make}
DOCKER_STAGING_DIR="${BUILD_DIR}/docker-staging"
DOCKER_BIN=${DOCKER_BIN:-$(which docker || true)}
DOCKER_BIN=${DOCKER_BIN:-$(which ${OCI_BUILDER} || true)}

readonly docker_staging_dir="docker-staging"

# _make_docker_staging_dir is responsible for ensuring that there exists a
# Docker staging directory under the build path. The staging directory's path
# is returned as the output of this function.
_make_docker_staging_dir() {
dir="${BUILD_DIR}/${docker_staging_dir}"
rm -rf "$dir"
mkdir -p "$dir"
echo "$dir"
dir="${BUILD_DIR}/${docker_staging_dir}"
rm -rf "$dir"
mkdir -p "$dir"
echo "$dir"
}

_juju_version() {
Expand All @@ -39,23 +40,23 @@ _image_version() {
}

microk8s_operator_update() {
echo "Uploading image $(operator_image_path) to microk8s"
# For macos we have to push the image into the microk8s multipass vm because
# we can't use the ctr to stream off the local machine.
if [[ $(uname) = "Darwin" ]]; then
tmp_docker_image="/tmp/juju-operator-image-${RANDOM}.image"
docker save $(operator_image_path) | multipass transfer - microk8s-vm:${tmp_docker_image}
microk8s ctr --namespace k8s.io image import ${tmp_docker_image}
multipass exec microk8s-vm rm "${tmp_docker_image}"
return
fi

# Linux we can stream the file like normal.
docker save "$(operator_image_path)" | microk8s.ctr --namespace k8s.io image import -
echo "Uploading image $(operator_image_path) to microk8s"
# For macos we have to push the image into the microk8s multipass vm because
# we can't use the ctr to stream off the local machine.
if [[ $(uname) = "Darwin" ]]; then
tmp_docker_image="/tmp/juju-operator-image-${RANDOM}.image"
"${DOCKER_BIN}" save $(operator_image_path) | multipass transfer - microk8s-vm:${tmp_docker_image}
microk8s ctr --namespace k8s.io image import ${tmp_docker_image}
multipass exec microk8s-vm rm "${tmp_docker_image}"
return
fi

# Linux we can stream the file like normal.
"${DOCKER_BIN}" save "$(operator_image_path)" | microk8s.ctr --namespace k8s.io image import -
}

juju_version() {
(cd "${PROJECT_DIR}" && go run version/helper/main.go)
(cd "${PROJECT_DIR}" && go run version/helper/main.go)
}

operator_image_release_path() {
Expand Down Expand Up @@ -85,52 +86,67 @@ operator_image_path() {
build_push_operator_image() {
build_multi_osarch=${1-""}
if [[ -z "$build_multi_osarch" ]]; then
build_multi_osarch="$(go env GOOS)/$(go env GOARCH)"
build_multi_osarch="$(go env GOOS)/$(go env GOARCH)"
fi

# We need to find any ppc64el references and move the build artefacts over
# to ppc64le so that it works with Docker.
for platform in $build_multi_osarch; do
if [[ "$platform" = *"ppc64el"* ]]; then
echo "detected operator image build for ppc64el \"${platform}\""
new_platform=$(echo "$platform" | sed 's/ppc64el/ppc64le/g')
echo "changing platform \"${platform}\" to platform \"${new_platform}\""

platform_dir="${BUILD_DIR}/$(echo "$platform" | sed 's/\//_/g')"
new_platform_dir="${BUILD_DIR}/$(echo "$new_platform" | sed 's/\//_/g')"
if ! [[ -d "$platform_dir" ]]; then
echo "platform build directory \"${platform_dir}\" does not exist"
exit 1
if [[ "$platform" = *"ppc64el"* ]]; then
echo "detected operator image build for ppc64el \"${platform}\""
new_platform=$(echo "$platform" | sed 's/ppc64el/ppc64le/g')
echo "changing platform \"${platform}\" to platform \"${new_platform}\""

platform_dir="${BUILD_DIR}/$(echo "$platform" | sed 's/\//_/g')"
new_platform_dir="${BUILD_DIR}/$(echo "$new_platform" | sed 's/\//_/g')"
if ! [[ -d "$platform_dir" ]]; then
echo "platform build directory \"${platform_dir}\" does not exist"
exit 1
fi

echo "copying platform build directory \"${platform_dir}\" to \"${new_platform_dir}\""
cp -r "$platform_dir" "$new_platform_dir"
fi

echo "copying platform build directory \"${platform_dir}\" to \"${new_platform_dir}\""
cp -r "$platform_dir" "$new_platform_dir"
fi
done
build_multi_osarch=$(echo "$build_multi_osarch" | sed 's/ppc64el/ppc64le/g')

push_image=${2:-"false"}

output="-o type=oci,dest=${BUILD_DIR}/oci.tar.gz"
if [[ "$push_image" = true ]]; then
output="-o type=image,push=true"
elif [[ $(echo "$build_multi_osarch" | wc -w) -eq 1 ]]; then
output="-o type=docker"
fi

build_multi_osarch=$(echo $build_multi_osarch | sed 's/ /,/g')

WORKDIR=$(_make_docker_staging_dir)
cp "${PROJECT_DIR}/caas/Dockerfile" "${WORKDIR}/"
cp "${PROJECT_DIR}/caas/requirements.txt" "${WORKDIR}/"
BUILDX_NO_DEFAULT_ATTESTATIONS=true DOCKER_BUILDKIT=1 "$DOCKER_BIN" buildx build \
--builder "$DOCKER_BUILDX_CONTEXT" \
-f "${WORKDIR}/Dockerfile" \
-t "$(operator_image_path)" \
--platform="$build_multi_osarch" \
--provenance=false \
${output} \
"${BUILD_DIR}"
if [[ "${OCI_BUILDER}" = "docker" ]]; then
output="-o type=oci,dest=${BUILD_DIR}/oci.tar.gz"
if [[ "$push_image" = true ]]; then
output="-o type=image,push=true"
elif [[ $(echo "$build_multi_osarch" | wc -w) -eq 1 ]]; then
output="-o type=docker"
fi
BUILDX_NO_DEFAULT_ATTESTATIONS=true DOCKER_BUILDKIT=1 "$DOCKER_BIN" buildx build \
--builder "$DOCKER_BUILDX_CONTEXT" \
-f "${WORKDIR}/Dockerfile" \
-t "$(operator_image_path)" \
--platform="$build_multi_osarch" \
--provenance=false \
${output} \
"${BUILD_DIR}"
elif [[ "${OCI_BUILDER}" = "podman" ]]; then
"$DOCKER_BIN" build \
--jobs "4" \
-f "${WORKDIR}/Dockerfile" \
-t "$(operator_image_path)" \
--platform="$build_multi_osarch" \
"${BUILD_DIR}"
if [[ "$push_image" = true ]]; then
"$DOCKER_BIN" push "$(operator_image_path)"
fi
else
echo "unknown OCI_BUILDER=${OCI_BUILDER} expected docker or podman"
exit 1
fi
}

wait_for_dpkg() {
Expand Down

0 comments on commit 3ffe3f7

Please sign in to comment.