Refactor to support multiple OS's

Signed-off-by: Marcus Noble <github@marcusnoble.co.uk>
This commit is contained in:
2021-11-29 08:07:17 +00:00
parent 48ad89385b
commit 3990c8fd9c
31 changed files with 1025 additions and 366 deletions

View File

@@ -0,0 +1,76 @@
#!/usr/bin/env bash
set -e
NAMESPACE="org-giantswarm"
RELEASE="20.0.0-alpha1"
PROVIDER="aws"
AZS="eu-west-1a"
print_usage() {
echo "gs-create-cluster - create a Giant Swarm managed workload cluster"
echo " "
echo "gs-create-cluster [cluster-name]"
echo " "
echo " "
echo "Options:"
echo "-h, --help show this help text"
echo "-n, --namespace the namespace the cluster is in (default: org-giantswarm)"
echo "-r, --release the namespace the cluster is in (default: 20.0.0-alpha1)"
echo "-p, --provider the cloud provider to use (default: aws)"
}
while test $# -gt 0; do
case "$1" in
-n|--namespace)
shift
NAMESPACE=$1
shift
;;
-r|--release)
shift
RELEASE=$1
shift
;;
-p|--provider)
shift
PROVIDER=$1
shift
;;
-h|--help)
print_usage
exit 0
;;
*)
break
;;
esac
done
# Positional args
NAME=${1:-wc001}
PREFIXED_NAMESPACE="org-$NAMESPACE"
case $NAMESPACE in org-*)
PREFIXED_NAMESPACE="$NAMESPACE"
NAMESPACE=${NAMESPACE#"org-"}
esac
echo "✨ Pre-flight checks"
gs-get-cluster --namespace ${PREFIXED_NAMESPACE} ${NAME} &>/dev/null
if [ $? -eq 0 ]; then
echo "Cluster named '${NAME}' already exists"
exit 1
else
echo "Cleaning up any old awsclusterroleidentities..."
kubectl get --namespace ${PREFIXED_NAMESPACE} awsclusterroleidentities ${NAME} >/dev/null && kubectl delete --namespace ${PREFIXED_NAMESPACE} awsclusterroleidentities ${NAME}
fi
echo "✨ Creating an ${PROVIDER} cluster called '${NAMESPACE}/${NAME}' with release '${RELEASE}'"
kubectl-gs template cluster --provider ${PROVIDER} --release ${RELEASE} --organization ${NAMESPACE} --name ${NAME} --description "$(whoami)'s test cluster" | kubectl apply -f -
echo "✨ Adding node pool to cluster"
kubectl-gs template nodepool --provider ${PROVIDER} --release ${RELEASE} --organization ${NAMESPACE} --cluster-name ${NAME} --description "$(whoami)'s test cluster" --availability-zones ${AZS} | kubectl apply -f -
echo "✨ Checking status..."
gs-get-cluster --namespace ${PREFIXED_NAMESPACE} ${NAME}

35
home/.bin/gs-get-cluster Normal file
View File

@@ -0,0 +1,35 @@
#!/usr/bin/env bash
set -e
NAMESPACE="org-giantswarm"
print_usage() {
echo "gs-get-cluster - get a Giant Swarm managed workload cluster"
echo " "
echo "gs-get-cluster [cluster-name]"
echo " "
echo " "
echo "Options:"
echo "-h, --help show this help text"
echo "-n, --namespace the namespace the cluster is in (default: org-giantswarm)"
}
while test $# -gt 0; do
case "$1" in
-n|--namespace)
shift
NAMESPACE=$1
shift
;;
-h|--help)
print_usage
exit 0
;;
*)
break
;;
esac
done
kubectl-gs get cluster --namespace $NAMESPACE $@ 2>/dev/null || kubectl get cl --namespace $NAMESPACE $@

59
home/.bin/gs-login Normal file
View File

