Skip to content

Commit c847254

Browse files
Merge pull request #281 from abays/improve_local_webhooks
Improve automated support for local webhooks
2 parents 251c5dc + cef6af1 commit c847254

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

Makefile

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -366,16 +366,15 @@ gowork: ## Generate go.work file
366366
go work sync
367367

368368
# Used for webhook testing
369-
# The configure_local_webhooks.sh script below will remove any OLM webhooks
369+
# The configure_local_webhook.sh script below will remove any OLM webhooks
370370
# for the operator and also scale its deployment replicas down to 0 so that
371371
# the operator can run locally.
372-
# Make sure to cleanup the webhook configuration for local testing by running
373-
# ./hack/clean_local_webhook.sh before deplying with OLM again.
372+
# We will attempt to catch SIGINT/SIGTERM and clean up the local webhooks,
373+
# but it may be necessary to manually run ./hack/clean_local_webhook.sh
374+
# before deploying with OLM again for other untrappable signals.
374375
SKIP_CERT ?=false
375376
.PHONY: run-with-webhook
376377
run-with-webhook: export METRICS_PORT?=8080
377378
run-with-webhook: export HEALTH_PORT?=8081
378-
run-with-webhook: export OPERATOR_TEMPLATES=./templates/
379379
run-with-webhook: manifests generate fmt vet ## Run a controller from your host.
380-
/bin/bash hack/configure_local_webhook.sh
381-
go run ./main.go -metrics-bind-address ":$(METRICS_PORT)" -health-probe-bind-address ":$(HEALTH_PORT)"
380+
/bin/bash hack/run_with_local_webhook.sh

hack/configure_local_webhook.sh renamed to hack/run_with_local_webhook.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
#!/bin/bash
22
set -ex
33

4+
# Define a cleanup function
5+
cleanup() {
6+
echo "Caught signal, cleaning up local webhooks..."
7+
./hack/clean_local_webhook.sh
8+
exit 0
9+
}
10+
11+
# Set trap to catch SIGINT and SIGTERM
12+
trap cleanup SIGINT SIGTERM
13+
414
TMPDIR=${TMPDIR:-"/tmp/k8s-webhook-server/serving-certs"}
515
SKIP_CERT=${SKIP_CERT:-false}
616
CRC_IP=${CRC_IP:-$(/sbin/ip -o -4 addr list crc | awk '{print $4}' | cut -d/ -f1)}
@@ -136,6 +146,23 @@ oc apply -n openstack -f ${TMPDIR}/patch_webhook_configurations.yaml
136146
CSV_NAME="$(oc get csv -n openstack-operators -l operators.coreos.com/mariadb-operator.openstack-operators -o name)"
137147

138148
if [ -n "${CSV_NAME}" ]; then
149+
CUR_REPLICAS=$(oc get -n openstack-operators "${CSV_NAME}" -o=jsonpath='{.spec.install.spec.deployments[0].spec.replicas}')
150+
CUR_WEBHOOK_DEFS=$(oc get -n openstack-operators "${CSV_NAME}" -o=jsonpath='{.spec.webhookdefinitions}')
151+
152+
# Back-up CSV if it currently uses OLM defaults for deployment replicas or webhook definitions
153+
if [[ "${CUR_REPLICAS}" -gt 0 || ( -n "${CUR_WEBHOOK_DEFS}" && "${CUR_WEBHOOK_DEFS}" != "[]" ) ]]; then
154+
CSV_FILE=$(mktemp -t "$(echo "${CSV_NAME}" | cut -d "/" -f 2).XXXXXX" --suffix .json)
155+
oc get -n openstack-operators "${CSV_NAME}" -o json | \
156+
jq -r 'del(.metadata.generation, .metadata.resourceVersion, .metadata.uid)' > "${CSV_FILE}"
157+
158+
printf \
159+
"\n\tNow patching operator CSV to remove its OLM deployment and associated webhooks.
160+
The original OLM version of the operator's CSV has been copied to %s. To restore it, use:
161+
oc patch -n openstack-operators %s --type=merge --patch-file=%s\n\n" "${CSV_FILE}" "${CSV_NAME}" "${CSV_FILE}"
162+
fi
163+
139164
oc patch "${CSV_NAME}" -n openstack-operators --type=json -p="[{'op': 'replace', 'path': '/spec/install/spec/deployments/0/spec/replicas', 'value': 0}]"
140165
oc patch "${CSV_NAME}" -n openstack-operators --type=json -p="[{'op': 'replace', 'path': '/spec/webhookdefinitions', 'value': []}]"
141166
fi
167+
168+
go run ./main.go -metrics-bind-address ":${METRICS_PORT}" -health-probe-bind-address ":${HEALTH_PORT}"

0 commit comments

Comments
 (0)