Skip to content

Commit b613acb

Browse files
committed
Update hive-generate-config.sh
* Add additional bash script logging utilities * Update default hive hash to latest * Update hive-dev-install.sh * Simplify * Create hive secret completely in memory to remove writing of temporary pull secret to filesystem * Check if util.sh has been already been sourced before attempting to source it. * Prevents failures caused by re-assigning readonly variables. Correct Hive repository url Update default hive image tag to use full commits (as used in the new repo)
1 parent 607ba83 commit b613acb

File tree

4 files changed

+268
-159
lines changed

4 files changed

+268
-159
lines changed

hack/hive/hive-dev-install.sh

Lines changed: 78 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -3,109 +3,94 @@
33
set -o errexit \
44
-o nounset
55

6-
declare -r utils=hack/util.sh
7-
if [[ -f "$utils" ]]; then
8-
# shellcheck source=../util.sh
9-
source "$utils"
10-
fi
11-
12-
HIVE_OPERATOR_NS="hive"
13-
KUBECTL="$(which kubectl 2> /dev/null || which oc 2> /dev/null)"
14-
15-
if [[ ! -f go.mod ]] || [[ ! -d ".git" ]]; then
16-
abort "this script must be run from the repo's root directory"
17-
fi
18-
196
main() {
20-
log "enter hive installation"
21-
local skip_deployments=${1:-"none"}
22-
23-
if [[ ! -f "./hack/hive/hive-config/hive-deployment.yaml" ]] || [[ ! -d "./hack/hive/hive-config/crds" ]]; then
24-
log "hive config is missing, generating config, please rerun this script afterwards"
25-
./hack/hive/hive-generate-config.sh
26-
if [[ $? -ne 0 ]]; then
27-
abort "error generating the hive configs"
28-
fi
29-
fi
7+
info "starting"
308

31-
if [[ -z "$PULL_SECRET" ]]; then
32-
log "global pull secret variable required, please source ./env"
33-
exit 1
9+
if [ ! -f "$HIVE_CONFIG/hive-deployment.yaml" ] || [ ! -d "$HIVE_CONFIG/crds" ]; then
10+
fatal "hive-config is incomplete. Regenerate by running hack/hive/hive-generate-config.sh"
3411
fi
35-
verify_tools
3612

37-
if [[ "$( $KUBECTL get namespace $HIVE_OPERATOR_NS -o yaml 2>/dev/null | wc -l )" -ne 0 ]]; then
38-
log "hive is already installed in namespace $HIVE_OPERATOR_NS"
39-
log "Reapplying the configs automatically"
40-
else
41-
$KUBECTL create namespace $HIVE_OPERATOR_NS
42-
fi
13+
kubectl="$(which kubectl 2> /dev/null)" \
14+
|| kubectl_install kubectl
15+
16+
info "Creating $HIVE_OPERATOR_NS"
17+
$kubectl get namespace "$HIVE_OPERATOR_NS" > /dev/null 2>&1 || $kubectl create namespace "$HIVE_OPERATOR_NS"
18+
19+
info "Applying $HIVE_CONFIG/crds"
20+
$kubectl apply -f "$HIVE_CONFIG/crds"
21+
22+
[ -z "$PULL_SECRET" ] && fatal "global pull secret variable required, please source ./env"
23+
info "Generating and applying secret $HIVE_GLOBAL_PULL_SECRET_NAME"
24+
kubectl create \
25+
secret \
26+
generic \
27+
"$HIVE_GLOBAL_PULL_SECRET_NAME" \
28+
--from-literal .dockerconfigjson="$PULL_SECRET" \
29+
--type=kubernetes.io/dockerconfigjson \
30+
--namespace="$HIVE_OPERATOR_NS" \
31+
-o yaml \
32+
--dry-run=client \
33+
| $kubectl apply -f -
34+
35+
sed "s/HIVE_OPERATOR_NS/$HIVE_OPERATOR_NS/g" "$HIVE_CONFIG/hive-config.yaml" | $kubectl apply -f -
36+
$kubectl apply -f "$HIVE_CONFIG/hive-additional-install-log-regexes.yaml"
37+
$kubectl apply -f "$HIVE_CONFIG/hive-deployment.yaml"
38+
39+
# shellcheck disable=SC2329
40+
kubectl_get() { $kubectl -n "$HIVE_OPERATOR_NS" get "$1"; }
41+
42+
info "Getting $HIVE_DEPLOYMENT_OPERATOR"
43+
# Ensure the deployment exists before we wait on it
44+
retry "kubectl_get $HIVE_DEPLOYMENT_OPERATOR" || fatal "$HIVE_DEPLOYMENT_CONTROLLERS failed to create."
45+
info "Waiting for $HIVE_DEPLOYMENT_OPERATOR to become available."
46+
$kubectl wait \
47+
--timeout=5m \
48+
--for=condition=Available \
49+
--namespace \
50+
"$HIVE_OPERATOR_NS" \
51+
"$HIVE_DEPLOYMENT_OPERATOR"
52+
53+
info "Getting $HIVE_DEPLOYMENT_CONTROLLERS"
54+
# Ensure the deployment exists before we wait on it
55+
retry "kubectl_get $HIVE_DEPLOYMENT_CONTROLLERS" || fatal "$HIVE_DEPLOYMENT_CONTROLLERS failed to create."
56+
info "Waiting for $HIVE_DEPLOYMENT_CONTROLLERS to be available..."
57+
$kubectl wait \
58+
--timeout=5m \
59+
--for=condition=Available \
60+
--namespace "$HIVE_OPERATOR_NS" \
61+
"$HIVE_DEPLOYMENT_CONTROLLERS"
62+
}
4363

44-
log "Hive is ready to be installed"
45-
$KUBECTL apply -f ./hack/hive/hive-config/crds
46-
echo "$PULL_SECRET" > /tmp/.tmp-secret
47-
# Using dry-run allows updates to work seamlessly
48-
$KUBECTL create secret generic hive-global-pull-secret \
49-
--from-file=.dockerconfigjson=/tmp/.tmp-secret \
50-
--type=kubernetes.io/dockerconfigjson \
51-
--namespace $HIVE_OPERATOR_NS \
52-
-o yaml \
53-
--dry-run=client \
54-
| $KUBECTL apply -f - 2>/dev/null
55-
rm -f /tmp/.tmp-secret
56-
57-
sed "s/HIVE_OPERATOR_NS/$HIVE_OPERATOR_NS/g" hack/hive/hive-config/hive-config.yaml | $KUBECTL apply -f -
58-
$KUBECTL apply -f ./hack/hive/hive-config/hive-additional-install-log-regexes.yaml
59-
$KUBECTL apply -f ./hack/hive/hive-config/hive-deployment.yaml
60-
61-
# Step added to wait for Hive readiness
62-
$KUBECTL wait --timeout=5m --for=condition=Available --namespace $HIVE_OPERATOR_NS deployment/hive-operator
63-
64-
log "Hive is installed but to check Hive readiness use one of the following options to monitor the deployment rollout:
65-
'kubectl wait --timeout=5m --for=condition=Available --namespace "$HIVE_OPERATOR_NS" deployment/hive-controllers'
66-
or 'kubectl wait --timeout=5m --for=condition=Ready --namespace "$HIVE_OPERATOR_NS" pod --selector control-plane=clustersync'"
67-
68-
# Add retry loop to wait for hive-controllers deployment
69-
local ATTEMPTS=0
70-
local MAX_ATTEMPTS=6
71-
local DELAY=10 # 10 seconds delay between each check
72-
73-
until $KUBECTL get deployment hive-controllers -n $HIVE_OPERATOR_NS || [ $ATTEMPTS -eq $MAX_ATTEMPTS ]; do
74-
log "Waiting for hive-controllers deployment to be created... (Attempt: $ATTEMPTS)"
75-
sleep $DELAY
76-
ATTEMPTS=$((ATTEMPTS + 1))
77-
done
78-
79-
if [ $ATTEMPTS -eq $MAX_ATTEMPTS ]; then
80-
abort "hive-controllers deployment was not found after $MAX_ATTEMPTS attempts."
81-
fi
64+
kubectl_install() {
65+
local -n kubectl_bin="$1"
66+
info "starting"
8267

83-
# Wait for hive-controllers readiness
84-
log "Waiting for Hive controllers to be available..."
85-
$KUBECTL wait --timeout=5m --for=condition=Available --namespace $HIVE_OPERATOR_NS deployment/hive-controllers
68+
kubectl_stable_version="$(curl -L -s https://dl.k8s.io/release/stable.txt)"
69+
kubectl_stable_url="https://dl.k8s.io/release/$kubectl_stable_version/bin/linux/amd64/kubectl"
8670

87-
exit 0
88-
}
71+
info "Downloading $kubectl_stable_url"
72+
curl -sLO \
73+
--create-dirs \
74+
--output-dir "$HOME/bin" \
75+
\
76+
|| error "Failed to download $kubectl_stable_url"
8977

