mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 21:47:07 +00:00
Refine NeedsHealthCheck logic
This commit is contained in:
parent
cb53dbbfae
commit
ae93b0da15
@ -70,19 +70,24 @@ func GetLoadBalancerSourceRanges(service *api.Service) (netsets.IPNet, error) {
|
|||||||
return ipnets, nil
|
return ipnets, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NeedsHealthCheck Check service for health check annotations
|
// RequestsOnlyLocalTraffic checks if service requests OnlyLocal traffic.
|
||||||
func NeedsHealthCheck(service *api.Service) bool {
|
func RequestsOnlyLocalTraffic(service *api.Service) bool {
|
||||||
|
if service.Spec.Type != api.ServiceTypeLoadBalancer &&
|
||||||
|
service.Spec.Type != api.ServiceTypeNodePort {
|
||||||
|
return false
|
||||||
|
}
|
||||||
// First check the alpha annotation and then the beta. This is so existing
|
// 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.
|
// Services continue to work till the user decides to transition to beta.
|
||||||
// If they transition to beta, there's no way to go back to alpha without
|
// If they transition to beta, there's no way to go back to alpha without
|
||||||
// rolling back the cluster.
|
// rolling back the cluster.
|
||||||
for _, annotation := range []string{AlphaAnnotationExternalTraffic, BetaAnnotationExternalTraffic} {
|
for _, annotation := range []string{AlphaAnnotationExternalTraffic, BetaAnnotationExternalTraffic} {
|
||||||
if l, ok := service.Annotations[annotation]; ok {
|
if l, ok := service.Annotations[annotation]; ok {
|
||||||
if l == AnnotationValueExternalTrafficLocal {
|
switch l {
|
||||||
|
case AnnotationValueExternalTrafficLocal:
|
||||||
return true
|
return true
|
||||||
} else if l == AnnotationValueExternalTrafficGlobal {
|
case AnnotationValueExternalTrafficGlobal:
|
||||||
return false
|
return false
|
||||||
} else {
|
default:
|
||||||
glog.Errorf("Invalid value for annotation %v: %v", annotation, l)
|
glog.Errorf("Invalid value for annotation %v: %v", annotation, l)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -90,6 +95,14 @@ func NeedsHealthCheck(service *api.Service) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NeedsHealthCheck Check if service needs health check.
|
||||||
|
func NeedsHealthCheck(service *api.Service) bool {
|
||||||
|
if service.Spec.Type != api.ServiceTypeLoadBalancer {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return RequestsOnlyLocalTraffic(service)
|
||||||
|
}
|
||||||
|
|
||||||
// GetServiceHealthCheckNodePort Return health check node port annotation for service, if one exists
|
// GetServiceHealthCheckNodePort Return health check node port annotation for service, if one exists
|
||||||
func GetServiceHealthCheckNodePort(service *api.Service) int32 {
|
func GetServiceHealthCheckNodePort(service *api.Service) int32 {
|
||||||
if !NeedsHealthCheck(service) {
|
if !NeedsHealthCheck(service) {
|
||||||
|
@ -70,19 +70,24 @@ func GetLoadBalancerSourceRanges(service *v1.Service) (netsets.IPNet, error) {
|
|||||||
return ipnets, nil
|
return ipnets, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NeedsHealthCheck Check service for health check annotations
|
// RequestsOnlyLocalTraffic checks if service requests OnlyLocal traffic.
|
||||||
func NeedsHealthCheck(service *v1.Service) bool {
|
func RequestsOnlyLocalTraffic(service *v1.Service) bool {
|
||||||
|
if service.Spec.Type != v1.ServiceTypeLoadBalancer &&
|
||||||
|
service.Spec.Type != v1.ServiceTypeNodePort {
|
||||||
|
return false
|
||||||
|
}
|
||||||
// First check the alpha annotation and then the beta. This is so existing
|
// 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.
|
// Services continue to work till the user decides to transition to beta.
|
||||||
// If they transition to beta, there's no way to go back to alpha without
|
// If they transition to beta, there's no way to go back to alpha without
|
||||||
// rolling back the cluster.
|
// rolling back the cluster.
|
||||||
for _, annotation := range []string{AlphaAnnotationExternalTraffic, BetaAnnotationExternalTraffic} {
|
for _, annotation := range []string{AlphaAnnotationExternalTraffic, BetaAnnotationExternalTraffic} {
|
||||||
if l, ok := service.Annotations[annotation]; ok {
|
if l, ok := service.Annotations[annotation]; ok {
|
||||||
if l == AnnotationValueExternalTrafficLocal {
|
switch l {
|
||||||
|
case AnnotationValueExternalTrafficLocal:
|
||||||
return true
|
return true
|
||||||
} else if l == AnnotationValueExternalTrafficGlobal {
|
case AnnotationValueExternalTrafficGlobal:
|
||||||
return false
|
return false
|
||||||
} else {
|
default:
|
||||||
glog.Errorf("Invalid value for annotation %v: %v", annotation, l)
|
glog.Errorf("Invalid value for annotation %v: %v", annotation, l)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -90,6 +95,14 @@ func NeedsHealthCheck(service *v1.Service) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NeedsHealthCheck Check if service needs health check.
|
||||||
|
func NeedsHealthCheck(service *v1.Service) bool {
|
||||||
|
if service.Spec.Type != v1.ServiceTypeLoadBalancer {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return RequestsOnlyLocalTraffic(service)
|
||||||
|
}
|
||||||
|
|
||||||
// GetServiceHealthCheckNodePort Return health check node port annotation for service, if one exists
|
// GetServiceHealthCheckNodePort Return health check node port annotation for service, if one exists
|
||||||
func GetServiceHealthCheckNodePort(service *v1.Service) int32 {
|
func GetServiceHealthCheckNodePort(service *v1.Service) int32 {
|
||||||
if !NeedsHealthCheck(service) {
|
if !NeedsHealthCheck(service) {
|
||||||
|
@ -163,8 +163,7 @@ func (e *endpointsInfo) String() string {
|
|||||||
func newServiceInfo(serviceName proxy.ServicePortName, port *api.ServicePort, service *api.Service) *serviceInfo {
|
func newServiceInfo(serviceName proxy.ServicePortName, port *api.ServicePort, service *api.Service) *serviceInfo {
|
||||||
onlyNodeLocalEndpoints := false
|
onlyNodeLocalEndpoints := false
|
||||||
if utilfeature.DefaultFeatureGate.Enabled(features.ExternalTrafficLocalOnly) &&
|
if utilfeature.DefaultFeatureGate.Enabled(features.ExternalTrafficLocalOnly) &&
|
||||||
(service.Spec.Type == api.ServiceTypeLoadBalancer || service.Spec.Type == api.ServiceTypeNodePort) &&
|
apiservice.RequestsOnlyLocalTraffic(service) {
|
||||||
apiservice.NeedsHealthCheck(service) {
|
|
||||||
onlyNodeLocalEndpoints = true
|
onlyNodeLocalEndpoints = true
|
||||||
}
|
}
|
||||||
info := &serviceInfo{
|
info := &serviceInfo{
|
||||||
|
@ -567,12 +567,7 @@ func shouldAssignNodePorts(service *api.Service) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func shouldCheckOrAssignHealthCheckNodePort(service *api.Service) bool {
|
func shouldCheckOrAssignHealthCheckNodePort(service *api.Service) bool {
|
||||||
if service.Spec.Type == api.ServiceTypeLoadBalancer {
|
return (utilfeature.DefaultFeatureGate.Enabled(features.ExternalTrafficLocalOnly) && apiservice.NeedsHealthCheck(service))
|
||||||
// True if Service-type == LoadBalancer AND annotation AnnotationExternalTraffic present
|
|
||||||
return (utilfeature.DefaultFeatureGate.Enabled(features.ExternalTrafficLocalOnly) && apiservice.NeedsHealthCheck(service))
|
|
||||||
}
|
|
||||||
glog.V(4).Infof("Service type: %v does not need health check node port", service.Spec.Type)
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loop through the service ports list, find one with the same port number and
|
// Loop through the service ports list, find one with the same port number and
|
||||||
|
Loading…
Reference in New Issue
Block a user