Skip to content

Commit 7baa796

Browse files
authored
Merge pull request #35783 from saschagrunert/simplify
CRI-O: Simplify config generation script
2 parents 3cf81e2 + c55fc4d commit 7baa796

File tree

1 file changed

+35
-36
lines changed

1 file changed

+35
-36
lines changed

jobs/e2e_node/crio/templates/generate

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
# limitations under the License.
1515

1616
set -euo pipefail
17-
IFS=$'\n\t'
1817

1918
cd "$(dirname "$0")"
2019

@@ -36,59 +35,59 @@ declare -A CONFIGURATIONS=(
3635
["crio_userns"]="root env-canary userns crun-enabled"
3736
)
3837

39-
CONTAINER_RUNTIME=$(which podman 2>/dev/null) ||
40-
CONTAINER_RUNTIME=$(which nerdctl 2>/dev/null) ||
41-
CONTAINER_RUNTIME=$(which docker 2>/dev/null)
38+
CONTAINER_RUNTIME=$(which podman 2>/dev/null || which nerdctl 2>/dev/null || which docker 2>/dev/null)
4239

4340
if [[ -z "$CONTAINER_RUNTIME" ]]; then
4441
echo "Neither podman, nerdctl nor docker found in \$PATH"
4542
exit 1
4643
fi
4744

48-
YQ_IMAGE=mikefarah/yq:4
49-
BUTANE_IMAGE=quay.io/coreos/butane:release
50-
BASE_PATH=base
45+
readonly YQ_IMAGE=mikefarah/yq:4
46+
readonly BUTANE_IMAGE=quay.io/coreos/butane:release
47+
readonly BASE_PATH=base
5148

52-
# Pull the latest image version
53-
for IMAGE in $YQ_IMAGE $BUTANE_IMAGE; do
54-
$CONTAINER_RUNTIME pull "$IMAGE"
55-
done
49+
# Helper function to run container commands
50+
run_container() {
51+
local image=$1
52+
shift
53+
$CONTAINER_RUNTIME run --privileged -i --rm -v "$PWD":/w -w /w "$image" "$@"
54+
}
5655

56+
# Merge multiple YAML files into one
5757
merge() {
58-
ARGS=()
59-
for ARG in "${@:2}"; do
60-
ARGS+=("$BASE_PATH/$ARG.yaml")
61-
done
58+
local output=$1
59+
shift
60+
local files=("${@/#/$BASE_PATH/}")
61+
local files=("${files[@]/%/.yaml}")
6262

63-
set -x
6463
# shellcheck disable=SC2016
65-
$CONTAINER_RUNTIME run --privileged -i --rm -v "$PWD":/w -w /w $YQ_IMAGE \
66-
ea '. as $item ireduce ({}; . *+ $item)' "${ARGS[@]}" >"$1.yaml"
67-
{ set +x; } 2>/dev/null
64+
run_container "$YQ_IMAGE" ea '. as $item ireduce ({}; . *+ $item)' "${files[@]}" >"$output.yaml"
6865
}
6966

70-
for i in "${!CONFIGURATIONS[@]}"; do
71-
echo "Building ${i}"
72-
73-
IFS=' ' read -r -a ARGS <<<"${CONFIGURATIONS[$i]}"
74-
merge "$i" "${ARGS[@]}"
67+
# Generate ignition files from configurations
68+
for name in "${!CONFIGURATIONS[@]}"; do
69+
echo "Building $name"
7570

76-
set -x
77-
$CONTAINER_RUNTIME run --privileged -i --rm -v "$PWD":/w -w /w $BUTANE_IMAGE \
78-
-ps -d $BASE_PATH <"$i.yaml" >../"$i.ign"
79-
{ set +x; } 2>/dev/null
71+
read -ra templates <<<"${CONFIGURATIONS[$name]}"
72+
merge "$name" "${templates[@]}"
73+
run_container "$BUTANE_IMAGE" -ps -d "$BASE_PATH" <"$name.yaml" >"../$name.ign"
8074
done
8175

82-
CRIO_SCRIPT_COMMIT=$(grep "CRIO_SCRIPT_COMMIT" "$BASE_PATH/env.yaml" | sed -rn 's/^.*CRIO_SCRIPT_COMMIT=([0-9a-f]+).*$/\1/p')
76+
# Validate CRI-O commit references
77+
extract_commit() {
78+
grep "$1" "$BASE_PATH/env.yaml" | grep -o '[0-9a-f]\{40\}'
79+
}
80+
81+
CRIO_SCRIPT_COMMIT=$(extract_commit "CRIO_SCRIPT_COMMIT")
8382
CRIO_SCRIPT_URL="https://raw.githubusercontent.com/cri-o/packaging/$CRIO_SCRIPT_COMMIT/get"
84-
curl -fs "$CRIO_SCRIPT_URL" -o /dev/null || {
85-
echo "Error: CRIO_SCRIPT_COMMIT in $BASE_PATH/env.yaml is wrong. The get script was not found at: $CRIO_SCRIPT_URL" 1>&2
83+
if ! curl -fs "$CRIO_SCRIPT_URL" -o /dev/null; then
84+
echo "Error: CRIO_SCRIPT_COMMIT in $BASE_PATH/env.yaml is wrong. The get script was not found at: $CRIO_SCRIPT_URL" >&2
8685
exit 1
87-
}
86+
fi
8887

89-
CRIO_COMMIT=$(grep "CRIO_COMMIT" "$BASE_PATH/env.yaml" | sed -rn 's/^.*CRIO_COMMIT=([0-9a-f]+).*$/\1/p')
88+
CRIO_COMMIT=$(extract_commit "CRIO_COMMIT")
9089
CRIO_BIN_URL="https://storage.googleapis.com/cri-o/artifacts/cri-o.amd64.$CRIO_COMMIT.tar.gz"
91-
curl -fs "$CRIO_BIN_URL" -o /dev/null || {
92-
echo "Error: CRIO_COMMIT in $BASE_PATH/env.yaml is wrong. The cri-o binary was not found at: $CRIO_BIN_URL" 1>&2
90+
if ! curl -fs "$CRIO_BIN_URL" -o /dev/null; then
91+
echo "Error: CRIO_COMMIT in $BASE_PATH/env.yaml is wrong. The cri-o binary was not found at: $CRIO_BIN_URL" >&2
9392
exit 1
94-
}
93+
fi

0 commit comments

Comments
 (0)