func (prc *Checker) doChecks(ctx context.Context) error {
	log.Infoln("Checking for pod BackOff events for all pods in the namespace:", prc.Namespace)
	podWarningEvents, err := prc.client.CoreV1().Events(prc.Namespace).List(ctx, metav1.ListOptions{FieldSelector: "type=Warning"})
	if err != nil {
		return err
	}
	if len(podWarningEvents.Items) != 0 {
		log.Infoln("Found `Warning` events in the namespace:", prc.Namespace)
		for _, event := range podWarningEvents.Items {
			if event.InvolvedObject.Kind == "Pod" && event.Reason == "BackOff" && event.Count > prc.MaxFailuresAllowed {
				errorMessage := "Found: " + strconv.FormatInt(int64(event.Count), 10) + " `BackOff` events for pod: " + event.InvolvedObject.Name + " in namespace: " + event.Namespace
				log.Infoln(errorMessage)
				prc.BadPods[event.InvolvedObject.Namespace+"/"+event.InvolvedObject.Name] = errorMessage
			}
		}
	}
	for pod := range prc.BadPods {
		err := prc.verifyBadPodRestartExists(ctx, pod)
		if err != nil {
			return err
		}
	}
	return err
}
doChecks grabs all events in a given namespace, then checks for pods with event type "Warning" with reason "BackOff",
and an event count greater than the MaxFailuresAllowed. If any of these pods are found, an error message is appended
to Checker struct errorMessages.
package main
import (
	"context"
	"os"
	"path/filepath"
	"strconv"
	"strings"
	"time"
	v1 "k8s.io/api/core/v1"
	log "github.com/sirupsen/logrus"
	"k8s.io/client-go/kubernetes"
	_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
	k8sErrors "k8s.io/apimachinery/pkg/api/errors"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	checkclient "github.com/kuberhealthy/kuberhealthy/v2/pkg/checks/external/checkclient"
	"github.com/kuberhealthy/kuberhealthy/v2/pkg/kubeClient"
)
const defaultMaxFailuresAllowed = 10
const defaultCheckTimeout = 10 * time.Minute
var KubeConfigFile = filepath.Join(os.Getenv("HOME"), ".kube", "config")
var Namespace string
var CheckTimeout time.Duration
var MaxFailuresAllowed int32
type Checker struct {
	Namespace          string
	MaxFailuresAllowed int32
	BadPods            map[string]string
	client             *kubernetes.Clientset
}
func init() {
	Namespace = os.Getenv("POD_NAMESPACE")
	if Namespace == "" {
		log.Infoln("Looking for pods across all namespaces, this requires a cluster role")
		Namespace = v1.NamespaceAll
	} else {
		log.Infoln("Looking for pods in namespace:", Namespace)
	}
	CheckTimeout = defaultCheckTimeout
	timeDeadline, err := checkclient.GetDeadline()
	if err != nil {
		log.Infoln("There was an issue getting the check deadline:", err.Error())
	}
	CheckTimeout = timeDeadline.Sub(time.Now().Add(time.Second * 5))
	log.Infoln("Check time limit set to:", CheckTimeout)
	MaxFailuresAllowed = defaultMaxFailuresAllowed
	maxFailuresAllowed := os.Getenv("MAX_FAILURES_ALLOWED")
	if len(maxFailuresAllowed) != 0 {
		conversion, err := strconv.ParseInt(maxFailuresAllowed, 10, 32)
		MaxFailuresAllowed = int32(conversion)
		if err != nil {
			log.Errorln("Error converting maxFailuresAllowed:", maxFailuresAllowed, "to int, err:", err)
			return
		}
	}
}
func main() {
	client, err := kubeClient.Create(KubeConfigFile)
	if err != nil {
		log.Fatalln("Unable to create kubernetes client", err)
	}
	prc := New(client)
	err = prc.Run()
	if err != nil {
		log.Errorln("Error running Pod Restarts check:", err)
		os.Exit(2)
	}
	log.Infoln("Done running Pod Restarts check")
	os.Exit(0)
}
func New(client *kubernetes.Clientset) *Checker {
	return &Checker{
		Namespace:          Namespace,
		MaxFailuresAllowed: MaxFailuresAllowed,
		BadPods:            make(map[string]string),
		client:             client,
	}
}
func (prc *Checker) Run() error {
	ctx := context.TODO()
	log.Infoln("Running Pod Restarts checker")
	doneChan := make(chan error)
	go func(doneChan chan error) {
		err := prc.doChecks(ctx)
		doneChan <- err
	}(doneChan)
	select {
	case <-time.After(CheckTimeout):
		errorMessage := "Failed to complete Pod Restart check in time! Timeout was reached."
		err := reportKHFailure([]string{errorMessage})
		if err != nil {
			return err
		}
		return err
	case err := <-doneChan:
		if len(prc.BadPods) != 0 || err != nil {
			var errorMessages []string
			if err != nil {
				log.Error(err)
				errorMessages = append(errorMessages, err.Error())
			}
			for _, msg := range prc.BadPods {
				errorMessages = append(errorMessages, msg)
			}
			return reportKHFailure(errorMessages)
		}
		return reportKHSuccess()
	}
}