Skip to content

Commit bb855fd

Browse files
authored
fetch cluster_name_label from configuration (zalando#684)
1 parent 647a4d3 commit bb855fd

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

docker/logical-backup/dump.sh

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ PG_BIN=$PG_DIR/$PG_VERSION/bin
1414
DUMP_SIZE_COEFF=5
1515

1616
TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
17-
K8S_API_URL=https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_SERVICE_PORT/api/v1
17+
K8S_API_URL=https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_SERVICE_PORT
1818
CERT=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt
19+
CLUSTER_NAME_LABEL=cluster-name
1920

2021
function estimate_size {
2122
"$PG_BIN"/psql -tqAc "${ALL_DB_SIZE_QUERY}"
@@ -48,33 +49,63 @@ function aws_upload {
4849
function get_pods {
4950
declare -r SELECTOR="$1"
5051

51-
curl "${K8S_API_URL}/namespaces/${POD_NAMESPACE}/pods?$SELECTOR" \
52+
curl "${K8S_API_URL}/api/v1/namespaces/${POD_NAMESPACE}/pods?$SELECTOR" \
5253
--cacert $CERT \
5354
-H "Authorization: Bearer ${TOKEN}" | jq .items[].status.podIP -r
5455
}
5556

5657
function get_current_pod {
57-
curl "${K8S_API_URL}/namespaces/${POD_NAMESPACE}/pods?fieldSelector=metadata.name%3D${HOSTNAME}" \
58+
curl "${K8S_API_URL}/api/v1/namespaces/${POD_NAMESPACE}/pods?fieldSelector=metadata.name%3D${HOSTNAME}" \
5859
--cacert $CERT \
5960
-H "Authorization: Bearer ${TOKEN}"
6061
}
6162

6263
declare -a search_strategy=(
64+
get_cluster_name_label
6365
list_all_replica_pods_current_node
6466
list_all_replica_pods_any_node
6567
get_master_pod
6668
)
6769

70+
function get_config_resource() {
71+
curl "${K8S_API_URL}/apis/apps/v1/namespaces/default/deployments/postgres-operator" \
72+
--cacert $CERT \
73+
-H "Authorization: Bearer ${TOKEN}" | jq '.spec.template.spec.containers[0].env[] | select(.name == "$1") | .value'
74+
}
75+
76+
function get_cluster_name_label {
77+
local config
78+
local clustername
79+
80+
config=$(get_config_resource "CONFIG_MAP_NAME")
81+
if [ -n "$config" ]; then
82+
clustername=$(curl "${K8S_API_URL}/api/v1/namespaces/default/configmaps/${config}" \
83+
--cacert $CERT \
84+
-H "Authorization: Bearer ${TOKEN}" | jq '.data.cluster_name_label')
85+
else
86+
config=$(get_config_resource "POSTGRES_OPERATOR_CONFIGURATION_OBJECT")
87+
if [ -n "$config" ]; then
88+
clustername=$(curl "${K8S_API_URL}/apis/acid.zalan.do/v1/namespaces/default/operatorconfigurations/${config}" \
89+
--cacert $CERT \
90+
-H "Authorization: Bearer ${TOKEN}" | jq '.configuration.kubernetes.cluster_name_label')
91+
fi
92+
fi
93+
94+
if [ -n "$clustername" ]; then
95+
CLUSTER_NAME_LABEL=${clustername}
96+
fi;
97+
}
98+
6899
function list_all_replica_pods_current_node {
69-
get_pods "labelSelector=version%3D${SCOPE},spilo-role%3Dreplica&fieldSelector=spec.nodeName%3D${CURRENT_NODENAME}" | head -n 1
100+
get_pods "labelSelector=${CLUSTER_NAME_LABEL}%3D${SCOPE},spilo-role%3Dreplica&fieldSelector=spec.nodeName%3D${CURRENT_NODENAME}" | head -n 1
70101
}
71102

72103
function list_all_replica_pods_any_node {
73-
get_pods "labelSelector=version%3D${SCOPE},spilo-role%3Dreplica" | head -n 1
104+
get_pods "labelSelector=${CLUSTER_NAME_LABEL}%3D${SCOPE},spilo-role%3Dreplica" | head -n 1
74105
}
75106

76107
function get_master_pod {
77-
get_pods "labelSelector=version%3D${SCOPE},spilo-role%3Dmaster" | head -n 1
108+
get_pods "labelSelector=${CLUSTER_NAME_LABEL}%3D${SCOPE},spilo-role%3Dmaster" | head -n 1
78109
}
79110

80111
CURRENT_NODENAME=$(get_current_pod | jq .items[].spec.nodeName --raw-output)

0 commit comments

Comments
 (0)