Skip to content

Commit 7e1d604

Browse files
committed
cert-manager auto rebase
Signed-off-by: Evgeny Slutsky <[email protected]>
1 parent 052ecc6 commit 7e1d604

File tree

2 files changed

+257
-0
lines changed

2 files changed

+257
-0
lines changed
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
#!/usr/bin/env bash
2+
# shellcheck disable=all
3+
# Copyright 2022 The MicroShift authors
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
set -o errexit
19+
set -o errtrace
20+
set -o nounset
21+
set -o pipefail
22+
23+
shopt -s expand_aliases
24+
shopt -s extglob
25+
26+
#debugging options
27+
#trap 'echo "#L$LINENO: $BASH_COMMAND" >&2' DEBUG
28+
#set -xo functrace
29+
#PS4='+ $LINENO '
30+
REPOROOT="$(readlink -f "$(dirname "${BASH_SOURCE[0]}")/../..")"
31+
STAGING_DIR="$REPOROOT/_output/staging"
32+
export REGISTRY_AUTH_FILE="${HOME}/.pull-secret.json"
33+
OPERATOR_INDEX="${STAGING_DIR}/redhat-operator-index.yaml"
34+
OPERATOR_CERT_MANAGER_INDEX="${STAGING_DIR}/redhat-operator-cert-manager-index.yaml"
35+
GO_MOD_DIRS=("$REPOROOT/" "$REPOROOT/etcd")
36+
37+
OPERATOR_COMPONENTS="cert-manager-controller cert-manager-ca-injector cert-manager-webhook cert-manager-acmesolver cert-manager-istiocsr"
38+
39+
declare -a ARCHS=("amd64" "arm64")
40+
declare -A GOARCH_TO_UNAME_MAP=( ["amd64"]="x86_64" ["arm64"]="aarch64" )
41+
42+
title() {
43+
echo -e "\E[34m$1\E[00m";
44+
}
45+
46+
check_preconditions() {
47+
if ! hash yq; then
48+
title "Installing yq"
49+
sudo DEST_DIR=/usr/bin/ "${REPOROOT}/scripts/fetch_tools.sh" yq
50+
fi
51+
52+
if ! hash opm; then
53+
title "Installing opm"
54+
sudo DEST_DIR=/usr/bin/ "${REPOROOT}/scripts/fetch_tools.sh" opm
55+
fi
56+
57+
58+
if ! hash python3; then
59+
echo "ERROR: python3 is not present on the system - please install"
60+
exit 1
61+
fi
62+
63+
if ! python3 -c "import yaml"; then
64+
echo "ERROR: missing python's yaml library - please install"
65+
exit 1
66+
fi
67+
}
68+
69+
# Clone a repo at a commit
70+
clone_repo() {
71+
local repo="$1"
72+
local commit="$2"
73+
local destdir="$3"
74+
75+
local repodir="${destdir}/${repo##*/}"
76+
77+
if [[ -d "${repodir}" ]]
78+
then
79+
return
80+
fi
81+
82+
git init "${repodir}"
83+
pushd "${repodir}" >/dev/null
84+
git remote add origin "${repo}"
85+
git fetch origin --quiet --filter=tree:0 --tags "${commit}"
86+
git checkout "${commit}"
87+
popd >/dev/null
88+
}
89+
90+
download_cert_manager(){
91+
rm -rf "${STAGING_DIR}"
92+
mkdir -p "${STAGING_DIR}"
93+
pushd "${STAGING_DIR}" >/dev/null
94+
95+
# export REGISTRY_AUTH_FILE=${PULL_SECRET_FILE}
96+
97+
operator_manifest="$1"
98+
99+
# get the whole operator yaml for 4.19
100+
opm render "${operator_manifest}" -o yaml >${OPERATOR_INDEX}
101+
102+
# find the latest published cert-manager-operator ie: cert-manager-operator.v1.16.0
103+
export operator=$(yq 'select(.package == "openshift-cert-manager-operator" and .name == "stable-v1") | .entries[-1].name' ${OPERATOR_INDEX})
104+
yq 'select (.name==env(operator))' ${OPERATOR_INDEX} >"${OPERATOR_CERT_MANAGER_INDEX}"
105+
106+
echo "found operator version ${operator}"
107+
108+
# convert from cert-manager-operator.v1.16.0 to cert-manager-x.y
109+
branch_name=$(echo ${operator} | awk -F'[^0-9]*' '{print "cert-manager-"$2"."$3}')
110+
clone_repo "https://github.com/openshift/cert-manager-operator" "$branch_name" "."
111+
112+
}
113+
114+
# Updates the image digests in pkg/release/release*.go
115+
# update_images() {
116+
# if [ ! -f "${STAGING_DIR}/release_amd64.json" ] || [ ! -f "${STAGING_DIR}/release_arm64.json" ]; then
117+
# >&2 echo "No release found in ${STAGING_DIR}, you need to download one first."
118+
# exit 1
119+
# fi
120+
# pushd "${STAGING_DIR}" >/dev/null
121+
122+
123+
# }
124+
125+
126+
127+
write_cert_manager_images_for_arch() {
128+
local arch="$1"
129+
title "Updating images for ${arch}"
130+
#local csv_manifest="${arch_dir}/servicemeshoperator3.clusterserviceversion.yaml"
131+
#local kustomization_arch_file="${REPOROOT}/assets/optional/gateway-api/kustomization.${GOARCH_TO_UNAME_MAP[${arch}]}.yaml"
132+
local cert_manager_release_json="${REPOROOT}/assets/optional/cert-manager/release-cert-manager-${GOARCH_TO_UNAME_MAP[${arch}]}.json"
133+
local cert_manager_operator_yaml="${REPOROOT}/assets/optional/cert-manager/manager/manager.yaml"
134+
local cert_manager_kustomization_yaml="${REPOROOT}/assets/optional/cert-manager/manager/kustomization.yaml"
135+
136+
local base_release=4.20
137+
jq -n "{\"release\": {\"base\": \"${base_release}\"}, \"images\": {}}" > "${cert_manager_release_json}"
138+
139+
#containerImage
140+
local operatorImage=$(yq '.properties[] | select(.type == "olm.csv.metadata").value.annotations.containerImage' "${OPERATOR_CERT_MANAGER_INDEX}")
141+
142+
yq -i -o json ".images += {\"cert-manager-operator\": \"${operatorImage}\"}" "${cert_manager_release_json}"
143+
sed -i "s#newName:.*openshift.io\/cert-manager-operator.*#newName: ${operatorImage}#g" "${cert_manager_kustomization_yaml}"
144+
145+
#relatedImages
146+
for index in $(yq '.relatedImages.[] | path | .[-1] ' "${OPERATOR_CERT_MANAGER_INDEX}"); do
147+
local image=$(yq ".relatedImages.${index}.image" "${OPERATOR_CERT_MANAGER_INDEX}" )
148+
local component=$(yq ".relatedImages.${index}.name" "${OPERATOR_CERT_MANAGER_INDEX}")
149+
if [[ -n "${component}" && "${OPERATOR_COMPONENTS}" == *"${component}"* ]]; then
150+
yq -i -o json ".images += {\"${component}\": \"${image}\"}" "${cert_manager_release_json}"
151+
sed -i "s#value:.*${component}.*#value: ${image}#g" "${cert_manager_operator_yaml}"
152+
153+
# handle special case istiocsr v istio-csr mismatch
154+
if [[ "${component}" == "cert-manager-istiocsr" ]]; then
155+
sed -i "s#value:.*cert-manager-istio-csr.*#value: ${image}#g" "${cert_manager_operator_yaml}"
156+
fi
157+
fi
158+
159+
160+
done
161+
162+
}
163+
164+
update_cert_manager_images() {
165+
title "Updating cert_manager images"
166+
local workdir="${STAGING_DIR}/cert-manager-operator"
167+
[ -d "${workdir}" ] || {
168+
>&2 echo 'cert_manager staging dir not found, aborting image update'
169+
return 1
170+
}
171+
for arch in "${ARCHS[@]}"; do
172+
write_cert_manager_images_for_arch "${arch}"
173+
done
174+
}
175+
176+
177+
copy_manifests() {
178+
# if [ ! -f "${STAGING_DIR}/release_amd64.json" ]; then
179+
# >&2 echo "No release found in ${STAGING_DIR}, you need to download one first."
180+
# exit 1
181+
# fi
182+
title "Copying manifests"
183+
"$REPOROOT/scripts/auto-rebase/handle_assets.py" "./scripts/auto-rebase/assets_cert_manager.yaml"
184+
}
185+
186+
187+
# Updates embedded component manifests by gathering these from various places
188+
# in the staged repos and copying them into the asset directorcay.
189+
update_cert_manager_manifests() {
190+
pushd "${STAGING_DIR}" >/dev/null
191+
192+
title "Modifying OpenShift manifests"
193+
194+
for index in $(yq '.[] | path | .[-1] ' "${OPERATOR_CERT_MANAGER_INDEX}")
195+
do
196+
image=$(yq ".${index}.image" "${OPERATOR_CERT_MANAGER_INDEX}")
197+
component=$(yq ".${index}.name" "${OPERATOR_CERT_MANAGER_INDEX}")
198+
199+
if [[ -n "${component}" && "${OPERATOR_COMPONENTS}" == *"${component}"* ]]; then
200+
#clone_repo "${repo}" "${commit}" "."
201+
#echo "${repo} embedded-component ${commit}" >> "${new_commits_file}"
202+
echo "${image} ${component}"
203+
fi
204+
done
205+
206+
207+
popd >/dev/null
208+
}
209+
210+
usage() {
211+
echo "Usage:"
212+
echo "$(basename "$0") to RELEASE_IMAGE_INTEL RELEASE_IMAGE_ARM Performs all the steps to rebase to a release image. Specify both amd64 and arm64 OCP releases."
213+
echo "$(basename "$0") download RELEASE_IMAGE_INTEL RELEASE_IMAGE_ARM Downloads the content of a release image to disk in preparation for rebasing. Specify both amd64 and arm64 OCP releases."
214+
echo "$(basename "$0") images Rebases the component images to the downloaded release"
215+
echo "$(basename "$0") manifests Rebases the component manifests to the downloaded release"
216+
exit 1
217+
}
218+
219+
check_preconditions
220+
221+
command=${1:-help}
222+
case "$command" in
223+
to)
224+
[[ $# -lt 3 ]] && usage
225+
rebase_to "$2" "$3"
226+
;;
227+
download)
228+
#[[ $# -lt 3 ]] && usage
229+
# download_release "$2" "$3"
230+
download_cert_manager "$2"
231+
;;
232+
images)
233+
update_cert_manager_images
234+
;;
235+
236+
manifests)
237+
copy_manifests
238+
update_cert_manager_manifests
239+
;;
240+
*) usage;;
241+
esac

scripts/fetch_tools.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,22 @@ gettool_oc() {
203203
popd &>/dev/null
204204
}
205205

206+
gettool_opm() {
207+
declare -A arch_map=(
208+
["x86_64"]="x86_64"
209+
["aarch64"]="arm64")
210+
211+
local arch="${arch_map[${ARCH}]}"
212+
213+
pushd "${WORK_DIR}" &>/dev/null
214+
215+
curl -s -f "https://mirror.openshift.com/pub/openshift-v4/${arch}/clients/ocp/latest/opm-linux-rhel9.tar.gz" -L -o "opm-linux-rhel9.tar.gz"
216+
tar xvzf opm-linux-rhel9.tar.gz
217+
sudo cp opm-rhel9 /usr/bin/opm
218+
219+
popd &>/dev/null
220+
}
221+
206222
gettool_brew() {
207223
# See https://spaces.redhat.com/display/Brew/Using+the+Brew+Prod+environment#UsingtheBrewProdenvironment-Fedora
208224
if ! command -v koji &>/dev/null ; then

0 commit comments

Comments
 (0)