Skip to content

Improved patching of NSPs & adding cleanup scripts based on ctfd-username list #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions scripts/delete-unused-ns.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

echo "This script DELETES all the namespaces that have not been used, it requires an export of the users in CTFD"
echo "It assumes you have the users.csv file from CTFD in the same folder"
echo "ONLY RUN THIS IF YOU ARE SATISFIED WITH THE OUTPUT OF list-unused-ns.sh!"

sleep 2
echo "Sleeping for 10 seconds to give you time to abort if you are not sure to use automatic deletion"
sleep 10

source check-available-commands.sh
checkCommandsAvailable kubectl jq awk
IFS=$'
'
USERS=($(awk -F , '{print $3}' users.csv))
unset IFS
for NAMESPACE in `kubectl get ns | grep t- | awk '{print $1;}'`
do
echo "found $NAMESPACE"
NO_TDASH_NAMESPACE=${NAMESPACE:2}
echo "checking list for $NO_TDASH_NAMESPACE"
if [[ " ${USERS[*]} " =~ " ${NO_TDASH_NAMESPACE} " ]]; then
echo "FOUND $NO_TDASH_NAMESPACE in users, skipping it"
else
echo "did not find $NO_TDASH_NAMESPACE in users, deleting it now!"
kubectl delete ns $NAMESPACE
echo "deleted $NAMESPACE"
fi
done
21 changes: 21 additions & 0 deletions scripts/list-unused-ns.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

echo "This script shows all the namespaces that have not been used, it requires an export of the users in CTFD"
echo "It assumes you have the users.csv file from CTFD in the same folder"
source check-available-commands.sh
checkCommandsAvailable kubectl jq awk
IFS=$'
'
USERS=($(awk -F , '{print $3}' users.csv))
unset IFS
for NAMESPACE in `kubectl get ns | grep t- | awk '{print $1;}'`
do
echo "found $NAMESPACE"
NO_TDASH_NAMESPACE=${NAMESPACE:2}
echo "checking list for $NO_TDASH_NAMESPACE"
if [[ " ${USERS[*]} " =~ " ${NO_TDASH_NAMESPACE} " ]]; then
echo "FOUND $NO_TDASH_NAMESPACE in users"
else
echo "did not find $NO_TDASH_NAMESPACE in users"
fi
done
15 changes: 13 additions & 2 deletions scripts/patch-nsp-for-kubectl.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
#!/bin/bash
# Deployiong for 1-20

echo "This script will patch the networkpolicies for every ns starting with 't-', and patch it to use a new cidr block."
echo "You can use this to patch the ns when autoscaling and rebalancing kubelets breaks the network of the pods "

source check-available-commands.sh
checkCommandsAvailable kubectl jq
echo "executing kubectl get endpoints kubernetes -o json | jq '.subsets[0].addresses[0].ip'"
IP_ENDPOINT_STRING=$(kubectl get endpoints kubernetes -o json | jq '.subsets[0].addresses[0].ip')
echo "We will base our CIDR on $IP_ENDPOINT_STRING "
IP_ENDPOINT="${IP_ENDPOINT_STRING//\"/}"
echo $IP_ENDPOINT
IFS=. ; set -- $IP_ENDPOINT
CIDR="$1.$2.0.0/16"
echo "We will use CIDR = ${CIDR}"

for NAMESPACE in `kubectl get ns | grep t- | awk '{print $1;}'`
do
sleep 1;
echo "Deployoing fix for $NAMESPACE 🚀"
CIDR = "172.16.0.0/16"
echo "IP whitelist set to cidr = $CIDR"
kubectl delete networkpolicy access-kubectl-from-virtualdeskop -n $NAMESPACE
cat <<EOF | kubectl create -f -
Expand Down