inject cluster name label into logical backup pod (#725)
* inject cluster name label into logical backup pod
This commit is contained in:
		
							parent
							
								
									0b544ae43f
								
							
						
					
					
						commit
						f9487e41c1
					
				|  | @ -14,9 +14,8 @@ 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 | K8S_API_URL=https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_SERVICE_PORT/api/v1 | ||||||
| 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}" | ||||||
|  | @ -49,53 +48,23 @@ function aws_upload { | ||||||
| function get_pods { | function get_pods { | ||||||
|     declare -r SELECTOR="$1" |     declare -r SELECTOR="$1" | ||||||
| 
 | 
 | ||||||
|     curl "${K8S_API_URL}/api/v1/namespaces/${POD_NAMESPACE}/pods?$SELECTOR"        \ |     curl "${K8S_API_URL}/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}/api/v1/namespaces/${POD_NAMESPACE}/pods?fieldSelector=metadata.name%3D${HOSTNAME}" \ |     curl "${K8S_API_URL}/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=${CLUSTER_NAME_LABEL}%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 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -103,12 +103,6 @@ rules: | ||||||
|   - delete |   - delete | ||||||
|   - get |   - get | ||||||
|   - patch |   - patch | ||||||
| - apiGroups: |  | ||||||
|   - apps |  | ||||||
|   resources: |  | ||||||
|   - deployments |  | ||||||
|   verbs: |  | ||||||
|   - get |  | ||||||
| - apiGroups: | - apiGroups: | ||||||
|   - apps |   - apps | ||||||
|   resources: |   resources: | ||||||
|  |  | ||||||
|  | @ -1557,6 +1557,10 @@ func (c *Cluster) generateLogicalBackupPodEnvVars() []v1.EnvVar { | ||||||
| 			Name:  "SCOPE", | 			Name:  "SCOPE", | ||||||
| 			Value: c.Name, | 			Value: c.Name, | ||||||
| 		}, | 		}, | ||||||
|  | 		{ | ||||||
|  | 			Name:  "CLUSTER_NAME_LABEL", | ||||||
|  | 			Value: c.OpConfig.ClusterNameLabel, | ||||||
|  | 		}, | ||||||
| 		{ | 		{ | ||||||
| 			Name: "POD_NAMESPACE", | 			Name: "POD_NAMESPACE", | ||||||
| 			ValueFrom: &v1.EnvVarSource{ | 			ValueFrom: &v1.EnvVarSource{ | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue