Skip to content

Commit

Permalink
Optionally avoid sleeping in entrypoint.sh
Browse files Browse the repository at this point in the history
Container image might be used as `initcontainers` in kubernetes Pods. In
such scenarios, it's important for the image entrypoint to exits once the
copy logic is completed. Otherwise, the pod would stuck in a `PodInitializing`
phase.

Add Makefile rule `image-test` to check the integrity of the entrypoint.sh script.

https://kubernetes.io/docs/concepts/workloads/pods/init-containers/
Signed-off-by: Andrea Panattoni <[email protected]>
  • Loading branch information
zeeke committed Feb 6, 2024
1 parent 5675950 commit f01ecd8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ COMMIT?=`git rev-parse --verify HEAD`
LDFLAGS="-X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.date=$(DATE)"

# Docker
IMAGE_BUILDER?=@docker
IMAGE_BUILDER?=docker
IMAGEDIR=$(BASE)/images
DOCKERFILE?=$(CURDIR)/Dockerfile
TAG?=k8snetworkplumbingwg/ib-sriov-cni
Expand Down Expand Up @@ -126,13 +126,16 @@ shellcheck: $(BASE) $(SHELLCHECK_TOOL); $(info running shellcheck...) @ ## Run
# Container image
.PHONY: image
image: | $(BASE) ; $(info Building Docker image...) ## Build conatiner image
$(IMAGE_BUILDER) build -t $(TAG) -f $(DOCKERFILE) $(CURDIR) $(IMAGE_BUILD_OPTS)
@$(IMAGE_BUILDER) build -t $(TAG) -f $(DOCKERFILE) $(CURDIR) $(IMAGE_BUILD_OPTS)

# Dependency management
.PHONY: deps-update
deps-update: ; $(info updating dependencies...)
go mod tidy && go mod vendor

test-image: image
$Q $(BASE)/images/image_test.sh $(IMAGE_BUILDER) $(TAG)

# Misc

.PHONY: clean
Expand Down
9 changes: 9 additions & 0 deletions images/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set -e
# Set known directories.
CNI_BIN_DIR="/host/opt/cni/bin"
IB_SRIOV_CNI_BIN_FILE="/usr/bin/ib-sriov"
NO_SLEEP=0

# Give help text for parameters.
usage()
Expand All @@ -18,6 +19,7 @@ usage()
printf "\t-h --help\n"
printf "\t--cni-bin-dir=%s\n" "$CNI_BIN_DIR"
printf "\t--ib-sriov-cni-bin-file=%s\n" "$IB_SRIOV_CNI_BIN_FILE"
printf "\t--no-sleep\n"
}

# Parse parameters given as arguments to this script.
Expand All @@ -35,6 +37,9 @@ while [ "$1" != "" ]; do
--ib-sriov-cni-bin-file)
IB_SRIOV_CNI_BIN_FILE=$VALUE
;;
--no-sleep)
AVOID_SLEEP=1
;;
*)
/bin/echo "ERROR: unknown parameter \"$PARAM\""
usage
Expand All @@ -57,6 +62,10 @@ done
# Copy file into proper place.
cp -f "$IB_SRIOV_CNI_BIN_FILE" "$CNI_BIN_DIR"

if [ $AVOID_SLEEP -eq 1 ]; then
exit 0
fi

echo "Entering sleep... (success)"
trap : TERM INT

Expand Down
22 changes: 22 additions & 0 deletions images/image_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh

set -x

OCI_RUNTIME=$1
IMAGE_UNDER_TEST=$2

OUTPUT_DIR=$(mktemp -d)

"${OCI_RUNTIME}" run -v "${OUTPUT_DIR}:/out" "${IMAGE_UNDER_TEST}" --cni-bin-dir=/out --no-sleep

if [ ! -e "${OUTPUT_DIR}/ib-sriov" ]; then
echo "Output file ${OUTPUT_DIR}/ib-sriov not found"
exit 1
fi

if [ ! -s "${OUTPUT_DIR}/ib-sriov" ]; then
echo "Output file ${OUTPUT_DIR}/ib-sriov is empty"
exit 1
fi

exit 0

0 comments on commit f01ecd8

Please sign in to comment.