github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/orm/server/podresources/server_v1.go (about) 1 /* 2 Copyright 2022 The Katalyst 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 v1 "k8s.io/kubelet/pkg/apis/podresources/v1" 23 maputil "k8s.io/kubernetes/pkg/util/maps" 24 25 "github.com/kubewharf/katalyst-core/pkg/metrics" 26 "github.com/kubewharf/katalyst-core/pkg/util/native" 27 ) 28 29 type v1PodResourcesServer struct { 30 podsProvider PodsProvider 31 resourcesProvider ResourcesProvider 32 devicesProvider DevicesProvider 33 emitter metrics.MetricEmitter 34 } 35 36 func NewV1PodResourcesServer( 37 podsProvider PodsProvider, 38 resourcesProvider ResourcesProvider, 39 devicesProvider DevicesProvider, 40 emitter metrics.MetricEmitter, 41 ) v1.PodResourcesListerServer { 42 return &v1PodResourcesServer{ 43 podsProvider: podsProvider, 44 resourcesProvider: resourcesProvider, 45 devicesProvider: devicesProvider, 46 emitter: emitter, 47 } 48 } 49 50 // List returns information about the resources assigned to pods on the node 51 func (p *v1PodResourcesServer) List(ctx context.Context, req *v1.ListPodResourcesRequest) (*v1.ListPodResourcesResponse, error) { 52 pods := p.podsProvider.GetPods() 53 podResources := make([]*v1.PodResources, len(pods)) 54 55 // sync pods resource before list 56 p.resourcesProvider.UpdateAllocatedResources() 57 58 devicesResources := p.getDevices() 59 60 for i, pod := range pods { 61 pRes := v1.PodResources{ 62 Name: pod.Name, 63 Namespace: pod.Namespace, 64 Labels: maputil.CopySS(pod.Labels), 65 Annotations: maputil.CopySS(pod.Annotations), 66 Containers: make([]*v1.ContainerResources, len(pod.Spec.Containers)), 67 } 68 69 podKey := native.GenerateNamespaceNameKey(pod.Namespace, pod.Name) 70 podDevicesResources, ok := devicesResources[podKey] 71 72 for j, container := range pod.Spec.Containers { 73 pRes.Containers[j] = &v1.ContainerResources{ 74 Name: container.Name, 75 Resources: p.resourcesProvider.GetTopologyAwareResources(pod, &container), 76 } 77 if ok { 78 pRes.Containers[j].Devices = podDevicesResources[container.Name] 79 } 80 } 81 podResources[i] = &pRes 82 } 83 84 _ = p.emitter.StoreInt64(podResourcesList, 1, metrics.MetricTypeNameCount) 85 return &v1.ListPodResourcesResponse{ 86 PodResources: podResources, 87 }, nil 88 } 89 90 func (p *v1PodResourcesServer) GetAllocatableResources(ctx context.Context, req *v1.AllocatableResourcesRequest) (*v1.AllocatableResourcesResponse, error) { 91 _ = p.emitter.StoreInt64(podResourceGetAllocatableResources, 1, metrics.MetricTypeNameCount) 92 93 return &v1.AllocatableResourcesResponse{ 94 Resources: p.resourcesProvider.GetTopologyAwareAllocatableResources(), 95 Devices: p.devicesProvider.GetAllocatableDevices(), 96 }, nil 97 } 98 99 func (p *v1PodResourcesServer) getDevices() map[string]map[string][]*v1.ContainerDevices { 100 res := make(map[string]map[string][]*v1.ContainerDevices) 101 102 podResources := p.devicesProvider.GetDevices() 103 if len(podResources) == 0 { 104 return nil 105 } 106 for _, podResource := range podResources { 107 key := native.GenerateNamespaceNameKey(podResource.Namespace, podResource.Name) 108 res[key] = make(map[string][]*v1.ContainerDevices) 109 for _, container := range podResource.Containers { 110 res[key][container.Name] = container.GetDevices() 111 } 112 } 113 114 return res 115 }