server.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package podresources
  14. import (
  15. "context"
  16. "k8s.io/api/core/v1"
  17. "k8s.io/kubernetes/pkg/kubelet/apis/podresources/v1alpha1"
  18. )
  19. // DevicesProvider knows how to provide the devices used by the given container
  20. type DevicesProvider interface {
  21. GetDevices(podUID, containerName string) []*v1alpha1.ContainerDevices
  22. }
  23. // PodsProvider knows how to provide the pods admitted by the node
  24. type PodsProvider interface {
  25. GetPods() []*v1.Pod
  26. }
  27. // podResourcesServer implements PodResourcesListerServer
  28. type podResourcesServer struct {
  29. podsProvider PodsProvider
  30. devicesProvider DevicesProvider
  31. }
  32. // NewPodResourcesServer returns a PodResourcesListerServer which lists pods provided by the PodsProvider
  33. // with device information provided by the DevicesProvider
  34. func NewPodResourcesServer(podsProvider PodsProvider, devicesProvider DevicesProvider) v1alpha1.PodResourcesListerServer {
  35. return &podResourcesServer{
  36. podsProvider: podsProvider,
  37. devicesProvider: devicesProvider,
  38. }
  39. }
  40. // List returns information about the resources assigned to pods on the node
  41. func (p *podResourcesServer) List(ctx context.Context, req *v1alpha1.ListPodResourcesRequest) (*v1alpha1.ListPodResourcesResponse, error) {
  42. pods := p.podsProvider.GetPods()
  43. podResources := make([]*v1alpha1.PodResources, len(pods))
  44. for i, pod := range pods {
  45. pRes := v1alpha1.PodResources{
  46. Name: pod.Name,
  47. Namespace: pod.Namespace,
  48. Containers: make([]*v1alpha1.ContainerResources, len(pod.Spec.Containers)),
  49. }
  50. for j, container := range pod.Spec.Containers {
  51. pRes.Containers[j] = &v1alpha1.ContainerResources{
  52. Name: container.Name,
  53. Devices: p.devicesProvider.GetDevices(string(pod.UID), container.Name),
  54. }
  55. }
  56. podResources[i] = &pRes
  57. }
  58. return &v1alpha1.ListPodResourcesResponse{
  59. PodResources: podResources,
  60. }, nil
  61. }