Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/kubelet/server: migrate to structured logs #98643

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/kubelet/server/auth.go
Expand Up @@ -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
}
16 changes: 8 additions & 8 deletions pkg/kubelet/server/server.go
Expand Up @@ -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)),
Expand All @@ -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{
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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")
}
}

Expand Down
1 change: 0 additions & 1 deletion pkg/kubelet/server/stats/BUILD
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubelet/server/stats/fs_resource_analyzer.go
Expand Up @@ -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)
})
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/kubelet/server/stats/handler.go
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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")
}
}

Expand All @@ -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)
}
}
2 changes: 1 addition & 1 deletion pkg/kubelet/server/stats/summary.go
Expand Up @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubelet/server/stats/summary_sys_containers.go
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions pkg/kubelet/server/stats/volume_stat_calculator.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down