Skip to content

Instantly share code, notes, and snippets.

@kksudo
Last active March 24, 2023 19:26
Show Gist options
  • Save kksudo/abc80640c99ea6334063449e5c788ab1 to your computer and use it in GitHub Desktop.
Save kksudo/abc80640c99ea6334063449e5c788ab1 to your computer and use it in GitHub Desktop.
bash_profile file is a personal initialization file for configuring the user environment.
# Preferred editor for local and remote sessions
if [[ -n $SSH_CONNECTION ]]; then
export EDITOR='vim'
else
export EDITOR='vim'
fi
export ZSH_AUTOSUGGEST_STRATEGY=(history completion)
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
export FPATH=/opt/linuxbrew/.linuxbrew/share/zsh/site-functions:$FPATH
# Terraform
export TERRAGRUNT_DOWNLOAD="$HOME/.terraform.d/plugin-cache"
export TF_PLUGIN_CACHE_DIR="$TERRAGRUNT_DOWNLOAD/.plugins"
export GPG_TTY=$(tty)
# Docker
export DOCKER_DEFAULT_PLATFORM=linux/amd64
#!/usr/bin/env bash
# Current directory
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
source "${CURRENT_DIR}"/../common-lib.sh
# Current k8s cluster is:
printf "Current k8s cluster is: %s\n" "$(kubectl config current-context)"
echo "----------------------------------------"
# type yes ot continue or exit
read -p "Are you sure you want to continue and find all orphan services? [y/N] " -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
printf "\nExit from the k8s service cleaner\n"
exit 0
fi
# Get all k8s services without endpoints
printf "\nGet all services without existing pods\n"
# get services with name and namespace and Selector
services=$(kubectl get services --all-namespaces -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.metadata.namespace}{"\t"}{.spec.selector}{"\n"}{end}' | grep -v "null")
services_to_delete=()
# If the service has no endpoints, then delete it
while read -r line; do
name=$(echo "$line" | awk '{print $1}')
namespace=$(echo "$line" | awk '{print $2}')
selector=$(echo "$line" | awk '{print $3}' | jq -r 'to_entries | map("\(.key)=\(.value|tostring)") | join(",")')
# printf "Service name: %s, namespace: %s, selector: %s\n" "$name" "$namespace" "$selector"
# If the pod not found by selector, then delete service
if [[ -z $(kubectl get pods -n "$namespace" -o jsonpath="{.items[*].metadata.name}" -l "$selector") ]]; then
printf "For service $YELLOW%s$NC in namespace $YELLOW%s$NC not found any pod by selector: %s\n" "$name" "$namespace" "$selector"
services_to_delete+=("$name")
fi
done <<< "$services"
if [[ ${#services_to_delete[@]} -eq 0 ]]; then
printf "No services to delete\n"
exit 0
fi
echo -e "$GREEN----------------------------------------$NC"
# print services to delete
printf "Services to delete:\n"
printf "%s\n" "${services_to_delete[@]}"
echo "----------------------------------------"
printf "Total services to delete: $GREEN%s$NC\n" "${#services_to_delete[@]}"
echo -e "$GREEN----------------------------------------$NC"
read -p "Are you sure you want to delete all orphan services? [y/N] " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]; then
count=0
for service in "${services_to_delete[@]}"; do
name=$(echo "$line" | awk '{print $1}')
namespace=$(echo "$line" | awk '{print $2}')
count=$((count+1))
printf "$count: kubectl delete service $GREEN%s$NC -n $GREEN%s$NC\n" "$name" "$namespace"
kubectl -n "$namespace" delete service "$service"
if $? -eq 0; then
printf "%s: Service $GREEN%s$NC deleted ...\n" "$count" "$service"
else
printf "Service $RED%s$NC not deleted\n" "$service"
fi
done
printf "Total deleted services: $GREEN%s$NC\n" "$count"
else
printf "\n$NCExit from script without any actions$NC\n"
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment