Skip to content

Commit 250f921

Browse files
Merge pull request #14534 from stevekuznetsov/skuznets/cleanup-image-bash
Bring image building logic into the Bash library
2 parents 22f308e + 9753bf5 commit 250f921

File tree

5 files changed

+155
-120
lines changed

5 files changed

+155
-120
lines changed

hack/build-base-images.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ tag_prefix="${OS_IMAGE_PREFIX:-"openshift/origin"}"
1111
os::util::ensure::gopath_binary_exists imagebuilder
1212

1313
# Build the base image without the default image args
14-
OS_BUILD_IMAGE_ARGS="${OS_BUILD_IMAGE_BASE_ARGS-}" os::build::image "${OS_ROOT}/images/source" "${tag_prefix}-source"
15-
OS_BUILD_IMAGE_ARGS="${OS_BUILD_IMAGE_BASE_ARGS-}" os::build::image "${OS_ROOT}/images/base" "${tag_prefix}-base"
14+
os::build::image "${tag_prefix}-source" "${OS_ROOT}/images/source"
15+
os::build::image "${tag_prefix}-base" "${OS_ROOT}/images/base"
1616

1717
ret=$?; ENDTIME=$(date +%s); echo "$0 took $(($ENDTIME - $STARTTIME)) seconds"; exit "$ret"

hack/build-dind-images.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
STARTTIME=$(date +%s)
99
source "$(dirname "${BASH_SOURCE}")/lib/init.sh"
1010

11-
os::build::image "${OS_ROOT}/images/dind" openshift/dind
12-
os::build::image "${OS_ROOT}/images/dind/node" openshift/dind-node
13-
os::build::image "${OS_ROOT}/images/dind/master" openshift/dind-master
11+
os::build::image openshift/dind "${OS_ROOT}/images/dind"
12+
os::build::image openshift/dind-node "${OS_ROOT}/images/dind/node"
13+
os::build::image openshift/dind-master "${OS_ROOT}/images/dind/master"
1414

1515
ret=$?; ENDTIME=$(date +%s); echo "$0 took $(($ENDTIME - $STARTTIME)) seconds"; exit "$ret"

hack/build-images.sh

Lines changed: 26 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66
# NOTE: you only need to run this script if your code changes are part of
77
# any images OpenShift runs internally such as origin-sti-builder, origin-docker-builder,
88
# origin-deployer, etc.
9-
STARTTIME=$(date +%s)
109
source "$(dirname "${BASH_SOURCE}")/lib/init.sh"
1110

11+
function cleanup() {
12+
return_code=$?
13+
os::util::describe_return_code "${return_code}"
14+
exit "${return_code}"
15+
}
16+
trap "cleanup" EXIT
17+
1218
os::util::ensure::gopath_binary_exists imagebuilder
1319
# image builds require RPMs to have been built
1420
os::build::release::check_for_rpms
@@ -32,54 +38,6 @@ function ln_or_cp {
3238
fi
3339
}
3440

