From a9b7dcc8c21b9fbd02821bc96e0f638089875814 Mon Sep 17 00:00:00 2001 From: Peter Hunt Date: Mon, 21 Jun 2021 13:05:25 -0400 Subject: [PATCH] kubelet: update remote runtimes for cri stat changes Signed-off-by: Peter Hunt --- pkg/kubelet/cri/remote/fake/fake_runtime.go | 21 ++++++++++ pkg/kubelet/cri/remote/remote_runtime.go | 40 +++++++++++++++++++ pkg/kubelet/dockershim/docker_stats.go | 15 +++++++ .../kuberuntime/instrumented_services.go | 18 +++++++++ 4 files changed, 94 insertions(+) diff --git a/pkg/kubelet/cri/remote/fake/fake_runtime.go b/pkg/kubelet/cri/remote/fake/fake_runtime.go index e49f311aa18..6329480cdc1 100644 --- a/pkg/kubelet/cri/remote/fake/fake_runtime.go +++ b/pkg/kubelet/cri/remote/fake/fake_runtime.go @@ -262,6 +262,27 @@ func (f *RemoteRuntime) ListContainerStats(ctx context.Context, req *kubeapi.Lis return &kubeapi.ListContainerStatsResponse{Stats: stats}, nil } +// PodSandboxStats returns stats of the pod. If the pod does not +// exist, the call returns an error. +func (f *RemoteRuntime) PodSandboxStats(ctx context.Context, req *kubeapi.PodSandboxStatsRequest) (*kubeapi.PodSandboxStatsResponse, error) { + stats, err := f.RuntimeService.PodSandboxStats(req.PodSandboxId) + if err != nil { + return nil, err + } + + return &kubeapi.PodSandboxStatsResponse{Stats: stats}, nil +} + +// ListPodSandboxStats returns stats of all running pods. +func (f *RemoteRuntime) ListPodSandboxStats(ctx context.Context, req *kubeapi.ListPodSandboxStatsRequest) (*kubeapi.ListPodSandboxStatsResponse, error) { + stats, err := f.RuntimeService.ListPodSandboxStats(req.Filter) + if err != nil { + return nil, err + } + + return &kubeapi.ListPodSandboxStatsResponse{Stats: stats}, nil +} + // UpdateRuntimeConfig updates the runtime configuration based on the given request. func (f *RemoteRuntime) UpdateRuntimeConfig(ctx context.Context, req *kubeapi.UpdateRuntimeConfigRequest) (*kubeapi.UpdateRuntimeConfigResponse, error) { err := f.RuntimeService.UpdateRuntimeConfig(req.RuntimeConfig) diff --git a/pkg/kubelet/cri/remote/remote_runtime.go b/pkg/kubelet/cri/remote/remote_runtime.go index d11e5899a30..15c84a921f5 100644 --- a/pkg/kubelet/cri/remote/remote_runtime.go +++ b/pkg/kubelet/cri/remote/remote_runtime.go @@ -569,6 +569,46 @@ func (r *remoteRuntimeService) ListContainerStats(filter *runtimeapi.ContainerSt return resp.GetStats(), nil } +// PodSandboxStats returns the stats of the pod. +func (r *remoteRuntimeService) PodSandboxStats(podSandboxID string) (*runtimeapi.PodSandboxStats, error) { + klog.V(10).InfoS("[RemoteRuntimeService] PodSandboxStats", "podSandboxID", podSandboxID, "timeout", r.timeout) + ctx, cancel := getContextWithTimeout(r.timeout) + defer cancel() + + resp, err := r.runtimeClient.PodSandboxStats(ctx, &runtimeapi.PodSandboxStatsRequest{ + PodSandboxId: podSandboxID, + }) + if err != nil { + if r.logReduction.ShouldMessageBePrinted(err.Error(), podSandboxID) { + klog.ErrorS(err, "PodSandbox from runtime service failed", "podSandboxID", podSandboxID) + } + return nil, err + } + r.logReduction.ClearID(podSandboxID) + klog.V(10).InfoS("[RemoteRuntimeService] PodSandbox Response", "podSandboxID", podSandboxID, "stats", resp.GetStats()) + + return resp.GetStats(), nil +} + +// ListPodSandboxStats returns the list of pod sandbox stats given the filter +func (r *remoteRuntimeService) ListPodSandboxStats(filter *runtimeapi.PodSandboxStatsFilter) ([]*runtimeapi.PodSandboxStats, error) { + klog.V(10).InfoS("[RemoteRuntimeService] ListPodSandboxStats", "filter", filter) + // Set timeout, because runtimes are able to cache disk stats results + ctx, cancel := getContextWithTimeout(r.timeout) + defer cancel() + + resp, err := r.runtimeClient.ListPodSandboxStats(ctx, &runtimeapi.ListPodSandboxStatsRequest{ + Filter: filter, + }) + if err != nil { + klog.ErrorS(err, "ListPodSandboxStats with filter from runtime service failed", "filter", filter) + return nil, err + } + klog.V(10).InfoS("[RemoteRuntimeService] ListPodSandboxStats Response", "filter", filter, "stats", resp.GetStats()) + + return resp.GetStats(), nil +} + // ReopenContainerLog reopens the container log file. func (r *remoteRuntimeService) ReopenContainerLog(containerID string) error { klog.V(10).InfoS("[RemoteRuntimeService] ReopenContainerLog", "containerID", containerID, "timeout", r.timeout) diff --git a/pkg/kubelet/dockershim/docker_stats.go b/pkg/kubelet/dockershim/docker_stats.go index c68df6279bc..e5c4305d46e 100644 --- a/pkg/kubelet/dockershim/docker_stats.go +++ b/pkg/kubelet/dockershim/docker_stats.go @@ -20,10 +20,13 @@ package dockershim import ( "context" + "errors" runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2" ) +var ErrNotImplemented = errors.New("Not implemented") + // ContainerStats returns stats for a container stats request based on container id. func (ds *dockerService) ContainerStats(_ context.Context, r *runtimeapi.ContainerStatsRequest) (*runtimeapi.ContainerStatsResponse, error) { stats, err := ds.getContainerStats(r.ContainerId) @@ -62,3 +65,15 @@ func (ds *dockerService) ListContainerStats(ctx context.Context, r *runtimeapi.L return &runtimeapi.ListContainerStatsResponse{Stats: stats}, nil } + +// PodSandboxStats returns stats for a pod sandbox based on pod sandbox id. +// This function is not implemented for the dockershim. +func (ds *dockerService) PodSandboxStats(_ context.Context, r *runtimeapi.PodSandboxStatsRequest) (*runtimeapi.PodSandboxStatsResponse, error) { + return nil, ErrNotImplemented +} + +// ListPodSandboxStats returns stats for a list of pod sandboxes based on a filter. +// This function is not implemented for the dockershim. +func (ds *dockerService) ListPodSandboxStats(ctx context.Context, r *runtimeapi.ListPodSandboxStatsRequest) (*runtimeapi.ListPodSandboxStatsResponse, error) { + return nil, ErrNotImplemented +} diff --git a/pkg/kubelet/kuberuntime/instrumented_services.go b/pkg/kubelet/kuberuntime/instrumented_services.go index d31807be5ac..91d57b097f9 100644 --- a/pkg/kubelet/kuberuntime/instrumented_services.go +++ b/pkg/kubelet/kuberuntime/instrumented_services.go @@ -244,6 +244,24 @@ func (in instrumentedRuntimeService) ListContainerStats(filter *runtimeapi.Conta return out, err } +func (in instrumentedRuntimeService) PodSandboxStats(podSandboxID string) (*runtimeapi.PodSandboxStats, error) { + const operation = "podsandbox_stats" + defer recordOperation(operation, time.Now()) + + out, err := in.service.PodSandboxStats(podSandboxID) + recordError(operation, err) + return out, err +} + +func (in instrumentedRuntimeService) ListPodSandboxStats(filter *runtimeapi.PodSandboxStatsFilter) ([]*runtimeapi.PodSandboxStats, error) { + const operation = "list_podsandbox_stats" + defer recordOperation(operation, time.Now()) + + out, err := in.service.ListPodSandboxStats(filter) + recordError(operation, err) + return out, err +} + func (in instrumentedRuntimeService) PortForward(req *runtimeapi.PortForwardRequest) (*runtimeapi.PortForwardResponse, error) { const operation = "port_forward" defer recordOperation(operation, time.Now())