Last active
August 11, 2022 01:52
-
-
Save able8/b88710ce54db41b57e6c3778dd4d45a7 to your computer and use it in GitHub Desktop.
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
// 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