Skip to content

Commit 01812b7

Browse files
authored
Merge pull request #1 from SamLR/master
Add usage & guards
2 parents ee740f4 + 573b089 commit 01812b7

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

bin/kubedecode

+28-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
11
#!/bin/bash
22

3+
function usage(){
4+
echo -e "usage: ${0} SECRET [NAMESPACE]\n"
5+
echo -e "SECRET The secret to decode"
6+
echo -e "NAMESPACE The namespace to retrieve the secret from (optional,"
7+
echo -e " defaults to the namespace of the current context)."
8+
}
9+
10+
if [ "${1}" == "-h" ]; then
11+
usage
12+
exit
13+
fi
14+
15+
if [[ -z "${1// }" ]]; then
16+
echo "ERROR: No secret set."
17+
usage
18+
exit 1
19+
fi
320
SECRET="$1"
4-
NAMESPACE="$2"
521

6-
for row in $(echo $(kubectl get secret $SECRET -o json -n $NAMESPACE) | jq -c '.data | to_entries[]'); do
7-
KEY=$(echo $row | jq -r '.key')
8-
DECODED=$(echo $row | jq -r '.value' | base64 --decode)
9-
echo $KEY: $DECODED
22+
if [[ -z "${2// }" ]]; then
23+
NAMESPACE=$(kubectl config get-contexts --no-headers | tr -s ' ' | cut -d ' ' -f5)
24+
echo "No namespace set, using default: ${NAMESPACE}"
25+
else
26+
NAMESPACE="$2"
27+
fi
28+
29+
for row in $(kubectl get secret "${SECRET}" -o json -n "${NAMESPACE}" | jq -c '.data | to_entries[]'); do
30+
KEY=$(echo "${row}" | jq -r '.key')
31+
DECODED=$(echo "${row}" | jq -r '.value' | base64 --decode)
32+
echo "${KEY}: ${DECODED}"
1033
done

0 commit comments

Comments
 (0)