utils.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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/kubernetes/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. func ExtractPodBandwidthResources(podAnnotations map[string]string) (ingress, egress *resource.Quantity, err error) {
  30. str, found := podAnnotations["kubernetes.io/ingress-bandwidth"]
  31. if found {
  32. ingressValue, err := resource.ParseQuantity(str)
  33. if err != nil {
  34. return nil, nil, err
  35. }
  36. ingress = &ingressValue
  37. if err := validateBandwidthIsReasonable(ingress); err != nil {
  38. return nil, nil, err
  39. }
  40. }
  41. str, found = podAnnotations["kubernetes.io/egress-bandwidth"]
  42. if found {
  43. egressValue, err := resource.ParseQuantity(str)
  44. if err != nil {
  45. return nil, nil, err
  46. }
  47. egress = &egressValue
  48. if err := validateBandwidthIsReasonable(egress); err != nil {
  49. return nil, nil, err
  50. }
  51. }
  52. return ingress, egress, nil
  53. }