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
|
||||
|
||||
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
|
||||
CLUSTER_NAME_LABEL=cluster-name
|
||||
|
||||
function estimate_size {
|
||||
"$PG_BIN"/psql -tqAc "${ALL_DB_SIZE_QUERY}"
|
||||
|
|
@ -49,53 +48,23 @@ function aws_upload {
|
|||
function get_pods {
|
||||
declare -r SELECTOR="$1"
|
||||
|
||||
curl "${K8S_API_URL}/api/v1/namespaces/${POD_NAMESPACE}/pods?$SELECTOR" \
|
||||
--cacert $CERT \
|
||||
curl "${K8S_API_URL}/namespaces/${POD_NAMESPACE}/pods?$SELECTOR" \
|
||||
--cacert $CERT \
|
||||
-H "Authorization: Bearer ${TOKEN}" | jq .items[].status.podIP -r
|
||||
}
|
||||
|
||||
function get_current_pod {
|
||||
curl "${K8S_API_URL}/api/v1/namespaces/${POD_NAMESPACE}/pods?fieldSelector=metadata.name%3D${HOSTNAME}" \
|
||||
--cacert $CERT \
|
||||
curl "${K8S_API_URL}/namespaces/${POD_NAMESPACE}/pods?fieldSelector=metadata.name%3D${HOSTNAME}" \
|
||||
--cacert $CERT \
|
||||
-H "Authorization: Bearer ${TOKEN}"
|
||||
}
|
||||
|
||||
declare -a search_strategy=(
|
||||
get_cluster_name_label
|
||||
list_all_replica_pods_current_node
|
||||
list_all_replica_pods_any_node
|
||||
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 {
|
||||
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
|
||||
- get
|
||||
- patch
|
||||
- apiGroups:
|
||||
- apps
|
||||
resources:
|
||||
- deployments
|
||||
verbs:
|
||||
- get
|
||||
- apiGroups:
|
||||
- apps
|
||||
resources:
|
||||
|
|
|
|||
|
|
@ -1557,6 +1557,10 @@ func (c *Cluster) generateLogicalBackupPodEnvVars() []v1.EnvVar {
|
|||
Name: "SCOPE",
|
||||
Value: c.Name,
|
||||
},
|
||||
{
|
||||
Name: "CLUSTER_NAME_LABEL",
|
||||
Value: c.OpConfig.ClusterNameLabel,
|
||||
},
|
||||
{
|
||||
Name: "POD_NAMESPACE",
|
||||
ValueFrom: &v1.EnvVarSource{
|
||||
|
|
|
|||
Loading…
Reference in New Issue