From 151463a6784b1192c02d37db60df345e06260ae3 Mon Sep 17 00:00:00 2001 From: Vitaliy Emporopulo Date: Wed, 14 Aug 2024 17:11:08 +0300 Subject: [PATCH] Add support for Podman Add podman as an option for building images and running on Kind --- .gitignore | 2 ++ Makefile | 22 ++++++++++++--------- README.md | 7 ++++++- demo/build-driver.sh | 2 +- demo/create-cluster.sh | 2 +- demo/scripts/build-driver-image.sh | 1 + demo/scripts/build-kind-image.sh | 9 +++++++-- demo/scripts/common.sh | 17 +++++++++++++++- demo/scripts/create-kind-cluster.sh | 2 +- demo/scripts/delete-kind-cluster.sh | 2 +- demo/scripts/load-driver-image-into-kind.sh | 8 ++++++-- deployments/container/Makefile | 4 ++-- 12 files changed, 57 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 20171eb0..a2fb73f8 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ vendor/ [._]*.sw[a-p] ./dra-example-kubeletplugin + +driver_image.tar diff --git a/Makefile b/Makefile index 66aad4d3..d3ae9ff5 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -DOCKER ?= docker +CONTAINER_TOOL ?= docker MKDIR ?= mkdir TR ?= tr DIST_DIR ?= $(CURDIR)/dist @@ -118,7 +118,7 @@ generate-deepcopy: vendor .PHONY: .build-image .build-image: docker/Dockerfile.devel if [ x"$(SKIP_IMAGE_BUILD)" = x"" ]; then \ - $(DOCKER) build \ + $(CONTAINER_TOOL) build \ --progress=plain \ --build-arg GOLANG_VERSION="$(GOLANG_VERSION)" \ --tag $(BUILDIMAGE) \ @@ -126,29 +126,33 @@ generate-deepcopy: vendor docker; \ fi +ifeq ($(CONTAINER_TOOL),podman) +CONTAINER_TOOL_OPTS=-v $(PWD):$(PWD):Z +else +CONTAINER_TOOL_OPTS=-v $(PWD):$(PWD) --user $$(id -u):$$(id -g) +endif + $(DOCKER_TARGETS): docker-%: .build-image - @echo "Running 'make $(*)' in docker container $(BUILDIMAGE)" - $(DOCKER) run \ + @echo "Running 'make $(*)' in container $(BUILDIMAGE)" + $(CONTAINER_TOOL) run \ --rm \ -e HOME=$(PWD) \ -e GOCACHE=$(PWD)/.cache/go \ -e GOPATH=$(PWD)/.cache/gopath \ - -v $(PWD):$(PWD) \ + $(CONTAINER_TOOL_OPTS) \ -w $(PWD) \ - --user $$(id -u):$$(id -g) \ $(BUILDIMAGE) \ make $(*) # Start an interactive shell using the development image. PHONY: .shell .shell: - $(DOCKER) run \ + $(CONTAINER_TOOL) run \ --rm \ -ti \ -e HOME=$(PWD) \ -e GOCACHE=$(PWD)/.cache/go \ -e GOPATH=$(PWD)/.cache/gopath \ - -v $(PWD):$(PWD) \ + $(CONTAINER_TOOL_OPTS) \ -w $(PWD) \ - --user $$(id -u):$$(id -g) \ $(BUILDIMAGE) diff --git a/README.md b/README.md index b623b25b..46cc3e73 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ The procedure below has been tested and verified on both Linux and Mac. * [GNU Make 3.81+](https://www.gnu.org/software/make/) * [GNU Tar 1.34+](https://www.gnu.org/software/tar/) -* [docker v20.10+ (including buildx)](https://docs.docker.com/engine/install/) +* [docker v20.10+ (including buildx)](https://docs.docker.com/engine/install/) or [Podman v4.9+](https://podman.io/docs/installation) * [kind v0.17.0+](https://kind.sigs.k8s.io/docs/user/quick-start/) * [helm v3.7.0+](https://helm.sh/docs/intro/install/) * [kubectl v1.18+](https://kubernetes.io/docs/reference/kubectl/) @@ -38,6 +38,11 @@ git clone https://github.com/kubernetes-sigs/dra-example-driver.git cd dra-example-driver ``` +**Note**: The scripts will automatically use either `docker`, or `podman` as the container tool command, whichever +can be found in the PATH. To override this behavior, set `CONTAINER_TOOL` environment variable either by calling +`export CONTAINER_TOOL=docker`, or by prepending `CONTAINER_TOOL=docker` to a script +(e.g. `CONTAINER_TOOL=docker ./path/to/script.sh`). Keep in mind that building Kind images currently requires Docker. + From here we will build the image for the example resource driver: ```bash ./demo/build-driver.sh diff --git a/demo/build-driver.sh b/demo/build-driver.sh index 55c28095..2cf19aea 100755 --- a/demo/build-driver.sh +++ b/demo/build-driver.sh @@ -31,7 +31,7 @@ source "${CURRENT_DIR}/scripts/common.sh" ${SCRIPTS_DIR}/build-driver-image.sh # If a cluster is already running, load the image onto its nodes -EXISTING_CLUSTER="$(kind get clusters | grep -w "${KIND_CLUSTER_NAME}" || true)" +EXISTING_CLUSTER="$(${KIND} get clusters | grep -w "${KIND_CLUSTER_NAME}" || true)" if [ "${EXISTING_CLUSTER}" != "" ]; then ${SCRIPTS_DIR}/load-driver-image-into-kind.sh fi diff --git a/demo/create-cluster.sh b/demo/create-cluster.sh index e4acd898..ef86a162 100755 --- a/demo/create-cluster.sh +++ b/demo/create-cluster.sh @@ -34,7 +34,7 @@ fi ${SCRIPTS_DIR}/create-kind-cluster.sh # If a driver image already exists load it into the cluster -EXISTING_IMAGE_ID="$(docker images --filter "reference=${DRIVER_IMAGE}" -q)" +EXISTING_IMAGE_ID="$(${CONTAINER_TOOL} images --filter "reference=${DRIVER_IMAGE}" -q)" if [ "${EXISTING_IMAGE_ID}" != "" ]; then ${SCRIPTS_DIR}/load-driver-image-into-kind.sh fi diff --git a/demo/scripts/build-driver-image.sh b/demo/scripts/build-driver-image.sh index 4e6ec9f5..a3ac1c2d 100755 --- a/demo/scripts/build-driver-image.sh +++ b/demo/scripts/build-driver-image.sh @@ -41,6 +41,7 @@ cd ${CURRENT_DIR}/../.. export REGISTRY="${DRIVER_IMAGE_REGISTRY}" export IMAGE="${DRIVER_IMAGE_NAME}" export VERSION="${DRIVER_IMAGE_TAG}" +export CONTAINER_TOOL="${CONTAINER_TOOL}" # Regenerate the CRDs and build the container image make docker-generate diff --git a/demo/scripts/build-kind-image.sh b/demo/scripts/build-kind-image.sh index 1f4a50ef..d844865f 100755 --- a/demo/scripts/build-kind-image.sh +++ b/demo/scripts/build-kind-image.sh @@ -28,11 +28,16 @@ set -o pipefail source "${CURRENT_DIR}/common.sh" # If an image ID already exists for the image we plan to build, we are done. -EXISTING_IMAGE_ID="$(docker images --filter "reference=${KIND_IMAGE}" -q)" +EXISTING_IMAGE_ID="$(${CONTAINER_TOOL} images --filter "reference=${KIND_IMAGE}" -q)" if [ "${EXISTING_IMAGE_ID}" != "" ]; then exit 0 fi +if [[ "${CONTAINER_TOOL}" != "docker" ]]; then + echo "Building kind images requires Docker. Cannot use '${CONTAINER_TOOL}'" + exit 1 +fi + # Create a temorary directory to hold all the artifacts we need for building the image TMP_DIR="$(mktemp -d)" cleanup() { @@ -47,4 +52,4 @@ KIND_K8S_DIR="${TMP_DIR}/kubernetes-${KIND_K8S_TAG}" git clone --depth 1 --branch ${KIND_K8S_TAG} ${KIND_K8S_REPO} ${KIND_K8S_DIR} # Build the kind base image -kind build node-image --image "${KIND_IMAGE}" "${KIND_K8S_DIR}" +${KIND} build node-image --image "${KIND_IMAGE}" "${KIND_K8S_DIR}" diff --git a/demo/scripts/common.sh b/demo/scripts/common.sh index 6152e870..b1b97597 100644 --- a/demo/scripts/common.sh +++ b/demo/scripts/common.sh @@ -22,7 +22,7 @@ # A reference to the current directory where this script is located SCRIPTS_DIR="$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd)" -# The name of the example driver +# The name of the example driver : ${DRIVER_NAME:=dra-example-driver} # The registry, image and tag for the example driver @@ -55,3 +55,18 @@ SCRIPTS_DIR="$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd)" # The name of the kind image to build / run : ${KIND_IMAGE:="kindest/node:${KIND_K8S_TAG}"} +# Container tool, e.g. docker/podman +if [[ -z "${CONTAINER_TOOL}" ]]; then + if [[ -n "$(which docker)" ]]; then + echo "Docker found in PATH." + CONTAINER_TOOL=docker + elif [[ -n "$(which podman)" ]]; then + echo "Podman found in PATH." + CONTAINER_TOOL=podman + else + echo "No container tool detected. Please install Docker or Podman." + return 1 + fi +fi + +: ${KIND:="env KIND_EXPERIMENTAL_PROVIDER=${CONTAINER_TOOL} kind"} diff --git a/demo/scripts/create-kind-cluster.sh b/demo/scripts/create-kind-cluster.sh index 19a0ce50..a948f188 100755 --- a/demo/scripts/create-kind-cluster.sh +++ b/demo/scripts/create-kind-cluster.sh @@ -27,7 +27,7 @@ set -o pipefail source "${CURRENT_DIR}/common.sh" -kind create cluster \ +${KIND} create cluster \ --name "${KIND_CLUSTER_NAME}" \ --image "${KIND_IMAGE}" \ --config "${KIND_CLUSTER_CONFIG_PATH}" diff --git a/demo/scripts/delete-kind-cluster.sh b/demo/scripts/delete-kind-cluster.sh index 872a6ff8..fea7c388 100755 --- a/demo/scripts/delete-kind-cluster.sh +++ b/demo/scripts/delete-kind-cluster.sh @@ -27,5 +27,5 @@ set -o pipefail source "${CURRENT_DIR}/common.sh" -kind delete cluster \ +${KIND} delete cluster \ --name "${KIND_CLUSTER_NAME}" diff --git a/demo/scripts/load-driver-image-into-kind.sh b/demo/scripts/load-driver-image-into-kind.sh index e7abb101..ca01790c 100755 --- a/demo/scripts/load-driver-image-into-kind.sh +++ b/demo/scripts/load-driver-image-into-kind.sh @@ -27,6 +27,10 @@ set -o pipefail source "${CURRENT_DIR}/common.sh" -kind load docker-image \ +# Work around kind not loading image with podman +IMAGE_ARCHIVE=driver_image.tar +${CONTAINER_TOOL} save -o "${IMAGE_ARCHIVE}" "${DRIVER_IMAGE}" && \ +${KIND} load image-archive \ --name "${KIND_CLUSTER_NAME}" \ - "${DRIVER_IMAGE}" + "${IMAGE_ARCHIVE}" +rm "${IMAGE_ARCHIVE}" diff --git a/deployments/container/Makefile b/deployments/container/Makefile index caaf707a..52229798 100644 --- a/deployments/container/Makefile +++ b/deployments/container/Makefile @@ -13,7 +13,7 @@ # limitations under the License. ##### Global variables ##### -DOCKER ?= docker +CONTAINER_TOOL ?= docker MKDIR ?= mkdir DISTRIBUTIONS := ubuntu22.04 DOCKERFILE := $(CURDIR)/deployments/container/Dockerfile @@ -31,7 +31,7 @@ ubuntu22.04: BASE_IMAGE = docker.io/ubuntu:22.04 # Use a generic build target to build the relevant images $(DISTRIBUTIONS): DOCKER_BUILDKIT=1 \ - $(DOCKER) build --pull \ + $(CONTAINER_TOOL) build --pull \ --tag $(IMAGE) \ --build-arg GOLANG_VERSION="$(GOLANG_VERSION)" \ --build-arg BASE_IMAGE="$(BASE_IMAGE)" \