Skip to content

Instantly share code, notes, and snippets.

@montmanu
Created May 18, 2018 06:37
Show Gist options
  • Save montmanu/dac6cb388a0983bfb1e6220a1c3b5604 to your computer and use it in GitHub Desktop.
Save montmanu/dac6cb388a0983bfb1e6220a1c3b5604 to your computer and use it in GitHub Desktop.
k8s container restarts by namespace, pod, container, service
#!/usr/bin/env bash
kubectl get pods --all-namespaces -o json | jq '[
.items[] | (
{
namespace: .metadata.namespace,
pod: .metadata.name,
statuses: (
[
.status.containerStatuses[] | { name, ready, restartCount }
] | map( select( .restartCount > 0 ) )
)
}
) | select( .statuses | length > 0 )
] | . as $original | (
{
namespaces: (
reduce $original[] as $item (
{} ; (
if ( . | has($item.namespace) ) then (
.[$item.namespace] += ( reduce $item.statuses[].restartCount as $count ( 0; . + $count ) )
) else (
.[$item.namespace] = ( reduce $item.statuses[].restartCount as $count ( 0; . + $count ) )
) end
)
)
),
pods: (
reduce $original[] as $item (
{} ; (
if ( . | has($item.pod) ) then (
.[$item.pod] += ( reduce $item.statuses[].restartCount as $count ( 0; . + $count ) )
) else (
.[$item.pod] = ( reduce $item.statuses[].restartCount as $count ( 0; . + $count ) )
) end
)
)
),
containers: (
reduce $original[] as $item (
{} ; (
reduce $item.statuses[] as $status (
. ;
if ( . | has($status.name) ) then (
.[$status.name] += $status.restartCount
) else (
.[$status.name] = $status.restartCount
) end
)
)
)
),
services: (
reduce $original[] as $item (
{} ; (
reduce $item.statuses[] as $status (
. ;
if ( . | has("" + $status.name + "." + $item.namespace + ".svc.cluster.local") ) then (
.["" + $status.name + "." + $item.namespace + ".svc.cluster.local"] += $status.restartCount
) else (
.["" + $status.name + "." + $item.namespace + ".svc.cluster.local"] = $status.restartCount
) end
)
)
)
)
}
)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment