@@ -14,8 +14,9 @@ PG_BIN=$PG_DIR/$PG_VERSION/bin
14
14
DUMP_SIZE_COEFF=5
15
15
16
16
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
18
18
CERT=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt
19
+ CLUSTER_NAME_LABEL=cluster-name
19
20
20
21
function estimate_size {
21
22
" $PG_BIN " /psql -tqAc " ${ALL_DB_SIZE_QUERY} "
@@ -48,33 +49,63 @@ function aws_upload {
48
49
function get_pods {
49
50
declare -r SELECTOR=" $1 "
50
51
51
- curl " ${K8S_API_URL} /namespaces/${POD_NAMESPACE} /pods?$SELECTOR " \
52
+ curl " ${K8S_API_URL} /api/v1/ namespaces/${POD_NAMESPACE} /pods?$SELECTOR " \
52
53
--cacert $CERT \
53
54
-H " Authorization: Bearer ${TOKEN} " | jq .items[].status.podIP -r
54
55
}
55
56
56
57
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} " \
58
59
--cacert $CERT \
59
60
-H " Authorization: Bearer ${TOKEN} "
60
61
}
61
62
62
63
declare -a search_strategy=(
64
+ get_cluster_name_label
63
65
list_all_replica_pods_current_node
64
66
list_all_replica_pods_any_node
65
67
get_master_pod
66
68
)
67
69
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
+
68
99
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
70
101
}
71
102
72
103
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
74
105
}
75
106
76
107
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
78
109
}
79
110
80
111
CURRENT_NODENAME=$( get_current_pod | jq .items[].spec.nodeName --raw-output)
0 commit comments