utils.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. Copyright 2015 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 bandwidth
  14. import (
  15. "fmt"
  16. "k8s.io/apimachinery/pkg/api/resource"
  17. )
  18. var minRsrc = resource.MustParse("1k")
  19. var maxRsrc = resource.MustParse("1P")
  20. func validateBandwidthIsReasonable(rsrc *resource.Quantity) error {
  21. if rsrc.Value() < minRsrc.Value() {
  22. return fmt.Errorf("resource is unreasonably small (< 1kbit)")
  23. }
  24. if rsrc.Value() > maxRsrc.Value() {
  25. return fmt.Errorf("resoruce is unreasonably large (> 1Pbit)")
  26. }
  27. return nil
  28. }
  29. // ExtractPodBandwidthResources extracts the ingress and egress from the given pod annotations
  30. func ExtractPodBandwidthResources(podAnnotations map[string]string) (ingress, egress *resource.Quantity, err error) {
  31. if podAnnotations == nil {
  32. return nil, nil, nil
  33. }
  34. str, found := podAnnotations["kubernetes.io/ingress-bandwidth"]
  35. if found {
  36. ingressValue, err := resource.ParseQuantity(str)
  37. if err != nil {
  38. return nil, nil, err
  39. }
  40. ingress = &ingressValue
  41. if err := validateBandwidthIsReasonable(ingress); err != nil {
  42. return nil, nil, err
  43. }
  44. }
  45. str, found = podAnnotations["kubernetes.io/egress-bandwidth"]
  46. if found {
  47. egressValue, err := resource.ParseQuantity(str)
  48. if err != nil {
  49. return nil, nil, err
  50. }
  51. egress = &egressValue
  52. if err := validateBandwidthIsReasonable(egress); err != nil {
  53. return nil, nil, err
  54. }
  55. }
  56. return ingress, egress, nil
  57. }