Refactor to support multiple OS's

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

View File

@ -1,24 +0,0 @@
alias aws='docker pull -q amazon/aws-cli:latest 1> /dev/null && docker run --rm -i -v ~/.aws:/root/.aws -v $(pwd):/aws -e NO_COLOR=true -e AWS_PROFILE -e AWS_DEFAULT_REGION -e AWS_REGION -e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY -e AWS_SESSION_TOKEN amazon/aws-cli'
assume-role() {
ROLE=$1
if [[ -z "$ROLE" ]]; then
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
export AWS_SESSION_TOKEN=
else
OUTPUT=$(aws sts assume-role --role-arn $ROLE --role-session-name assumed-role-session --query Credentials --output json)
export AWS_ACCESS_KEY_ID=$(echo $OUTPUT | jq -r .AccessKeyId)
export AWS_SECRET_ACCESS_KEY=$(echo $OUTPUT | jq -r .SecretAccessKey)
export AWS_SESSION_TOKEN=$(echo $OUTPUT | jq -r .SessionToken)
fi
aws sts get-caller-identity
}
aws-decode-message() {
ENCODED=$1
aws sts decode-authorization-message --query DecodedMessage --output text --encoded-message $ENCODED | jq -r '.context.action, .context.resource'
}

View File

@ -1,198 +0,0 @@
alias k='kubectl '
alias kshell='kubectl run -it shell --image bash --restart Never --rm -- sh'
kube-ssh() {
sh -c "$(curl -sSL https://raw.githubusercontent.com/AverageMarcus/kube-ssh/master/ssh.sh)"
}
kube-forward() {
if [ -n "$ZSH_VERSION" ]; then
setopt LOCAL_OPTIONS NO_NOTIFY NO_MONITOR
fi
IFS=$'\n'
SERVICES=( $(kubectl get service --no-headers -o json | jq '[.items[] | select(.metadata.annotations."kube-forward" != "false")] | (.[] | .metadata.name + "\t" + ([.spec.ports[].port] | join(",")))' -r | column -t) )
unset IFS
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
HOST_PORT=9001
echo "Forwarding..."
for s in "${SERVICES[@]}"
do
SERVICE=( $(echo $s) )
if [ -n "$ZSH_VERSION" ]; then
NAME=${SERVICE[1]}
PORT=${SERVICE[2]}
else
NAME=${SERVICE[0]}
PORT=${SERVICE[1]}
fi
PORTS=($(echo $PORT | tr "," "\n"))
for PORT in "${PORTS[@]}"
do
(kubectl port-forward svc/$NAME $HOST_PORT:$PORT > /dev/null 2>&1) &
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
if [ -n "$BASH_VERSION" ]; then
cleanup
fi
}
source <(kubectl completion zsh)
## Kubectl exec
kx () {
local pod=($(kubectl get pods --all-namespaces -owide | fzf | awk '{print $1, $2}'))
local cmd=${@:-"sh"}
echo kubectl exec -it --namespace $pod[1] $pod[2] $cmd
kubectl exec -it --namespace $pod[1] $pod[2] $cmd
}
## Kubectl logs
kl () {
local pod=($(kubectl get pods --all-namespaces -owide | fzf | awk '{print $1, $2}'))
local attr=${@:-""}
echo kubectl logs -f $attr --namespace $pod[1] $pod[2]
kubectl logs -f $attr --namespace $pod[1] $pod[2]
}
## Display everything
kall() {
NAMESPACE=""
LABEL=""
ALL_NAMESPACES=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)
echo "kall - get all Kubernetes resources matching a given label selector"
echo " "
echo "kall [options]"
echo " "
echo "Options:"
echo "-h, --help show this help text"
echo "-n, --namespace the namespace to check against"
echo "-l, --selector the label selector to match on"
echo "-A, --all-namespaces search all namespaces"
return 0
;;
*)
break
;;
esac
done
if [[ "${LABEL}" == "" ]]; then
echo "Please provide a label selector to match on"
return 1
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 -l ${LABEL} -A
else
kubectl get "${NAMES:0:-1}" --show-kind --ignore-not-found -l ${LABEL} -n ${NAMESPACE}
fi
}
k-version-test() {
VERSION=""
FILES=""
while test $# -gt 0; do
case "$1" in
-v|--version)
shift
VERSION=$1
shift
;;
-f|--files)
shift
FILES=$1
shift
;;
-h|--help)
echo "k-version-test - test Kubernetes manifest files against different versions of Kubernetes"
echo " "
echo "k-version-test [options]"
echo " "
echo "Options:"
echo "-h, --help show this help text"
echo "-v, --version the version of kubernetes to test against"
echo "-f, --files the manifest file(s) to test against"
return 0
;;
*)
break
;;
esac
done
which kind &> /dev/null || (echo "'kind' not installed. Follow install instructions here: https://github.com/kubernetes-sigs/kind/"; return 1)
if [ ! -z $VERSION ];
then
TAG=$(curl -s 'https://registry.hub.docker.com/v2/repositories/kindest/node/tags/' | jq '."results" | map(.name | select(startswith("v'$VERSION'"))) | .[0]' | xargs)
kind create cluster --image kindest/node:$TAG
else
kind create cluster
fi
echo
if kubectl apply --dry-run -f $FILES ; then
echo "\n☸ Kubernetes $VERSION. Result: ✅\n"
else
echo "\n☸ Kubernetes $VERSION. Result: ❌\n"
fi
kind delete cluster
}
fix-broken-replicasets() {
kubectl get replicasets --all-namespaces -o jsonpath='{range .items[?(@.status.replicas==0)]}--namespace {@.metadata.namespace} {@.metadata.name};{end}' | tr ";" "\n" | xargs -I {} sh -c "kubectl delete rs {}"
}
source <(tkn completion zsh)

13
.zshrc
View File

@ -1,13 +0,0 @@
if [ ! -z ~/.additional_dotfiles/credentials ]; then
source ~/.additional_dotfiles/credentials
fi
for filename in ~/.dotfiles/*; do
source $filename
done
for filename in ~/.additional_dotfiles/*; do
source $filename
done
eval "$(starship init zsh)"

114
Makefile
View File

@ -1,118 +1,8 @@
SHELL := bash
.PHONY: Install
install: pre-reqs dotfiles ## Installs all dotfiles and associated.
.PHONY: pre-reqs
pre-reqs: ## Install all required binaries.
which brew > /dev/null || bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"; \
[ -d ~/.oh-my-zsh ] || sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"; \
brew list --cask homebrew/cask-fonts/font-open-dyslexic-nerd-font > /dev/null || brew install homebrew/cask-fonts/font-open-dyslexic-nerd-font; \
brew list rust > /dev/null || brew install rust; \
which rustup > /dev/null || curl https://sh.rustup.rs -sSf | sh -s -- -y --no-modify-path; \
which fzf > /dev/null || brew install fzf; \
which bat > /dev/null || brew install bat; \
which curlie > /dev/null || brew install rs/tap/curlie; \
which exa > /dev/null || brew install exa; \
which kubectl > /dev/null || brew install kubectl; \
which tkn > /dev/null || brew install tektoncd/tools/tektoncd-cli; \
which k9s > /dev/null || brew install k9s; \
which helm > /dev/null || brew install helm; \
which go > /dev/null || brew install go; \
which jq > /dev/null || brew install jq; \
which kind > /dev/null || brew install kind; \
which kubectx > /dev/null || brew install kubectx; \
which tldr > /dev/null || brew install tldr; \
which progress > /dev/null || brew install progress; \
which htop > /dev/null || brew install htop; \
which starship > /dev/null || brew install starship; \
which macchina > /dev/null || cargo install macchina; \
which rg > /dev/null || brew install ripgrep; \
which delta > /dev/null || brew install git-delta; \
which dust > /dev/null || brew install dust; \
which duf > /dev/null || brew install duf; \
which fd > /dev/null || brew install fd; \
which bandwhich > /dev/null || brew install bandwhich; \
which btm > /dev/null || cargo install bottom; \
which procs > /dev/null || brew install procs; \
which dog > /dev/null || brew install dog; \
which delta > /dev/null || brew install git-delta; \
which switcher > /dev/null || brew install danielfoehrkn/switch/switch; \
which pinentry-mac > dev/null || brew install pinentry-mac; \
brew install gpg gawk coreutils; \
git clone https://github.com/zsh-users/zsh-autosuggestions $${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions; \
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting; \
RUST_WITHOUT=rust-docs asdf plugin-add rust https://github.com/asdf-community/asdf-rust.git;
.PHONY: upgrade
upgrade: ## Upgrade all required binaries.
brew list --cask homebrew/cask-fonts/font-open-dyslexic-nerd-font > /dev/null && brew upgrade homebrew/cask-fonts/font-open-dyslexic-nerd-font; \
brew list rust > /dev/null || brew upgrade rust; \
which rustup > /dev/null || rustup update; \
which fzf > /dev/null && brew upgrade fzf; \
which bat > /dev/null && brew upgrade bat; \
which curlie > /dev/null && brew upgrade rs/tap/curlie; \
which exa > /dev/null && brew upgrade exa; \
which kubectl > /dev/null && brew upgrade kubectl; \
which tkn > /dev/null && brew upgrade tektoncd/tools/tektoncd-cli; \
which k9s > /dev/null && brew upgrade k9s; \
which helm > /dev/null && brew upgrade helm; \
which go > /dev/null && brew upgrade go; \
which jq > /dev/null && brew upgrade jq; \
which kind > /dev/null && brew upgrade kind; \
which kubectx > /dev/null && brew upgrade kubectx; \
which tldr > /dev/null && brew upgrade tldr; \
which progress > /dev/null && brew upgrade progress; \
which htop > /dev/null && brew upgrade htop; \
which starship > /dev/null && brew upgrade starship; \
which macchina > /dev/null && cargo install macchina; \
which rg > /dev/null && brew upgrade ripgrep; \
which delta > /dev/null && brew upgrade git-delta; \
which dust > /dev/null && brew upgrade dust; \
which duf > /dev/null && brew upgrade duf; \
which fd > /dev/null && brew upgrade fd; \
which bandwhich > /dev/null && brew upgrade bandwhich; \
which btm > /dev/null && cargo install bottom; \
which procs > /dev/null && brew upgrade procs; \
which dog > /dev/null && brew upgrade dog; \
which delta > /dev/null && brew upgrade git-delta; \
which switcher > /dev/null && brew upgrade switch; \
which pinentry-mac > dev/null && brew upgrade pinentry-mac; \
cd $${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions && git pull && cd -; \
cd $${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting && git pull && cd -; \
cd ~/.oh-my-zsh && git pull && cd -; \
npm install -g git-split-diffs;
.PHONY: dotfiles
dotfiles: ## Installs the dotfiles.
@GITEMAIL=$$(git config --get user.email); \
for file in $(shell find $(CURDIR) -name ".*" -not -name ".gitignore" -not -name ".git" -not -name ".config" -not -name ".k9s" -not -name ".github" -not -name ".*.swp" -not -name ".gnupg"); do \
f=$$(basename $$file); \
ln -sfn $$file $(HOME)/$$f; \
done; \
git config --system --add user.email $$GITEMAIL; \
mkdir -p $(HOME)/.additional_dotfiles; touch $(HOME)/.additional_dotfiles/credentials; \
mkdir -p $(HOME)/.config; \
for file in $(shell find $(CURDIR)/.config -type f); do \
f=$$(basename $$file); \
ln -sfn $$file $(HOME)/.config/$$f; \
done; \
mkdir -p $(HOME)/.k9s; \
for file in $(shell find $(CURDIR)/.k9s -type f); do \
f=$$(basename $$file); \
ln -sfn $$file $(HOME)/.k9s/$$f; \
done; \
mkdir -p $(HOME)/.kube; \
for file in $(shell find $(CURDIR)/.kube -type f); do \
f=$$(basename $$file); \
ln -sfn $$file $(HOME)/.kube/$$f; \
done; \
mkdir -p $(HOME)/.gnupg; \
for file in $(shell find $(CURDIR)/.gnupg -type f); do \
f=$$(basename $$file); \
ln -sfn $$file $(HOME)/.gnupg/$$f; \
done; \
install: ## Installs all dotfiles and associated.
@bash ./install.sh
.PHONY: help
help:

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

View File

@ -1,17 +1,22 @@
export ZSH="$HOME/.oh-my-zsh"
plugins=(
aws
zsh-syntax-highlighting
)
autoload -U compinit && compinit
autoload -U compinit
# Use this setting if you want to disable marking untracked files under VCS as dirty.
# This makes repository status checks for large repositories much, much faster.
DISABLE_UNTRACKED_FILES_DIRTY="true"
SHOW_AWS_PROMPT=false
COMPLETION_WAITING_DOTS=true
source $ZSH/oh-my-zsh.sh
zstyle ':omz:update' mode reminder
source `brew --prefix switch`/switch.sh
# History
HISTFILE="$HOME/.zsh_history"
HISTIGNORE="&:exit:reset:clear:zh"
@ -26,8 +31,6 @@ setopt HIST_REDUCE_BLANKS
setopt autocd
autoload -U add-zsh-hook
BAT_THEME="Monokai Extended Light"
BAT_STYLE="grid,header"
DISABLE_AUTO_TITLE="true"
@ -51,3 +54,9 @@ preexec() {
if overridden; then return; fi
printf "\033]0;%s\a" "${1%% *} | $cwd" # Omit construct from $1 to show args
}
# Giant Swarm specific
which opsctl &>/dev/null && opsctl completion zsh > /usr/local/share/zsh/site-functions/_opsctl
which gsctl &>/dev/null && gsctl completion zsh --stdout > /usr/local/share/zsh/site-functions/_gsctl
which devctl &>/dev/null && devctl completion zsh > /usr/local/share/zsh/site-functions/_devctl

View File

@ -3,18 +3,19 @@ alias _cat=`which cat`
alias _curl=`which curl`
alias _ls="/bin/ls"
alias _grep="/bin/grep"
alias _diff=`which diff`
alias _diff="/usr/bin/diff"
alias _du=`which du`
alias _df=`which df`
alias _find=`which find`
alias _top=`which top`
alias _ps="/bin/ps"
alias _dig=`which dig`
alias _git=`which git`
# Aliases
alias cat=' bat '
alias cat='bat '
alias curl='curlie'
alias ls=' exa --group-directories-first --icons --header --git --ignore-glob=.git'
alias ls='exa --group-directories-first --icons --header --git --ignore-glob=.git'
alias grep='rg'
alias diff='delta'
alias du='dust'
@ -26,10 +27,10 @@ alias ps='procs'
alias dig='dog'
alias kubectx='switch'
alias kctx='switch'
alias machine-info='macchina -t Boron --bar'
alias watch='watch '
alias tmp='cd $(mktemp -d)'
lt() {
DEPTH=$(echo $1 | grep "^[0-9]*$")
if [ "$DEPTH" = "" ]; then
@ -39,16 +40,24 @@ lt() {
fi
ls -l --tree -L $DEPTH -I ".git|cache|log|logs|node_modules|vendor" $@
}
alias aws='docker run --rm -ti -v ~/.aws:/root/.aws -v $(pwd):/aws -e AWS_PROFILE -e AWS_DEFAULT_REGION -e AWS_REGION -e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY amazon/aws-cli'
git() {
if [ "$1" = "take" ]; then
/usr/local/bin/git clone $2
if [ "$1" = "take" ]; then # Git clone then cd into new directory
_git clone $2
cd $(basename $2 | sed 's/\.git$//')
elif [ "$1" = "commit" ]; then
elif [ "$1" = "commit" ]; then # Sign all commits
shift
/usr/local/bin/git commit -s $@
_git commit -s $@
else
/usr/local/bin/git $@
_git $@
fi
}
# Giant Swarm specific
alias prom='opsctl open -a prometheus --sso -i'
alias happa='opsctl open -a happa --sso -i'
alias grafana='opsctl open -a grafana --sso -i'
alias argo='opsctl open -a argocd --sso -i'
alias cloud-provider='opsctl open -a cloudprovider -i'
alias get-cluster='gs-get-cluster'
alias create-cluster='gs-create-cluster'

View File

@ -1,4 +1,3 @@
export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:$HOME/goworkspace/bin:$HOME/.cargo/bin:/usr/local/kubebuilder/bin:$HOME/.asdf/shims"
export EDITOR='vim'
export VISUAL='code'
export GPG_TTY=$(tty)
@ -12,14 +11,16 @@ fi
# Node
export NODE_ENV=localhost
export NPM_TOKEN=`head -n 1 ~/.npmrc | sed 's~//registry.npmjs.org/:_authToken=\(.*\)~\1~'`
[ -f ~/.npmrc ] && export NPM_TOKEN=`head -n 1 ~/.npmrc | sed 's~//registry.npmjs.org/:_authToken=\(.*\)~\1~'`
# Go
export GOPATH=$HOME/goworkspace
export GO111MODULE=on
# Krew
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
# Starship
export STARSHIP_CONFIG=~/.starship/config.toml
export STARSHIP_LOG=error
# Bat
export BAT_THEME="Monokai Extended Light"
export BAT_STYLE="grid,header"

10
home/.dotfiles/kubernetes Normal file
View File

@ -0,0 +1,10 @@
alias k='kubectl '
alias kx='kube-exec '
alias kl='kube-logs '
alias kshell='kube-shell '
alias kall='kube-all '
alias fix-broken-replicasets='kube-clean-replicasets '
alias kube-forward='kube-forward-all '
source <(kubectl completion zsh)
source <(tkn completion zsh)

View File

View File

@ -0,0 +1,92 @@
"########################################
"########################################
" Tickle Contrast (rainglow)
"
" https://github.com/rainglow/vim
"
" Copyright (c) Dayle Rees.
"########################################
"########################################
"########################################
"# Settings. #
"########################################
set background=dark
highlight clear
if exists("syntax_on")
syntax reset
endif
let g:colors_name = "tickle-contrast"
"########################################
"# Base Colors. #
"########################################
hi Cursor guifg=#181819 guibg=#f8f8f0 gui=NONE
hi Visual guifg=#85ffc7 guibg=#ffffff gui=NONE
hi CursorLine guifg=NONE guibg=#1f1f21 gui=NONE
hi CursorLineNr guifg=#636367 guibg=#060607 gui=NONE
hi CursorColumn guifg=NONE guibg=#060607 gui=NONE
hi ColorColumn guifg=NONE guibg=#000000 gui=NONE
hi LineNr guifg=#313133 guibg=#0b0b0c gui=NONE
hi VertSplit guifg=#313133 guibg=#313133 gui=NONE
hi MatchParen guifg=#40a5a5 guibg=NONE gui=underline
hi StatusLine guifg=#c1c1c1 guibg=#0b0b0c gui=bold
hi StatusLineNC guifg=#c1c1c1 guibg=#0b0b0c gui=NONE
hi Pmenu guifg=#c1c1c1 guibg=#0b0b0c gui=NONE
hi PmenuSel guifg=NONE guibg=#85ffc7 gui=NONE
hi IncSearch guifg=#c1c1c1 guibg=#afd1d1 gui=NONE
hi Search guifg=NONE guibg=NONE gui=underline
hi Directory guifg=#85ffc7 guibg=NONE gui=NONE
hi Folded guifg=#b4b4b4 guibg=#000000 gui=NONE
hi Normal guifg=#ff8552 guibg=#181819 gui=NONE
hi Boolean guifg=#ff8552 guibg=NONE gui=NONE
hi Character guifg=#86baba guibg=NONE gui=NONE
hi Comment guifg=#606063 guibg=NONE gui=NONE
hi Conditional guifg=#85ffc7 guibg=NONE gui=NONE
hi Constant guifg=NONE guibg=NONE gui=NONE
hi Define guifg=#85FFC7 guibg=NONE gui=NONE
hi DiffAdd guifg=#3d3d40 guibg=#a7da1e gui=bold
hi DiffDelete guifg=#3d3d40 guibg=#e61f44 gui=NONE
hi DiffChange guifg=#3d3d40 guibg=#f7b83d gui=NONE
hi DiffText guifg=#3d3d40 guibg=#f7b83d gui=bold
hi ErrorMsg guifg=#3d3d40 guibg=#e61f44 gui=NONE
hi WarningMsg guifg=#3d3d40 guibg=#f7b83d gui=NONE
hi Float guifg=#afd1d1 guibg=NONE gui=NONE
hi Function guifg=#85ffc7 guibg=NONE gui=NONE
hi Identifier guifg=#ffffff guibg=NONE gui=NONE
hi Keyword guifg=#85ffc7 guibg=NONE gui=NONE
hi Label guifg=#86baba guibg=NONE gui=NONE
hi NonText guifg=#5b5b5b guibg=#131314 gui=NONE
hi Number guifg=#afd1d1 guibg=NONE gui=NONE
hi Operator guifg=#c1c1c1 guibg=NONE gui=NONE
hi PreProc guifg=#86868a guibg=NONE gui=NONE
hi Special guifg=#c1c1c1 guibg=NONE gui=NONE
hi SpecialKey guifg=#c1c1c1 guibg=#85ffc7 gui=NONE
hi Statement guifg=#85ffc7 guibg=NONE gui=NONE
hi StorageClass guifg=#40a5a5 guibg=NONE gui=NONE
hi String guifg=#86baba guibg=NONE gui=NONE
hi Tag guifg=#85ffc7 guibg=NONE gui=NONE
hi Title guifg=#85ffc7 guibg=NONE gui=bold
hi Todo guifg=#86868a guibg=NONE gui=inverse,bold
hi Type guifg=NONE guibg=NONE gui=NONE
hi Underlined guifg=NONE guibg=NONE gui=underline
"########################################
"# Language Overrides #
"########################################
hi phpIdentifier guifg=#ffffff
hi phpMethodsVar guifg=#cacacc
hi xmlTag guifg=#85ffc7 guibg=NONE gui=NONE
hi xmlTagName guifg=#85ffc7 guibg=NONE gui=NONE
hi xmlEndTag guifg=#85ffc7 guibg=NONE gui=NONE
"########################################
"# Light Theme Overrides #
"########################################

5
home/.vimrc Normal file
View File

@ -0,0 +1,5 @@
colorscheme tickle-contrast
set paste
set number
set linespace=3
set cursorline

30
home/.zshrc Normal file
View File

@ -0,0 +1,30 @@
PATH_DIRS=(
"${HOME}/.bin"
"${KREW_ROOT:-${HOME}/.krew}/bin"
"${GOPATH:-${HOME}/goworkspace}/bin"
"${HOME}/.cargo/bin"
"/home/linuxbrew/.linuxbrew/bin"
"/usr/local/bin"
"/usr/bin"
"/bin"
"/usr/sbin"
"/sbin"
"${PATH}"
)
export PATH=${"${PATH_DIRS[*]}"// /:}
if [ ! -z ~/.additional_dotfiles/credentials ]; then
source ~/.additional_dotfiles/credentials
fi
for filename in ~/.dotfiles/*; do
source $filename
done
for filename in ~/.additional_dotfiles/*; do
source $filename
done
rm -f ~/.zcompdump; compinit
eval "$(starship init zsh)"

101
install.sh Normal file
View File

@ -0,0 +1,101 @@
#!/usr/bin/env bash
export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
GITEMAIL=$(git config --get user.email)
[ -d ~/.additional_dotfiles ] || (mkdir -p ~/.additional_dotfiles && touch ~/.additional_dotfiles/credentials)
# Install homebrew
which brew >/dev/null || /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
echo "🔵 Setting up zsh"
# Install oh-my-zsh
printf "Cloning oh-my-zsh..."
[ -d ~/.oh-my-zsh ] || sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
printf " ✅\n"
# Install ZSH plugins
printf "Cloning zsh plugins..."
[ -d ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions ] || git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
[ -d ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting ] || git clone https://github.com/zsh-users/zsh-syntax-highlighting ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
printf " ✅\n"
# Install tools
BREW_TOOLS=(
argocd bandwhich bat danielfoehrkn/switch/switch derailed/k9s/k9s dive dog duf dust exa fd fzf
git-delta git-delta go helm homebrew/cask-fonts/font-open-dyslexic-nerd-font htop jq kind krew
kubectl kubectx kustomize node procs progress ripgrep rs/tap/curlie rust starship
tektoncd/tools/tektoncd-cli tldr tailscale
)
CARGO_TOOLS=( macchina bottom )
NODE_TOOLS=( git-split-diffs )
KREW_TOOLS=( gs )
echo "🔵 Installing / updating tools"
# Homebrew
export HOMEBREW_NO_INSTALL_CLEANUP=true
for tool in "${BREW_TOOLS[@]}"
do
printf "${tool}..."
brew upgrade ${tool} &>/dev/null || brew install ${tool} &>/dev/null
printf " ✅\n"
done
# Cargo
for tool in "${CARGO_TOOLS[@]}"
do
printf "${tool}..."
cargo install ${tool} &>/dev/null
printf " ✅\n"
done
# Krew
kubectl-krew update &>/dev/null
for tool in "${KREW_TOOLS[@]}"
do
printf "${tool}..."
kubectl-krew upgrade ${tool} &>/dev/null || kubectl-krew install ${tool} &>/dev/null
printf " ✅\n"
done
echo "🔵 Adding configuration"
FILES=$(find ./home -maxdepth 1 -mindepth 1 -printf '%f ')
for file in $FILES
do
f=$(readlink -f "./home/${file}")
printf "Linking ${f}..."
ln -sfn ${f} ~/$(basename "./home/${file}")
printf " ✅\n"
done
echo "🔵 OS Specific setup"
echo "Detected OS type: ${OSTYPE}"
case "${OSTYPE}" in
*linux*)
# Do stuff
;;
*darwin*)
# Mac specific setup
BREW_TOOLS=( pinentry-mac gpg gawk coreutils )
for tool in "${MAC_BREW_TOOLS[@]}"
do
printf "${tool}..."
brew upgrade ${tool} &>/dev/null || brew install ${tool} &>/dev/null
printf " ✅\n"
done
FILES=$(find ./os-specific/darwin/home -maxdepth 1 -mindepth 1 -printf '%f ')
for file in $FILES
do
f=$(readlink -f "./os-specific/darwin/home/${file}")
printf "Linking ${f}..."
ln -sfn ${f} ~/$(basename "./os-specific/darwin/home/${file}")
printf " ✅\n"
done
;;
esac