@@ -0,0 +1,59 @@
#!/usr/bin/env bash
set -e
TTL="24h"
CERTIFICATE_GROUP="system:masters"
print_usage() {
echo "gs-login - login to Giant Swarm managed clusters"
echo " "
echo "gs-login [INSTALLATION] [WORKLOAD CLUSTER] [ORGANISATION]"
echo " "
echo "Examples:"
echo "> gs-login gauss"
echo "> gs-login gauss mywc1"
echo " "
echo "Options:"
echo "-h, --help show this help text"
echo "-t, --ttl the certificate ttl for the workload cluster login (default: 24h)"
echo "-g, --certificate-group the certificate group to login as on the workload cluster (default: system:masters)"
}
while test $# -gt 0; do
case "$1" in
-t|--ttl)
shift
TTL=$1
shift
;;
-g|--certificate-group)
shift
CERTIFICATE_GROUP=$1
shift
;;
-h|--help)
print_usage
exit 0
;;
*)
break
;;
esac
done
case $# in
1)
kubectl gs login $1 2>/dev/null || opsctl kgs login -i $1
;;
2)
kubectl gs login $1 --workload-cluster $2 --certificate-group system:masters --certificate-ttl 24h
;;
3)
kubectl gs login $1 --workload-cluster $2 --certificate-group system:masters --certificate-ttl 24h --organization $3
;;
*)
print_usage
exit 1
;;
esac

103
home/.bin/gs-release Normal file
View File

@@ -0,0 +1,103 @@
#!/usr/bin/env bash
set -e
print_usage() {
echo "gs-release - create a new release of a Giant Swarm repo"
echo " "
echo "gs-release [SEMVER LEVEL]"
echo " "
echo " "
echo "Options:"
echo "-h, --help show this help text"
}
while test $# -gt 0; do
case "$1" in
-h|--help)
print_usage
exit 0
;;
*)
break
;;
esac
done
SEMVER=$1
CURRENT_TAG=$(git describe --tags --abbrev=0 2>/dev/null)
MAIN_BRANCH=$(git remote show origin 2>/dev/null|grep HEAD|sed 's/.* //')
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
if [ "$MAIN_BRANCH" != "$CURRENT_BRANCH" ]; then
echo "Not currently on main branch, please switch to ${MAIN_BRANCH} to perform a release"
exit 1
fi
VERSION_PARTS=($(echo $CURRENT_TAG | tr "." "\n"))
VERSION_MAJOR=${VERSION_PARTS[1]}
VERSION_MINOR=${VERSION_PARTS[2]}
VERSION_PATCH=${VERSION_PARTS[3]}
echo "The latest released version is ${CURRENT_TAG}"
echo ""
if [[ "$SEMVER" == "" ]]; then
printf "What semver release level? (patch, minor or major): "
read SEMVER
fi
case ${SEMVER} in
patch)
VERSION_PATCH=$((VERSION_PATCH+1))
;;
minor)
VERSION_MINOR=$((VERSION_MINOR+1))
VERSION_PATCH=0
;;
major)
if [[ ${VERSION_MAJOR:0:1} == "v" ]]; then
VERSION_MAJOR="v$((VERSION_MAJOR+1))"
else
VERSION_MAJOR=$((VERSION_MAJOR+1))
fi
VERSION_MINOR=0
VERSION_PATCH=0
;;
*)
echo "Unknown Semver level provided"
exit 1
;;
esac
NEW_VERSION="${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}"
NEW_BRANCH="${MAIN_BRANCH}#release#${NEW_VERSION}"
echo ""
echo "✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ "
echo "Current version ${CURRENT_TAG}"
echo " New version ${NEW_VERSION}"
echo " Release branch ${NEW_BRANCH}"
echo "✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ "
echo ""
printf "Confirm? (y/n): "
read CONFIRM
if [ "${CONFIRM}" = "y" ]; then
echo "Publishing new release branch..."
git checkout -b "${NEW_BRANCH}"
git push -u origin "${NEW_BRANCH}"
ORG_NAME=$(git remote get-url origin | sed 's/.*github.com[:|\/]//' | sed 's/\.git$//' | tr '/' ' ' | awk '{print $1}')
REPO_NAME=$(git remote get-url origin | sed 's/.*github.com[:|\/]//' | sed 's/\.git$//' | tr '/' ' ' | awk '{print $2}')
echo "🚀 Keep an eye on https://github.com/${ORG_NAME}/${REPO_NAME}/pulls for the new release PR"
else
echo "Aborting..."
exit 1
fi

57
home/.bin/kube-all Normal file
View File

@@ -0,0 +1,57 @@
#!/usr/bin/env bash
set -e
NAMESPACE="$(kubectl config view --minify --output 'jsonpath={..namespace}')"
LABEL=""
ALL_NAMESPACES=false
print_usage() {
echo "kube-all - A better 'kubectl get all' - actually get all Kubernetes resources, including custom resources"
echo " "
echo "kube-all [options]"
echo " "
echo "Options:"
echo "-h, --help show this help text"
echo "-n, --namespace the namespace the to search in"
echo "-l, --selector the label selector to match on"
echo "-A, --all-namespaces match resources in all namespaces (default: false)"
}
while test $# -gt 0; do
case "$1" in
-n|--namespace)
shift
NAMESPACE=$1
shift
;;
-l|--selector)
shift
LABEL=$1
shift
;;
-A|--all-namespaces)
ALL_NAMESPACES=true
shift
;;
-h|--help)
print_usage
exit 0
;;
*)
break
;;
esac
done
if [[ "${LABEL}" != "" ]]; then
LABEL="-l ${LABEL}"
fi
NAMES="$(kubectl api-resources --namespaced --verbs list -o name | tr '\n' ,)"
if [ $ALL_NAMESPACES ]; then
kubectl get "${NAMES:0:-1}" --show-kind --ignore-not-found ${LABEL} -A
else
kubectl get "${NAMES:0:-1}" --show-kind --ignore-not-found ${LABEL} -n ${NAMESPACE}
fi

View File

@@ -0,0 +1,59 @@
#!/usr/bin/env bash
set -e
NAMESPACE="$(kubectl config view --minify --output 'jsonpath={..namespace}')"
LABEL=""
ALL_NAMESPACES=false
print_usage() {
echo "kube-clean-replicasets - Remove all olf ReplicaSets with 0 desired pods"
echo " "
echo "kube-clean-replicasets [options]"
echo " "
echo "Options:"
echo "-h, --help show this help text"
echo "-n, --namespace the namespace the to search in"
echo "-l, --selector the label selector to match on"
echo "-A, --all-namespaces match resources in all namespaces (default: false)"
}
while test $# -gt 0; do
case "$1" in
-n|--namespace)
shift
NAMESPACE=$1
shift
;;
-l|--selector)
shift
LABEL=$1
shift
;;
-A|--all-namespaces)
ALL_NAMESPACES=true
shift
;;
-h|--help)
print_usage
exit 0
;;
*)
break
;;
esac
done
if [[ "${LABEL}" != "" ]]; then
LABEL="-l ${LABEL}"
fi
if [ $ALL_NAMESPACES ]; then
kubectl get replicasets --all-namespaces $LABEL -o jsonpath='{range .items[?(@.status.replicas==0)]}--namespace {@.metadata.namespace} {@.metadata.name};{end}' | \
tr ";" "\n" | \
xargs -I {} sh -c "kubectl delete rs {}"
else
kubectl get replicasets --namespace $NAMESPACE $LABEL -o jsonpath='{range .items[?(@.status.replicas==0)]}--namespace {@.metadata.namespace} {@.metadata.name};{end}' | \
tr ";" "\n" | \
xargs -I {} sh -c "kubectl delete rs {}"
fi

62
home/.bin/kube-exec Normal file
View File

@@ -0,0 +1,62 @@
#!/usr/bin/env bash
set -e
NAMESPACE="$(kubectl config view --minify --output 'jsonpath={..namespace}')"
POD=""
CMD="sh"
print_usage() {
echo "kube-exec - execute commands within a pod"
echo " "
echo "kube-exec [options]"
echo " "
echo "Options:"
echo "-h, --help show this help text"
echo "-n, --namespace the namespace the pod is in"
echo "-p, --pod the name of the pod"
echo "-c, --command the command to run in the pod (default: sh)"
}
while test $# -gt 0; do
case "$1" in
-n|--namespace)
shift
NAMESPACE=$1
shift
;;
-p|--pod)
shift
POD=$1
shift
;;
-c|--command)
shift
CMD=$1
shift
;;
-h|--help)
print_usage
exit 0
;;
*)
break
;;
esac
done
if [[ "$POD" == "" ]]; then
which fzf &>/dev/null || (
echo "If no pod provided, fzf is required to select pods"
echo ""
print_usage
exit 1
)
pod=($(kubectl get pods --all-namespaces -owide | fzf | awk '{print $1, $2}'))
POD=$pod[1]
NAMESPACE=$pod[0]
fi
echo kubectl exec -it --namespace $NAMESPACE $POD $CMD
kubectl exec -it --namespace $NAMESPACE $POD $CMD

View File

@@ -0,0 +1,86 @@
#!/usr/bin/env bash
set -e
NAMESPACE="$(kubectl config view --minify --output 'jsonpath={..namespace}')"
HOST_PORT=10001
print_usage() {
echo "kube-forward-all - create port-forwards for all pods in the given namespace"
echo " "
echo "kube-forward-all [options]"
echo " "
echo "Options:"
echo "-h, --help show this help text"
echo "-n, --namespace the namespace to launch the pod in"
echo "-p, --port the port to start at (and increment from for each service) (default: 10001)"
}
while test $# -gt 0; do
case "$1" in
-n|--namespace)
shift
NAMESPACE=$1
shift
;;
-p|--port)
shift
HOST_PORT=$1
shift
;;
-h|--help)
print_usage
exit 0
;;
*)
break
;;
esac
done
# Get all services first
IFS=$'\n'
SERVICES=( $(kubectl get service --namespace ${NAMESPACE} --no-headers -o json | jq '[.items[] | select(.metadata.annotations."kube-forward" != "false")] | (.[] | .metadata.name + "\t" + ([.spec.ports[].port] | join(",")))' -r | column -t) )
unset IFS
# Track the port-forwards we need to clean up
TO_KILL=()
cleanup() {
echo "\nClosing connections..."
for pid in "${TO_KILL[@]}"
do
(kill -2 $pid) &> /dev/null
done
trap - INT TERM
}
trap 'cleanup' INT TERM
echo "Forwarding..."
for s in "${SERVICES[@]}"
do
SERVICE=( $(echo $s) )
NAME=${SERVICE[0]}
PORT=${SERVICE[1]}
PORTS=($(echo $PORT | tr "," "\n"))
for PORT in "${PORTS[@]}"
do
(kubectl port-forward --namespace ${NAMESPACE} svc/$NAME $HOST_PORT:$PORT &>/dev/null) &
BG_PID=$!
if `curl -s -o /dev/null --retry 5 --retry-delay 0 --retry-connrefused -m 3 http://localhost:$HOST_PORT`
then
echo "\e[1m$NAME:$PORT\e[0m ➡ \e[34mhttp://localhost:$HOST_PORT\e[0m"
TO_KILL+=($BG_PID)
((HOST_PORT=HOST_PORT+1))
else
(kill -2 $BG_PID) &> /dev/null
fi
done
done
echo "\n\e[2m(Use [Ctl + C] to exit)"
cat
cleanup

62
home/.bin/kube-logs Normal file
View File

@@ -0,0 +1,62 @@
#!/usr/bin/env bash
set -e
NAMESPACE="$(kubectl config view --minify --output 'jsonpath={..namespace}')"
POD=""
ARGS=""
print_usage() {
echo "kube-logs - tail logs from a pod"
echo " "
echo "kube-logs [options]"
echo " "
echo "Options:"
echo "-h, --help show this help text"
echo "-n, --namespace the namespace the pod is in"
echo "-p, --pod the name of the pod to get logs for"
echo "-a, --args additional arguments to pass to kubectl logs command"
}
while test $# -gt 0; do
case "$1" in
-n|--namespace)
shift
NAMESPACE=$1
shift
;;
-p|--pod)
shift
POD=$1
shift
;;
-a|--args)
shift
ARGS=$1
shift
;;
-h|--help)
print_usage
exit 0
;;
*)
break
;;
esac
done
if [[ "$POD" == "" ]]; then
which fzf &>/dev/null || (
echo "If no pod provided, fzf is required to select pods"
echo ""
print_usage
exit 1
)
pod=($(kubectl get pods --all-namespaces -owide | fzf | awk '{print $1, $2}'))
POD=$pod[1]
NAMESPACE=$pod[0]
fi
echo kubectl logs -f $ARGS --namespace $NAMESPACE $POD
kubectl logs -f $ARGS --namespace $NAMESPACE $POD

57
home/.bin/kube-shell Normal file
View File

@@ -0,0 +1,57 @@
#!/usr/bin/env bash
set -e
NAMESPACE="$(kubectl config view --minify --output 'jsonpath={..namespace}')"
POD="shell"
IMAGE="bash"
CMD="sh"
print_usage() {
echo "kube-shell - create a new pod and exec into it's shell"
echo " "
echo "kube-shell [options]"
echo " "
echo "Options:"
echo "-h, --help show this help text"
echo "-n, --namespace the namespace the pod should launch in"
echo "-p, --pod the name of the pod to get logs for (default: shell)"
echo "-i, --image the image to use for the shell container (default: bash)"
echo "-c, --command the initial command to execute in the container (default: sh)"
}
while test $# -gt 0; do
case "$1" in
-n|--namespace)
shift
NAMESPACE=$1
shift
;;
-p|--pod)
shift
POD=$1
shift
;;
-i|--image)
shift
IMAGE=$1
shift
;;
-c|--command)
shift
CMD=$1
shift
;;
-h|--help)
print_usage
exit 0
;;
*)
break
;;
esac
done
echo kubectl run -it --namespace $NAMESPACE $POD --image $IMAGE --restart Never --rm -- $CMD
kubectl run -it --namespace $NAMESPACE $POD --image $IMAGE --restart Never --rm -- $CMD

91
home/.bin/kube-ssh Normal file
View File

@@ -0,0 +1,91 @@
#!/usr/bin/env bash
set -e
NAMESPACE="$(kubectl config view --minify --output 'jsonpath={..namespace}')"
POD="kube-ssh"
NODE=""
print_usage() {
echo "kube-ssh - gain access to a Kubernetes host node (ssh-like for when a host doesn't have ssh)"
echo " "
echo "kube-ssh [options]"
echo " "
echo "Options:"
echo "-h, --help show this help text"
echo "-n, --namespace the namespace to launch the pod in"
echo "-p, --pod the name of the pod to launch (default: kube-ssh)"
echo "-N, --node the name of the node to access"
}
while test $# -gt 0; do
case "$1" in
-n|--namespace)
shift
NAMESPACE=$1
shift
;;
-p|--pod)
shift
POD=$1
shift
;;
-N|--node)
shift
NODE=$1
shift
;;
-h|--help)
print_usage
exit 0
;;
*)
break
;;
esac
done
if [[ "$NODE" == "" ]]; then
NODES=$(kubectl get nodes --no-headers -o custom-columns=name:.metadata.name)
if [ -z "$(which fzf)" ]; then
i=0
while read -r node; do
echo "[$i] - $node"
i=$((i+1))
done <<< "$NODES"
read -p "Which node would you like to connect to? " -r
echo ""
IFS=$'\n' NODES=($NODES)
NODE=${NODES[$REPLY]}
else
NODE=$(echo "$NODES" | fzf)
fi
fi
NODE_NAME=$(kubectl get node $NODE -o template --template='{{index .metadata.labels "kubernetes.io/hostname"}}')
NODE_SELECTOR='"nodeSelector": { "kubernetes.io/hostname": "'${NODE_NAME}'" },'
kubectl run --namespace ${NAMESPACE} $POD --restart=Never -it --rm --image overriden --overrides '
{
"spec": {
"hostPID": true,
"hostNetwork": true,
'"${NODE_SELECTOR}"'
"tolerations": [{
"operator": "Exists"
}],
"containers": [
{
"name": "kube-ssh",
"image": "averagemarcus/kube-ssh:latest",
"stdin": true,
"tty": true,
"securityContext": {
"privileged": true
}
}
]
}
}' --attach