Skip to content

Commit 7ab0920

Browse files
committed
Split scripts
1 parent 13f7cee commit 7ab0920

File tree

4 files changed

+260
-143
lines changed

4 files changed

+260
-143
lines changed

README.md

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,49 @@ Kubectl plugins
33

44
> Kubectl plugins repository.
55
6-
| plugin | description |
7-
|---------------------|-------------|
8-
| prune | Delete secrets or configmaps that are not being used in a given namespace. It checks from mounted volumes, env, envFrom and imagePullSecrets.
6+
| plugin | description |
7+
|------------------|-------------|
8+
| prune-configmaps | Delete configmaps that are not being used in a given namespace. It checks against all resources from mounted volumes, env and envFrom.
9+
| prune-secrets | Delete secrets that are not being used in a given namespace. It checks against all resources from mounted volumes, env, envFrom and imagePullSecrets.
910

1011
## Getting started
1112

1213
Install [krew](https://krew.dev) to manage Kubectl plugins. Refer to the
1314
[Krew documentation](https://krew.dev) to get started.
1415

1516
```bash
16-
# install the prune plugin
17-
$ kubectl krew install prune
17+
# install the prune-configmaps and prune-secrets plugins
18+
$ kubectl krew install prune-configmaps
19+
$ kubectl krew install prune-secrets
20+
```
21+
22+
### Prune configmaps usage
23+
24+
```bash
25+
$ kubectl prune-configmaps -h
26+
Delete configmaps that are not being used in a given namespace. It
27+
checks against all resources from mounted volumes, env and envFrom.
28+
29+
Usage:
30+
kubectl prune-configmaps [options]
1831

19-
# usage
20-
$ kubectl prune <resource type> <namespace>
32+
Options:
33+
-n, --namespace='': If present, the namespace scope for this CLI request
34+
-h, --help='': Deplay this help
35+
```
36+
37+
### prune-secrets usage
38+
39+
```bash
40+
$ kubectl prune-secrets -h
41+
Delete secrets that are not being used in a given namespace. It
42+
checks against all resources from mounted volumes, env, envFrom and
43+
imagePullSecrets.
2144

22-
# delete unused secrets
23-
$ kubectl prune secrets my-namespace
45+
Usage:
46+
kubectl prune-secrets [options]
2447

25-
# delete unused configmaps
26-
$ kubectl prune configmaps my-namespace
48+
Options:
49+
-n, --namespace='': If present, the namespace scope for this CLI request
50+
-h, --help='': Deplay this help
2751
```

prune/kubectl-prune-configmaps.sh

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/usr/bin/env bash
2+
# Delete unused configmaps by checking references from env, envFrom and volumes.
3+
4+
set -e
5+
6+
namespace_arg=""
7+
while test $# -gt 0
8+
do
9+
case "$1" in
10+
-n|--namespace)
11+
shift
12+
namespace_arg="--namespace=$1"
13+
shift
14+
;;
15+
-h|--help)
16+
echo "Delete configmaps that are not being used in a given namespace. It"
17+
echo "checks against all resources from mounted volumes, env and envFrom."
18+
echo ""
19+
echo "Usage:"
20+
echo " kubectl prune-configmaps [options]"
21+
echo ""
22+
echo "Options:"
23+
echo " -n, --namespace='': If present, the namespace scope for this CLI request"
24+
echo " -h, --help='': Deplay this help"
25+
exit 0
26+
;;
27+
*)
28+
break
29+
;;
30+
esac
31+
done
32+
33+
declare -a pod_field_list=(
34+
"containers[*].envFrom[*].configMapRef.name"
35+
"containers[*].env[*].valueFrom.configMapKeyRef.name"
36+
"initContainers[*].envFrom[*].configMapRef.name"
37+
"initContainers[*].env[*].valueFrom.configMapKeyRef.name"
38+
"volumes[*].configMap.name"
39+
)
40+
41+
for field in ${pod_field_list[@]}
42+
do
43+
cronjob_resources=$(kubectl get cronjobs $namespace_arg \
44+
-o jsonpath='{.items[*].spec.jobTemplate.spec.template.spec.'${field}'}')
45+
deploy_resources=$(kubectl get deploy $namespace_arg \
46+
-o jsonpath='{.items[*].spec.template.spec.'${field}'}')
47+
job_resources=$(kubectl get jobs $namespace_arg \
48+
-o jsonpath='{.items[*].spec.template.spec.'${field}'}')
49+
pod_resources=$(kubectl get pods $namespace_arg \
50+
-o jsonpath='{.items[*].spec.'${field}'}')
51+
rs_resources=$(kubectl get rs $namespace_arg \
52+
-o jsonpath='{.items[*].spec.template.spec.'${field}'}')
53+
rc_resources=$(kubectl get rc $namespace_arg \
54+
-o jsonpath='{.items[*].spec.template.spec.'${field}'}')
55+
sts_resources=$(kubectl get sts $namespace_arg \
56+
-o jsonpath='{.items[*].spec.template.spec.'${field}'}')
57+
58+
resources=$(echo "
59+
${cronjob_resources}
60+
${deploy_resources}
61+
${job_resources}
62+
${pod_resources}
63+
${rc_resources}
64+
${rs_resources}
65+
${sts_resources}
66+
" | xargs -n1 | uniq)
67+
68+
used_resources="${used_resources} ${resources}"
69+
done
70+
71+
# get all configmaps
72+
available_resources=$(kubectl get configmaps $namespace_arg \
73+
-o jsonpath='{.items[*].metadata.name}' | xargs -n1 | uniq)
74+
75+
# only keep unused configmaps
76+
resource_name_list=""
77+
for available_name in $available_resources
78+
do
79+
delete=true
80+
for resource_name in $used_resources
81+
do
82+
if [ "$available_name" == "$resource_name" ]
83+
then
84+
delete=false
85+
break
86+
fi
87+
done
88+
89+
if [ "$delete" == "true" ]
90+
then
91+
resource_name_list="${available_name} ${resource_name_list}"
92+
fi
93+
done
94+
95+
if [ "$resource_name_list" == "" ]
96+
then
97+
echo "No resource found."
98+
exit 0
99+
fi
100+
101+
echo "About to delete the following configMaps: ${resource_name_list}"
102+
103+
# confirmation prompt
104+
read -p "Delete listed resources? (yes/no): " -r
105+
if [[ $REPLY =~ ^[Yy]es$ ]]
106+
then
107+
for resource_name in $resource_name_list
108+
do
109+
kubectl delete configmap $resource_name $namespace_arg
110+
done
111+
fi

prune/kubectl-prune-secrets.sh

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/usr/bin/env bash
2+
# Delete unused secrets by checking references from env, envFrom, volumes and
3+
# imagePullSecrets.
4+
5+
set -e
6+
7+
namespace_arg=""
8+
while test $# -gt 0
9+
do
10+
case "$1" in
11+
-n|--namespace)
12+
shift
13+
namespace_arg="--namespace=$1"
14+
shift
15+
;;
16+
-h|--help)
17+
echo "Delete secrets that are not being used in a given namespace. It "
18+
echo "checks against all resources from mounted volumes, env, envFrom and"
19+
echo "imagePullSecrets."
20+
echo ""
21+
echo "Usage:"
22+
echo " kubectl prune-secrets [options]"
23+
echo ""
24+
echo "Options:"
25+
echo " -n, --namespace='': If present, the namespace scope for this CLI request"
26+
echo " -h, --help='': Deplay this help"
27+
exit 0
28+
;;
29+
*)
30+
break
31+
;;
32+
esac
33+
done
34+
35+
declare -a pod_field_list=(
36+
"containers[*].envFrom[*].secretRef.name"
37+
"containers[*].env[*].valueFrom.secretKeyRef.name"
38+
"imagePullSecrets[*].name"
39+
"initContainers[*].envFrom[*].secretRef.name"
40+
"initContainers[*].env[*].valueFrom.secretKeyRef.name"
41+
"volumes[*].secret.secretName"
42+
)
43+
44+
for field in ${pod_field_list[@]}
45+
do
46+
cronjob_resources=$(kubectl get cronjobs $namespace_arg \
47+
-o jsonpath='{.items[*].spec.jobTemplate.spec.template.spec.'${field}'}')
48+
deploy_resources=$(kubectl get deploy $namespace_arg \
49+
-o jsonpath='{.items[*].spec.template.spec.'${field}'}')
50+
job_resources=$(kubectl get jobs $namespace_arg \
51+
-o jsonpath='{.items[*].spec.template.spec.'${field}'}')
52+
pod_resources=$(kubectl get pods $namespace_arg \
53+
-o jsonpath='{.items[*].spec.'${field}'}')
54+
rs_resources=$(kubectl get rs $namespace_arg \
55+
-o jsonpath='{.items[*].spec.template.spec.'${field}'}')
56+
rc_resources=$(kubectl get rc $namespace_arg \
57+
-o jsonpath='{.items[*].spec.template.spec.'${field}'}')
58+
sts_resources=$(kubectl get sts $namespace_arg \
59+
-o jsonpath='{.items[*].spec.template.spec.'${field}'}')
60+
61+
resources=$(echo "
62+
${cronjob_resources}
63+
${deploy_resources}
64+
${job_resources}
65+
${pod_resources}
66+
${rc_resources}
67+
${rs_resources}
68+
${sts_resources}
69+
" | xargs -n1 | uniq)
70+
71+
used_resources="${used_resources} ${resources}"
72+
done
73+
74+
# get all secrets
75+
available_resources=$(kubectl get secrets $namespace_arg \
76+
-o jsonpath='{.items[*].metadata.name}' | xargs -n1 | uniq)
77+
78+
# only keep unused secrets
79+
resource_name_list=""
80+
for available_name in $available_resources
81+
do
82+
delete=true
83+
for resource_name in $used_resources
84+
do
85+
if [ "$available_name" == "$resource_name" ]
86+
then
87+
delete=false
88+
break
89+
fi
90+
done
91+
92+
if [ "$delete" == "true" ]
93+
then
94+
resource_name_list="${available_name} ${resource_name_list}"
95+
fi
96+
done
97+
98+
if [ "$resource_name_list" == "" ]
99+
then
100+
echo "No resource found."
101+
exit 0
102+
fi
103+
104+
echo "About to delete the following secrets: ${resource_name_list}"
105+
106+
# confirmation prompt
107+
read -p "Delete listed resources? (yes/no): " -r
108+
if [[ $REPLY =~ ^[Yy]es$ ]]
109+
then
110+
for resource_name in $resource_name_list
111+
do
112+
kubectl delete secret $resource_name $namespace_arg
113+
done
114+
fi

0 commit comments

Comments
 (0)