Skip to content

Instantly share code, notes, and snippets.

@rm-rf-etc
Created August 9, 2018 21:59
Show Gist options
  • Save rm-rf-etc/638dfb515857be74c995013b7e3c4d57 to your computer and use it in GitHub Desktop.
Save rm-rf-etc/638dfb515857be74c995013b7e3c4d57 to your computer and use it in GitHub Desktop.
#!/bin/bash
###################
## SAMPLE OUTPUT ##
###################
: <<'END'
Press Ctrl-C to exit at any point
Please enter a name for this cluster: prod1
Use "prod1"? (Y/n): y
What is the IP address of the master node? 159.0.0.0
Use "159.0.0.0"? (Y/n): y
Select a connection port (default is 6443):
Name: prod1
IP: 159.0.0.0
Port: 6443
The following will be done:
| mkdir -p /Users/rob/.kube/prod1
|
| scp -oStrictHostKeyChecking=no \
| root@159.0.0.0:/etc/kubernetes/pki/{apiserver-kubelet-client.key,apiserver-kubelet-client.crt,ca.crt} \
| /Users/rob/.kube/prod1
|
| kubectl config set-cluster prod1 \
| --certificate-authority=/Users/rob/.kube/prod1/ca.crt \
| --server=https://159.0.0.0:6443 \
| --embed-certs=true
|
| kubectl config set-credentials prod1-admin \
| --client-key=/Users/rob/.kube/prod1/apiserver-kubelet-client.key \
| --client-certificate=/Users/rob/.kube/prod1/apiserver-kubelet-client.crt \
| --embed-certs=true
|
| kubectl config set-context prod1 \
| --cluster=prod1 \
| --user=prod1-admin
|
| kubectl config use-context prod1
| kubectl get nodes
"yes" to execute:
END
function promptConfirm () {
local return_var=$2
local input=""
echo
while [ "$input" = "" ]
do
read -p "$1 " input
if [ "$input" = "" ] || [[ $input =~ [[:space:]] ]]
then
input=""
else
read -p "Use \"$input\"? (Y/n): " -n 1 -r
if [[ $REPLY =~ ^[Nn]$ ]]
then
input=""
fi
fi
done
eval $return_var="'$input'"
}
function promptAccept () {
local return_var=$2
local val=""
while [ "$val" = "" ]
do
echo
read -p "$1 " val
val=${val:-6443}
if [[ $val =~ [[:space:]] ]]
then
echo Value cannot contain spaces
val=""
fi
done
eval $return_var="'$val'"
}
function to_int {
local -i num="10#${1}"
echo "${num}"
}
function port_is_ok {
local return_var=$2
local input="$1"
local -i port_num=$(to_int "${input}" 2>/dev/null)
if (( $port_num < 1 || $port_num > 65535 )) ; then
echo Not a valid port number
eval $return_var="''"
else
eval $return_var="'$port_num'"
fi
}
echo "Press Ctrl-C to exit at any point"
promptConfirm "Please enter a name for this cluster:" CLUSTER_NAME
echo
promptConfirm "What is the IP address of the master node?" MASTER_IP
echo
while [ "$PORT" = "" ]
do
promptAccept "Select a connection port (default is 6443):" PORT
if [ "$PORT" = "" ]
then
PORT=6443
fi
port_is_ok $PORT PORT
done
echo
echo Name: $CLUSTER_NAME
echo IP: $MASTER_IP
echo Port: $PORT
echo "
The following will be done:
| mkdir -p $HOME/.kube/$CLUSTER_NAME
|
| scp -oStrictHostKeyChecking=no \\
| root@$(echo "$MASTER_IP"):/etc/kubernetes/pki/{apiserver-kubelet-client.key,apiserver-kubelet-client.crt,ca.crt} \\
| $(echo "$HOME")/.kube/$(echo "$CLUSTER_NAME")
|
| kubectl config set-cluster $(echo "$CLUSTER_NAME") \\
| --certificate-authority=$(echo "$HOME")/.kube/$(echo "$CLUSTER_NAME")/ca.crt \\
| --server=https://$(echo "$MASTER_IP"):$(echo "$PORT") \\
| --embed-certs=true
|
| kubectl config set-credentials $(echo "$CLUSTER_NAME")-admin \\
| --client-key=$(echo "$HOME")/.kube/$(echo "$CLUSTER_NAME")/apiserver-kubelet-client.key \\
| --client-certificate=$(echo "$HOME")/.kube/$(echo "$CLUSTER_NAME")/apiserver-kubelet-client.crt \\
| --embed-certs=true
|
| kubectl config set-context $(echo "$CLUSTER_NAME") \\
| --cluster=$(echo "$CLUSTER_NAME") \\
| --user=$(echo "$CLUSTER_NAME")-admin
|
| kubectl config use-context $(echo "$CLUSTER_NAME")
| kubectl get nodes"
echo
read -p "\"yes\" to execute: " RUN_IT
if [ "$RUN_IT" = "yes" ]
then
mkdir -p $HOME/.kube/$CLUSTER_NAME
scp -oStrictHostKeyChecking=no \
root@$MASTER_IP:/etc/kubernetes/pki/{apiserver-kubelet-client.key,apiserver-kubelet-client.crt,ca.crt} \
$HOME/.kube/$CLUSTER_NAME/
kubectl config set-cluster $CLUSTER_NAME \
--certificate-authority=$HOME/.kube/$CLUSTER_NAME/ca.crt \
--server=https://$MASTER_IP:$PORT \
--embed-certs=true
kubectl config set-credentials $CLUSTER_NAME-admin \
--client-key=$HOME/.kube/$CLUSTER_NAME/apiserver-kubelet-client.key \
--client-certificate=$HOME/.kube/$CLUSTER_NAME/apiserver-kubelet-client.crt \
--embed-certs=true
kubectl config set-context $CLUSTER_NAME \
--cluster=$CLUSTER_NAME \
--user=$CLUSTER_NAME-admin
kubectl config use-context $CLUSTER_NAME
kubectl get nodes
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment