@@ -80,6 +80,20 @@ func newProber(
80
80
}
81
81
}
82
82
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
+
83
97
// probe probes the container.
84
98
func (pb * prober ) probe (probeType probeType , pod * v1.Pod , status v1.PodStatus , container v1.Container , containerID kubecontainer.ContainerID ) (results.Result , error ) {
85
99
var probeSpec * v1.Probe
@@ -103,27 +117,17 @@ func (pb *prober) probe(probeType probeType, pod *v1.Pod, status v1.PodStatus, c
103
117
result , output , err := pb .runProbeWithRetries (probeType , probeSpec , pod , status , container , containerID , maxProbeRetries )
104
118
if err != nil || (result != probe .Success && result != probe .Warning ) {
105
119
// 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
- }
110
120
if err != nil {
111
121
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 )
115
123
} else { // result != probe.Success
116
124
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 )
120
126
}
121
127
return results .Failure , err
122
128
}
123
129
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 )
127
131
klog .V (3 ).Infof ("%s probe for %q succeeded with a warning: %s" , probeType , ctrName , output )
128
132
} else {
129
133
klog .V (3 ).Infof ("%s probe for %q succeeded" , probeType , ctrName )
0 commit comments