Prints the full ID of a Kubernetes pod by a partial name.
Pods in kubernetes usually has names such as
my-app-123sdk-54lda5
other-app-789sdk-89jds
To get logs, the workflow is to first use kubectl get pod to list them all, then copy the ID of the pod you want,
then run kubectl logs <pod id> to get the logs.
This app aims to remove the list-pods-and-copy-id steps. With pod-id you can instead run kubectl logs $(podid my-app).
Pod-id will use your current Kubernetes context referenced by environment variable KUBECONFIG or in ~/.kube/config to read all pods and find the ones matching the
app name you provide as first argument. It is recommended you use kubens to manage your current namespace (see usage).
kubectx and kubens are excellent tools to use if you're working a lot with kubernetes regardless of this app.
Build it:
go build -o podid main.goPut podid somewhere on your PATH.
Standalone:
podid my-app # my-app-123sdk-54lda5With kubectl:
kubectl logs $(podid my-app)
App names can be partial. If you have a pod with ID card-payment-api-123sdk-54lda5 you can use a part of the name such as podid payment to look it up.
TL;DR: Use kubens. Pod-id will read kubens --current to find the current namespace.
Internally, pod-id needs to know in which kubernetes namespace to fetch pods. This can be done in a few ways:
First it will check if you gave namespace as part of the app name. Some examples:
podid <namespace>/<app>
podid monitoring/my-app # namespace=monitoring, appName=my-app
podid apps/other-app # namespace=apps, appName=other-appIf no namespace was found that way, it will check if the environment variable PODID_NAMESPACE is set and use that if it is.
If not, it will try the last method of executing the shell command kubens --current and reading the result.
Therefore, the most reliable and hassle-free method is using kubens to manage your active kubernetes context and namespace.
If an app has multiple pods such as:
my-app-123sdk-54lda5
my-app-465dsa-sd87d8Then adding the pod number after the app name will print the ID for that number:
podid my-app 1 # my-app-123sdk-54lda5
podid my-app 2 # my-app-465dsa-sd87d8Add the "copy" parameter to also copy the pod id to your clipboard:
podid my-app copy # Clipboard is now: my-app-123sdk-54lda5
podid my-app 2 copy # Clipboard is now: my-app-465dsa-sd87d8It will still print the pod id to stdout, so you can still use it like kubectl logs $(podid my-app copy) and get the logs in addition to having the ID copied to your clipboard.