Created
November 25, 2025 15:24
-
-
Save mingliangguo/d6ba92830f431868b1e0616139b48286 to your computer and use it in GitHub Desktop.
Script to extract k8s pods information and load into bigquery table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| K8S_CLUSTER=$1 | |
| kubectx ${K8S_CLUSTER} | |
| OUTPUT_FILE="${K8S_CLUSTER}-pods-$(date "+%Y-%m-%d")-$(date "+%s").json" | |
| BQ_TABLE_NAME="my-bq-project:my-dataset.k8s_pods_${K8S_CLUSTER}" | |
| echo "getting pod information ..." | |
| kubectl get pods -A -o json | \ | |
| # jq -c '.items[] | {node_name: .spec.nodeName, namespace: .metadata.namespace, pod_name: .metadata.name}' > ${OUTPUT_FILE} | |
| # jq -c '.items[] | | |
| # { | |
| # node_name: .spec.nodeName, | |
| # namespace: .metadata.namespace, | |
| # pod_name: .metadata.name, | |
| # owner_kind: .metadata.ownerReferences[0].kind, | |
| # owner_name: .metadata.ownerReferences[0].name | |
| # }' > ${OUTPUT_FILE} | |
| jq -c '.items[] | | |
| { | |
| node_name: .spec.nodeName, | |
| namespace: .metadata.namespace, | |
| pod_name: .metadata.name, | |
| container_name: .name, | |
| owner_kind: .metadata.ownerReferences[0].kind, | |
| owner_name: .metadata.ownerReferences[0].name | |
| } | |
| + | |
| ( | |
| .spec.containers[0].resources.requests | |
| | { cpu_request: .cpu, memory_request: .memory } | |
| )' > ${OUTPUT_FILE} | |
| # kubectl get pods -A -o json | \ | |
| # jq -c '.items[] | {NODE: .spec.nodeName, NAMESPACE: .metadata.namespace, POD: .metadata.name}' | \ | |
| # jq -s '.' > ${OUTPUT_FILE} | |
| # | |
| echo "loading data into bigquery ..." | |
| bq load --autodetect --replace --source_format=NEWLINE_DELIMITED_JSON ${BQ_TABLE_NAME} ${OUTPUT_FILE} | |
| echo "Data uploaded into: ${BQ_TABLE_NAME}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment