Skip to content

Commit 862aa6d

Browse files
authored
Merge pull request kubernetes#99970 from krzysiekg/structured_logging_pkg_kubelet_kuberuntime
Migrate pkg/kubelet/kuberuntime to structured logging
2 parents b5c6434 + 629d5ab commit 862aa6d

8 files changed

+50
-49
lines changed

pkg/kubelet/kuberuntime/helpers.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func toRuntimeProtocol(protocol v1.Protocol) runtimeapi.Protocol {
8181
return runtimeapi.Protocol_SCTP
8282
}
8383

84-
klog.Warningf("Unknown protocol %q: defaulting to TCP", protocol)
84+
klog.InfoS("Unknown protocol, defaulting to TCP", "protocol", protocol)
8585
return runtimeapi.Protocol_TCP
8686
}
8787

pkg/kubelet/kuberuntime/kuberuntime_container_linux.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ func GetHugepageLimitsFromResources(resources v1.ResourceRequirements) []*runtim
112112

113113
pageSize, err := v1helper.HugePageSizeFromResourceName(resourceObj)
114114
if err != nil {
115-
klog.Warningf("Failed to get hugepage size from resource name: %v", err)
115+
klog.InfoS("Failed to get hugepage size from resource", "object", resourceObj, "err", err)
116116
continue
117117
}
118118

119119
sizeString, err := v1helper.HugePageUnitSizeFromByteSize(pageSize.Value())
120120
if err != nil {
121-
klog.Warningf("pageSize is invalid: %v", err)
121+
klog.InfoS("Size is invalid", "object", resourceObj, "err", err)
122122
continue
123123
}
124124
requiredHugepageLimits[sizeString] = uint64(amountObj.Value())

pkg/kubelet/kuberuntime/kuberuntime_container_windows.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (m *kubeGenericRuntimeManager) generateWindowsContainerConfig(container *v1
9797
if wc.Resources.CpuCount > 0 {
9898
if wc.Resources.CpuMaximum > 0 {
9999
wc.Resources.CpuMaximum = 0
100-
klog.Warningf("Mutually exclusive options: CPUCount priority > CPUMaximum priority on Windows Server Containers. CPUMaximum should be ignored")
100+
klog.InfoS("Mutually exclusive options: CPUCount priority > CPUMaximum priority on Windows Server Containers. CPUMaximum should be ignored")
101101
}
102102
}
103103

pkg/kubelet/kuberuntime/kuberuntime_image.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ func (m *kubeGenericRuntimeManager) PullImage(image kubecontainer.ImageSpec, pul
4444

4545
creds, withCredentials := keyring.Lookup(repoToPull)
4646
if !withCredentials {
47-
klog.V(3).Infof("Pulling image %q without credentials", img)
47+
klog.V(3).InfoS("Pulling image without credentials", "image", img)
4848

4949
imageRef, err := m.imageService.PullImage(imgSpec, nil, podSandboxConfig)
5050
if err != nil {
51-
klog.Errorf("Pull image %q failed: %v", img, err)
51+
klog.ErrorS(err, "Failed to pull image", "image", img)
5252
return "", err
5353
}
5454

@@ -83,7 +83,7 @@ func (m *kubeGenericRuntimeManager) PullImage(image kubecontainer.ImageSpec, pul
8383
func (m *kubeGenericRuntimeManager) GetImageRef(image kubecontainer.ImageSpec) (string, error) {
8484
status, err := m.imageService.ImageStatus(toRuntimeAPIImageSpec(image))
8585
if err != nil {
86-
klog.Errorf("ImageStatus for image %q failed: %v", image, err)
86+
klog.ErrorS(err, "Failed to get image status", "image", image.Image)
8787
return "", err
8888
}
8989
if status == nil {
@@ -98,7 +98,7 @@ func (m *kubeGenericRuntimeManager) ListImages() ([]kubecontainer.Image, error)
9898

9999
allImages, err := m.imageService.ListImages(nil)
100100
if err != nil {
101-
klog.Errorf("ListImages failed: %v", err)
101+
klog.ErrorS(err, "Failed to list images")
102102
return nil, err
103103
}
104104

@@ -119,7 +119,7 @@ func (m *kubeGenericRuntimeManager) ListImages() ([]kubecontainer.Image, error)
119119
func (m *kubeGenericRuntimeManager) RemoveImage(image kubecontainer.ImageSpec) error {
120120
err := m.imageService.RemoveImage(&runtimeapi.ImageSpec{Image: image.Image})
121121
if err != nil {
122-
klog.Errorf("Remove image %q failed: %v", image.Image, err)
122+
klog.ErrorS(err, "Failed to remove image", "image", image.Image)
123123
return err
124124
}
125125

@@ -133,7 +133,7 @@ func (m *kubeGenericRuntimeManager) RemoveImage(image kubecontainer.ImageSpec) e
133133
func (m *kubeGenericRuntimeManager) ImageStats() (*kubecontainer.ImageStats, error) {
134134
allImages, err := m.imageService.ListImages(nil)
135135
if err != nil {
136-
klog.Errorf("ListImages failed: %v", err)
136+
klog.ErrorS(err, "Failed to list images")
137137
return nil, err
138138
}
139139
stats := &kubecontainer.ImageStats{}

pkg/kubelet/kuberuntime/kuberuntime_sandbox.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -37,35 +37,35 @@ import (
3737
func (m *kubeGenericRuntimeManager) createPodSandbox(pod *v1.Pod, attempt uint32) (string, string, error) {
3838
podSandboxConfig, err := m.generatePodSandboxConfig(pod, attempt)
3939
if err != nil {
40-
message := fmt.Sprintf("GeneratePodSandboxConfig for pod %q failed: %v", format.Pod(pod), err)
41-
klog.Error(message)
40+
message := fmt.Sprintf("Failed to generate sandbox config for pod %q: %v", format.Pod(pod), err)
41+
klog.ErrorS(err, "Failed to generate sandbox config for pod", "pod", klog.KObj(pod))
4242
return "", message, err
4343
}
4444

4545
// Create pod logs directory
4646
err = m.osInterface.MkdirAll(podSandboxConfig.LogDirectory, 0755)
4747
if err != nil {
48-
message := fmt.Sprintf("Create pod log directory for pod %q failed: %v", format.Pod(pod), err)
49-
klog.Errorf(message)
48+
message := fmt.Sprintf("Failed to create log directory for pod %q: %v", format.Pod(pod), err)
49+
klog.ErrorS(err, "Failed to create log directory for pod", "pod", klog.KObj(pod))
5050
return "", message, err
5151
}
5252

5353
runtimeHandler := ""
5454
if m.runtimeClassManager != nil {
5555
runtimeHandler, err = m.runtimeClassManager.LookupRuntimeHandler(pod.Spec.RuntimeClassName)
5656
if err != nil {
57-
message := fmt.Sprintf("CreatePodSandbox for pod %q failed: %v", format.Pod(pod), err)
57+
message := fmt.Sprintf("Failed to create sandbox for pod %q: %v", format.Pod(pod), err)
5858
return "", message, err
5959
}
6060
if runtimeHandler != "" {
61-
klog.V(2).Infof("Running pod %s with RuntimeHandler %q", format.Pod(pod), runtimeHandler)
61+
klog.V(2).InfoS("Running pod with runtime handler", "pod", klog.KObj(pod), "runtimeHandler", runtimeHandler)
6262
}
6363
}
6464

6565
podSandBoxID, err := m.runtimeService.RunPodSandbox(podSandboxConfig, runtimeHandler)
6666
if err != nil {
67-
message := fmt.Sprintf("CreatePodSandbox for pod %q failed: %v", format.Pod(pod), err)
68-
klog.Error(message)
67+
message := fmt.Sprintf("Failed to create sandbox for pod %q: %v", format.Pod(pod), err)
68+
klog.ErrorS(err, "Failed to create sandbox for pod", "pod", klog.KObj(pod))
6969
return "", message, err
7070
}
7171

@@ -220,7 +220,7 @@ func (m *kubeGenericRuntimeManager) getKubeletSandboxes(all bool) ([]*runtimeapi
220220

221221
resp, err := m.runtimeService.ListPodSandbox(filter)
222222
if err != nil {
223-
klog.Errorf("ListPodSandbox failed: %v", err)
223+
klog.ErrorS(err, "Failed to list pod sandboxes")
224224
return nil, err
225225
}
226226

@@ -231,7 +231,7 @@ func (m *kubeGenericRuntimeManager) getKubeletSandboxes(all bool) ([]*runtimeapi
231231
func (m *kubeGenericRuntimeManager) determinePodSandboxIPs(podNamespace, podName string, podSandbox *runtimeapi.PodSandboxStatus) []string {
232232
podIPs := make([]string, 0)
233233
if podSandbox.Network == nil {
234-
klog.Warningf("Pod Sandbox status doesn't have network information, cannot report IPs")
234+
klog.InfoS("Pod Sandbox status doesn't have network information, cannot report IPs", "pod", klog.KRef(podNamespace, podName))
235235
return podIPs
236236
}
237237

@@ -241,7 +241,7 @@ func (m *kubeGenericRuntimeManager) determinePodSandboxIPs(podNamespace, podName
241241
// pick primary IP
242242
if len(podSandbox.Network.Ip) != 0 {
243243
if net.ParseIP(podSandbox.Network.Ip) == nil {
244-
klog.Warningf("Pod Sandbox reported an unparseable IP (Primary) %v", podSandbox.Network.Ip)
244+
klog.InfoS("Pod Sandbox reported an unparseable primary IP", "pod", klog.KRef(podNamespace, podName), "IP", podSandbox.Network.Ip)
245245
return nil
246246
}
247247
podIPs = append(podIPs, podSandbox.Network.Ip)
@@ -250,7 +250,7 @@ func (m *kubeGenericRuntimeManager) determinePodSandboxIPs(podNamespace, podName
250250
// pick additional ips, if cri reported them
251251
for _, podIP := range podSandbox.Network.AdditionalIps {
252252
if nil == net.ParseIP(podIP.Ip) {
253-
klog.Warningf("Pod Sandbox reported an unparseable IP (additional) %v", podIP.Ip)
253+
klog.InfoS("Pod Sandbox reported an unparseable additional IP", "pod", klog.KRef(podNamespace, podName), "IP", podIP.Ip)
254254
return nil
255255
}
256256
podIPs = append(podIPs, podIP.Ip)
@@ -272,7 +272,7 @@ func (m *kubeGenericRuntimeManager) getSandboxIDByPodUID(podUID kubetypes.UID, s
272272
}
273273
sandboxes, err := m.runtimeService.ListPodSandbox(filter)
274274
if err != nil {
275-
klog.Errorf("ListPodSandbox with pod UID %q failed: %v", podUID, err)
275+
klog.ErrorS(err, "Failed to list sandboxes for pod", "podUID", podUID)
276276
return nil, err
277277
}
278278

pkg/kubelet/kuberuntime/labels.go

+12-13
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ import (
2020
"encoding/json"
2121
"strconv"
2222

23-
"k8s.io/api/core/v1"
23+
v1 "k8s.io/api/core/v1"
2424
kubetypes "k8s.io/apimachinery/pkg/types"
2525
"k8s.io/klog/v2"
2626
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
2727
"k8s.io/kubernetes/pkg/kubelet/types"
28-
"k8s.io/kubernetes/pkg/kubelet/util/format"
2928
)
3029

3130
const (
@@ -129,7 +128,7 @@ func newContainerAnnotations(container *v1.Container, pod *v1.Pod, restartCount
129128
// Using json encoding so that the PreStop handler object is readable after writing as a label
130129
rawPreStop, err := json.Marshal(container.Lifecycle.PreStop)
131130
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))
133132
} else {
134133
annotations[containerPreStopHandlerLabel] = string(rawPreStop)
135134
}
@@ -138,7 +137,7 @@ func newContainerAnnotations(container *v1.Container, pod *v1.Pod, restartCount
138137
if len(container.Ports) > 0 {
139138
rawContainerPorts, err := json.Marshal(container.Ports)
140139
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))
142141
} else {
143142
annotations[containerPortsLabel] = string(rawContainerPorts)
144143
}
@@ -192,28 +191,28 @@ func getContainerInfoFromAnnotations(annotations map[string]string) *annotatedCo
192191
}
193192

194193
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)
196195
}
197196
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)
199198
}
200199
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)
202201
}
203202
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)
205204
}
206205

207206
preStopHandler := &v1.Handler{}
208207
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)
210209
} else if found {
211210
containerInfo.PreStopHandler = preStopHandler
212211
}
213212

214213
containerPorts := []v1.ContainerPort{}
215214
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)
217216
} else if found {
218217
containerInfo.ContainerPorts = containerPorts
219218
}
@@ -226,7 +225,7 @@ func getStringValueFromLabel(labels map[string]string, label string) string {
226225
return value
227226
}
228227
// 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)
230229
// Return empty string "" for these containers, the caller will get value by other ways.
231230
return ""
232231
}
@@ -241,7 +240,7 @@ func getIntValueFromLabel(labels map[string]string, label string) (int, error) {
241240
return intValue, nil
242241
}
243242
// 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)
245244
// Just set the value to 0
246245
return 0, nil
247246
}
@@ -256,7 +255,7 @@ func getUint64ValueFromLabel(labels map[string]string, label string) (uint64, er
256255
return intValue, nil
257256
}
258257
// 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)
260259
// Just set the value to 0
261260
return 0, nil
262261
}

