From 6298ce68e2ca502fef534d02ee37aa4c9024b6c6 Mon Sep 17 00:00:00 2001 From: Peter Hunt Date: Mon, 31 Oct 2022 14:59:47 -0400 Subject: [PATCH] kubelet: wire ListPodSandboxMetrics Signed-off-by: Peter Hunt --- pkg/kubelet/container/runtime.go | 8 +++++ pkg/kubelet/container/testing/fake_runtime.go | 16 ++++++++++ pkg/kubelet/container/testing/runtime_mock.go | 30 +++++++++++++++++++ pkg/kubelet/kubelet.go | 10 +++++++ .../kuberuntime/kuberuntime_manager.go | 8 +++++ pkg/kubelet/server/server.go | 2 ++ pkg/kubelet/server/server_test.go | 8 +++++ 7 files changed, 82 insertions(+) diff --git a/pkg/kubelet/container/runtime.go b/pkg/kubelet/container/runtime.go index d4398ae04d8..e53b74bca54 100644 --- a/pkg/kubelet/container/runtime.go +++ b/pkg/kubelet/container/runtime.go @@ -124,6 +124,14 @@ type Runtime interface { CheckpointContainer(ctx context.Context, options *runtimeapi.CheckpointContainerRequest) error // Generate pod status from the CRI event GeneratePodStatus(event *runtimeapi.ContainerEventResponse) (*PodStatus, error) + // ListMetricDescriptors gets the descriptors for the metrics that will be returned in ListPodSandboxMetrics. + // This list should be static at startup: either the client and server restart together when + // adding or removing metrics descriptors, or they should not change. + // Put differently, if ListPodSandboxMetrics references a name that is not described in the initial + // ListMetricDescriptors call, then the metric will not be broadcasted. + ListMetricDescriptors(ctx context.Context) ([]*runtimeapi.MetricDescriptor, error) + // ListPodSandboxMetrics retrieves the metrics for all pod sandboxes. + ListPodSandboxMetrics(ctx context.Context) ([]*runtimeapi.PodSandboxMetrics, error) } // StreamingRuntime is the interface implemented by runtimes that handle the serving of the diff --git a/pkg/kubelet/container/testing/fake_runtime.go b/pkg/kubelet/container/testing/fake_runtime.go index 2525bb9e52d..1d13f25a37b 100644 --- a/pkg/kubelet/container/testing/fake_runtime.go +++ b/pkg/kubelet/container/testing/fake_runtime.go @@ -379,6 +379,22 @@ func (f *FakeRuntime) CheckpointContainer(_ context.Context, options *runtimeapi return f.Err } +func (f *FakeRuntime) ListMetricDescriptors(_ context.Context) ([]*runtimeapi.MetricDescriptor, error) { + f.Lock() + defer f.Unlock() + + f.CalledFunctions = append(f.CalledFunctions, "ListMetricDescriptors") + return nil, f.Err +} + +func (f *FakeRuntime) ListPodSandboxMetrics(_ context.Context) ([]*runtimeapi.PodSandboxMetrics, error) { + f.Lock() + defer f.Unlock() + + f.CalledFunctions = append(f.CalledFunctions, "ListPodSandboxMetrics") + return nil, f.Err +} + func (f *FakeRuntime) ImageStats(_ context.Context) (*kubecontainer.ImageStats, error) { f.Lock() defer f.Unlock() diff --git a/pkg/kubelet/container/testing/runtime_mock.go b/pkg/kubelet/container/testing/runtime_mock.go index 60ed691757e..1ad1d0431ef 100644 --- a/pkg/kubelet/container/testing/runtime_mock.go +++ b/pkg/kubelet/container/testing/runtime_mock.go @@ -286,6 +286,36 @@ func (mr *MockRuntimeMockRecorder) ListImages(ctx interface{}) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListImages", reflect.TypeOf((*MockRuntime)(nil).ListImages), ctx) } +// ListMetricDescriptors mocks base method. +func (m *MockRuntime) ListMetricDescriptors(ctx context.Context) ([]*v10.MetricDescriptor, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListMetricDescriptors", ctx) + ret0, _ := ret[0].([]*v10.MetricDescriptor) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListMetricDescriptors indicates an expected call of ListMetricDescriptors. +func (mr *MockRuntimeMockRecorder) ListMetricDescriptors(ctx interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListMetricDescriptors", reflect.TypeOf((*MockRuntime)(nil).ListMetricDescriptors), ctx) +} + +// ListPodSandboxMetrics mocks base method. +func (m *MockRuntime) ListPodSandboxMetrics(ctx context.Context) ([]*v10.PodSandboxMetrics, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListPodSandboxMetrics", ctx) + ret0, _ := ret[0].([]*v10.PodSandboxMetrics) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListPodSandboxMetrics indicates an expected call of ListPodSandboxMetrics. +func (mr *MockRuntimeMockRecorder) ListPodSandboxMetrics(ctx interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListPodSandboxMetrics", reflect.TypeOf((*MockRuntime)(nil).ListPodSandboxMetrics), ctx) +} + // PullImage mocks base method. func (m *MockRuntime) PullImage(ctx context.Context, image container.ImageSpec, pullSecrets []v1.Secret, podSandboxConfig *v10.PodSandboxConfig) (string, error) { m.ctrl.T.Helper() diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go index e71eeb6efcc..b74f4f5090f 100644 --- a/pkg/kubelet/kubelet.go +++ b/pkg/kubelet/kubelet.go @@ -2562,6 +2562,16 @@ func (kl *Kubelet) CheckpointContainer( return nil } +// ListMetricDescriptors gets the descriptors for the metrics that will be returned in ListPodSandboxMetrics. +func (kl *Kubelet) ListMetricDescriptors(ctx context.Context) ([]*runtimeapi.MetricDescriptor, error) { + return kl.containerRuntime.ListMetricDescriptors(ctx) +} + +// ListPodSandboxMetrics retrieves the metrics for all pod sandboxes. +func (kl *Kubelet) ListPodSandboxMetrics(ctx context.Context) ([]*runtimeapi.PodSandboxMetrics, error) { + return kl.containerRuntime.ListPodSandboxMetrics(ctx) +} + func (kl *Kubelet) supportLocalStorageCapacityIsolation() bool { return kl.GetConfiguration().LocalStorageCapacityIsolation } diff --git a/pkg/kubelet/kuberuntime/kuberuntime_manager.go b/pkg/kubelet/kuberuntime/kuberuntime_manager.go index 90f66b51920..db0f1b21ee1 100644 --- a/pkg/kubelet/kuberuntime/kuberuntime_manager.go +++ b/pkg/kubelet/kuberuntime/kuberuntime_manager.go @@ -1123,3 +1123,11 @@ func (m *kubeGenericRuntimeManager) UpdatePodCIDR(ctx context.Context, podCIDR s func (m *kubeGenericRuntimeManager) CheckpointContainer(ctx context.Context, options *runtimeapi.CheckpointContainerRequest) error { return m.runtimeService.CheckpointContainer(ctx, options) } + +func (m *kubeGenericRuntimeManager) ListMetricDescriptors(ctx context.Context) ([]*runtimeapi.MetricDescriptor, error) { + return m.runtimeService.ListMetricDescriptors(ctx) +} + +func (m *kubeGenericRuntimeManager) ListPodSandboxMetrics(ctx context.Context) ([]*runtimeapi.PodSandboxMetrics, error) { + return m.runtimeService.ListPodSandboxMetrics(ctx) +} diff --git a/pkg/kubelet/server/server.go b/pkg/kubelet/server/server.go index cae302289dc..be3a6a311ed 100644 --- a/pkg/kubelet/server/server.go +++ b/pkg/kubelet/server/server.go @@ -250,6 +250,8 @@ type HostInterface interface { GetExec(ctx context.Context, podFullName string, podUID types.UID, containerName string, cmd []string, streamOpts remotecommandserver.Options) (*url.URL, error) GetAttach(ctx context.Context, podFullName string, podUID types.UID, containerName string, streamOpts remotecommandserver.Options) (*url.URL, error) GetPortForward(ctx context.Context, podName, podNamespace string, podUID types.UID, portForwardOpts portforward.V4Options) (*url.URL, error) + ListMetricDescriptors(ctx context.Context) ([]*runtimeapi.MetricDescriptor, error) + ListPodSandboxMetrics(ctx context.Context) ([]*runtimeapi.PodSandboxMetrics, error) } // NewServer initializes and configures a kubelet.Server object to handle HTTP requests. diff --git a/pkg/kubelet/server/server_test.go b/pkg/kubelet/server/server_test.go index 61dc349626f..03969100c5c 100644 --- a/pkg/kubelet/server/server_test.go +++ b/pkg/kubelet/server/server_test.go @@ -156,6 +156,14 @@ func (fk *fakeKubelet) CheckpointContainer(_ context.Context, podUID types.UID, return nil } +func (fk *fakeKubelet) ListMetricDescriptors(ctx context.Context) ([]*runtimeapi.MetricDescriptor, error) { + return nil, nil +} + +func (fk *fakeKubelet) ListPodSandboxMetrics(ctx context.Context) ([]*runtimeapi.PodSandboxMetrics, error) { + return nil, nil +} + type fakeRuntime struct { execFunc func(string, []string, io.Reader, io.WriteCloser, io.WriteCloser, bool, <-chan remotecommand.TerminalSize) error attachFunc func(string, io.Reader, io.WriteCloser, io.WriteCloser, bool, <-chan remotecommand.TerminalSize) error