1
0
mirror of https://github.com/ubuntu/microk8s.git synced 2021-05-23 02:23:41 +03:00
Files
microk8s/microk8s-resources/wrappers/microk8s-add-node.wrapper
2021-03-04 11:04:02 +02:00

79 lines
2.6 KiB
Bash
Executable File

#!/usr/bin/env bash
set -eu
export PATH="$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH"
ARCH="$($SNAP/bin/uname -m)"
export LD_LIBRARY_PATH="$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/$ARCH-linux-gnu:$SNAP/usr/lib/$ARCH-linux-gnu"
export OPENSSL_CONF="/snap/microk8s/current/etc/ssl/openssl.cnf"
export IN_SNAP_LD_LIBRARY_PATH="$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/$ARCH-linux-gnu:$SNAP/usr/lib/$ARCH-linux-gnu"
export PYTHONNOUSERSITE=false
source $SNAP/actions/common/utils.sh
if [ -e ${SNAP_DATA}/var/lock/clustered.lock ]
then
echo "This MicroK8s deployment is acting as a node in a cluster. Please use the microk8s add-node on the master."
exit 1
fi
if echo "$*" | grep -q -- 'help'; then
# Call add_token.py help
LD_LIBRARY_PATH=$IN_SNAP_LD_LIBRARY_PATH ${SNAP}/usr/bin/python3 ${SNAP}/scripts/cluster/add_token.py --help
exit 0
fi
exit_if_no_permissions
subject=$(openssl x509 -sha256 -days 365 -noout -subject -in "$SNAP_DATA/certs/ca.crt")
if [[ $subject == *"127.0.0.1"* ]]; then
echo "Clustering requires a fresh MicroK8s installation. Reinstall with:"
echo "sudo snap remove microk8s"
echo "sudo snap install microk8s --classic"
exit 1
fi
exit_if_stopped
if [ ! -f "$SNAP_DATA/credentials/cluster-tokens.txt" ]; then
touch $SNAP_DATA/credentials/cluster-tokens.txt
fi
if getent group microk8s >/dev/null 2>&1
then
chgrp microk8s $SNAP_DATA/credentials/cluster-tokens.txt || true
fi
# Use python's built-in (3.6+) secrets generator to produce the token.
LD_LIBRARY_PATH=$IN_SNAP_LD_LIBRARY_PATH ${SNAP}/usr/bin/python3 ${SNAP}/scripts/cluster/add_token.py $@
# Get the generated token and display it, so users can use it to join the cluster.
OIFS=$IFS
TOKEN_IN_LINE=$(tail -1 $SNAP_DATA/credentials/cluster-tokens.txt)
IFS='|'
read -ra ADDR <<< "$TOKEN_IN_LINE" # str is read into an array as tokens separated by IFS
token=${ADDR[0]}
IFS=$OIFS
port="25000"
if grep -e port "${SNAP_DATA}"/args/cluster-agent &> /dev/null
then
port=$(cat "${SNAP_DATA}"/args/cluster-agent | "$SNAP"/usr/bin/gawk '{print $2}')
fi
default_ip="$(get_default_ip)"
all_ips="$(get_ips)"
check=$(openssl x509 -in "$SNAP_DATA"/certs/server.crt -outform der | sha256sum | cut -d' ' -f1 | cut -c1-12)
echo "From the node you wish to join to this cluster, run the following:"
echo "microk8s join ${default_ip}:$port/${token}/${check}"
echo ""
echo "If the node you are adding is not reachable through the default interface you can use one of the following:"
for addr in $(echo "${all_ips}"); do
if ! [[ $addr == *":"* ]]; then
echo " microk8s join ${addr}:$port/${token}/${check}"
fi
done