Skip to content

Commit 63cf0f6

Browse files
authored
Updated scripts for looking up and deleting unused namespaces, now checks for teams.csv as well
* adding users/team checks * fix for ns lookup * fix for uppercase checks * updated ns deletion script
1 parent ee5adf1 commit 63cf0f6

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

scripts/delete-unused-ns.sh

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,25 @@ source check-available-commands.sh
1212
checkCommandsAvailable kubectl jq awk
1313
IFS=$'
1414
'
15-
USERS=($(awk -F , '{print $3}' users.csv))
15+
USERS=($(awk -F , '{print tolower($3)}' users.csv))
16+
TEAMS=($(awk -F , '{print tolower($3)}' teams.csv))
1617
unset IFS
1718
for NAMESPACE in `kubectl get ns | grep t- | awk '{print $1;}'`
1819
do
1920
echo "found $NAMESPACE"
20-
NO_TDASH_NAMESPACE=${NAMESPACE:2}
21+
CUT_NAMESPACE=${NAMESPACE:2}
22+
NO_TDASH_NAMESPACE=`echo $CUT_NAMESPACE | awk '{print tolower($0)}'`
2123
echo "checking list for $NO_TDASH_NAMESPACE"
2224
if [[ " ${USERS[*]} " =~ " ${NO_TDASH_NAMESPACE} " ]]; then
23-
echo "FOUND $NO_TDASH_NAMESPACE in users, skipping it"
25+
echo "FOUND $NO_TDASH_NAMESPACE in users"
2426
else
25-
echo "did not find $NO_TDASH_NAMESPACE in users, deleting it now!"
26-
kubectl delete ns $NAMESPACE
27-
echo "deleted $NAMESPACE"
27+
if [[ " ${TEAMS[*]} " =~ " ${NO_TDASH_NAMESPACE} " ]]; then
28+
echo "FOUND $NO_TDASH_NAMESPACE in teams"
29+
else
30+
echo "did NOT find $NO_TDASH_NAMESPACE in users and teams. Deletig it now!"
31+
kubectl delete ns $NAMESPACE
32+
echo "deleted $NAMESPACE"
33+
fi
34+
2835
fi
2936
done

scripts/list-unused-ns.sh

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
#!/bin/bash
22

33
echo "This script shows all the namespaces that have not been used, it requires an export of the users in CTFD"
4-
echo "It assumes you have the users.csv file from CTFD in the same folder"
4+
echo "It assumes you have the users.csv file and teams.csv file from CTFD in the same folder"
5+
echo "This script results in a file unusedteams.txt with all the teams that have no exact match with the registration of users and teams in CTFD"
6+
rm unusedteams.txt
57
source check-available-commands.sh
68
checkCommandsAvailable kubectl jq awk
79
IFS=$'
810
'
9-
USERS=($(awk -F , '{print $3}' users.csv))
11+
USERS=($(awk -F , '{print tolower($3)}' users.csv))
12+
TEAMS=($(awk -F , '{print tolower($3)}' teams.csv))
1013
unset IFS
1114
for NAMESPACE in `kubectl get ns | grep t- | awk '{print $1;}'`
1215
do
1316
echo "found $NAMESPACE"
14-
NO_TDASH_NAMESPACE=${NAMESPACE:2}
17+
CUT_NAMESPACE=${NAMESPACE:2}
18+
NO_TDASH_NAMESPACE=`echo $CUT_NAMESPACE | awk '{print tolower($0)}'`
1519
echo "checking list for $NO_TDASH_NAMESPACE"
1620
if [[ " ${USERS[*]} " =~ " ${NO_TDASH_NAMESPACE} " ]]; then
1721
echo "FOUND $NO_TDASH_NAMESPACE in users"
1822
else
19-
echo "did not find $NO_TDASH_NAMESPACE in users"
23+
if [[ " ${TEAMS[*]} " =~ " ${NO_TDASH_NAMESPACE} " ]]; then
24+
echo "FOUND $NO_TDASH_NAMESPACE in teams"
25+
else
26+
echo "did NOT find $NO_TDASH_NAMESPACE in users and teams"
27+
echo $NAMESPACE >> unusedteams.txt
28+
fi
29+
2030
fi
2131
done

0 commit comments

Comments
 (0)