@@ -20,12 +20,11 @@ import (
20
20
"encoding/json"
21
21
"strconv"
22
22
23
- "k8s.io/api/core/v1"
23
+ v1 "k8s.io/api/core/v1"
24
24
kubetypes "k8s.io/apimachinery/pkg/types"
25
25
"k8s.io/klog/v2"
26
26
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
27
27
"k8s.io/kubernetes/pkg/kubelet/types"
28
- "k8s.io/kubernetes/pkg/kubelet/util/format"
29
28
)
30
29
31
30
const (
@@ -129,7 +128,7 @@ func newContainerAnnotations(container *v1.Container, pod *v1.Pod, restartCount
129
128
// Using json encoding so that the PreStop handler object is readable after writing as a label
130
129
rawPreStop , err := json .Marshal (container .Lifecycle .PreStop )
131
130
if err != nil {
132
- klog .Errorf ( "Unable to marshal lifecycle PreStop handler for container %q of pod %q: %v" , container .Name , format . Pod (pod ), err )
131
+ klog .ErrorS ( err , "Unable to marshal lifecycle PreStop handler for container" , "containerName" , container .Name , "pod" , klog . KObj (pod ))
133
132
} else {
134
133
annotations [containerPreStopHandlerLabel ] = string (rawPreStop )
135
134
}
@@ -138,7 +137,7 @@ func newContainerAnnotations(container *v1.Container, pod *v1.Pod, restartCount
138
137
if len (container .Ports ) > 0 {
139
138
rawContainerPorts , err := json .Marshal (container .Ports )
140
139
if err != nil {
141
- klog .Errorf ( "Unable to marshal container ports for container %q for pod %q: %v" , container .Name , format . Pod (pod ), err )
140
+ klog .ErrorS ( err , "Unable to marshal container ports for container" , "containerName" , container .Name , "pod" , klog . KObj (pod ))
142
141
} else {
143
142
annotations [containerPortsLabel ] = string (rawContainerPorts )
144
143
}
@@ -192,28 +191,28 @@ func getContainerInfoFromAnnotations(annotations map[string]string) *annotatedCo
192
191
}
193
192
194
193
if containerInfo .Hash , err = getUint64ValueFromLabel (annotations , containerHashLabel ); err != nil {
195
- klog .Errorf ( "Unable to get %q from annotations %q: %v" , containerHashLabel , annotations , err )
194
+ klog .ErrorS ( err , "Unable to get label value from annotations" , "label" , containerHashLabel , " annotations" , annotations )
196
195
}
197
196
if containerInfo .RestartCount , err = getIntValueFromLabel (annotations , containerRestartCountLabel ); err != nil {
198
- klog .Errorf ( "Unable to get %q from annotations %q: %v" , containerRestartCountLabel , annotations , err )
197
+ klog .ErrorS ( err , "Unable to get label value from annotations" , "label" , containerRestartCountLabel , " annotations" , annotations )
199
198
}
200
199
if containerInfo .PodDeletionGracePeriod , err = getInt64PointerFromLabel (annotations , podDeletionGracePeriodLabel ); err != nil {
201
- klog .Errorf ( "Unable to get %q from annotations %q: %v" , podDeletionGracePeriodLabel , annotations , err )
200
+ klog .ErrorS ( err , "Unable to get label value from annotations" , "label" , podDeletionGracePeriodLabel , " annotations" , annotations )
202
201
}
203
202
if containerInfo .PodTerminationGracePeriod , err = getInt64PointerFromLabel (annotations , podTerminationGracePeriodLabel ); err != nil {
204
- klog .Errorf ( "Unable to get %q from annotations %q: %v" , podTerminationGracePeriodLabel , annotations , err )
203
+ klog .ErrorS ( err , "Unable to get label value from annotations" , "label" , podTerminationGracePeriodLabel , " annotations" , annotations )
205
204
}
206
205
207
206
preStopHandler := & v1.Handler {}
208
207
if found , err := getJSONObjectFromLabel (annotations , containerPreStopHandlerLabel , preStopHandler ); err != nil {
209
- klog .Errorf ( "Unable to get %q from annotations %q: %v" , containerPreStopHandlerLabel , annotations , err )
208
+ klog .ErrorS ( err , "Unable to get label value from annotations" , "label" , containerPreStopHandlerLabel , " annotations" , annotations )
210
209
} else if found {
211
210
containerInfo .PreStopHandler = preStopHandler
212
211
}
213
212
214
213
containerPorts := []v1.ContainerPort {}
215
214
if found , err := getJSONObjectFromLabel (annotations , containerPortsLabel , & containerPorts ); err != nil {
216
- klog .Errorf ( "Unable to get %q from annotations %q: %v" , containerPortsLabel , annotations , err )
215
+ klog .ErrorS ( err , "Unable to get label value from annotations" , "label" , containerPortsLabel , " annotations" , annotations )
217
216
} else if found {
218
217
containerInfo .ContainerPorts = containerPorts
219
218
}
@@ -226,7 +225,7 @@ func getStringValueFromLabel(labels map[string]string, label string) string {
226
225
return value
227
226
}
228
227
// Do not report error, because there should be many old containers without label now.
229
- klog .V (3 ).Infof ("Container doesn't have label %s , it may be an old or invalid container" , label )
228
+ klog .V (3 ).InfoS ("Container doesn't have requested label , it may be an old or invalid container" , "label " , label )
230
229
// Return empty string "" for these containers, the caller will get value by other ways.
231
230
return ""
232
231
}
@@ -241,7 +240,7 @@ func getIntValueFromLabel(labels map[string]string, label string) (int, error) {
241
240
return intValue , nil
242
241
}
243
242
// Do not report error, because there should be many old containers without label now.
244
- klog .V (3 ).Infof ("Container doesn't have label %s , it may be an old or invalid container" , label )
243
+ klog .V (3 ).InfoS ("Container doesn't have requested label , it may be an old or invalid container" , "label " , label )
245
244
// Just set the value to 0
246
245
return 0 , nil
247
246
}
@@ -256,7 +255,7 @@ func getUint64ValueFromLabel(labels map[string]string, label string) (uint64, er
256
255
return intValue , nil
257
256
}
258
257
// Do not report error, because there should be many old containers without label now.
259
- klog .V (3 ).Infof ("Container doesn't have label %s , it may be an old or invalid container" , label )
258
+ klog .V (3 ).InfoS ("Container doesn't have requested label , it may be an old or invalid container" , "label " , label )
260
259
// Just set the value to 0
261
260
return 0 , nil
262
261
}
0 commit comments