Skip to content

Commit

Permalink
Revive '--get-prebuilt-image' for 'mok build image'
Browse files Browse the repository at this point in the history
  • Loading branch information
mclarkson committed Nov 22, 2024
1 parent 9d5377a commit 6d1cea9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ sudo make install

For linux users: `alias mok="sudo /usr/local/bin/mok"`

Get the latest prebuilt image, which is about 1.2 GiB in size:

```bash
# Takes around 10 minutes
mok build image
mok build image --get-prebuilt-image
```

### Create a multi node kuberenetes cluster
Expand Down
34 changes: 17 additions & 17 deletions src/buildimage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ BI_baseimagename() {
# Args: None expected.
BI_usage() {

# NOTE: This flag to be added back in the future
# --get-prebuilt-image - Instead of building a 'node' image
# locally, download it from a container registry instead.

cat <<'EnD'
BUILD subcommands are:
Expand All @@ -45,6 +41,8 @@ build image options:
build image
Flags:
--get-prebuilt-image - Instead of building a 'node' image
locally, download it from a container registry instead.
--tailf - Show the build output whilst building.
EnD
Expand Down Expand Up @@ -79,11 +77,10 @@ BI_process_options() {
_BI[tailf]="${TRUE}"
return "${OK}"
;;
# NOTE: This flag to be added back in the future
# --get-prebuilt-image)
# _BI[useprebuiltimage]="${TRUE}"
# return "${OK}"
# ;;
--get-prebuilt-image)
_BI[useprebuiltimage]="${TRUE}"
return "${OK}"
;;
*)
BI_usage
printf 'ERROR: "%s" is not a valid "build" option.\n' "${1}" \
Expand Down Expand Up @@ -161,31 +158,32 @@ _BI_sanity_checks() {
# Args: No args expected.
_BI_build_container_image() {

local cmd retval tagname buildargs text buildtype
local cmd retval tagname buildargs text buildtype basename

_BI_create_docker_build_dir || return

buildargs=$(_BI_get_build_args_for_latest) || return
tagname="${_BI[baseimagename]}-v${K8SVERSION}"
basename="${_BI[baseimagename]}"
tagname="${K8SVERSION}"

local imgprefix
imgprefix=$(CU_imgprefix) || err || return
if [[ ${_BI[useprebuiltimage]} == "${FALSE}" ]]; then
buildtype="create"
cmd="docker build \
-t "${imgprefix}local/${tagname}" \
-t "${imgprefix}local/${basename}:${tagname}" \
--force-rm \
${buildargs} \
${_BI[dockerbuildtmpdir]}/${_BI[baseimagename]}"
text="Creating"
else
buildtype="download"
cmd="docker pull myownkind/${tagname}"
cmd="docker pull docker.io/myownkind/${basename}:${tagname}"
text="Downloading"
fi

UT_run_with_progress \
" ${text} base image, '${tagname}'" "${cmd}"
" ${text} base image, '${basename}:${tagname}'" "${cmd}"

retval=$?
[[ ${retval} -ne ${OK} ]] && {
Expand Down Expand Up @@ -235,6 +233,7 @@ _BI_modify_container_image() {
return

# Wait for crio to become ready
local counter
printf '\n\n ** WAITING FOR CRIO TO BECOME READY **\n\n'
while ! docker exec mok-build-modify systemctl status crio; do
sleep 1
Expand All @@ -253,10 +252,11 @@ _BI_modify_container_image() {
docker stop -t 5 "mok-build-modify"

# Write image
local imgprefix tagname
local imgprefix tagname basename
imgprefix=$(CU_imgprefix) || err || return
tagname="${_BI[baseimagename]}-v${K8SVERSION}"
docker commit mok-build-modify "${imgprefix}local/${tagname}" || err || return
basename="${_BI[baseimagename]}"
tagname="${K8SVERSION}"
docker commit mok-build-modify "${imgprefix}local/${basename}:${tagname}" || err || return

# Delete container, just in case it was left behind
docker stop -t 5 mok-build-modify
Expand Down
13 changes: 7 additions & 6 deletions src/containerutils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,20 @@ CU_create_container() {

img=$(BI_baseimagename) || err || return

local imglocal="${_CU[imgprefix]}local/${img}-v${3}"
local imgremote="myownkind/${img}-v${3}"
local imglocal="${_CU[imgprefix]}local/${img}"
local imgremote="myownkind/${img}"
local imgtag="${3}"

# Prefer a locally built container over one downloaded from a registry
allimgs=$(docker images | tail -n +2) || {
printf 'ERROR: %s returned an error\n' "${_CU[containerrt]}" >"${STDERR}"
err || return
}

if echo "${allimgs}" | grep -qs "${imglocal}"; then
imagename="${imglocal}"
elif echo "${allimgs}" | grep -qs "${imgremote}"; then
imagename="${imgremote}"
if echo "${allimgs}" | grep -qs "${imglocal} *${imgtag}"; then
imagename="${imglocal}:${imgtag}"
elif echo "${allimgs}" | grep -qs "${imgremote} *${imgtag}"; then
imagename="${imgremote}:${imgtag}"
else
cat <<EnD
ERROR: No container base image found. Use either:
Expand Down

0 comments on commit 6d1cea9

Please sign in to comment.