Skip to content

Commit 46fcbcf

Browse files
authored
Merge pull request kubernetes#84792 from DataDog/eric.mountain/simple_probe_no_ref_fix_master
Fixes `No ref for container` in probes after kubelet restart
2 parents fe9073b + 4cb28f6 commit 46fcbcf

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

pkg/kubelet/prober/prober.go

+17-13
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@ func newProber(
8080
}
8181
}
8282

83+
// recordContainerEvent should be used by the prober for all container related events.
84+
func (pb *prober) recordContainerEvent(pod *v1.Pod, container *v1.Container, containerID kubecontainer.ContainerID, eventType, reason, message string, args ...interface{}) {
85+
var err error
86+
ref, hasRef := pb.refManager.GetRef(containerID)
87+
if !hasRef {
88+
ref, err = kubecontainer.GenerateContainerRef(pod, container)
89+
if err != nil {
90+
klog.Errorf("Can't make a ref to pod %q, container %v: %v", format.Pod(pod), container.Name, err)
91+
return
92+
}
93+
}
94+
pb.recorder.Eventf(ref, eventType, reason, message, args...)
95+
}
96+
8397
// probe probes the container.
8498
func (pb *prober) probe(probeType probeType, pod *v1.Pod, status v1.PodStatus, container v1.Container, containerID kubecontainer.ContainerID) (results.Result, error) {
8599
var probeSpec *v1.Probe
@@ -103,27 +117,17 @@ func (pb *prober) probe(probeType probeType, pod *v1.Pod, status v1.PodStatus, c
103117
result, output, err := pb.runProbeWithRetries(probeType, probeSpec, pod, status, container, containerID, maxProbeRetries)
104118
if err != nil || (result != probe.Success && result != probe.Warning) {
105119
// Probe failed in one way or another.
106-
ref, hasRef := pb.refManager.GetRef(containerID)
107-
if !hasRef {
108-
klog.Warningf("No ref for container %q (%s)", containerID.String(), ctrName)
109-
}
110120
if err != nil {
111121
klog.V(1).Infof("%s probe for %q errored: %v", probeType, ctrName, err)
112-
if hasRef {
113-
pb.recorder.Eventf(ref, v1.EventTypeWarning, events.ContainerUnhealthy, "%s probe errored: %v", probeType, err)
114-
}
122+
pb.recordContainerEvent(pod, &container, containerID, v1.EventTypeWarning, events.ContainerUnhealthy, "%s probe errored: %v", probeType, err)
115123
} else { // result != probe.Success
116124
klog.V(1).Infof("%s probe for %q failed (%v): %s", probeType, ctrName, result, output)
117-
if hasRef {
118-
pb.recorder.Eventf(ref, v1.EventTypeWarning, events.ContainerUnhealthy, "%s probe failed: %s", probeType, output)
119-
}
125+
pb.recordContainerEvent(pod, &container, containerID, v1.EventTypeWarning, events.ContainerUnhealthy, "%s probe failed: %v", probeType, output)
120126
}
121127
return results.Failure, err
122128
}
123129
if result == probe.Warning {
124-
if ref, hasRef := pb.refManager.GetRef(containerID); hasRef {
125-
pb.recorder.Eventf(ref, v1.EventTypeWarning, events.ContainerProbeWarning, "%s probe warning: %s", probeType, output)
126-
}
130+
pb.recordContainerEvent(pod, &container, containerID, v1.EventTypeWarning, events.ContainerProbeWarning, "%s probe warning: %v", probeType, output)
127131
klog.V(3).Infof("%s probe for %q succeeded with a warning: %s", probeType, ctrName, output)
128132
} else {
129133
klog.V(3).Infof("%s probe for %q succeeded", probeType, ctrName)

0 commit comments

Comments
 (0)