pkg/kubelet/kuberuntime/logs/logs.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232
"github.com/fsnotify/fsnotify"
3333
"k8s.io/klog/v2"
3434

35-
"k8s.io/api/core/v1"
35+
v1 "k8s.io/api/core/v1"
3636
internalapi "k8s.io/cri-api/pkg/apis"
3737
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
3838
"k8s.io/kubernetes/pkg/kubelet/types"
@@ -316,7 +316,7 @@ func ReadLogs(ctx context.Context, path, containerID string, opts *LogOptions, r
316316
msg := &logMessage{}
317317
for {
318318
if stop || (limitedMode && limitedNum == 0) {
319-
klog.V(2).Infof("Finish parsing log file %q", path)
319+
klog.V(2).InfoS("Finished parsing log file", "path", path)
320320
return nil
321321
}
322322
l, err := r.ReadBytes(eol[0])
@@ -363,7 +363,7 @@ func ReadLogs(ctx context.Context, path, containerID string, opts *LogOptions, r
363363
}
364364
f.Close()
365365
if err := watcher.Remove(f.Name()); err != nil && !os.IsNotExist(err) {
366-
klog.Errorf("failed to remove file watch %q: %v", f.Name(), err)
366+
klog.ErrorS(err, "Failed to remove file watch", "path", f.Name())
367367
}
368368
f = newF
369369
if err := watcher.Add(f.Name()); err != nil {
@@ -379,7 +379,7 @@ func ReadLogs(ctx context.Context, path, containerID string, opts *LogOptions, r
379379
if len(l) == 0 {
380380
continue
381381
}
382-
klog.Warningf("Incomplete line in log file %q: %q", path, l)
382+
klog.InfoS("Incomplete line in log file", "path", path, "line", l)
383383
}
384384
if parse == nil {
385385
// Initialize the log parsing function.
@@ -391,16 +391,16 @@ func ReadLogs(ctx context.Context, path, containerID string, opts *LogOptions, r
391391
// Parse the log line.
392392
msg.reset()
393393
if err := parse(l, msg); err != nil {
394-
klog.Errorf("Failed with err %v when parsing log for log file %q: %q", err, path, l)
394+
klog.ErrorS(err, "Failed when parsing line in log file", "path", path, "line", l)
395395
continue
396396
}
397397
// Write the log line into the stream.
398398
if err := writer.write(msg); err != nil {
399399
if err == errMaximumWrite {
400-
klog.V(2).Infof("Finish parsing log file %q, hit bytes limit %d(bytes)", path, opts.bytes)
400+
klog.V(2).InfoS("Finished parsing log file, hit bytes limit", "path", path, "limit", opts.bytes)
401401
return nil
402402
}
403-
klog.Errorf("Failed with err %v when writing log for log file %q: %+v", err, path, msg)
403+
klog.ErrorS(err, "Failed when writing line to log file", "path", path, "line", msg)
404404
return err
405405
}
406406
if limitedMode {
@@ -417,7 +417,7 @@ func isContainerRunning(id string, r internalapi.RuntimeService) (bool, error) {
417417
}
418418
// Only keep following container log when it is running.
419419
if s.State != runtimeapi.ContainerState_CONTAINER_RUNNING {
420-
klog.V(5).Infof("Container %q is not running (state=%q)", id, s.State)
420+
klog.V(5).InfoS("Container is not running", "containerId", id, "state", s.State)
421421
// Do not return error because it's normal that the container stops
422422
// during waiting.
423423
return false, nil
@@ -451,10 +451,10 @@ func waitLogs(ctx context.Context, id string, w *fsnotify.Watcher, runtimeServic
451451
case fsnotify.Chmod:
452452
return true, true, nil
453453
default:
454-
klog.Errorf("Unexpected fsnotify event: %v, retrying...", e)
454+
klog.ErrorS(nil, "Received unexpected fsnotify event, retrying", "event", e)
455455
}
456456
case err := <-w.Errors:
457-
klog.Errorf("Fsnotify watch error: %v, %d error retries remaining", err, errRetry)
457+
klog.ErrorS(err, "Received fsnotify watch error, retrying unless no more retries left", "retries", errRetry)
458458
if errRetry == 0 {
459459
return false, false, err
460460
}

pkg/kubelet/kuberuntime/security_context_windows.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ func verifyRunAsNonRoot(pod *v1.Pod, container *v1.Container, uid *int64, userna
4343
return nil
4444
}
4545
if effectiveSc.RunAsUser != nil {
46-
klog.Warningf("Windows container does not support SecurityContext.RunAsUser, please use SecurityContext.WindowsOptions (pod: %q, container: %s)", format.Pod(pod), container.Name)
46+
klog.InfoS("Windows container does not support SecurityContext.RunAsUser, please use SecurityContext.WindowsOptions",
47+
"pod", klog.KObj(pod), "containerName", container.Name)
4748
}
4849
if effectiveSc.SELinuxOptions != nil {
49-
klog.Warningf("Windows container does not support SecurityContext.SELinuxOptions, please use SecurityContext.WindowsOptions (pod: %q, container: %s)", format.Pod(pod), container.Name)
50+
klog.InfoS("Windows container does not support SecurityContext.SELinuxOptions, please use SecurityContext.WindowsOptions",
51+
"pod", klog.KObj(pod), "containerName", container.Name)
5052
}
5153
if effectiveSc.RunAsGroup != nil {
52-
klog.Warningf("Windows container does not support SecurityContext.RunAsGroup (pod: %q, container: %s)", format.Pod(pod), container.Name)
54+
klog.InfoS("Windows container does not support SecurityContext.RunAsGroup", "pod", klog.KObj(pod), "containerName", container.Name)
5355
}
5456
if effectiveSc.WindowsOptions != nil {
5557
if effectiveSc.WindowsOptions.RunAsUserName != nil {

0 commit comments

Comments
 (0)