fetch cluster_name_label from configuration
This commit is contained in:
		
							parent
							
								
									baae1887b3
								
							
						
					
					
						commit
						d450f0dcf8
					
				|  | @ -14,8 +14,9 @@ PG_BIN=$PG_DIR/$PG_VERSION/bin | ||||||
| DUMP_SIZE_COEFF=5 | DUMP_SIZE_COEFF=5 | ||||||
| 
 | 
 | ||||||
| TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token) | TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token) | ||||||
| K8S_API_URL=https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_SERVICE_PORT/api/v1 | K8S_API_URL=https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_SERVICE_PORT | ||||||
| CERT=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt | CERT=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt | ||||||
|  | CLUSTER_NAME_LABEL=cluster-name | ||||||
| 
 | 
 | ||||||
| function estimate_size { | function estimate_size { | ||||||
|     "$PG_BIN"/psql -tqAc "${ALL_DB_SIZE_QUERY}" |     "$PG_BIN"/psql -tqAc "${ALL_DB_SIZE_QUERY}" | ||||||
|  | @ -48,33 +49,63 @@ function aws_upload { | ||||||
| function get_pods { | function get_pods { | ||||||
|     declare -r SELECTOR="$1" |     declare -r SELECTOR="$1" | ||||||
| 
 | 
 | ||||||
|     curl "${K8S_API_URL}/namespaces/${POD_NAMESPACE}/pods?$SELECTOR"        \ |     curl "${K8S_API_URL}/api/v1/namespaces/${POD_NAMESPACE}/pods?$SELECTOR"        \ | ||||||
|         --cacert $CERT                          \ |         --cacert $CERT                          \ | ||||||
|         -H "Authorization: Bearer ${TOKEN}" | jq .items[].status.podIP -r |         -H "Authorization: Bearer ${TOKEN}" | jq .items[].status.podIP -r | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function get_current_pod { | function get_current_pod { | ||||||
|     curl "${K8S_API_URL}/namespaces/${POD_NAMESPACE}/pods?fieldSelector=metadata.name%3D${HOSTNAME}" \ |     curl "${K8S_API_URL}/api/v1/namespaces/${POD_NAMESPACE}/pods?fieldSelector=metadata.name%3D${HOSTNAME}" \ | ||||||
|         --cacert $CERT   \ |         --cacert $CERT   \ | ||||||
|         -H "Authorization: Bearer ${TOKEN}" |         -H "Authorization: Bearer ${TOKEN}" | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| declare -a search_strategy=( | declare -a search_strategy=( | ||||||
|  |     get_cluster_name_label | ||||||
|     list_all_replica_pods_current_node |     list_all_replica_pods_current_node | ||||||
|     list_all_replica_pods_any_node |     list_all_replica_pods_any_node | ||||||
|     get_master_pod |     get_master_pod | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | function get_config_resource() { | ||||||
|  |     curl "${K8S_API_URL}/apis/apps/v1/namespaces/default/deployments/postgres-operator" \ | ||||||
|  |         --cacert $CERT   \ | ||||||
|  |         -H "Authorization: Bearer ${TOKEN}" | jq '.spec.template.spec.containers[0].env[] | select(.name == "$1") | .value' | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | function get_cluster_name_label { | ||||||
|  |     local config | ||||||
|  |     local clustername | ||||||
|  | 
 | ||||||
|  |     config=$(get_config_resource "CONFIG_MAP_NAME") | ||||||
|  |     if [ -n "$config" ]; then | ||||||
|  |         clustername=$(curl "${K8S_API_URL}/api/v1/namespaces/default/configmaps/${config}" \ | ||||||
|  |                             --cacert $CERT   \ | ||||||
|  |                             -H "Authorization: Bearer ${TOKEN}" | jq '.data.cluster_name_label') | ||||||
|  |     else | ||||||
|  |         config=$(get_config_resource "POSTGRES_OPERATOR_CONFIGURATION_OBJECT") | ||||||
|  |         if [ -n "$config" ]; then | ||||||
|  |             clustername=$(curl "${K8S_API_URL}/apis/acid.zalan.do/v1/namespaces/default/operatorconfigurations/${config}" \ | ||||||
|  |                                 --cacert $CERT   \ | ||||||
|  |                                 -H "Authorization: Bearer ${TOKEN}" | jq '.configuration.kubernetes.cluster_name_label') | ||||||
|  |         fi | ||||||
|  |     fi | ||||||
|  | 
 | ||||||
|  |     if [ -n "$clustername" ]; then | ||||||
|  |         CLUSTER_NAME_LABEL=${clustername} | ||||||
|  |     fi; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| function list_all_replica_pods_current_node { | function list_all_replica_pods_current_node { | ||||||
|     get_pods "labelSelector=version%3D${SCOPE},spilo-role%3Dreplica&fieldSelector=spec.nodeName%3D${CURRENT_NODENAME}" | head -n 1 |     get_pods "labelSelector=${CLUSTER_NAME_LABEL}%3D${SCOPE},spilo-role%3Dreplica&fieldSelector=spec.nodeName%3D${CURRENT_NODENAME}" | head -n 1 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function list_all_replica_pods_any_node { | function list_all_replica_pods_any_node { | ||||||
|     get_pods "labelSelector=version%3D${SCOPE},spilo-role%3Dreplica" | head -n 1 |     get_pods "labelSelector=${CLUSTER_NAME_LABEL}%3D${SCOPE},spilo-role%3Dreplica" | head -n 1 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function get_master_pod { | function get_master_pod { | ||||||
|     get_pods "labelSelector=version%3D${SCOPE},spilo-role%3Dmaster" | head -n 1 |     get_pods "labelSelector=${CLUSTER_NAME_LABEL}%3D${SCOPE},spilo-role%3Dmaster" | head -n 1 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| CURRENT_NODENAME=$(get_current_pod | jq .items[].spec.nodeName --raw-output) | CURRENT_NODENAME=$(get_current_pod | jq .items[].spec.nodeName --raw-output) | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue