Skip to content

Instantly share code, notes, and snippets.

@brews
Last active December 1, 2023 21:07
Show Gist options
  • Save brews/a56b429e1d12eab055b05c0d5b74c11c to your computer and use it in GitHub Desktop.
Save brews/a56b429e1d12eab055b05c0d5b74c11c to your computer and use it in GitHub Desktop.
Parse GCP logging to get JSON list of container images used in the past 60 days in a GKE jupyterhub deployment.
#!/usr/bin/env bash
# Parse GCP logging to print sample JSON list of container images used in the
# past 60 days in a GKE singleuser-server Jupyterhub deployment.
# Be sure to set PROJECT_ID and have jq installed.
set -e
PROJECT_ID="gcpprojectid"
# Pull cluster audit logs for every time jupyterhub tries to create a new
# singleuse-server pod. Then use jq to yank the image names and spit them out
# as a unique list.
gcloud logging read "protoPayload.methodName=io.k8s.core.v1.pods.create AND protoPayload.request.metadata.labels.app=jupyterhub AND protoPayload.request.metadata.labels.component=singleuser-server AND resource.type=k8s_cluster AND logName=projects/${PROJECT_ID}/logs/cloudaudit.googleapis.com%2Factivity" \
--project="${PROJECT_ID}" \
--freshness="60d" \
--format=json \
--limit=500 \
| jq '[ .[] | .protoPayload.request.spec.containers[0].image ] | unique'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment