kubelet_resources.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. Copyright 2016 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 kubelet
  14. import (
  15. "fmt"
  16. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/fieldpath"
  18. )
  19. // defaultPodLimitsForDownwardApi copies the input pod, and optional container,
  20. // and applies default resource limits. it returns a copy of the input pod,
  21. // and a copy of the input container (if specified) with default limits
  22. // applied. if a container has no limit specified, it will default the limit to
  23. // the node allocatable.
  24. // TODO: if/when we have pod level resources, we need to update this function
  25. // to use those limits instead of node allocatable.
  26. func (kl *Kubelet) defaultPodLimitsForDownwardApi(pod *api.Pod, container *api.Container) (*api.Pod, *api.Container, error) {
  27. if pod == nil {
  28. return nil, nil, fmt.Errorf("invalid input, pod cannot be nil")
  29. }
  30. node, err := kl.getNodeAnyWay()
  31. if err != nil {
  32. return nil, nil, fmt.Errorf("failed to find node object, expected a node")
  33. }
  34. allocatable := node.Status.Allocatable
  35. podCopy, err := api.Scheme.Copy(pod)
  36. if err != nil {
  37. return nil, nil, fmt.Errorf("failed to perform a deep copy of pod object: %v", err)
  38. }
  39. outputPod, ok := podCopy.(*api.Pod)
  40. if !ok {
  41. return nil, nil, fmt.Errorf("unexpected type returned from deep copy of pod object")
  42. }
  43. for idx := range outputPod.Spec.Containers {
  44. fieldpath.MergeContainerResourceLimits(&outputPod.Spec.Containers[idx], allocatable)
  45. }
  46. var outputContainer *api.Container
  47. if container != nil {
  48. containerCopy, err := api.Scheme.DeepCopy(container)
  49. if err != nil {
  50. return nil, nil, fmt.Errorf("failed to perform a deep copy of container object: %v", err)
  51. }
  52. outputContainer, ok = containerCopy.(*api.Container)
  53. if !ok {
  54. return nil, nil, fmt.Errorf("unexpected type returned from deep copy of container object")
  55. }
  56. fieldpath.MergeContainerResourceLimits(outputContainer, allocatable)
  57. }
  58. return outputPod, outputContainer, nil
  59. }