Skip to content

Commit

Permalink
sriov provider, etcd in memory: create unique directory
Browse files Browse the repository at this point in the history
In order to prevent leftovers and overwriting existing data
esopecially on local environments it is necessary to create
directory for ectd data with unique name.

This commit presents '$BASE_ETCD_DATA_DIR' env variable
it is the path for the directory that will be mounted.

Now '$ETCD_DATA_DIR' env var is the current cluster etcd data
directory unique name.
The uniqness of the name is defined by concating
then number of seconds since 1970-01-01 to the cluster name.

The function 'ensure_memory_dir_for_etcd_data' creates
and mounts the  directory 'BASE_ETCD_DATA_DIR'
(e.g: /mnt/cluster-etcd).
Then creates 'BASE_ETCD_DATA_DIR/ETCD_DATA_DIR'
(e.g: /mnt/cluster-etcd/sriov-123211), which is actually on
the RAM memory at this point,it means that it will be disposed
once '$BASE_ETCD_DATA_DIR' (/mnt/cluster-etcd) is unmounted.

The function 'cleanup_dir_for_etcd_data' unmount and deletes
'$BASE_ETCD_DATA_DIR' (which containers '$ETCD_DATA_DIR').

Signed-off-by: Or Mergi <[email protected]>
  • Loading branch information
ormergi committed Nov 18, 2020
1 parent e2bb958 commit 549396c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
4 changes: 3 additions & 1 deletion cluster-up/cluster/kind-k8s-sriov-1.17.0/provider.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ set -e
export CLUSTER_NAME="sriov"
export KIND_NODE_IMAGE="kindest/node:v1.17.0"

export BASE_ETCD_DATA_DIR="/mnt/kind-cluster-etcd"
export ETCD_DATA_DIR="$CLUSTER_NAME-$(date +%s)"

source ${KUBEVIRTCI_PATH}/cluster/kind/common.sh

function up() {
Expand All @@ -19,7 +22,6 @@ function up() {

cp $KIND_MANIFESTS_DIR/kind.yaml ${KUBEVIRTCI_CONFIG_PATH}/$KUBEVIRT_PROVIDER/kind.yaml

export ETCD_DATA_DIR="/mnt/etcd"
ensure_memory_dir_for_etcd_data

kind_up
Expand Down
60 changes: 38 additions & 22 deletions cluster-up/cluster/kind/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ REGISTRY_NAME=${CLUSTER_NAME}-registry
MASTER_NODES_PATTERN="control-plane"
WORKER_NODES_PATTERN="worker"

BASE_ETCD_DATA_DIR=${BASE_ETCD_DATA_DIR:-none}
ETCD_DATA_DIR=${ETCD_DATA_DIR:-none}

function _wait_kind_up {
Expand Down Expand Up @@ -195,40 +196,55 @@ function setup_kind() {
}

function ensure_memory_dir_for_etcd_data() {
if [ "$BASE_ETCD_DATA_DIR" == none ];then
echo "BASE_ETCD_DATA_DIR is not defined"
exit 1
fi
if [ "$ETCD_DATA_DIR" == none ];then
echo "ETCD_DATA_DIR is not defined"
exit 1
fi

trap cleanup_dir_for_etcd_data ERR SIGINT SIGTERM

if grep -qs $ETCD_DATA_DIR /proc/mounts; then
echo "etcd data mount already exists"
df -h $ETCD_DATA_DIR
if [ -d "$ETCD_DATA_DIR" ];then
if grep -qs $BASE_ETCD_DATA_DIR /proc/mounts; then
echo "etcd data, base directory mount already exists"
df -h $BASE_ETCD_DATA_DIR
if [ -d "$BASE_ETCD_DATA_DIR" ];then
echo "ectd data directory already exists, cleaning up directory content..."
rm -rf $ETCD_DATA_DIR/*
df -h $ETCD_DATA_DIR
rm -rf $BASE_ETCD_DATA_DIR/*
df -h $BASE_ETCD_DATA_DIR
fi
else
echo "creating directory for ectd data"
mkdir -p $ETCD_DATA_DIR
[ "$(echo $?)" != 0 ] && echo "failed to create etcd data directory" && exit 1
ls -lsah $ETCD_DATA_DIR
echo "Creating cluster etcd data base directory: $BASE_ETCD_DATA_DIR"
mkdir -p $BASE_ETCD_DATA_DIR

echo "mount etcd data directory as tempfs"
mount -t tmpfs tmpfs $ETCD_DATA_DIR
echo "Mount etcd data base directory to memory: $BASE_ETCD_DATA_DIR"
mount -t tmpfs -o size=1G tmpfs $BASE_ETCD_DATA_DIR
[ "$(echo $?)" != 0 ] && echo "failed to mount etcd data directory" && exit 1
df -h $ETCD_DATA_DIR
df -h $BASE_ETCD_DATA_DIR

current_cluster_etcd_data_dir="$BASE_ETCD_DATA_DIR/$ETCD_DATA_DIR"
echo "Creating directory for current cluster etcd data: $current_cluster_etcd_data_dir"
mkdir -p $current_cluster_etcd_data_dir
[ "$(echo $?)" != 0 ] && echo "failed to create etcd data directory" && exit 1
ls -lsah $current_cluster_etcd_data_dir
fi
}

function cleanup_dir_for_etcd_data() {
if grep -qs $ETCD_DATA_DIR /proc/mounts; then
umount -f $ETCD_DATA_DIR &> 1
[ "$(echo $?)" == 0 ] && echo "etcd data directory umounted"
if grep -qs $BASE_ETCD_DATA_DIR /proc/mounts; then
echo "Unmounting etcd data base directory: $BASE_ETCD_DATA_DIR"
df -h $BASE_ETCD_DATA_DIR
umount -f $BASE_ETCD_DATA_DIR &> 1
[ "$(echo $?)" == 0 ] && echo "etcd data base directory umounted"
fi
if [ -d "$ETCD_DATA_DIR" ];then
rm -rf $ETCD_DATA_DIR
echo "etcd data directory deleted"

if [ -d "$BASE_ETCD_DATA_DIR" ];then
echo "Removing etcd current cluster data directory: $BASE_ETCD_DATA_DIR"
rm -rf $BASE_ETCD_DATA_DIR
[ "$(echo $?)" == 0 ] && echo "etcd data base directory deleted"

fi
}

Expand Down Expand Up @@ -272,11 +288,11 @@ EOF

function _add_control_plane_extra_mounts() {
if [[ "$KUBEVIRT_PROVIDER" =~ sriov.* ]]; then
if [ "$ETCD_DATA_DIR" != none ]; then
if [ -d "$BASE_ETCD_DATA_DIR/$ETCD_DATA_DIR" ]; then
cat <<EOF >> ${KUBEVIRTCI_CONFIG_PATH}/$KUBEVIRT_PROVIDER/kind.yaml
extraMounts:
- containerPath: /var/lib/etcd
hostPath: $ETCD_DATA_DIR
hostPath: $BASE_ETCD_DATA_DIR/$ETCD_DATA_DIR
EOF
fi
fi
Expand Down Expand Up @@ -308,7 +324,7 @@ function down() {
$KIND delete cluster --name=${CLUSTER_NAME}
rm -f ${KUBEVIRTCI_CONFIG_PATH}/$KUBEVIRT_PROVIDER/kind.yaml

if [ "$ETCD_DATA_DIR" != none ];then
if [ "$ETCD_DATA_DIR" != none ] && [ "$BASE_ETCD_DATA_DIR" != none ];then
cleanup_dir_for_etcd_data
fi
}

0 comments on commit 549396c

Please sign in to comment.