90-
function download_tmp_kubectl() {
91-
if ! curl -sLO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"; then
92-
abort ": error downloading kubectl"
93-
fi
94-
chmod 755 kubectl
95-
KUBECTL="$(pwd)/kubectl"
78+
# shellcheck disable=SC2034
79+
kubectl_bin="$HOME/bin/kubectl"
80+
chmod 755 "$kubectl_bin" || fatal "failed to mark $kubectl_bin as executable."
9681
}
9782

98-
function verify_tools() {
99-
if [[ -n "$KUBECTL" ]]; then
100-
return
101-
fi
102-
log "kubectl or oc not detected, downloading"
103-
download_tmp_kubectl
104-
log "done: downloading kubectl/oc was completed"
83+
# declare -r source_file_not_found_err="not found. Are you in the ARO-RP repository root?"
84+
declare -r source_file_not_found_err="not found. Are you in the ARO-RP repository root?"
10585

106-
if [[ "$( $KUBECTL get nodes 2>/dev/null | wc -l )" -eq 0 ]]; then
107-
abort "unable to connect to the cluster"
108-
fi
109-
}
86+
declare -r util_lib="hack/util.sh"
87+
[ -f "$util_lib" ] || "$(echo "$util_lib $source_file_not_found_err"; exit 1)"
88+
# shellcheck source=../util.sh
89+
[ "${__hack_util_sourced:-'false'}" == "false" ] || . "$util_lib"
90+
91+
declare -r hive_env="hack/hive/hive.env"
92+
[ -f "$hive_env" ] || fatal "$hive_env $source_file_not_found_err"
93+
# shellcheck source=./hive.env
94+
[ "${__hive_env_sourced:-'false'}" == "false" ] || . "$hive_env"
11095

11196
main "$@"

hack/hive/hive-generate-config.sh

Lines changed: 67 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,61 @@ set -o errexit \
44
-o nounset
55

