pkg/kubelet/server: migrate to structured logs

This commit is contained in:
chenyw1990 2021-02-01 10:47:38 +08:00
parent 765249c524
commit db5de9169f
8 changed files with 19 additions and 21 deletions

View File

@ -109,7 +109,7 @@ func (n nodeAuthorizerAttributesGetter) GetRequestAttributes(u user.Info, r *htt
attrs.Subresource = "spec"
}
klog.V(5).Infof("Node request attributes: user=%#v attrs=%#v", attrs.GetUser(), attrs)
klog.V(5).InfoS("Node request attributes", "user", attrs.GetUser().GetName(), "verb", attrs.GetVerb(), "resource", attrs.GetResource(), "subresource", attrs.GetSubresource())
return attrs
}

View File

@ -145,7 +145,7 @@ func ListenAndServeKubeletServer(
enableDebuggingHandlers,
enableContentionProfiling,
enableSystemLogHandler bool) {
klog.Infof("Starting to listen on %s:%d", address, port)
klog.InfoS("Starting to listen", "address", address, "port", port)
handler := NewServer(host, resourceAnalyzer, auth, enableCAdvisorJSONEndpoints, enableDebuggingHandlers, enableContentionProfiling, enableSystemLogHandler)
s := &http.Server{
Addr: net.JoinHostPort(address.String(), strconv.FormatUint(uint64(port), 10)),
@ -167,7 +167,7 @@ func ListenAndServeKubeletServer(
// ListenAndServeKubeletReadOnlyServer initializes a server to respond to HTTP network requests on the Kubelet.
func ListenAndServeKubeletReadOnlyServer(host HostInterface, resourceAnalyzer stats.ResourceAnalyzer, address net.IP, port uint, enableCAdvisorJSONEndpoints bool) {
klog.V(1).Infof("Starting to listen read-only on %s:%d", address, port)
klog.InfoS("Starting to listen read-only", "address", address, "port", port)
s := NewServer(host, resourceAnalyzer, nil, enableCAdvisorJSONEndpoints, false, false, false)
server := &http.Server{
@ -256,7 +256,7 @@ func (s *Server) InstallAuthFilter() {
// Authenticate
info, ok, err := s.auth.AuthenticateRequest(req.Request)
if err != nil {
klog.Errorf("Unable to authenticate the request due to an error: %v", err)
klog.ErrorS(err, "Unable to authenticate the request due to an error")
resp.WriteErrorString(http.StatusUnauthorized, "Unauthorized")
return
}
@ -271,14 +271,14 @@ func (s *Server) InstallAuthFilter() {
// Authorize
decision, _, err := s.auth.Authorize(req.Request.Context(), attrs)
if err != nil {
klog.ErrorS(err, "Authorization error", "user", attrs.GetUser().GetName(), "verb", attrs.GetVerb(), "resource", attrs.GetResource(), "subresource", attrs.GetSubresource())
msg := fmt.Sprintf("Authorization error (user=%s, verb=%s, resource=%s, subresource=%s)", attrs.GetUser().GetName(), attrs.GetVerb(), attrs.GetResource(), attrs.GetSubresource())
klog.Errorf(msg, err)
resp.WriteErrorString(http.StatusInternalServerError, msg)
return
}
if decision != authorizer.DecisionAllow {
klog.V(2).InfoS("Forbidden", "user", attrs.GetUser().GetName(), "verb", attrs.GetVerb(), "resource", attrs.GetResource(), "subresource", attrs.GetSubresource())
msg := fmt.Sprintf("Forbidden (user=%s, verb=%s, resource=%s, subresource=%s)", attrs.GetUser().GetName(), attrs.GetVerb(), attrs.GetResource(), attrs.GetSubresource())
klog.V(2).Info(msg)
resp.WriteErrorString(http.StatusForbidden, msg)
return
}
@ -407,7 +407,7 @@ const pprofBasePath = "/debug/pprof/"
// InstallDebuggingHandlers registers the HTTP request patterns that serve logs or run commands/containers
func (s *Server) InstallDebuggingHandlers() {
klog.Infof("Adding debug handlers to kubelet server.")
klog.InfoS("Adding debug handlers to kubelet server")
s.addMetricsBucketMatcher("run")
ws := new(restful.WebService)
@ -744,7 +744,7 @@ func getPortForwardRequestParams(req *restful.Request) portForwardRequestParams
type responder struct{}
func (r *responder) Error(w http.ResponseWriter, req *http.Request, err error) {
klog.Errorf("Error while proxying request: %v", err)
klog.ErrorS(err, "Error while proxying request")
http.Error(w, err.Error(), http.StatusInternalServerError)
}
@ -833,7 +833,7 @@ func writeJSONResponse(response *restful.Response, data []byte) {
response.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
response.WriteHeader(http.StatusOK)
if _, err := response.Write(data); err != nil {
klog.Errorf("Error writing response: %v", err)
klog.ErrorS(err, "Error writing response")
}
}

View File

@ -18,7 +18,6 @@ go_library(
"//pkg/kubelet/cm:go_default_library",
"//pkg/kubelet/container:go_default_library",
"//pkg/kubelet/util:go_default_library",
"//pkg/kubelet/util/format:go_default_library",
"//pkg/volume:go_default_library",
"//staging/src/k8s.io/api/core/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",

View File

@ -58,10 +58,10 @@ func newFsResourceAnalyzer(statsProvider Provider, calcVolumePeriod time.Duratio
func (s *fsResourceAnalyzer) Start() {
s.startOnce.Do(func() {
if s.calcPeriod <= 0 {
klog.Info("Volume stats collection disabled.")
klog.InfoS("Volume stats collection disabled")
return
}
klog.Info("Starting FS ResourceAnalyzer")
klog.InfoS("Starting FS ResourceAnalyzer")
go wait.Forever(func() { s.updateCachedPodVolumeStats() }, s.calcPeriod)
})
}

View File

@ -259,7 +259,7 @@ func (h *handler) handleSystemContainer(request *restful.Request, response *rest
if err != nil {
if _, ok := stats[containerName]; ok {
// If the failure is partial, log it and return a best-effort response.
klog.Errorf("Partial failure issuing GetRawContainerInfo(%v): %v", query, err)
klog.ErrorS(err, "Partial failure issuing GetRawContainerInfo", "query", query)
} else {
handleError(response, fmt.Sprintf("/stats/container %v", query), err)
return
@ -295,7 +295,7 @@ func (h *handler) handlePodContainer(request *restful.Request, response *restful
pod, ok := h.provider.GetPodByName(params["namespace"], params["podName"])
if !ok {
klog.V(4).Infof("Container not found: %v", params)
klog.V(4).InfoS("Container not found", "pod", klog.KRef(params["namespace"], params["podName"]))
response.WriteError(http.StatusNotFound, kubecontainer.ErrContainerNotFound)
return
}
@ -314,7 +314,7 @@ func (h *handler) handlePodContainer(request *restful.Request, response *restful
func writeResponse(response *restful.Response, stats interface{}) {
if err := response.WriteAsJson(stats); err != nil {
klog.Errorf("Error writing response: %v", err)
klog.ErrorS(err, "Error writing response")
}
}
@ -326,7 +326,7 @@ func handleError(response *restful.Response, request string, err error) {
response.WriteError(http.StatusNotFound, err)
default:
msg := fmt.Sprintf("Internal Error: %v", err)
klog.Errorf("HTTP InternalServerError serving %s: %s", request, msg)
klog.ErrorS(err, "HTTP InternalServerError serving", "request", request)
response.WriteErrorString(http.StatusInternalServerError, msg)
}
}

View File

@ -54,7 +54,7 @@ func NewSummaryProvider(statsProvider Provider) SummaryProvider {
bootTime, err := util.GetBootTime()
if err != nil {
// bootTime will be zero if we encounter an error getting the boot time.
klog.Warningf("Error getting system boot time. Node metrics will have an incorrect start time: %v", err)
klog.InfoS("Error getting system boot time. Node metrics will have an incorrect start time", "err", err)
}
return &summaryProviderImpl{

View File

@ -44,7 +44,7 @@ func (sp *summaryProviderImpl) GetSystemContainersStats(nodeConfig cm.NodeConfig
}
s, _, err := sp.provider.GetCgroupStats(cont.name, cont.forceStatsUpdate)
if err != nil {
klog.Errorf("Failed to get system container stats for %q: %v", cont.name, err)
klog.ErrorS(err, "Failed to get system container stats", "containerName", cont.name)
continue
}
// System containers don't have a filesystem associated with them.
@ -79,7 +79,7 @@ func (sp *summaryProviderImpl) GetSystemContainersCPUAndMemoryStats(nodeConfig c
}
s, err := sp.provider.GetCgroupCPUAndMemoryStats(cont.name, cont.forceStatsUpdate)
if err != nil {
klog.Errorf("Failed to get system container stats for %q: %v", cont.name, err)
klog.ErrorS(err, "Failed to get system container stats", "containerName", cont.name)
continue
}
s.Name = sys

View File

@ -24,7 +24,6 @@ import (
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/wait"
stats "k8s.io/kubelet/pkg/apis/stats/v1alpha1"
"k8s.io/kubernetes/pkg/kubelet/util/format"
"k8s.io/kubernetes/pkg/volume"
"k8s.io/klog/v2"
@ -109,7 +108,7 @@ func (s *volumeStatCalculator) calcAndStoreStats() {
if err != nil {
// Expected for Volumes that don't support Metrics
if !volume.IsNotSupported(err) {
klog.V(4).Infof("Failed to calculate volume metrics for pod %s volume %s: %+v", format.Pod(s.pod), name, err)
klog.V(4).InfoS("Failed to calculate volume metrics", "pod", klog.KObj(s.pod), "podUID", s.pod.UID, "volumeName", name, "err", err)
}
continue
}