Skip to content

Commit 43e8288

Browse files
authored
Fix run operator locally (zalando#462)
* make test namespace optional * Update spilo/operator images * Add a command to replace operator image w/o minikube restart
1 parent 3544cc9 commit 43e8288

File tree

4 files changed

+54
-14
lines changed

4 files changed

+54
-14
lines changed

manifests/configmap.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ data:
1010

1111
debug_logging: "true"
1212
workers: "4"
13-
docker_image: registry.opensource.zalan.do/acid/spilo-cdp-11:1.5-p42
13+
docker_image: registry.opensource.zalan.do/acid/spilo-11:1.5-p4
1414
pod_service_account_name: "zalando-postgres-operator"
1515
secret_name_template: '{username}.{cluster}.credentials'
1616
super_username: postgres

manifests/minimal-postgres-manifest.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apiVersion: "acid.zalan.do/v1"
22
kind: postgresql
33
metadata:
44
name: acid-minimal-cluster
5-
namespace: test # assumes namespace exists beforehand
5+
namespace: default
66
spec:
77
teamId: "ACID"
88
volume:

manifests/postgres-operator.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ spec:
1212
serviceAccountName: zalando-postgres-operator
1313
containers:
1414
- name: postgres-operator
15-
image: registry.opensource.zalan.do/acid/smoke-tested-postgres-operator:v1.0.0-21-ge39915c
15+
image: registry.opensource.zalan.do/acid/smoke-tested-postgres-operator:v1.0.0-37-g2422d72
1616
imagePullPolicy: IfNotPresent
1717
resources:
1818
requests:

run_operator_locally.sh

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
# Deploy a Postgres operator to a minikube aka local Kubernetes cluster
44
# Optionally re-build the operator binary beforehand to test local changes
55

6+
# Known limitations:
7+
# 1) minikube provides a single node k8s cluster. That is, you will not be able test functions like pod
8+
# migration between multiple nodes locally
9+
# 2) this script configures the operator via configmap, not the operator CRD
10+
611

712
# enable unofficial bash strict mode
813
set -o errexit
@@ -13,6 +18,7 @@ IFS=$'\n\t'
1318

1419
readonly PATH_TO_LOCAL_OPERATOR_MANIFEST="/tmp/local-postgres-operator-manifest.yaml"
1520
readonly PATH_TO_PORT_FORWARED_KUBECTL_PID="/tmp/kubectl-port-forward.pid"
21+
readonly PATH_TO_THE_PG_CLUSTER_MANIFEST="/tmp/minimal-postgres-manifest.yaml"
1622
readonly LOCAL_PORT="8080"
1723
readonly OPERATOR_PORT="8080"
1824

@@ -37,18 +43,16 @@ function retry(){
3743
return 1
3844
}
3945

40-
4146
function display_help(){
42-
echo "Usage: $0 [ -r | --rebuild-operator ] [ -h | --help ]"
47+
echo "Usage: $0 [ -r | --rebuild-operator ] [ -h | --help ] [ -n | --deploy-new-operator-image ] [ -t | --deploy-pg-to-namespace-test ]"
4348
}
4449

45-
4650
function clean_up(){
4751

4852
echo "==== CLEAN UP PREVIOUS RUN ==== "
4953

5054
local status
51-
status=$(minikube status --format "{{.MinikubeStatus}}" || true)
55+
status=$(minikube status --format "{{.Host}}" || true)
5256

5357
if [[ "$status" = "Running" ]] || [[ "$status" = "Stopped" ]]; then
5458
echo "Delete the existing local cluster so that we can cleanly apply resources from scratch..."
@@ -123,7 +127,7 @@ function deploy_self_built_image() {
123127
# docker should not attempt to fetch it from the registry due to imagePullPolicy
124128
sed -e "s/\(image\:.*\:\).*$/\1$TAG/; s/smoke-tested-//" manifests/postgres-operator.yaml > "$PATH_TO_LOCAL_OPERATOR_MANIFEST"
125129

126-
retry "kubectl create -f \"$PATH_TO_LOCAL_OPERATOR_MANIFEST\"" "attempt to create $PATH_TO_LOCAL_OPERATOR_MANIFEST resource"
130+
retry "kubectl apply -f \"$PATH_TO_LOCAL_OPERATOR_MANIFEST\"" "attempt to create $PATH_TO_LOCAL_OPERATOR_MANIFEST resource"
127131
}
128132

129133

@@ -139,17 +143,18 @@ function start_operator(){
139143
retry "kubectl create -f manifests/\"$file\"" "attempt to create $file resource"
140144
done
141145

146+
cp manifests/postgres-operator.yaml $PATH_TO_LOCAL_OPERATOR_MANIFEST
147+
142148
if [[ "$should_build_custom_operator" = true ]]; then # set in main()
143149
deploy_self_built_image
144150
else
145-
retry "kubectl create -f manifests/postgres-operator.yaml" "attempt to create /postgres-operator.yaml resource"
151+
retry "kubectl create -f ${PATH_TO_LOCAL_OPERATOR_MANIFEST}" "attempt to create ${PATH_TO_LOCAL_OPERATOR_MANIFEST} resource"
146152
fi
147153

148154
local -r msg="Wait for the postgresql custom resource definition to register..."
149155
local -r cmd="kubectl get crd | grep --quiet 'postgresqls.acid.zalan.do'"
150156
retry "$cmd" "$msg "
151157

152-
kubectl create -f manifests/minimal-postgres-manifest.yaml
153158
}
154159

155160

@@ -186,16 +191,38 @@ function check_health(){
186191
}
187192

188193

194+
function submit_postgresql_manifest(){
195+
196+
echo "==== SUBMIT MINIMAL POSTGRES MANIFEST ==== "
197+
198+
local namespace="default"
199+
cp manifests/minimal-postgres-manifest.yaml $PATH_TO_THE_PG_CLUSTER_MANIFEST
200+
201+
if $should_deploy_pg_to_namespace_test; then
202+
kubectl create namespace test
203+
namespace="test"
204+
sed --in-place 's/namespace: default/namespace: test/' $PATH_TO_THE_PG_CLUSTER_MANIFEST
205+
fi
206+
207+
kubectl create -f $PATH_TO_THE_PG_CLUSTER_MANIFEST
208+
echo "The operator will create the PG cluster with minimal manifest $PATH_TO_THE_PG_CLUSTER_MANIFEST in the ${namespace} namespace"
209+
210+
}
211+
212+
189213
function main(){
190214

191215
if ! [[ $(basename "$PWD") == "postgres-operator" ]]; then
192-
echo "Please execute the script only from the root directory of the Postgres opepator repo."
216+
echo "Please execute the script only from the root directory of the Postgres operator repo."
193217
exit 1
194218
fi
195219

196220
trap "echo 'If you observe issues with minikube VM not starting/not proceeding, consider deleting the .minikube dir and/or rebooting before re-running the script'" EXIT
197221

198-
local should_build_custom_operator=false # used in start_operator()
222+
local should_build_custom_operator=false
223+
local should_deploy_pg_to_namespace_test=false
224+
local should_replace_operator_image=false
225+
199226
while true
200227
do
201228
# if the 1st param is unset, use the empty string as a default value
@@ -204,19 +231,32 @@ function main(){
204231
display_help
205232
exit 0
206233
;;
207-
-r | --rebuild-operator)
234+
-r | --rebuild-operator) # with minikube restart
208235
should_build_custom_operator=true
209236
break
210237
;;
238+
-n | --deploy-new-operator-image) # without minikube restart that takes minutes
239+
should_replace_operator_image=true
240+
break
241+
;;
242+
-t | --deploy-pg-to-namespace-test) # to test multi-namespace support locally
243+
should_deploy_pg_to_namespace_test=true
244+
break
245+
;;
211246
*) break
212247
;;
213248
esac
214249
done
215250

251+
if ${should_replace_operator_image}; then
252+
deploy_self_built_image
253+
exit 0
254+
fi
255+
216256
clean_up
217257
start_minikube
218-
kubectl create namespace test
219258
start_operator
259+
submit_postgresql_manifest
220260
forward_ports
221261
check_health
222262

0 commit comments

Comments
 (0)