|
| 1 | +# Environment variable common to all scripts |
| 2 | +APISERVER=https://kubernetes.default.svc |
| 3 | +SERVICEACCOUNT=/var/run/secrets/kubernetes.io/serviceaccount |
| 4 | +NAMESPACE=$(cat ${SERVICEACCOUNT}/namespace) |
| 5 | +TOKEN=$(cat ${SERVICEACCOUNT}/token) |
| 6 | +CACERT=${SERVICEACCOUNT}/ca.crt |
| 7 | + |
| 8 | +TIMEOUT=3 |
| 9 | + |
| 10 | +POD_NAME=$HOSTNAME |
| 11 | +POD_FQDN=$HOSTNAME.$SVC_FQDN |
| 12 | + |
| 13 | +if test -d /var/lib/config-data/tls; then |
| 14 | + REDIS_CLI_CMD="valkey-cli --tls" |
| 15 | + REDIS_CONFIG=/var/lib/valkey/valkey-tls.conf |
| 16 | + SENTINEL_CONFIG=/var/lib/valkey/sentinel-tls.conf |
| 17 | +else |
| 18 | + REDIS_CLI_CMD=valkey-cli |
| 19 | + REDIS_CONFIG=/var/lib/valkey/valkey.conf |
| 20 | + SENTINEL_CONFIG=/var/lib/valkey/sentinel.conf |
| 21 | +fi |
| 22 | + |
| 23 | +function log() { |
| 24 | + echo "$(date +%F_%H_%M_%S) $*" |
| 25 | +} |
| 26 | + |
| 27 | +function log_error() { |
| 28 | + echo "$(date +%F_%H_%M_%S) ERROR: $*" |
| 29 | +} |
| 30 | + |
| 31 | +function generate_configs() { |
| 32 | + # Copying config files except template files |
| 33 | + tar -C /var/lib/config-data --exclude '..*' --exclude '*.in' -h -c default | tar -C /var/lib/config-data/generated -x --strip=1 |
| 34 | + # Generating config files from templates |
| 35 | + cd /var/lib/config-data/default |
| 36 | + for cfg in $(find -L * -name '*.conf.in'); do |
| 37 | + log "Generating config file from template $PWD/${cfg}" |
| 38 | + sed -e "s/{ POD_FQDN }/${POD_FQDN}/" "${cfg}" > "/var/lib/config-data/generated/${cfg%.in}" |
| 39 | + done |
| 40 | +} |
| 41 | + |
| 42 | +function is_bootstrap_pod() { |
| 43 | + echo "$1" | grep -qe '-0$' |
| 44 | +} |
| 45 | + |
| 46 | +function extract() { |
| 47 | + local var="$1" |
| 48 | + local output="$2" |
| 49 | + # parse curl vars as well as kube api error fields |
| 50 | + echo "$output" | awk -F'[:,]' "/\"?${var}\"?:/ {print \$2; exit}" |
| 51 | +} |
| 52 | + |
| 53 | +function configure_pod_label() { |
| 54 | + local pod="$1" |
| 55 | + local patch="$2" |
| 56 | + local success="$3" |
| 57 | + local curlvars="\nexitcode:%{exitcode}\nerrormsg:%{errormsg}\nhttpcode:%{response_code}\n" |
| 58 | + |
| 59 | + response=$(curl -s -w "${curlvars}" --cacert ${CACERT} --header "Content-Type:application/json-patch+json" --header "Authorization: Bearer ${TOKEN}" --request PATCH --data "$patch" ${APISERVER}/api/v1/namespaces/${NAMESPACE}/pods/${pod}) |
| 60 | + |
| 61 | + exitcode=$(extract exitcode "$response") |
| 62 | + if [ $exitcode -ne 0 ]; then |
| 63 | + errormsg=$(extract errormsg "$response") |
| 64 | + log_error "Error when running curl: ${errormsg} (${exitcode})" |
| 65 | + return 1 |
| 66 | + fi |
| 67 | + |
| 68 | + httpcode=$(extract httpcode "$response") |
| 69 | + if echo "${httpcode}" | grep -v -E "^${success}$"; then |
| 70 | + message=$(extract message "$response") |
| 71 | + log_error "Error when calling API server: ${message} (${httpcode})" |
| 72 | + return 1 |
| 73 | + fi |
| 74 | +} |
| 75 | + |
| 76 | +function remove_pod_label() { |
| 77 | + local pod="$1" |
| 78 | + local label="$2" |
| 79 | + local patch="[{\"op\": \"remove\", \"path\": \"/metadata/labels/${label}\"}]" |
| 80 | + # 200: OK, 422: not found |
| 81 | + configure_pod_label $pod "$patch" "(200|422)" |
| 82 | +} |
| 83 | + |
| 84 | +function set_pod_label() { |
| 85 | + local pod="$1" |
| 86 | + local label="$2" |
| 87 | + local patch="[{\"op\": \"add\", \"path\": \"/metadata/labels/${label}\", \"value\": \"true\"}]" |
| 88 | + # 200: OK |
| 89 | + configure_pod_label $pod "$patch" "200" |
| 90 | +} |
0 commit comments