Skip to content

Commit 00e3539

Browse files
committed
utils/build_ds_container.sh: Enable in-cluster builds
This enables a mode of building the content in-cluster, as opposed to having folks build the content locally and then pushing it. To enable this mode, the `-c` flag was introduced.
1 parent fa0890b commit 00e3539

File tree

3 files changed

+86
-23
lines changed

3 files changed

+86
-23
lines changed

ocp-resources/ds-build-remote.yaml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
kind: ImageStream
2+
apiVersion: image.openshift.io/v1
3+
metadata:
4+
name: "openscap-ocp4-ds"
5+
spec:
6+
lookupPolicy:
7+
local: true
8+
---
9+
kind: BuildConfig
10+
apiVersion: build.openshift.io/v1
11+
metadata:
12+
name: "openscap-ocp4-ds"
13+
spec:
14+
runPolicy: "Serial"
15+
triggers:
16+
-
17+
type: "ImageChange"
18+
source:
19+
dockerfile: |
20+
FROM registry.fedoraproject.org/fedora-minimal:33 as builder
21+
22+
WORKDIR /content
23+
24+
COPY . .
25+
26+
RUN microdnf -y install cmake make git /usr/bin/python3 python3-pyyaml python3-jinja2 openscap-utils
27+
28+
RUN ./build_product --datastream-only --debug ocp4 rhcos4
29+
30+
FROM registry.access.redhat.com/ubi8/ubi-minimal
31+
WORKDIR /
32+
COPY --from=builder /content/build/ssg-ocp4-ds.xml .
33+
COPY --from=builder /content/build/ssg-rhcos4-ds.xml .
34+
strategy:
35+
dockerStrategy:
36+
noCache: true
37+
output:
38+
to:
39+
kind: "ImageStreamTag"
40+
name: "openscap-ocp4-ds:latest"
File renamed without changes.

utils/build_ds_container.sh

+46-23
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ print_usage() {
1111
echo " $cmdname -h Display this help message."
1212
echo " $cmdname -n [namespace] Build image in the given namespace (Defaults to 'openshift-compliance')."
1313
echo " $cmdname -p Create ProfileBundle objects for the image."
14+
echo " $cmdname -c Build content in-cluster (NOTE: This ignores the products and debug flags)."
1415
echo " $cmdname -d Build content using the --debug flag."
1516
echo " $cmdname -P [product] (-P ...) Specify applicable product(s) to build. This option can be specified multiple times. (Defaults to 'ocp4' 'rhcos4')"
1617
exit 0
@@ -22,10 +23,11 @@ parms=(--datastream-only)
2223
# "openshift-compliance"
2324
namespace="openshift-compliance"
2425
create_profile_bundles="false"
26+
build_in_cluster="false"
2527
products=()
2628
default_products=(ocp4 rhcos4)
2729

28-
while getopts ":hdpn:P:" opt; do
30+
while getopts ":hdpcn:P:" opt; do
2931
case ${opt} in
3032
n ) # Set the namespace
3133
namespace=$OPTARG
@@ -36,6 +38,9 @@ while getopts ":hdpn:P:" opt; do
3638
p ) # Create ProfileBundle objects
3739
create_profile_bundles="true"
3840
;;
41+
c ) # Build content in-cluster
42+
build_in_cluster="true"
43+
;;
3944
h ) # Display help
4045
display_description
4146
print_usage
@@ -61,38 +66,51 @@ pushd $root_dir
6166

6267
echo "* Building $(echo ${products[@]} | sed 's/ /, /g') products"
6368

64-
# build the product's content
65-
"$root_dir/build_product" ${products[@]} "${params[@]}"
66-
result=$?
67-
68-
if [ "$result" != "0" ]; then
69-
echo "Error building content"
70-
exit $result
71-
fi
72-
7369
if [ "$namespace" == "openshift-compliance" ]; then
7470
# Ensure openshift-compliance namespace exists. If it already exists, this
7571
# is not a problem.
7672
oc apply -f "$root_dir/ocp-resources/compliance-operator-ns.yaml"
7773
fi
7874

79-
# Create buildconfig and ImageStream
80-
# This enables us to create a configuration so we can build a container
81-
# with the datastream
82-
# If they already exist, this is not a problem
83-
oc apply -n "$namespace" -f "$root_dir/ocp-resources/ds-build.yaml"
75+
if [ "$build_in_cluster" == "false" ];then
76+
# build the product's content
77+
"$root_dir/build_product" ${products[@]} "${params[@]}"
78+
result=$?
8479

85-
# Create output directory
86-
ds_dir=$(mktemp -d)
80+
if [ "$result" != "0" ]; then
81+
echo "Error building content"
82+
exit $result
83+
fi
8784

88-
# Copy datastream files to output directory
89-
cp "$root_dir/build/"*-ds.xml "$ds_dir"
85+
# Create buildconfig and ImageStream
86+
# This enables us to create a configuration so we can build a container
87+
# with the datastream
88+
# If they already exist, this is not a problem
89+
oc apply -n "$namespace" -f "$root_dir/ocp-resources/ds-from-local-build.yaml"
90+
91+
# Create output directory
92+
from_dir=$(mktemp -d)
93+
94+
# Copy datastream files to output directory
95+
cp "$root_dir/build/"*-ds.xml "$from_dir"
96+
else
97+
# Create buildconfig and ImageStream
98+
# This enables us to create a configuration so we can build a container
99+
# with the datastream
100+
# If they already exist, this is not a problem
101+
oc apply -n "$namespace" -f "$root_dir/ocp-resources/ds-build-remote.yaml"
102+
103+
# We'll copy the local contents for the build to happen remotely
104+
from_dir="."
105+
fi
90106

91107
# Start build
92-
oc start-build -n "$namespace" "openscap-ocp4-ds" --from-dir="$ds_dir"
108+
oc start-build -n "$namespace" "openscap-ocp4-ds" --from-dir="$from_dir"
93109

94-
# Clean output directory
95-
rm -rf "$ds_dir"
110+
if [ "$build_in_cluster" == "false" ];then
111+
# Clean output directory
112+
rm -rf "$from_dir"
113+
fi
96114

97115
# Wait some seconds until the object gets persisted
98116
sleep 5
@@ -126,6 +144,11 @@ while true; do
126144
echo "Check the logs"
127145
exit 1
128146
fi
129-
echo "Retrying... build status is still: $build_status"
147+
echo "Build status is still: $build_status"
148+
149+
# Follow logs to express actual output
150+
if [ "$build_in_cluster" == "true" ];then
151+
oc logs -f "openscap-ocp4-ds-$latest_build-build"
152+
fi
130153
done
131154

0 commit comments

Comments
 (0)