func getCheckState(c *external.Checker) (khstatev1.WorkloadDetails, error) {
	var state = khstatev1.NewWorkloadDetails(khstatev1.KHCheck)
	var err error
	name := sanitizeResourceName(c.Name())
	err = ensureStateResourceExists(c.Name(), c.CheckNamespace(), khstatev1.KHCheck)
	if err != nil {
		return state, errors.New("Error validating CRD exists: " + name + " " + err.Error())
	}
	log.Debugln("Retrieving khstate custom resource for:", name)
	khstate, err := khStateClient.KuberhealthyStates(c.CheckNamespace()).Get(name, metav1.GetOptions{})
	if err != nil {
		return state, errors.New("Error retrieving custom khstate resource: " + name + " " + err.Error())
	}
	log.Debugln("Successfully retrieved khstate resource:", name)
	return khstate.Spec, nil
}
getCheckState retrieves the check values from the kuberhealthy khstate
custom resource
package main
import (
	"errors"
	"strings"
	k8sErrors "k8s.io/apimachinery/pkg/api/errors"
	log "github.com/sirupsen/logrus"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	khjobv1 "github.com/kuberhealthy/kuberhealthy/v2/pkg/apis/khjob/v1"
	khstatev1 "github.com/kuberhealthy/kuberhealthy/v2/pkg/apis/khstate/v1"
	"github.com/kuberhealthy/kuberhealthy/v2/pkg/checks/external"
)
func setCheckStateResource(checkName string, checkNamespace string, state khstatev1.WorkloadDetails) error {
	name := sanitizeResourceName(checkName)
	existingState, err := khStateClient.KuberhealthyStates(checkNamespace).Get(name, metav1.GetOptions{})
	if err != nil {
		return errors.New("Error retrieving CRD for: " + name + " " + err.Error())
	}
	resourceVersion := existingState.GetResourceVersion()
	state.AuthoritativePod = podHostname
	now := metav1.Now()  
	state.LastRun = &now
	khState := khstatev1.NewKuberhealthyState(name, state)
	khState.SetResourceVersion(resourceVersion)
	log.Debugln(checkNamespace, checkName, "writing khstate with ok:", state.OK, "and errors:", state.Errors, "at last run:", state.LastRun)
	_, err = khStateClient.KuberhealthyStates(checkNamespace).Update(&khState)
	return err
}
func sanitizeResourceName(c string) string {
	nameLower := strings.ToLower(c)
	return strings.Replace(nameLower, " ", "-", -1)
}
func ensureStateResourceExists(checkName string, checkNamespace string, workload khstatev1.KHWorkload) error {
	name := sanitizeResourceName(checkName)
	log.Debugln("Checking existence of custom resource:", name)
	state, err := khStateClient.KuberhealthyStates(checkNamespace).Get(name, metav1.GetOptions{})
	if err != nil {
		if k8sErrors.IsNotFound(err) || strings.Contains(err.Error(), "not found") {
			log.Infoln("Custom resource not found, creating resource:", name, " - ", err)
			initialDetails := khstatev1.NewWorkloadDetails(workload)
			initialState := khstatev1.NewKuberhealthyState(name, initialDetails)
			_, err := khStateClient.KuberhealthyStates(checkNamespace).Create(&initialState)
			if err != nil {
				return errors.New("Error creating custom resource: " + name + ": " + err.Error())
			}
		} else {
			return err
		}
	}
	if state.Spec.Errors != nil {
		log.Debugln("khstate custom resource found:", name)
	}
	return nil
}