Commit 79ca7170 authored by Zihong Zheng's avatar Zihong Zheng

Adjust ESIPP validation and service registry codes

parent 73fa1ddc
...@@ -76,23 +76,22 @@ func RequestsOnlyLocalTraffic(service *api.Service) bool { ...@@ -76,23 +76,22 @@ func RequestsOnlyLocalTraffic(service *api.Service) bool {
service.Spec.Type != api.ServiceTypeNodePort { service.Spec.Type != api.ServiceTypeNodePort {
return false return false
} }
// First check the alpha annotation and then the beta. This is so existing
// Services continue to work till the user decides to transition to beta. // First check the beta annotation and then the first class field. This is so that
// If they transition to beta, there's no way to go back to alpha without // existing Services continue to work till the user decides to transition to the
// rolling back the cluster. // first class field.
for _, annotation := range []string{AlphaAnnotationExternalTraffic, BetaAnnotationExternalTraffic} { if l, ok := service.Annotations[BetaAnnotationExternalTraffic]; ok {
if l, ok := service.Annotations[annotation]; ok { switch l {
switch l { case AnnotationValueExternalTrafficLocal:
case AnnotationValueExternalTrafficLocal: return true
return true case AnnotationValueExternalTrafficGlobal:
case AnnotationValueExternalTrafficGlobal: return false
return false default:
default: glog.Errorf("Invalid value for annotation %v: %v", BetaAnnotationExternalTraffic, l)
glog.Errorf("Invalid value for annotation %v: %v", annotation, l) return false
}
} }
} }
return false return service.Spec.ExternalTrafficPolicy == api.ServiceExternalTrafficPolicyTypeLocal
} }
// NeedsHealthCheck Check if service needs health check. // NeedsHealthCheck Check if service needs health check.
...@@ -103,21 +102,61 @@ func NeedsHealthCheck(service *api.Service) bool { ...@@ -103,21 +102,61 @@ func NeedsHealthCheck(service *api.Service) bool {
return RequestsOnlyLocalTraffic(service) return RequestsOnlyLocalTraffic(service)
} }
// GetServiceHealthCheckNodePort Return health check node port annotation for service, if one exists // GetServiceHealthCheckNodePort Return health check node port for service, if one exists
func GetServiceHealthCheckNodePort(service *api.Service) int32 { func GetServiceHealthCheckNodePort(service *api.Service) int32 {
// First check the alpha annotation and then the beta. This is so existing // First check the beta annotation and then the first class field. This is so that
// Services continue to work till the user decides to transition to beta. // existing Services continue to work till the user decides to transition to the
// If they transition to beta, there's no way to go back to alpha without // first class field.
// rolling back the cluster. if l, ok := service.Annotations[BetaAnnotationHealthCheckNodePort]; ok {
for _, annotation := range []string{AlphaAnnotationHealthCheckNodePort, BetaAnnotationHealthCheckNodePort} { p, err := strconv.Atoi(l)
if l, ok := service.Annotations[annotation]; ok { if err != nil {
p, err := strconv.Atoi(l) glog.Errorf("Failed to parse annotation %v: %v", BetaAnnotationHealthCheckNodePort, err)
if err != nil { return 0
glog.Errorf("Failed to parse annotation %v: %v", annotation, err) }
continue return int32(p)
} }
return int32(p) return service.Spec.HealthCheckNodePort
}
// SetDefaultExternalTrafficPolicyIfNeeded defaults the ExternalTrafficPolicy field
// for NodePort / LoadBalancer service to Global for consistency.
// TODO: Move this default logic to default.go once beta annotation is deprecated.
func SetDefaultExternalTrafficPolicyIfNeeded(service *api.Service) {
if _, ok := service.Annotations[BetaAnnotationExternalTraffic]; ok {
// Don't default this field if beta annotation exists.
return
} else if (service.Spec.Type == api.ServiceTypeNodePort ||
service.Spec.Type == api.ServiceTypeLoadBalancer) &&
service.Spec.ExternalTrafficPolicy == "" {
service.Spec.ExternalTrafficPolicy = api.ServiceExternalTrafficPolicyTypeGlobal
}
}
// ClearExternalTrafficPolicy resets the ExternalTrafficPolicy field.
func ClearExternalTrafficPolicy(service *api.Service) {
// First check the beta annotation and then the first class field. This is so that
// existing Services continue to work till the user decides to transition to the
// first class field.
if _, ok := service.Annotations[BetaAnnotationExternalTraffic]; ok {
delete(service.Annotations, BetaAnnotationExternalTraffic)
return
}
service.Spec.ExternalTrafficPolicy = api.ServiceExternalTrafficPolicyType("")
}
// SetServiceHealthCheckNodePort sets the given health check node port on service.
// It does not check whether this service needs healthCheckNodePort.
func SetServiceHealthCheckNodePort(service *api.Service, hcNodePort int32) {
// First check the beta annotation and then the first class field. This is so that
// existing Services continue to work till the user decides to transition to the
// first class field.
if _, ok := service.Annotations[BetaAnnotationExternalTraffic]; ok {
if hcNodePort == 0 {
delete(service.Annotations, BetaAnnotationHealthCheckNodePort)
} else {
service.Annotations[BetaAnnotationHealthCheckNodePort] = fmt.Sprintf("%d", hcNodePort)
} }
return
} }
return 0 service.Spec.HealthCheckNodePort = hcNodePort
} }
...@@ -76,23 +76,22 @@ func RequestsOnlyLocalTraffic(service *v1.Service) bool { ...@@ -76,23 +76,22 @@ func RequestsOnlyLocalTraffic(service *v1.Service) bool {
service.Spec.Type != v1.ServiceTypeNodePort { service.Spec.Type != v1.ServiceTypeNodePort {
return false return false
} }
// First check the alpha annotation and then the beta. This is so existing
// Services continue to work till the user decides to transition to beta. // First check the beta annotation and then the first class field. This is so that
// If they transition to beta, there's no way to go back to alpha without // existing Services continue to work till the user decides to transition to the
// rolling back the cluster. // first class field.
for _, annotation := range []string{AlphaAnnotationExternalTraffic, BetaAnnotationExternalTraffic} { if l, ok := service.Annotations[BetaAnnotationExternalTraffic]; ok {
if l, ok := service.Annotations[annotation]; ok { switch l {
switch l { case AnnotationValueExternalTrafficLocal:
case AnnotationValueExternalTrafficLocal: return true
return true case AnnotationValueExternalTrafficGlobal:
case AnnotationValueExternalTrafficGlobal: return false
return false default:
default: glog.Errorf("Invalid value for annotation %v: %v", BetaAnnotationExternalTraffic, l)
glog.Errorf("Invalid value for annotation %v: %v", annotation, l) return false
}
} }
} }
return false return service.Spec.ExternalTrafficPolicy == v1.ServiceExternalTrafficPolicyTypeLocal
} }
// NeedsHealthCheck Check if service needs health check. // NeedsHealthCheck Check if service needs health check.
...@@ -103,23 +102,62 @@ func NeedsHealthCheck(service *v1.Service) bool { ...@@ -103,23 +102,62 @@ func NeedsHealthCheck(service *v1.Service) bool {
return RequestsOnlyLocalTraffic(service) return RequestsOnlyLocalTraffic(service)
} }
// GetServiceHealthCheckNodePort Return health check node port annotation for service, if one exists // GetServiceHealthCheckNodePort Return health check node port for service, if one exists
func GetServiceHealthCheckNodePort(service *v1.Service) int32 { func GetServiceHealthCheckNodePort(service *v1.Service) int32 {
// First check the alpha annotation and then the beta. This is so existing // First check the beta annotation and then the first class field. This is so that
// Services continue to work till the user decides to transition to beta. // existing Services continue to work till the user decides to transition to the
// If they transition to beta, there's no way to go back to alpha without // first class field.
// rolling back the cluster. if l, ok := service.Annotations[BetaAnnotationHealthCheckNodePort]; ok {
for _, annotation := range []string{AlphaAnnotationHealthCheckNodePort, BetaAnnotationHealthCheckNodePort} { p, err := strconv.Atoi(l)
if l, ok := service.Annotations[annotation]; ok { if err != nil {
p, err := strconv.Atoi(l) glog.Errorf("Failed to parse annotation %v: %v", BetaAnnotationHealthCheckNodePort, err)
if err != nil { return 0
glog.Errorf("Failed to parse annotation %v: %v", annotation, err) }
continue return int32(p)
} }
return int32(p) return service.Spec.HealthCheckNodePort
}
// SetDefaultExternalTrafficPolicyIfNeeded defaults the ExternalTrafficPolicy field
// for NodePort / LoadBalancer service to Global for consistency.
// TODO: Move this default logic to default.go once beta annotation is deprecated.
func SetDefaultExternalTrafficPolicyIfNeeded(service *v1.Service) {
if _, ok := service.Annotations[BetaAnnotationExternalTraffic]; ok {
// Don't default this field if beta annotation exists.
return
} else if (service.Spec.Type == v1.ServiceTypeNodePort ||
service.Spec.Type == v1.ServiceTypeLoadBalancer) &&
service.Spec.ExternalTrafficPolicy == "" {
service.Spec.ExternalTrafficPolicy = v1.ServiceExternalTrafficPolicyTypeGlobal
}
}
// ClearExternalTrafficPolicy resets the ExternalTrafficPolicy field.
func ClearExternalTrafficPolicy(service *v1.Service) {
// First check the beta annotation and then the first class field. This is so existing
// Services continue to work till the user decides to transition to the first class field.
if _, ok := service.Annotations[BetaAnnotationExternalTraffic]; ok {
delete(service.Annotations, BetaAnnotationExternalTraffic)
return
}
service.Spec.ExternalTrafficPolicy = v1.ServiceExternalTrafficPolicyType("")
}
// SetServiceHealthCheckNodePort sets the given health check node port on service.
// It does not check whether this service needs healthCheckNodePort.
func SetServiceHealthCheckNodePort(service *v1.Service, hcNodePort int32) {
// First check the beta annotation and then the first class field. This is so that
// existing Services continue to work till the user decides to transition to the
// first class field.
if _, ok := service.Annotations[BetaAnnotationExternalTraffic]; ok {
if hcNodePort == 0 {
delete(service.Annotations, BetaAnnotationHealthCheckNodePort)
} else {
service.Annotations[BetaAnnotationHealthCheckNodePort] = fmt.Sprintf("%d", hcNodePort)
} }
return
} }
return 0 service.Spec.HealthCheckNodePort = hcNodePort
} }
// GetServiceHealthCheckPathPort Return the path and nodePort programmed into the Cloud LB Health Check // GetServiceHealthCheckPathPort Return the path and nodePort programmed into the Cloud LB Health Check
......
...@@ -2742,7 +2742,8 @@ func ValidateService(service *api.Service) field.ErrorList { ...@@ -2742,7 +2742,8 @@ func ValidateService(service *api.Service) field.ErrorList {
} }
} }
allErrs = append(allErrs, validateServiceExternalTrafficFields(service)...) allErrs = append(allErrs, validateServiceExternalTrafficFieldsValue(service)...)
allErrs = append(allErrs, validateServiceExternalTrafficAPIVersion(service)...)
return allErrs return allErrs
} }
...@@ -2785,61 +2786,68 @@ func validateServicePort(sp *api.ServicePort, requireName, isHeadlessService boo ...@@ -2785,61 +2786,68 @@ func validateServicePort(sp *api.ServicePort, requireName, isHeadlessService boo
return allErrs return allErrs
} }
// validateServiceExternalTrafficFields validates ExternalTraffic related annotations // validateServiceExternalTrafficFieldsValue validates ExternalTraffic related annotations
// have legal value. // have legal value.
func validateServiceExternalTrafficFields(service *api.Service) field.ErrorList { func validateServiceExternalTrafficFieldsValue(service *api.Service) field.ErrorList {
allErrs := field.ErrorList{} allErrs := field.ErrorList{}
for _, annotation := range []string{apiservice.AlphaAnnotationExternalTraffic, apiservice.BetaAnnotationExternalTraffic} { // Check beta annotations.
if l, ok := service.Annotations[annotation]; ok { if l, ok := service.Annotations[apiservice.BetaAnnotationExternalTraffic]; ok {
if l != apiservice.AnnotationValueExternalTrafficLocal && if l != apiservice.AnnotationValueExternalTrafficLocal &&
l != apiservice.AnnotationValueExternalTrafficGlobal { l != apiservice.AnnotationValueExternalTrafficGlobal {
allErrs = append(allErrs, field.Invalid(field.NewPath("metadata", "annotations").Key(annotation), l, allErrs = append(allErrs, field.Invalid(field.NewPath("metadata", "annotations").Key(apiservice.BetaAnnotationExternalTraffic), l,
fmt.Sprintf("ExternalTraffic must be %v or %v", apiservice.AnnotationValueExternalTrafficLocal, apiservice.AnnotationValueExternalTrafficGlobal))) fmt.Sprintf("ExternalTraffic must be %v or %v", apiservice.AnnotationValueExternalTrafficLocal, apiservice.AnnotationValueExternalTrafficGlobal)))
}
} }
} }
for _, annotation := range []string{apiservice.AlphaAnnotationHealthCheckNodePort, apiservice.BetaAnnotationHealthCheckNodePort} { if l, ok := service.Annotations[apiservice.BetaAnnotationHealthCheckNodePort]; ok {
if l, ok := service.Annotations[annotation]; ok { p, err := strconv.Atoi(l)
p, err := strconv.Atoi(l) if err != nil {
if err != nil { allErrs = append(allErrs, field.Invalid(field.NewPath("metadata", "annotations").Key(apiservice.BetaAnnotationHealthCheckNodePort), l,
allErrs = append(allErrs, field.Invalid(field.NewPath("metadata", "annotations").Key(annotation), l, "HealthCheckNodePort must be a valid port number"))
"HealthCheckNodePort must be a valid port number")) } else if p <= 0 {
} else if p <= 0 { allErrs = append(allErrs, field.Invalid(field.NewPath("metadata", "annotations").Key(apiservice.BetaAnnotationHealthCheckNodePort), l,
allErrs = append(allErrs, field.Invalid(field.NewPath("metadata", "annotations").Key(annotation), l, "HealthCheckNodePort must be greater than 0"))
"HealthCheckNodePort must be greater than 0"))
}
} }
} }
allErrs = append(allErrs, validateServiceExternalTrafficAPIVersion(service)...) // Check first class fields.
if service.Spec.ExternalTrafficPolicy != "" &&
service.Spec.ExternalTrafficPolicy != api.ServiceExternalTrafficPolicyTypeGlobal &&
service.Spec.ExternalTrafficPolicy != api.ServiceExternalTrafficPolicyTypeLocal {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("externalTrafficPolicy"), service.Spec.ExternalTrafficPolicy,
fmt.Sprintf("ExternalTrafficPolicy must be empty, %v or %v", api.ServiceExternalTrafficPolicyTypeGlobal, api.ServiceExternalTrafficPolicyTypeLocal)))
}
if service.Spec.HealthCheckNodePort < 0 {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("healthCheckNodePort"), service.Spec.HealthCheckNodePort,
"HealthCheckNodePort must be not less than 0"))
}
return allErrs return allErrs
} }
// serviceExternalTrafficStatus stores flags indicating whether ExternalTraffic // serviceExternalTrafficStatus stores flags indicating whether ExternalTraffic
// related beta annotations and alpha annotations are set on service. // related beta annotations and GA fields are set on service.
type serviceExternalTrafficStatus struct { type serviceExternalTrafficStatus struct {
alphaExternalTrafficIsSet bool betaExternalTrafficIsSet bool
alphaHealthCheckIsSet bool betaHealthCheckIsSet bool
betaExternalTrafficIsSet bool gaExternalTrafficIsSet bool
betaHealthCheckIsSet bool gaHealthCheckIsSet bool
} }
func (s *serviceExternalTrafficStatus) useAlphaExternalTrafficWithBeta() bool { func (s *serviceExternalTrafficStatus) useBetaExternalTrafficWithGA() bool {
return s.alphaExternalTrafficIsSet && (s.betaExternalTrafficIsSet || s.betaHealthCheckIsSet) return s.betaExternalTrafficIsSet && (s.gaExternalTrafficIsSet || s.gaHealthCheckIsSet)
} }
func (s *serviceExternalTrafficStatus) useAlphaHealthCheckWithBeta() bool { func (s *serviceExternalTrafficStatus) useBetaHealthCheckWithGA() bool {
return s.alphaHealthCheckIsSet && (s.betaExternalTrafficIsSet || s.betaHealthCheckIsSet) return s.betaHealthCheckIsSet && (s.gaExternalTrafficIsSet || s.gaHealthCheckIsSet)
} }
func getServiceExternalTrafficStatus(service *api.Service) *serviceExternalTrafficStatus { func getServiceExternalTrafficStatus(service *api.Service) *serviceExternalTrafficStatus {
s := serviceExternalTrafficStatus{} s := serviceExternalTrafficStatus{}
_, s.alphaExternalTrafficIsSet = service.Annotations[apiservice.AlphaAnnotationExternalTraffic]
_, s.alphaHealthCheckIsSet = service.Annotations[apiservice.AlphaAnnotationHealthCheckNodePort]
_, s.betaExternalTrafficIsSet = service.Annotations[apiservice.BetaAnnotationExternalTraffic] _, s.betaExternalTrafficIsSet = service.Annotations[apiservice.BetaAnnotationExternalTraffic]
_, s.betaHealthCheckIsSet = service.Annotations[apiservice.BetaAnnotationHealthCheckNodePort] _, s.betaHealthCheckIsSet = service.Annotations[apiservice.BetaAnnotationHealthCheckNodePort]
s.gaExternalTrafficIsSet = service.Spec.ExternalTrafficPolicy != ""
s.gaHealthCheckIsSet = service.Spec.HealthCheckNodePort != 0
return &s return &s
} }
...@@ -2850,16 +2858,39 @@ func validateServiceExternalTrafficAPIVersion(service *api.Service) field.ErrorL ...@@ -2850,16 +2858,39 @@ func validateServiceExternalTrafficAPIVersion(service *api.Service) field.ErrorL
status := getServiceExternalTrafficStatus(service) status := getServiceExternalTrafficStatus(service)
if status.useAlphaExternalTrafficWithBeta() { if status.useBetaExternalTrafficWithGA() {
fieldPath := field.NewPath("metadata", "annotations").Key(apiservice.AlphaAnnotationExternalTraffic) fieldPath := field.NewPath("metadata", "annotations").Key(apiservice.BetaAnnotationExternalTraffic)
msg := fmt.Sprintf("please replace the alpha annotation with beta annotation") msg := fmt.Sprintf("please replace the beta annotation with 'ExternalTrafficPolicy' field")
allErrs = append(allErrs, field.Invalid(fieldPath, apiservice.AlphaAnnotationExternalTraffic, msg)) allErrs = append(allErrs, field.Invalid(fieldPath, apiservice.BetaAnnotationExternalTraffic, msg))
}
if status.useBetaHealthCheckWithGA() {
fieldPath := field.NewPath("metadata", "annotations").Key(apiservice.BetaAnnotationHealthCheckNodePort)
msg := fmt.Sprintf("please replace the beta annotation with 'HealthCheckNodePort' field")
allErrs = append(allErrs, field.Invalid(fieldPath, apiservice.BetaAnnotationHealthCheckNodePort, msg))
}
return allErrs
}
// ValidateServiceExternalTrafficFieldsCombination validates if ExternalTrafficPolicy,
// HealthCheckNodePort and Type combination are legal. For update, it should be called
// after clearing externalTraffic related fields for the ease of transitioning between
// different service types.
func ValidateServiceExternalTrafficFieldsCombination(service *api.Service) field.ErrorList {
allErrs := field.ErrorList{}
if service.Spec.Type != api.ServiceTypeLoadBalancer &&
service.Spec.Type != api.ServiceTypeNodePort &&
service.Spec.ExternalTrafficPolicy != "" {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "externalTrafficPolicy"), service.Spec.ExternalTrafficPolicy,
"ExternalTrafficPolicy can only be set on NodePort and LoadBalancer service"))
} }
if status.useAlphaHealthCheckWithBeta() { if !apiservice.NeedsHealthCheck(service) &&
fieldPath := field.NewPath("metadata", "annotations").Key(apiservice.AlphaAnnotationHealthCheckNodePort) service.Spec.HealthCheckNodePort != 0 {
msg := fmt.Sprintf("please replace the alpha annotation with beta annotation") allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "healthCheckNodePort"), service.Spec.HealthCheckNodePort,
allErrs = append(allErrs, field.Invalid(fieldPath, apiservice.AlphaAnnotationHealthCheckNodePort, msg)) "HealthCheckNodePort can only be set on LoadBalancer service with ExternalTrafficPolicy=Local"))
} }
return allErrs return allErrs
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment