k8s.io/kubernetes@v1.29.3/pkg/kubelet/apis/podresources/server_v1alpha1.go (about) 1 /* 2 Copyright 2018 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package podresources 18 19 import ( 20 "context" 21 22 "k8s.io/kubernetes/pkg/kubelet/metrics" 23 24 "k8s.io/kubelet/pkg/apis/podresources/v1" 25 "k8s.io/kubelet/pkg/apis/podresources/v1alpha1" 26 ) 27 28 // v1alpha1PodResourcesServer implements PodResourcesListerServer 29 type v1alpha1PodResourcesServer struct { 30 podsProvider PodsProvider 31 devicesProvider DevicesProvider 32 } 33 34 // NewV1alpha1PodResourcesServer returns a PodResourcesListerServer which lists pods provided by the PodsProvider 35 // with device information provided by the DevicesProvider 36 func NewV1alpha1PodResourcesServer(providers PodResourcesProviders) v1alpha1.PodResourcesListerServer { 37 return &v1alpha1PodResourcesServer{ 38 podsProvider: providers.Pods, 39 devicesProvider: providers.Devices, 40 } 41 } 42 43 func v1DevicesToAlphaV1(alphaDevs []*v1.ContainerDevices) []*v1alpha1.ContainerDevices { 44 var devs []*v1alpha1.ContainerDevices 45 for _, alphaDev := range alphaDevs { 46 dev := v1alpha1.ContainerDevices{ 47 ResourceName: alphaDev.ResourceName, 48 DeviceIds: alphaDev.DeviceIds, 49 } 50 devs = append(devs, &dev) 51 } 52 53 return devs 54 } 55 56 // List returns information about the resources assigned to pods on the node 57 func (p *v1alpha1PodResourcesServer) List(ctx context.Context, req *v1alpha1.ListPodResourcesRequest) (*v1alpha1.ListPodResourcesResponse, error) { 58 metrics.PodResourcesEndpointRequestsTotalCount.WithLabelValues("v1alpha1").Inc() 59 pods := p.podsProvider.GetPods() 60 podResources := make([]*v1alpha1.PodResources, len(pods)) 61 p.devicesProvider.UpdateAllocatedDevices() 62 63 for i, pod := range pods { 64 pRes := v1alpha1.PodResources{ 65 Name: pod.Name, 66 Namespace: pod.Namespace, 67 Containers: make([]*v1alpha1.ContainerResources, len(pod.Spec.Containers)), 68 } 69 70 for j, container := range pod.Spec.Containers { 71 pRes.Containers[j] = &v1alpha1.ContainerResources{ 72 Name: container.Name, 73 Devices: v1DevicesToAlphaV1(p.devicesProvider.GetDevices(string(pod.UID), container.Name)), 74 } 75 } 76 podResources[i] = &pRes 77 } 78 79 return &v1alpha1.ListPodResourcesResponse{ 80 PodResources: podResources, 81 }, nil 82 }