mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-20 01:23:48 +00:00
Move defaulting logic for ExternalTrafficPolicy into defaults.go
This commit is contained in:
@@ -118,20 +118,6 @@ func GetServiceHealthCheckNodePort(service *api.Service) int32 {
|
||||
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[api.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
|
||||
|
@@ -20,7 +20,6 @@ import (
|
||||
"testing"
|
||||
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
@@ -298,106 +297,6 @@ func TestGetServiceHealthCheckNodePort(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestSetDefaultExternalTrafficPolicyIfNeeded(t *testing.T) {
|
||||
testCases := []struct {
|
||||
inputService *api.Service
|
||||
expectedService *api.Service
|
||||
}{
|
||||
// First class fields cases.
|
||||
{
|
||||
&api.Service{
|
||||
Spec: api.ServiceSpec{
|
||||
Type: api.ServiceTypeLoadBalancer,
|
||||
},
|
||||
},
|
||||
&api.Service{
|
||||
Spec: api.ServiceSpec{
|
||||
Type: api.ServiceTypeLoadBalancer,
|
||||
ExternalTrafficPolicy: api.ServiceExternalTrafficPolicyTypeGlobal,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
&api.Service{
|
||||
Spec: api.ServiceSpec{
|
||||
Type: api.ServiceTypeNodePort,
|
||||
},
|
||||
},
|
||||
&api.Service{
|
||||
Spec: api.ServiceSpec{
|
||||
Type: api.ServiceTypeNodePort,
|
||||
ExternalTrafficPolicy: api.ServiceExternalTrafficPolicyTypeGlobal,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
&api.Service{
|
||||
Spec: api.ServiceSpec{
|
||||
Type: api.ServiceTypeClusterIP,
|
||||
},
|
||||
},
|
||||
&api.Service{
|
||||
Spec: api.ServiceSpec{
|
||||
Type: api.ServiceTypeClusterIP,
|
||||
},
|
||||
},
|
||||
},
|
||||
// Beta annotations cases.
|
||||
{
|
||||
&api.Service{
|
||||
Spec: api.ServiceSpec{
|
||||
Type: api.ServiceTypeLoadBalancer,
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Annotations: map[string]string{
|
||||
api.BetaAnnotationExternalTraffic: api.AnnotationValueExternalTrafficLocal,
|
||||
},
|
||||
},
|
||||
},
|
||||
&api.Service{
|
||||
Spec: api.ServiceSpec{
|
||||
Type: api.ServiceTypeLoadBalancer,
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Annotations: map[string]string{
|
||||
api.BetaAnnotationExternalTraffic: api.AnnotationValueExternalTrafficLocal,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
&api.Service{
|
||||
Spec: api.ServiceSpec{
|
||||
Type: api.ServiceTypeLoadBalancer,
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Annotations: map[string]string{
|
||||
api.BetaAnnotationExternalTraffic: api.AnnotationValueExternalTrafficGlobal,
|
||||
},
|
||||
},
|
||||
},
|
||||
&api.Service{
|
||||
Spec: api.ServiceSpec{
|
||||
Type: api.ServiceTypeLoadBalancer,
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Annotations: map[string]string{
|
||||
api.BetaAnnotationExternalTraffic: api.AnnotationValueExternalTrafficGlobal,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for i, tc := range testCases {
|
||||
SetDefaultExternalTrafficPolicyIfNeeded(tc.inputService)
|
||||
if !reflect.DeepEqual(tc.inputService, tc.expectedService) {
|
||||
t.Errorf("%v: got unexpected service", i)
|
||||
spew.Dump(tc)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestClearExternalTrafficPolicy(t *testing.T) {
|
||||
testCases := []struct {
|
||||
inputService *api.Service
|
||||
|
Reference in New Issue
Block a user