Merge pull request #99848 from qingwave/structred-log-kubelet-preemption

Migrate kubelet/preemption and kubelet/logs to structured logging
This commit is contained in:
Kubernetes Prow Robot 2021-03-16 14:49:21 -07:00 committed by GitHub
commit 97f59e9431
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 11 deletions

View File

@ -179,7 +179,7 @@ func (c *containerLogManager) Start() {
// Start a goroutine periodically does container log rotation.
go wait.Forever(func() {
if err := c.rotateLogs(); err != nil {
klog.Errorf("Failed to rotate container logs: %v", err)
klog.ErrorS(err, "Failed to rotate container logs")
}
}, logMonitorPeriod)
}
@ -226,27 +226,27 @@ func (c *containerLogManager) rotateLogs() error {
// Note that we should not block log rotate for an error of a single container.
status, err := c.runtimeService.ContainerStatus(id)
if err != nil {
klog.Errorf("Failed to get container status for %q: %v", id, err)
klog.ErrorS(err, "Failed to get container status", "containerID", id)
continue
}
path := status.GetLogPath()
info, err := c.osInterface.Stat(path)
if err != nil {
if !os.IsNotExist(err) {
klog.Errorf("Failed to stat container log %q: %v", path, err)
klog.ErrorS(err, "Failed to stat container log", "path", path)
continue
}
// In rotateLatestLog, there are several cases that we may
// lose original container log after ReopenContainerLog fails.
// We try to recover it by reopening container log.
if err := c.runtimeService.ReopenContainerLog(id); err != nil {
klog.Errorf("Container %q log %q doesn't exist, reopen container log failed: %v", id, path, err)
klog.ErrorS(err, "Container log doesn't exist, reopen container log failed", "containerID", id, "path", path)
continue
}
// The container log should be recovered.
info, err = c.osInterface.Stat(path)
if err != nil {
klog.Errorf("Failed to stat container log %q after reopen: %v", path, err)
klog.ErrorS(err, "Failed to stat container log after reopen", "path", path)
continue
}
}
@ -255,7 +255,7 @@ func (c *containerLogManager) rotateLogs() error {
}
// Perform log rotation.
if err := c.rotateLog(id, path); err != nil {
klog.Errorf("Failed to rotate log %q for container %q: %v", path, id, err)
klog.ErrorS(err, "Failed to rotate log for container", "path", path, "containerID", id)
continue
}
}
@ -412,7 +412,7 @@ func (c *containerLogManager) rotateLatestLog(id, log string) error {
// This shouldn't happen.
// Report an error if this happens, because we will lose original
// log.
klog.Errorf("Failed to rename rotated log %q back to %q: %v, reopen container log error: %v", rotated, log, renameErr, err)
klog.ErrorS(renameErr, "Failed to rename rotated log", "rotatedLog", rotated, "newLog", log, "containerID", id)
}
return fmt.Errorf("failed to reopen container log %q: %v", id, err)
}

View File

@ -30,7 +30,6 @@ import (
"k8s.io/kubernetes/pkg/kubelet/lifecycle"
"k8s.io/kubernetes/pkg/kubelet/metrics"
kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
"k8s.io/kubernetes/pkg/kubelet/util/format"
)
const message = "Preempted in order to admit critical pod"
@ -95,7 +94,8 @@ func (c *CriticalPodAdmissionHandler) evictPodsToFreeRequests(admitPod *v1.Pod,
if err != nil {
return fmt.Errorf("preemption: error finding a set of pods to preempt: %v", err)
}
klog.Infof("preemption: attempting to evict pods %v, in order to free up resources: %s", podsToPreempt, insufficientResources.toString())
klog.InfoS("Attempting to evict pods in order to free up resources", "pods", podsToPreempt,
"insufficientResources", insufficientResources.toString())
for _, pod := range podsToPreempt {
status := v1.PodStatus{
Phase: v1.PodFailed,
@ -107,7 +107,7 @@ func (c *CriticalPodAdmissionHandler) evictPodsToFreeRequests(admitPod *v1.Pod,
// this is a blocking call and should only return when the pod and its containers are killed.
err := c.killPodFunc(pod, status, nil)
if err != nil {
klog.Warningf("preemption: pod %s failed to evict %v", format.Pod(pod), err)
klog.ErrorS(err, "Failed to evict pod", "pod", klog.KObj(pod))
// In future syncPod loops, the kubelet will retry the pod deletion steps that it was stuck on.
continue
}
@ -116,7 +116,7 @@ func (c *CriticalPodAdmissionHandler) evictPodsToFreeRequests(admitPod *v1.Pod,
} else {
metrics.Preemptions.WithLabelValues("").Inc()
}
klog.Infof("preemption: pod %s evicted successfully", format.Pod(pod))
klog.InfoS("Pod evicted successfully", "pod", klog.KObj(pod))
}
return nil
}