resource.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. Copyright 2017 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 v1
  14. import (
  15. "k8s.io/apimachinery/pkg/api/resource"
  16. )
  17. // Returns string version of ResourceName.
  18. func (rn ResourceName) String() string {
  19. return string(rn)
  20. }
  21. // Cpu returns the Cpu limit if specified.
  22. func (rl *ResourceList) Cpu() *resource.Quantity {
  23. return rl.Name(ResourceCPU, resource.DecimalSI)
  24. }
  25. // Memory returns the Memory limit if specified.
  26. func (rl *ResourceList) Memory() *resource.Quantity {
  27. return rl.Name(ResourceMemory, resource.BinarySI)
  28. }
  29. // Storage returns the Storage limit if specified.
  30. func (rl *ResourceList) Storage() *resource.Quantity {
  31. return rl.Name(ResourceStorage, resource.BinarySI)
  32. }
  33. // Pods returns the list of pods
  34. func (rl *ResourceList) Pods() *resource.Quantity {
  35. return rl.Name(ResourcePods, resource.DecimalSI)
  36. }
  37. // StorageEphemeral returns the list of ephemeral storage volumes, if any
  38. func (rl *ResourceList) StorageEphemeral() *resource.Quantity {
  39. return rl.Name(ResourceEphemeralStorage, resource.BinarySI)
  40. }
  41. // Name returns the resource with name if specified, otherwise it returns a nil quantity with default format.
  42. func (rl *ResourceList) Name(name ResourceName, defaultFormat resource.Format) *resource.Quantity {
  43. if val, ok := (*rl)[name]; ok {
  44. return &val
  45. }
  46. return &resource.Quantity{Format: defaultFormat}
  47. }