Skip to content

Commit

Permalink
build: add builder args to support build with nerdctl
Browse files Browse the repository at this point in the history
Signed-off-by: Wei Zhang <kweizh@gmail.com>
Signed-off-by: WeiZhang <kweizh@gmail.com>
  • Loading branch information
zwpaper committed Feb 16, 2023
1 parent 50ad3c7 commit d0e20e1
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 19 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ jobs:
sudo systemctl daemon-reload
sudo systemctl enable --now containerd
containerd-rootless-setuptool.sh install
containerd-rootless-setuptool.sh install-buildkit-containerd
- name: Make pki directory
if: ${{ matrix.kwokctl-runtime == 'binary' }}
Expand Down
43 changes: 36 additions & 7 deletions images/cluster/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ PLATFORMS=()
VERSION=""
STAGING_PREFIX=""
KUBE_VERSIONS=()
BUILDER="docker"

function usage() {
echo "Usage: ${0} [--help] [--version <version>] [--kube-version <kube-version> ...] [--image <image> ...] [--extra-tag <extra-tag> ...] [--staging-prefix <staging-prefix>] [--platform <platform> ...] [--push] [--dry-run]"
Expand All @@ -37,6 +38,7 @@ function usage() {
echo " --platform <platform> is multi-platform capable for image"
echo " --push will push image to registry"
echo " --dry-run just show what would be done"
echo " --builder <builder> specify image builder, default: docker. available options: docker, nerdctl"
}

function args() {
Expand Down Expand Up @@ -76,6 +78,10 @@ function args() {
[[ "${arg#*=}" != "${arg}" ]] && DRY_RUN="${arg#*=}" || DRY_RUN="true"
shift
;;
--builder | --builder=*)
[[ "${arg#*=}" != "${arg}" ]] && BUILDER="${arg#*=}" || { BUILDER="${2}" && shift; }
shift
;;
--help)
usage
exit 0
Expand Down Expand Up @@ -158,18 +164,41 @@ function main() {
"--platform=${platform}"
)
done
if [[ "${PUSH}" == "true" ]]; then
extra_args+=("--push")

if [[ "${BUILDER}" == "nerdctl" ]]; then
build_with_nerdctl "${extra_args[@]}"
else
extra_args+=("--load")
build_with_docker "${extra_args[@]}"
fi
dry_run docker buildx build \
"${extra_args[@]}" \
-f "${DOCKERFILE}" \
.
done
}

function build_with_docker() {
local extra_args
extra_args=("$@")
if [[ "${PUSH}" == "true" ]]; then
extra_args+=("--push")
else
extra_args+=("--load")
fi
dry_run docker buildx build \
"${extra_args[@]}" \
-f "${DOCKERFILE}" \
.
}

function build_with_nerdctl() {
local extra_args
extra_args=("$@")
dry_run nerdctl build \
"${extra_args[@]}" \
-f "${DOCKERFILE}" \
.
if [[ "${PUSH}" == "true" ]]; then
nerdctl push --all-platforms ${image}:${tag}
fi
}

args "$@"

cd "${ROOT_DIR}" && main
31 changes: 30 additions & 1 deletion images/kwok/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ EXTRA_TAGS=()
PLATFORMS=()
VERSION=""
STAGING_PREFIX=""
BUILDER="docker"

function usage() {
echo "Usage: ${0} [--help] [--version <version>] [--image <image> ...] [--extra-tag <extra-tag> ...] [--staging-prefix <staging-prefix>] [--platform <platform> ...] [--push] [--dry-run]"
echo "Usage: ${0} [--help] [--version <version>] [--image <image> ...] [--extra-tag <extra-tag> ...] [--staging-prefix <staging-prefix>] [--platform <platform> ...] [--push] [--dry-run] [--builder <builder>]"
echo " --version <version> is kwok version, is required"
echo " --image <image> is image, is required"
echo " --extra-tag <extra-tag> is extra tag"
echo " --staging-prefix <staging-prefix> is staging prefix for tag"
echo " --platform <platform> is multi-platform capable for image"
echo " --push will push image to registry"
echo " --dry-run just show what would be done"
echo " --builder <builder> specify image builder, default: docker. available options: docker, nerdctl"
}

function args() {
Expand Down Expand Up @@ -70,6 +72,10 @@ function args() {
[[ "${arg#*=}" != "${arg}" ]] && DRY_RUN="${arg#*=}" || DRY_RUN="true"
shift
;;
--builder | --builder=*)
[[ "${arg#*=}" != "${arg}" ]] && BUILDER="${arg#*=}" || { BUILDER="${2}" && shift; }
shift
;;
--help)
usage
exit 0
Expand Down Expand Up @@ -139,6 +145,17 @@ function main() {
"--platform=${platform}"
)
done

if [[ "${BUILDER}" == "nerdctl" ]]; then
build_with_nerdctl "${extra_args[@]}"
else
build_with_docker "${extra_args[@]}"
fi
}

function build_with_docker() {
local extra_args
extra_args=("$@")
if [[ "${PUSH}" == "true" ]]; then
extra_args+=("--push")
else
Expand All @@ -150,6 +167,18 @@ function main() {
.
}

function build_with_nerdctl() {
local extra_args
extra_args=("$@")
dry_run nerdctl build \
"${extra_args[@]}" \
-f "${DOCKERFILE}" \
.
if [[ "${PUSH}" == "true" ]]; then
nerdctl push --all-platforms ${image}:${tag}
fi
}

args "$@"

cd "${ROOT_DIR}" && main
15 changes: 4 additions & 11 deletions test/kwokctl/helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,12 @@ function build_kwok() {
}

function build_image() {
if docker image inspect "${KWOK_CONTROLLER_IMAGE}" >/dev/null 2>&1; then
builder=${1:-"docker"}
if ${builder} image inspect "${KWOK_CONTROLLER_IMAGE}" >/dev/null 2>&1; then
return
fi
"${ROOT_DIR}/hack/releases.sh" --bin kwok --version "${KWOK_CONTROLLER_IMAGE##*:}" --platform "linux/${GOARCH}"
"${ROOT_DIR}/images/kwok/build.sh" --image "${KWOK_CONTROLLER_IMAGE%%:*}" --version "${VERSION}"
}

function build_image_for_nerdctl() {
build_image
mkdir "tmp"
docker save -o "tmp/kwok.tar" "${KWOK_CONTROLLER_IMAGE}"
nerdctl load -i "tmp/kwok.tar"
"${ROOT_DIR}/images/kwok/build.sh" --image "${KWOK_CONTROLLER_IMAGE%%:*}" --version "${VERSION}" --builder ${builder}
}

function requirements() {
Expand All @@ -87,9 +81,8 @@ function requirements() {

function requirements_for_nerdctl() {
install_kubectl
install_buildx
build_kwokctl
build_image_for_nerdctl
build_image nerdctl
}

function requirements_for_binary() {
Expand Down

0 comments on commit d0e20e1

Please sign in to comment.