35-
36-
# image-build is wrapped to allow output to be captured
37-
function image-build() {
38-
local tag=$1
39-
local dir=$2
40-
local dest="${tag}"
41-
local extra=
42-
if [[ ! "${tag}" == *":"* ]]; then
43-
dest="${tag}:latest"
44-
# tag to release commit unless we specified a hardcoded tag
45-
extra="${tag}:${OS_RELEASE_COMMIT}"
46-
fi
47-
48-
local STARTTIME
49-
local ENDTIME
50-
STARTTIME="$(date +%s)"
51-
52-
# build the image
53-
if ! os::build::image "${dir}" "${dest}" "" "${extra}"; then
54-
os::log::warning "Retrying build once"
55-
if ! os::build::image "${dir}" "${dest}" "" "${extra}"; then
56-
return 1
57-
fi
58-
fi
59-
60-
# ensure the temporary contents are cleaned up
61-
git clean -fdx "${dir}"
62-
63-
ENDTIME="$(date +%s)"
64-
echo "Finished in $(($ENDTIME - $STARTTIME)) seconds"
65-
}
66-
67-
# builds an image and tags it two ways - with latest, and with the release tag
68-
function image() {
69-
local tag=$1
70-
local dir=$2
71-
local out
72-
mkdir -p "${BASETMPDIR}"
73-
out="$( mktemp "${BASETMPDIR}/imagelogs.XXXXX" )"
74-
if ! image-build "${tag}" "${dir}" > "${out}" 2>&1; then
75-
sed -e "s|^|$1: |" "${out}" 1>&2
76-
os::log::error "Failed to build $1"
77-
return 1
78-
fi
79-
sed -e "s|^|$1: |" "${out}"
80-
return 0
81-
}
82-
8341
# Link or copy image binaries to the appropriate locations.
8442
ln_or_cp "${OS_OUTPUT_BINPATH}/linux/amd64/hello-openshift" examples/hello-openshift/bin
8543
ln_or_cp "${OS_OUTPUT_BINPATH}/linux/amd64/gitserver" examples/gitserver/bin
@@ -88,32 +46,28 @@ ln_or_cp "${OS_OUTPUT_BINPATH}/linux/amd64/gitserver" examples/gitserver/b
8846
tag_prefix="${OS_IMAGE_PREFIX:-"openshift/origin"}"
8947

9048
# images that depend on "${tag_prefix}-source"
91-
image "${tag_prefix}-pod" images/pod
92-
image "${tag_prefix}-cluster-capacity" images/cluster-capacity
93-
image "${tag_prefix}-service-catalog" images/service-catalog
49+
os::build::image "${tag_prefix}-pod" images/pod
50+
os::build::image "${tag_prefix}-cluster-capacity" images/cluster-capacity
51+
os::build::image "${tag_prefix}-service-catalog" images/service-catalog
9452

9553
# images that depend on "${tag_prefix}-base"
96-
image "${tag_prefix}" images/origin
97-
image "${tag_prefix}-haproxy-router" images/router/haproxy
98-
image "${tag_prefix}-keepalived-ipfailover" images/ipfailover/keepalived
99-
image "${tag_prefix}-docker-registry" images/dockerregistry
100-
image "${tag_prefix}-egress-router" images/egress/router
101-
image "${tag_prefix}-egress-http-proxy" images/egress/http-proxy
102-
image "${tag_prefix}-federation" images/federation
54+
os::build::image "${tag_prefix}" images/origin
55+
os::build::image "${tag_prefix}-haproxy-router" images/router/haproxy
56+
os::build::image "${tag_prefix}-keepalived-ipfailover" images/ipfailover/keepalived
57+
os::build::image "${tag_prefix}-docker-registry" images/dockerregistry
58+
os::build::image "${tag_prefix}-egress-router" images/egress/router
59+
os::build::image "${tag_prefix}-egress-http-proxy" images/egress/http-proxy
60+
os::build::image "${tag_prefix}-federation" images/federation
10361
# images that depend on "${tag_prefix}
104-
image "${tag_prefix}-gitserver" examples/gitserver
105-
image "${tag_prefix}-deployer" images/deployer
106-
image "${tag_prefix}-recycler" images/recycler
107-
image "${tag_prefix}-docker-builder" images/builder/docker/docker-builder
108-
image "${tag_prefix}-sti-builder" images/builder/docker/sti-builder
109-
image "${tag_prefix}-f5-router" images/router/f5
110-
image "openshift/node" images/node
62+
os::build::image "${tag_prefix}-gitserver" examples/gitserver
63+
os::build::image "${tag_prefix}-deployer" images/deployer
64+
os::build::image "${tag_prefix}-recycler" images/recycler
65+
os::build::image "${tag_prefix}-docker-builder" images/builder/docker/docker-builder
66+
os::build::image "${tag_prefix}-sti-builder" images/builder/docker/sti-builder
67+
os::build::image "${tag_prefix}-f5-router" images/router/f5
68+
os::build::image "openshift/node" images/node
11169
# images that depend on "openshift/node"
112-
image "openshift/openvswitch" images/openvswitch
70+
os::build::image "openshift/openvswitch" images/openvswitch
11371

11472
# extra images (not part of infrastructure)
115-
image "openshift/hello-openshift" examples/hello-openshift
116-
117-
echo
118-
119-
ret=$?; ENDTIME=$(date +%s); echo "$0 took $(($ENDTIME - $STARTTIME)) seconds"; exit "$ret"
73+
os::build::image "openshift/hello-openshift" examples/hello-openshift

hack/dind-cluster.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ function build-image() {
526526
local build_root=$1
527527
local image_name=$2
528528

529-
os::build::image "${build_root}" "${image_name}"
529+
os::build::image "${image_name}" "${build_root}"
530530
}
531531

532532
function os::build::get-bin-output-path() {

hack/lib/build/images.sh

Lines changed: 123 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,128 @@
22

33
# This library holds utility functions for building container images.
44

5-
# os::build::image builds an image from a directory, to a tag, with an optional dockerfile to
6-
# use as the third argument. The environment variable OS_BUILD_IMAGE_ARGS adds additional
7-
# options to the command. The default is to use the imagebuilder binary if it is available
8-
# on the path with fallback to docker build if it is not available.
5+
# os::build::image builds an image from a directory, to a tag or tags The default
6+
# behavior is to use the imagebuilder binary if it is available on the path with
7+
# fallback to docker build if it is not available.
8+
#
9+
# Globals:
10+
# - OS_BUILD_IMAGE_ARGS
11+
# - OS_BUILD_IMAGE_NUM_RETRIES
12+
# Arguments:
13+
# - 1: the directory in which to build
14+
# - 2: the tag to apply to the image
15+
# Returns:
16+
# None
917
function os::build::image() {
10-
local directory=$1
11-
local tag=$2
12-
local dockerfile="${3-}"
13-
local extra_tag="${4-}"
14-
local options="${OS_BUILD_IMAGE_ARGS-}"
15-
local mode="${OS_BUILD_IMAGE_TYPE:-imagebuilder}"
16-
17-
if [[ "${mode}" == "imagebuilder" ]]; then
18-
if os::util::find::system_binary 'imagebuilder'; then
19-
if [[ -n "${extra_tag}" ]]; then
20-
extra_tag="-t '${extra_tag}'"
21-
fi
22-
if [[ -n "${dockerfile}" ]]; then
23-
eval "imagebuilder -f '${dockerfile}' -t '${tag}' ${extra_tag} ${options} '${directory}'"
24-
return $?
25-
fi
26-
eval "imagebuilder -t '${tag}' ${extra_tag} ${options} '${directory}'"
27-
return $?
28-
fi
29-
30-
os::log::warning "Unable to locate 'imagebuilder' on PATH, falling back to Docker build"
31-
# clear options since we were unable to select imagebuilder
32-
options=""
33-
fi
34-
35-
if [[ -n "${dockerfile}" ]]; then
36-
eval "docker build -f '${dockerfile}' -t '${tag}' ${options} '${directory}'"
37-
if [[ -n "${extra_tag}" ]]; then
38-
docker tag "${tag}" "${extra_tag}"
39-
fi
40-
return $?
41-
fi
42-
eval "docker build -t '${tag}' ${options} '${directory}'"
43-
if [[ -n "${extra_tag}" ]]; then
44-
docker tag "${tag}" "${extra_tag}"
45-
fi
46-
return $?
18+
local tag=$1
19+
local directory=$2
20+
local extra_tag
21+
22+
if [[ ! "${tag}" == *":"* ]]; then
23+
# if no tag was specified in the image name,
24+
# tag with :latest and the release commit, if
25+
# available, falling back to the last commit
26+
# if no release commit is recorded
27+
local release_commit
28+
release_commit="${OS_RELEASE_COMMIT:-"$( git log -1 --pretty=%h )"}"
29+
extra_tag="${tag}:${release_commit}"
30+
31+
tag="${tag}:latest"
32+
fi
33+
34+
local result='1'
35+
local image_build_log
36+
image_build_log="$( mktemp "${BASETMPDIR}/imagelogs.XXXXX" )"
37+
for (( i = 0; i < "${OS_BUILD_IMAGE_NUM_RETRIES:-2}"; i++ )); do
38+
if [[ "${i}" -gt 0 ]]; then
39+
os::log::warning "Retrying image build for ${tag}, attempt ${i}..."
40+
fi
41+
42+
if os::build::image::internal::generic "${tag}" "${directory}" "${extra_tag:-}" >>"${image_build_log}" 2>&1; then
43+
result='0'
44+
break
45+
fi
46+
done
47+
48+
os::log::internal::prefix_lines "[${tag%:*}]" "$( cat "${image_build_log}" )"
49+
return "${result}"
50+
}
51+
readonly -f os::build::image
52+
53+
# os::build::image::internal::generic builds a container image using either imagebuilder
54+
# or docker, defaulting to imagebuilder if present
55+
#
56+
# Globals:
57+
# - OS_BUILD_IMAGE_ARGS
58+
# Arguments:
59+
# - 1: the directory in which to build
60+
# - 2: the tag to apply to the image
61+
# - 3: optionally, extra tags to add
62+
# Returns:
63+
# None
64+
function os::build::image::internal::generic() {
65+
local directory=$2
66+
67+
if os::util::find::system_binary 'imagebuilder' >/dev/null; then
68+
os::build::image::internal::imagebuilder "$@"
69+
else
70+
os::log::warning "Unable to locate 'imagebuilder' on PATH, falling back to Docker build"
71+
os::build::image::internal::docker "$@"
72+
fi
73+
74+
# ensure the temporary contents are cleaned up
75+
git clean -fdx "${directory}"
76+
}
77+
readonly -f os::build::image::internal::generic
78+
79+
# os::build::image::internal::imagebuilder builds a container image using imagebuilder
80+
#
81+
# Globals:
82+
# - OS_BUILD_IMAGE_ARGS
83+
# Arguments:
84+
# - 1: the directory in which to build
85+
# - 2: the tag to apply to the image
86+
# - 3: optionally, extra tags to add
87+
# Returns:
88+
# None
89+
function os::build::image::internal::imagebuilder() {
90+
local tag=$1
91+
local directory=$2
92+
local extra_tag="${3-}"
93+
local options=()
94+
95+
if [[ -n "${OS_BUILD_IMAGE_ARGS:-}" ]]; then
96+
options=( ${OS_BUILD_IMAGE_ARGS} )
97+
fi
98+
99+
if [[ -n "${extra_tag}" ]]; then
100+
options+=( -t "${extra_tag}" )
101+
fi
102+
103+
imagebuilder "${options[@]:-}" -t "${tag}" "${directory}"
104+
}
105+
readonly -f os::build::image::internal::imagebuilder
106+
107+
# os::build::image::internal::docker builds a container image using docker
108+
#
109+
# Globals:
110+
# - OS_BUILD_IMAGE_ARGS
111+
# Arguments:
112+
# - 1: the directory in which to build
113+
# - 2: the tag to apply to the image
114+
# - 3: optionally, extra tags to add
115+
# Returns:
116+
# None
117+
function os::build::image::internal::docker() {
118+
local tag=$1
119+
local directory=$2
120+
local extra_tag="${3-}"
121+
local options=()
122+
123+
docker build ${OS_BUILD_IMAGE_ARGS:-} -t "${tag}" "${directory}"
124+
125+
if [[ -n "${extra_tag}" ]]; then
126+
docker tag "${tag}" "${extra_tag}"
127+
fi
47128
}
48-
readonly -f os::build::image
129+
readonly -f os::build::image::internal::docker

0 commit comments

Comments
 (0)