Skip to content

Instantly share code, notes, and snippets.

@able8
Last active August 11, 2022 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save able8/b88710ce54db41b57e6c3778dd4d45a7 to your computer and use it in GitHub Desktop.
Save able8/b88710ce54db41b57e6c3778dd4d45a7 to your computer and use it in GitHub Desktop.
// Watch Pod update events
podInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: func(old interface{}, new interface{}) {
oldPod, newPod := old.(*v1.Pod), new.(*v1.Pod)
oldPodRestartCount := getPodRestartCount(oldPod)
newPodRestartCount := getPodRestartCount(newPod)
// Compare the RestartCount
if newPodRestartCount > oldPodRestartCount {
key, err := cache.MetaNamespaceKeyFunc(new)
if err == nil {
queue.Add(key)
}
klog.Infof("Found: %s/%s restarted, restartCount: %d -> %d\n", newPod.Namespace, newPod.Name, oldPodRestartCount, newPodRestartCount)
}
},
})
// getPodRestartCount gets Pod RestartCount from Pod Status
func getPodRestartCount(pod *v1.Pod) int {
var restarts int = 0
for i := range pod.Status.ContainerStatuses {
container := pod.Status.ContainerStatuses[i]
restarts += int(container.RestartCount)
}
return restarts
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment