Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bitnami/etcd] Stop relying on files for state #75906

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,11 @@ etcd_env_vars=(
ETCD_ON_K8S
ETCD_INIT_SNAPSHOT_FILENAME
ETCDCTL_API
ETCD_DISABLE_STORE_MEMBER_ID
ETCD_DISABLE_PRESTOP
ETCD_NAME
ETCD_LOG_LEVEL
ETCD_LISTEN_CLIENT_URLS
ETCD_ADVERTISE_CLIENT_URLS
ETCD_INITIAL_CLUSTER
ETCD_INITIAL_CLUSTER_STATE
ETCD_LISTEN_PEER_URLS
ETCD_INITIAL_ADVERTISE_PEER_URLS
ETCD_INITIAL_CLUSTER_TOKEN
Expand Down Expand Up @@ -94,16 +91,13 @@ export ETCD_DISASTER_RECOVERY="${ETCD_DISASTER_RECOVERY:-no}"
export ETCD_ON_K8S="${ETCD_ON_K8S:-no}"
export ETCD_INIT_SNAPSHOT_FILENAME="${ETCD_INIT_SNAPSHOT_FILENAME:-}"
export ETCDCTL_API="${ETCDCTL_API:-3}"
export ETCD_DISABLE_STORE_MEMBER_ID="${ETCD_DISABLE_STORE_MEMBER_ID:-no}"
export ETCD_DISABLE_PRESTOP="${ETCD_DISABLE_PRESTOP:-no}"

# etcd native environment variables (see https://etcd.io/docs/current/op-guide/configuration)
export ETCD_NAME="${ETCD_NAME:-}"
export ETCD_LOG_LEVEL="${ETCD_LOG_LEVEL:-info}"
export ETCD_LISTEN_CLIENT_URLS="${ETCD_LISTEN_CLIENT_URLS:-http://0.0.0.0:2379}"
export ETCD_ADVERTISE_CLIENT_URLS="${ETCD_ADVERTISE_CLIENT_URLS:-http://127.0.0.1:2379}"
export ETCD_INITIAL_CLUSTER="${ETCD_INITIAL_CLUSTER:-}"
export ETCD_INITIAL_CLUSTER_STATE="${ETCD_INITIAL_CLUSTER_STATE:-}"
export ETCD_LISTEN_PEER_URLS="${ETCD_LISTEN_PEER_URLS:-}"
export ETCD_INITIAL_ADVERTISE_PEER_URLS="${ETCD_INITIAL_ADVERTISE_PEER_URLS:-}"
export ETCD_INITIAL_CLUSTER_TOKEN="${ETCD_INITIAL_CLUSTER_TOKEN:-}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,5 @@ set -o pipefail
set -o nounset
# set -o xtrace # Uncomment this line for debugging purposes

# Load libraries
. /opt/bitnami/scripts/libetcd.sh

# Load etcd environment settings
. /opt/bitnami/scripts/etcd-env.sh

if is_boolean_yes "$ETCD_DISABLE_PRESTOP"; then
return 0
fi

endpoints="$(etcdctl_get_endpoints true)"
if is_empty_value "${endpoints}"; then
exit 0
fi
read -r -a extra_flags <<<"$(etcdctl_auth_flags)"
extra_flags+=("--endpoints=${endpoints}" "--debug=true")
# We use 'sync' to ensure memory buffers are flushed to disk
# so we reduce the chances that the "member_removal.log" file is empty.
# ref: https://man7.org/linux/man-pages/man1/sync.1.html
etcdctl member remove "$(get_member_id)" "${extra_flags[@]}" >"$(dirname "$ETCD_DATA_DIR")/member_removal.log"
sync -d "$(dirname "$ETCD_DATA_DIR")/member_removal.log"
# This script should be removed when the chart no longer use it
return 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/bin/bash
# Copyright Broadcom, Inc. All Rights Reserved.
# SPDX-License-Identifier: APACHE-2.0

# shellcheck disable=SC1091

set -o errexit
set -o pipefail
set -o nounset

# Load libraries
. /opt/bitnami/scripts/libfs.sh
. /opt/bitnami/scripts/libos.sh
. /opt/bitnami/scripts/libetcd.sh

# Load etcd environment settings
. /opt/bitnami/scripts/etcd-env.sh

########################
# Obtain endpoints to connect when running 'ectdctl' in a hook job
# Globals:
# ETCD_*
# Arguments:
# None
# Returns:
# String
########################
etcdctl_job_endpoints() {
local -a endpoints=()
local host domain port count

# get number of endpoints from initial cluster endpoints
count="$(echo $ETCD_INITIAL_CLUSTER | awk -F, '{print NF}')"

# This piece of code assumes this code is executed on a K8s environment
# where etcd members are part of a statefulset that uses a headless service
# to create a unique FQDN per member. Under these circumstances, the
# ETCD_ADVERTISE_CLIENT_URLS env. variable is created as follows:
# SCHEME://POD_NAME.HEADLESS_SVC_DOMAIN:CLIENT_PORT,SCHEME://SVC_DOMAIN:SVC_CLIENT_PORT
#
# Assuming this, we can extract the HEADLESS_SVC_DOMAIN and obtain
# every available endpoint
read -r -a advertised_array <<<"$(tr ',;' ' ' <<<"$ETCD_ADVERTISE_CLIENT_URLS")"
port="$(parse_uri "${advertised_array[0]}" "port")"

for i in $(seq 0 $(($count - 1))); do
pod_name="${MY_STS_NAME}-${i}"
endpoints+=("${pod_name}.${ETCD_CLUSTER_DOMAIN}:${port:-2380}")
done

debug "etcdctl endpoints are ${endpoints[*]}"
echo "${endpoints[*]}" | tr ' ' ','
}

########################
# Remove members that are not named in ETCD_INITIAL_CLUSTER
# Globals:
# ETCD_*
# Arguments:
# None
# Returns:
# None
#########################
remove_members() {
local -r current=$(mktemp)
local -r expected=$(mktemp)

local -a extra_flags
read -r -a extra_flags <<<"$(etcdctl_auth_flags)"
is_boolean_yes "$ETCD_ON_K8S" && extra_flags+=("--endpoints=$(etcdctl_job_endpoints)")
debug "Listing members"
if ! etcdctl member list ${extra_flags[@]} --write-out simple | awk -F ", " '{print $1 "," $3}' > $current; then
debug "Error listing members, is this a new cluster?"
return 0
fi
info "Current cluster members are: $(cat $current | awk -F, '{print $2}' | tr -s '\n' ', ' | sed 's/,$//g')"

echo $ETCD_INITIAL_CLUSTER | sed 's/,/\n/g' | awk -F= '{print $1}' > $expected
info "Expected cluster members are: $(cat $expected | tr -s '\n' ', ' | sed 's/,$//g')"

for member in $(comm -23 <(cat $current | awk -F, '{print $2}' | sort) <(sort $expected)); do
info "Removing obsolete member $member"
etcdctl member remove ${extra_flags[@]} $(cat $current | grep "$member" | awk -F, '{print $1}')
done

rm -f $current $expected
}

remove_members
Loading
Loading