github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/orm/deviceprovider/kubelet/provider.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 kubelet 18 19 import ( 20 "context" 21 "encoding/json" 22 "time" 23 24 "google.golang.org/grpc" 25 "k8s.io/klog/v2" 26 podresourcesapi "k8s.io/kubelet/pkg/apis/podresources/v1" 27 28 podresourcesserver "github.com/kubewharf/katalyst-core/pkg/agent/orm/server/podresources" 29 "github.com/kubewharf/katalyst-core/pkg/util/general" 30 "github.com/kubewharf/katalyst-core/pkg/util/kubelet/podresources" 31 ) 32 33 const ( 34 podResourcesClientTimeout = 10 * time.Second 35 podResourcesClientMaxMsgSize = 1024 * 1024 * 16 36 ) 37 38 // Provider provide devices resource by kubelet v1 podResources api 39 type Provider struct { 40 client podresourcesapi.PodResourcesListerClient 41 endpoints []string 42 conn *grpc.ClientConn 43 } 44 45 func NewProvider(endpoints []string, getClientFunc podresources.GetClientFunc) (podresourcesserver.DevicesProvider, error) { 46 klog.V(5).Infof("new kubelet devices provider, endpoints: %v", endpoints) 47 48 p := &Provider{ 49 endpoints: endpoints, 50 } 51 52 var err error 53 54 p.client, p.conn, err = getClientFunc( 55 general.GetOneExistPath(endpoints), 56 podResourcesClientTimeout, 57 podResourcesClientMaxMsgSize) 58 if err != nil { 59 klog.Error(err) 60 return nil, err 61 } 62 63 return p, nil 64 } 65 66 func (p *Provider) GetDevices() []*podresourcesapi.PodResources { 67 ctx, cancel := context.WithTimeout(context.Background(), time.Second*1) 68 defer cancel() 69 70 response, err := p.client.List(ctx, &podresourcesapi.ListPodResourcesRequest{}) 71 if err != nil { 72 klog.Errorf("list resources from kubelet fail: %v", err) 73 return nil 74 } 75 if response == nil { 76 klog.Error("list resources from kubelet, get nil response without err") 77 return nil 78 } 79 if response.GetPodResources() == nil { 80 klog.Error("list resources from kubelet, get nil podResources without err") 81 return nil 82 } 83 84 if klog.V(6).Enabled() { 85 str, _ := json.Marshal(response) 86 klog.Infof("GetDevices: %v", string(str)) 87 } 88 89 return response.PodResources 90 } 91 92 func (p *Provider) GetAllocatableDevices() []*podresourcesapi.ContainerDevices { 93 ctx, cancel := context.WithTimeout(context.Background(), time.Second*1) 94 defer cancel() 95 96 response, err := p.client.GetAllocatableResources(ctx, &podresourcesapi.AllocatableResourcesRequest{}) 97 if err != nil { 98 klog.Errorf("GetAllocatableResources from kubelet fail: %v", err) 99 return nil 100 } 101 if response == nil { 102 klog.Error("GetAllocatableResources from kubelet, get nil response without err") 103 return nil 104 } 105 if response.GetDevices() == nil { 106 klog.Error("GetAllocatableResources from kubelet, get nil response without err") 107 return nil 108 } 109 110 if klog.V(6).Enabled() { 111 str, _ := json.Marshal(response) 112 klog.Infof("GetAllocatableDevices: %v", str) 113 } 114 return response.GetDevices() 115 }