diff --git a/pkg/kubelet/prober/prober.go b/pkg/kubelet/prober/prober.go index dd94f1705b2..5ac781d0b2e 100644 --- a/pkg/kubelet/prober/prober.go +++ b/pkg/kubelet/prober/prober.go @@ -80,6 +80,20 @@ func newProber( } } +// recordContainerEvent should be used by the prober for all container related events. +func (pb *prober) recordContainerEvent(pod *v1.Pod, container *v1.Container, containerID kubecontainer.ContainerID, eventType, reason, message string, args ...interface{}) { + var err error + ref, hasRef := pb.refManager.GetRef(containerID) + if !hasRef { + ref, err = kubecontainer.GenerateContainerRef(pod, container) + if err != nil { + klog.Errorf("Can't make a ref to pod %q, container %v: %v", format.Pod(pod), container.Name, err) + return + } + } + pb.recorder.Eventf(ref, eventType, reason, message, args...) +} + // probe probes the container. func (pb *prober) probe(probeType probeType, pod *v1.Pod, status v1.PodStatus, container v1.Container, containerID kubecontainer.ContainerID) (results.Result, error) { var probeSpec *v1.Probe @@ -103,27 +117,17 @@ func (pb *prober) probe(probeType probeType, pod *v1.Pod, status v1.PodStatus, c result, output, err := pb.runProbeWithRetries(probeType, probeSpec, pod, status, container, containerID, maxProbeRetries) if err != nil || (result != probe.Success && result != probe.Warning) { // Probe failed in one way or another. - ref, hasRef := pb.refManager.GetRef(containerID) - if !hasRef { - klog.Warningf("No ref for container %q (%s)", containerID.String(), ctrName) - } if err != nil { klog.V(1).Infof("%s probe for %q errored: %v", probeType, ctrName, err) - if hasRef { - pb.recorder.Eventf(ref, v1.EventTypeWarning, events.ContainerUnhealthy, "%s probe errored: %v", probeType, err) - } + pb.recordContainerEvent(pod, &container, containerID, v1.EventTypeWarning, events.ContainerUnhealthy, "%s probe errored: %v", probeType, err) } else { // result != probe.Success klog.V(1).Infof("%s probe for %q failed (%v): %s", probeType, ctrName, result, output) - if hasRef { - pb.recorder.Eventf(ref, v1.EventTypeWarning, events.ContainerUnhealthy, "%s probe failed: %s", probeType, output) - } + pb.recordContainerEvent(pod, &container, containerID, v1.EventTypeWarning, events.ContainerUnhealthy, "%s probe failed: %v", probeType, output) } return results.Failure, err } if result == probe.Warning { - if ref, hasRef := pb.refManager.GetRef(containerID); hasRef { - pb.recorder.Eventf(ref, v1.EventTypeWarning, events.ContainerProbeWarning, "%s probe warning: %s", probeType, output) - } + pb.recordContainerEvent(pod, &container, containerID, v1.EventTypeWarning, events.ContainerProbeWarning, "%s probe warning: %v", probeType, output) klog.V(3).Infof("%s probe for %q succeeded with a warning: %s", probeType, ctrName, output) } else { klog.V(3).Infof("%s probe for %q succeeded", probeType, ctrName)