annotations.go 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 service
  14. import (
  15. "strconv"
  16. "github.com/golang/glog"
  17. "k8s.io/kubernetes/pkg/api"
  18. )
  19. const (
  20. // AnnotationLoadBalancerSourceRangesKey is the key of the annotation on a service to set allowed ingress ranges on their LoadBalancers
  21. //
  22. // It should be a comma-separated list of CIDRs, e.g. `0.0.0.0/0` to
  23. // allow full access (the default) or `18.0.0.0/8,56.0.0.0/8` to allow
  24. // access only from the CIDRs currently allocated to MIT & the USPS.
  25. //
  26. // Not all cloud providers support this annotation, though AWS & GCE do.
  27. AnnotationLoadBalancerSourceRangesKey = "service.beta.kubernetes.io/load-balancer-source-ranges"
  28. // AnnotationExternalTraffic An annotation that denotes if this Service desires to route external traffic to local
  29. // endpoints only. This preserves Source IP and avoids a second hop.
  30. AnnotationExternalTraffic = "service.alpha.kubernetes.io/external-traffic"
  31. // AnnotationValueExternalTrafficLocal Value of annotation to specify local endpoints behaviour
  32. AnnotationValueExternalTrafficLocal = "OnlyLocal"
  33. // AnnotationValueExternalTrafficGlobal Value of annotation to specify global (legacy) behaviour
  34. AnnotationValueExternalTrafficGlobal = "Global"
  35. // AnnotationHealthCheckNodePort Annotation specifying the healthcheck nodePort for the service
  36. // If not specified, annotation is created by the service api backend with the allocated nodePort
  37. // Will use user-specified nodePort value if specified by the client
  38. AnnotationHealthCheckNodePort = "service.alpha.kubernetes.io/healthcheck-nodeport"
  39. )
  40. // NeedsHealthCheck Check service for health check annotations
  41. func NeedsHealthCheck(service *api.Service) bool {
  42. if l, ok := service.Annotations[AnnotationExternalTraffic]; ok {
  43. if l == AnnotationValueExternalTrafficLocal {
  44. return true
  45. } else if l == AnnotationValueExternalTrafficGlobal {
  46. return false
  47. } else {
  48. glog.Errorf("Invalid value for annotation %v", AnnotationExternalTraffic)
  49. return false
  50. }
  51. }
  52. return false
  53. }
  54. // GetServiceHealthCheckNodePort Return health check node port annotation for service, if one exists
  55. func GetServiceHealthCheckNodePort(service *api.Service) int32 {
  56. if NeedsHealthCheck(service) {
  57. if l, ok := service.Annotations[AnnotationHealthCheckNodePort]; ok {
  58. p, err := strconv.Atoi(l)
  59. if err != nil {
  60. glog.Errorf("Failed to parse annotation %v: %v", AnnotationHealthCheckNodePort, err)
  61. return 0
  62. }
  63. return int32(p)
  64. }
  65. }
  66. return 0
  67. }
  68. // GetServiceHealthCheckPathPort Return the path and nodePort programmed into the Cloud LB Health Check
  69. func GetServiceHealthCheckPathPort(service *api.Service) (string, int32) {
  70. if !NeedsHealthCheck(service) {
  71. return "", 0
  72. }
  73. port := GetServiceHealthCheckNodePort(service)
  74. if port == 0 {
  75. return "", 0
  76. }
  77. return "/healthz", port
  78. }