This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
kube-exporter.sh
executable file
·39 lines (33 loc) · 2.59 KB
/
kube-exporter.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env bash
set -eo pipefail
command -v kubectl >/dev/null 2>&1 || { \
echo >&2 "kubectl is needed, but it's not installed. Aborting."
echo >&2 "Refer to: https://kubernetes.io/docs/tasks/tools/install-kubectl/"
exit 1
}
command -v jq >/dev/null 2>&1 || { \
echo >&2 "jq is needed, but it's not installed. Aborting."
echo >&2 "Refer to: https://stedolan.github.io/jq/"
exit 1
}
# Exports all resources from a K8s cluster
# while redacting secrets values and env vars
# Uses the current context
CONTEXT="$(kubectl config current-context | sed -e 's/\//_/g' | sed -e 's/:/_/g')"
OUTPUTFILE="${CONTEXT}.json"
touch "${OUTPUTFILE}"
cat /dev/null > "${OUTPUTFILE}"
# API Resources
APIRESOURCES="$(kubectl api-resources -o name --verbs list | sort -u)"
REDACTEDRESOURCES='^(secrets|managedcertificate.+)$'
for res in $APIRESOURCES; do
if [[ $res =~ $REDACTEDRESOURCES ]]; then
# redact secrets data values
kubectl get $res --all-namespaces --chunk-size=50 -ojson | jq -rc --arg CONTEXT "$CONTEXT" '.items[] | walk(if type=="object" and has("kubectl.kubernetes.io/last-applied-configuration") then ."kubectl.kubernetes.io/last-applied-configuration"="REDACTED" else . end) | walk(if type == "object" and has("data") then .data[] = "REDACTED" else . end) | {asset_type: ("k8s.io/"+ .kind), name: ("//"+ $CONTEXT + .metadata.selfLink), resource: {version: "v1", discovery_document_uri: "https://raw.githubusercontent.com/kubernetes/kubernetes/master/api/openapi-spec/swagger.json", data: .}}' >> "${OUTPUTFILE}"
else
# redact anywhere env vars have "value"
kubectl get $res --all-namespaces --chunk-size=50 -ojson | jq -rc --arg CONTEXT "$CONTEXT" '.items[] | walk(if type=="object" and has("kubectl.kubernetes.io/last-applied-configuration") then ."kubectl.kubernetes.io/last-applied-configuration"="REDACTED" else . end) | walk(if type=="object" and has("env") and (.env|type=="array") then walk(if type=="object" and has("name") and has("value") then .value="REDACTED" else . end) else . end) | {asset_type: ("k8s.io/"+ .kind), name: ("//"+ $CONTEXT + .metadata.selfLink), resource: {version: "v1", discovery_document_uri: "https://raw.githubusercontent.com/kubernetes/kubernetes/master/api/openapi-spec/swagger.json", data: .}}' >> "${OUTPUTFILE}"
fi
done
# Server version
kubectl version -o json | jq -r '.serverVersion'| jq -rc --arg CONTEXT "$CONTEXT" '{asset_type: ("k8s.io/Version"), name: ("//"+ $CONTEXT + .metadata.selfLink), resource: {version: "v1", discovery_document_uri: "https://raw.githubusercontent.com/kubernetes/kubernetes/master/api/openapi-spec/swagger.json", data: .}}' >> "${OUTPUTFILE}"