66
main() {
7+
local -r hive_commit_override="${1:-}"
8+
9+
# This is the commit sha that the image was built from and ensures we use the correct configs for the release
10+
# Ensure it is the latest when testing pre production deployments
11+
local -r default_commit="f48f47857f6a1dda25ad46957927ee6fe3afe1eb"
12+
if [ -z "$hive_commit_override" ]; then
13+
warn "Using default hive commit hash: $default_commit"
14+
warn "Hive commit hashes can be found here: https://quay.io/repository/redhat-user-workloads/crt-redhat-acm-tenant/hive-operator/hive?tab=tags"
15+
fi
16+
local -r hive_image_commit_hash="${hive_commit_override:-$default_commit}"
17+
18+
info "Using hive commit: $hive_image_commit_hash"
19+
# shellcheck disable=SC2034
20+
721
local -r tmpdir="$(mktemp -d)"
822
# shellcheck disable=SC2064
923
trap "cleanup $tmpdir" EXIT
1024

11-
# This is the commit sha that the image was built from and ensures we use the correct configs for the release
12-
local -r default_commit="8796c4f534"
13-
local -r hive_image_commit_hash="${1:-$default_commit}"
14-
log "Using hive commit: $hive_image_commit_hash"
1525
# shellcheck disable=SC2034
16-
local -r hive_operator_namespace="hive"
26+
kustomize_bin="$(which kustomize 2> /dev/null)" \
27+
|| install_kustomize "$tmpdir" kustomize_bin
1728

18-
# shellcheck disable=SC2034
19-
local -r hive_image="arointsvc.azurecr.io/redhat-services-prod/crt-redhat-acm-tenant/hive-operator/hive:${hive_image_commit_hash}"
29+
hive_repo_clone "$tmpdir"
2030

31+
hive_repo_hash_checkout "$tmpdir" "$hive_image_commit_hash"
2132

22-
# shellcheck disable=SC2034
23-
local kustomize_bin
24-
install_kustomize tmpdir \
25-
kustomize_bin
26-
hive_repo_clone tmpdir
27-
hive_repo_hash_checkout tmpdir \
28-
"$hive_image_commit_hash"
33+
local -r hive_image="arointsvc.azurecr.io/redhat-services-prod/crt-redhat-acm-tenant/hive-operator/hive:${hive_image_commit_hash}"
2934
generate_hive_config kustomize_bin \
30-
"$hive_operator_namespace" \
35+
"$HIVE_OPERATOR_NS" \
3136
"$hive_image" \
32-
tmpdir
37+
"$tmpdir"
3338

34-
log "Hive config generated."
39+
info "Hive config generated."
3540
}
3641

3742
install_kustomize() {
38-
local -n tmpd="$1"
43+
local tmpd="$1"
3944
local -n kustomize="$2"
40-
log "starting"
41-
42-
if kustomize="$(which kustomize 2> /dev/null)"; then
43-
return 0
44-
fi
45+
info "starting"
4546

4647
pushd "$tmpd" 1> /dev/null
4748

4849
# This version is specified in the hive repo and is the only hard dependency for this script
4950
# https://github.com/openshift/hive/blob/master/vendor/github.com/openshift/build-machinery-go/make/targets/openshift/kustomize.mk#L7
5051
local -r kustomize_version="4.1.3"
51-
log "kustomize not detected, downloading..."
52+
info "kustomize not detected, downloading..."
5253
if ! curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/kustomize/v${kustomize_version}/hack/install_kustomize.sh" | bash -s "$kustomize_version" "$tmpd" 1> /dev/null; then
53-
abort "error downloading kustomize"
54+
fatal "error downloading kustomize"
5455
fi
5556

56-
if [ ! -d "${HOME}/bin" ]; then
57-
mkdir -p "${HOME}/bin"
58-
fi
57+
[ -d "${HOME}/bin" ] || mkdir -p "${HOME}/bin"
5958

6059
kustomize_new="${tmpd}/kustomize"
6160
kustomize_dest="${HOME}/bin/kustomize"
62-
log "Installing $kustomize_new into $kustomize_dest"
61+
info "Installing $kustomize_new into $kustomize_dest"
6362
mv "$kustomize_new" "$kustomize_dest"
6463

6564
popd 1> /dev/null
@@ -68,27 +67,26 @@ install_kustomize() {
6867
}
6968

7069
hive_repo_clone() {
71-
local -n tmpd="$1"
72-
log "starting"
70+
local tmpd="$1"
71+
info "starting"
7372

7473
local -r repo="https://github.com/openshift/hive.git"
75-
log "Cloning $repo into $tmpd for config generation"
74+
info "Cloning $repo into $tmpd"
7675
if ! git clone "$repo" "$tmpd"; then
77-
log "error cloning the hive repo"
78-
return 1
76+
fatal "error cloning the hive repo"
7977
fi
8078
}
8179

8280
hive_repo_hash_checkout() {
83-
local -n tmpd="$1"
81+
local tmpd="$1"
8482
local commit="$2"
85-
log "starting"
86-
log "Attempting to use commit: $commit"
83+
info "starting"
84+
info "Attempting to use commit: $commit"
8785

8886
pushd "$tmpd" 1> /dev/null
89-
git reset --hard "$commit"
90-
if [ "$?" -ne 0 ] || [ "$( git rev-parse --short="${#commit}" HEAD )" != "$commit" ]; then
91-
abort "error resetting the hive repo to the correct git hash '${commit}'"
87+
88+
if git reset --hard "$commit" && [ "$( git rev-parse --short="${#commit}" HEAD )" != "$commit" ]; then
89+
fatal "error resetting the hive repo to the correct git hash '${commit}'"
9290
fi
9391

9492
popd 1> /dev/null
@@ -99,36 +97,51 @@ generate_hive_config() {
9997
local -n kustomize="$1"
10098
local namespace="$2"
10199
local image="$3"
102-
local -n tmpd="$4"
103-
log "starting"
100+
local tmpd="$4"
101+
info "starting"
104102

105103
pushd "$tmpd" 1> /dev/null
106-
# Create the hive operator install config using kustomize
107104
mkdir -p overlays/deploy
105+
106+
debug "copying template kustomization.yaml"
108107
cp overlays/template/kustomization.yaml overlays/deploy
109-
pushd overlays/deploy >& /dev/null
108+
pushd overlays/deploy 1> /dev/null
109+
debug "Setting hive image."
110110
$kustomize edit set image registry.ci.openshift.org/openshift/hive-v4.0:hive="$image"
111111
$kustomize edit set namespace "$namespace"
112-
popd >& /dev/null
112+
popd 1> /dev/null
113113

114+
info "Building hive deployment"
114115
$kustomize build overlays/deploy > hive-deployment.yaml
115116

116-
# return to the repo directory to copy the generated config from $TMPDIR
117117
popd 1> /dev/null
118-
mv "$tmpd/hive-deployment.yaml" ./hack/hive/hive-config/
118+
mv "$tmpd/hive-deployment.yaml" "$HIVE_CONFIG"
119119

120-
# ensure the hive deployment uses the pull secret
121-
yq -i 'select(.kind == "ServiceAccount").imagePullSecrets = [{"name": "hive-global-pull-secret"}]' ./hack/hive/hive-config/hive-deployment.yaml
120+
debug "Verifying hive deployment pull secret exists in deployment."
121+
yq -i 'select(.kind == "ServiceAccount").imagePullSecrets = [{"name": "hive-global-pull-secret"}]' "$HIVE_CONFIG/hive-deployment.yaml"
122122

123-
if [ -d ./hack/hive/hive-config/crds ]; then
124-
rm -rf ./hack/hive/hive-config/crds
123+
crds_old="$HIVE_CONFIG/crds"
124+
if [ -d "$crds_old" ]; then
125+
info "Deleting $crds_old"
126+
rm -rf "$HIVE_CONFIG/crds"
125127
fi
126-
cp -R "$tmpd/config/crds" ./hack/hive/hive-config/
128+
129+
crds_new="$tmpd/config/crds"
130+
info "Copying $crds_new into $HIVE_CONFIG"
131+
cp -R "$crds_new" "$HIVE_CONFIG/"
127132
}
128133

134+
# declare -r source_file_not_found_err="not found. Are you in the ARO-RP repository root?"
135+
declare -r source_file_not_found_err="not found. Are you in the ARO-RP repository root?"
136+
129137
declare -r util_lib="hack/util.sh"
130-
[ -f "$util_lib" ] || "$(echo "$util_lib not found. Are you in the ARO-RP repository root?"; exit 1)"
138+
[ -f "$util_lib" ] || "$(echo "$util_lib $source_file_not_found_err"; exit 1)"
131139
# shellcheck source=../util.sh
132-
. "$util_lib"
140+
[ "${__hack_util_sourced:-'false'}" == "false" ] || . "$util_lib"
141+
142+
declare -r hive_env="hack/hive/hive.env"
143+
[ -f "$hive_env" ] || fatal "$hive_env $source_file_not_found_err"
144+
# shellcheck source=./hive.env
145+
[ "${__hive_env_sourced:-'false'}" == "false" ] || . "$hive_env"
133146

134147
main "$@"

0 commit comments

Comments
 (0)