Last active
November 5, 2019 12:24
-
-
Save dukuo/cdbe4aa1cb3e2d795f26d189063999f9 to your computer and use it in GitHub Desktop.
Kubernetes Context Configuration command line utility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
############################################################################################ | |
# | |
# MIT License | |
# | |
# Copyright (c) 2019 Dilip J. Ramírez (https://github.com/dukuo) | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
# | |
############################################################################################ | |
# Config cluster config. To use mainly with minikube. | |
# | |
# Usage: | |
# kb-set-ctx.sh (-c|--cluster-name) CLUSTER_NAME (-i|--ip) IP_ADDRESS (-p|--port) PORT (-d|--config-dir) CONFIG_DIR | |
# Default values | |
SET_CTX=false | |
# Print command help. | |
function print_help() { | |
echo " | |
Kubernetes Context Configuration command utility | |
-- | |
Usage: | |
kb-config-ctx.sh [(-e|--embed-certs) (-s|--set-context)] (-c|--cluster-name) CLUSTER_NAME (-i|--ip) IP_ADDRESS (-p|--port) PORT (-d|--config-dir) CONFIG_DIR | |
Don't forget to `chmod +x kb-config-ctx.sh` for execution permissions. | |
-- | |
Options: | |
Required: | |
-i | --ip IP_ADDRESS : Set cluster server IP_ADDRESS | |
-p | --port PORT : Set cluster server PORT | |
-c | --cluster-name : Set cluster server NAME | |
-d | --config-dir : Set context CONFIGURATION DIRECTORY | |
Optional: | |
-e | --embed-certs : Embed certificates in kube config | |
-s | --set-context : Set the configured context to current context | |
" | |
exit 1 | |
} | |
# Test an IP_ADDRESS address for validity: | |
# source: https://www.linuxjournal.com/content/validating-ip-address-bash-script | |
# Usage: | |
# valid_ip IP_ADDRESS | |
# if [[ $? -eq 0 ]]; then echo good; else echo bad; fi | |
# OR | |
# if valid_ip IP_ADDRESS; then echo good; else echo bad; fi | |
# | |
function valid_ip() | |
{ | |
local ip=$1 | |
local stat=1 | |
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then | |
OIFS=$IFS | |
IFS='.' | |
ip=($ip) | |
IFS=$OIFS | |
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \ | |
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]] | |
stat=$? | |
fi | |
return $stat | |
} | |
# Fetch Options (required) | |
while [ -n "$1" ]; do | |
case "$1" in | |
-i|--ip) | |
if valid_ip $2; then | |
IP_ADDRESS="$2"; | |
else | |
echo "Must enter a valid IP ADDRESS"; | |
exit 1; | |
fi | |
shift | |
;; | |
-p|--port) | |
if [ ! -z $2 ] && [ $2 -gt 0 ]; then | |
PORT="$2" | |
else | |
echo "Must enter a valid PORT" | |
exit 1 | |
fi | |
shift | |
;; | |
-c|--cluster-name) | |
if [ ! -z $2 ]; then | |
CLUSTER_NAME=$2 | |
else | |
echo "Must enter CLUSTER NAME." | |
exit 1 | |
fi | |
shift | |
;; | |
-d|--config-dir) | |
if [ ! -z $2 ]; then | |
CONFIG_DIR=$2 | |
else | |
echo "Enter valid DIRECTORY" | |
exit 1 | |
fi | |
shift | |
;; | |
-h|-help) | |
print_help | |
shift | |
;; | |
-e|--embed) | |
EMBED_CERTS=true | |
shift | |
;; | |
-s|--set-context) | |
SET_CTX=true | |
shift | |
;; | |
""|*) | |
echo "Option not recognized. Use -h or --help to get the available options." | |
print_help | |
shift | |
;; | |
esac | |
shift | |
done | |
#} | |
kube_config() { | |
# 1 = CLUSTER_NAME | 2 = IP_ADDRESS | 3 = PORT | 4 = CONF_DIR | |
if [ "$EMBED_CERTS" = true ] ; then | |
E="--embed-certs" | |
else | |
E="" | |
fi | |
SERVER="https://$IP_ADDRESS:$PORT" | |
CA="$CONFIG_DIR/$CA_CRT" | |
CC="$CONFIG_DIR/$CLIENT_CRT" | |
CK="$CONFIG_DIR/$CLIENT_KEY" | |
# Run kubectl commands (finally) | |
kubectl config set-cluster $CLUSTER_NAME --server=$SERVER --certificate-authority=$CA $E | |
kubectl config set-credentials $CLUSTER_NAME --client-certificate=$CC --client-key=$CK $E | |
if [ "$SET_CTX" = true ] ; then | |
kubectl config set-context $CLUSTER_NAME --cluster=$CLUSTER_NAME --user=$CLUSTER_NAME | |
fi | |
exit 1 | |
} | |
if [ ! -z $CLUSTER_NAME ] && [ ! -z $IP_ADDRESS ] && [ ! -z $PORT ] && [ ! -z $CONFIG_DIR ]; then | |
read -p "Set Certificate Authority Certificate filename (default: ca.crt): " CA_CRT | |
read -p "Set Client Certificate filename (default: client.crt)" CLIENT_CRT | |
read -p "Set Client Key filename (default: client.key)" CLIENT_KEY | |
CA_CRT=${CA_CRT:=ca.crt} | |
CLIENT_CRT=${CLIENT_CRT:=client.crt} | |
CLIENT_KEY=${CLIENT_KEY:=client.key} | |
kube_config | |
else | |
echo "Some options are required." | |
print_help | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment