Merge pull request #119150 from tnqn/external-traffic-policy-external-ips
Allow specifying ExternalTrafficPolicy for Services with ExternalIPs
This commit is contained in:
@@ -29,7 +29,7 @@ import (
|
||||
|
||||
// Test_ExternalNameServiceStopsDefaultingInternalTrafficPolicy tests that Services no longer default
|
||||
// the internalTrafficPolicy field when Type is ExternalName. This test exists due to historic reasons where
|
||||
// the internalTrafficPolicy field was being defaulted in older versions. New versions stop defauting the
|
||||
// the internalTrafficPolicy field was being defaulted in older versions. New versions stop defaulting the
|
||||
// field and drop on read, but for compatibility reasons we still accept the field.
|
||||
func Test_ExternalNameServiceStopsDefaultingInternalTrafficPolicy(t *testing.T) {
|
||||
server := kubeapiservertesting.StartTestServerOrDie(t, nil, nil, framework.SharedEtcd())
|
||||
@@ -74,7 +74,7 @@ func Test_ExternalNameServiceStopsDefaultingInternalTrafficPolicy(t *testing.T)
|
||||
|
||||
// Test_ExternalNameServiceDropsInternalTrafficPolicy tests that Services accepts the internalTrafficPolicy field on Create,
|
||||
// but drops the field on read. This test exists due to historic reasons where the internalTrafficPolicy field was being defaulted
|
||||
// in older versions. New versions stop defauting the field and drop on read, but for compatibility reasons we still accept the field.
|
||||
// in older versions. New versions stop defaulting the field and drop on read, but for compatibility reasons we still accept the field.
|
||||
func Test_ExternalNameServiceDropsInternalTrafficPolicy(t *testing.T) {
|
||||
server := kubeapiservertesting.StartTestServerOrDie(t, nil, nil, framework.SharedEtcd())
|
||||
defer server.TearDownFn()
|
||||
@@ -120,7 +120,7 @@ func Test_ExternalNameServiceDropsInternalTrafficPolicy(t *testing.T) {
|
||||
|
||||
// Test_ConvertingToExternalNameServiceDropsInternalTrafficPolicy tests that converting a Service to Type=ExternalName
|
||||
// results in the internalTrafficPolicy field being dropped.This test exists due to historic reasons where the internalTrafficPolicy
|
||||
// field was being defaulted in older versions. New versions stop defauting the field and drop on read, but for compatibility reasons
|
||||
// field was being defaulted in older versions. New versions stop defaulting the field and drop on read, but for compatibility reasons
|
||||
// we still accept the field.
|
||||
func Test_ConvertingToExternalNameServiceDropsInternalTrafficPolicy(t *testing.T) {
|
||||
server := kubeapiservertesting.StartTestServerOrDie(t, nil, nil, framework.SharedEtcd())
|
||||
@@ -164,7 +164,7 @@ func Test_ConvertingToExternalNameServiceDropsInternalTrafficPolicy(t *testing.T
|
||||
|
||||
service, err = client.CoreV1().Services(ns.Name).Update(context.TODO(), newService, metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
t.Fatalf("error getting service: %v", err)
|
||||
t.Fatalf("error updating service: %v", err)
|
||||
}
|
||||
|
||||
if service.Spec.InternalTrafficPolicy != nil {
|
||||
@@ -180,3 +180,87 @@ func Test_ConvertingToExternalNameServiceDropsInternalTrafficPolicy(t *testing.T
|
||||
t.Errorf("service internalTrafficPolicy should be droppped but is set: %v", service.Spec.InternalTrafficPolicy)
|
||||
}
|
||||
}
|
||||
|
||||
// Test_RemovingExternalIPsFromClusterIPServiceDropsExternalTrafficPolicy tests that removing externalIPs from a
|
||||
// ClusterIP Service results in the externalTrafficPolicy field being dropped.
|
||||
func Test_RemovingExternalIPsFromClusterIPServiceDropsExternalTrafficPolicy(t *testing.T) {
|
||||
server := kubeapiservertesting.StartTestServerOrDie(t, nil, nil, framework.SharedEtcd())
|
||||
defer server.TearDownFn()
|
||||
|
||||
client, err := clientset.NewForConfig(server.ClientConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating clientset: %v", err)
|
||||
}
|
||||
|
||||
ns := framework.CreateNamespaceOrDie(client, "test-removing-external-ips-drops-external-traffic-policy", t)
|
||||
defer framework.DeleteNamespaceOrDie(client, ns, t)
|
||||
|
||||
service := &corev1.Service{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-123",
|
||||
},
|
||||
Spec: corev1.ServiceSpec{
|
||||
Type: corev1.ServiceTypeClusterIP,
|
||||
Ports: []corev1.ServicePort{{
|
||||
Port: int32(80),
|
||||
}},
|
||||
Selector: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
ExternalIPs: []string{"1.1.1.1"},
|
||||
},
|
||||
}
|
||||
|
||||
service, err = client.CoreV1().Services(ns.Name).Create(context.TODO(), service, metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating test service: %v", err)
|
||||
}
|
||||
|
||||
if service.Spec.ExternalTrafficPolicy != corev1.ServiceExternalTrafficPolicyCluster {
|
||||
t.Error("service externalTrafficPolicy was not set for clusterIP Service with externalIPs")
|
||||
}
|
||||
|
||||
// externalTrafficPolicy should be dropped after removing externalIPs.
|
||||
newService := service.DeepCopy()
|
||||
newService.Spec.ExternalIPs = []string{}
|
||||
|
||||
service, err = client.CoreV1().Services(ns.Name).Update(context.TODO(), newService, metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
t.Fatalf("error updating service: %v", err)
|
||||
}
|
||||
|
||||
if service.Spec.ExternalTrafficPolicy != "" {
|
||||
t.Errorf("service externalTrafficPolicy should be droppped but is set: %v", service.Spec.ExternalTrafficPolicy)
|
||||
}
|
||||
|
||||
service, err = client.CoreV1().Services(ns.Name).Get(context.TODO(), service.Name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
t.Fatalf("error getting service: %v", err)
|
||||
}
|
||||
|
||||
if service.Spec.ExternalTrafficPolicy != "" {
|
||||
t.Errorf("service externalTrafficPolicy should be droppped but is set: %v", service.Spec.ExternalTrafficPolicy)
|
||||
}
|
||||
|
||||
// externalTrafficPolicy should be set after adding externalIPs again.
|
||||
newService = service.DeepCopy()
|
||||
newService.Spec.ExternalIPs = []string{"1.1.1.1"}
|
||||
|
||||
service, err = client.CoreV1().Services(ns.Name).Update(context.TODO(), newService, metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
t.Fatalf("error updating service: %v", err)
|
||||
}
|
||||
|
||||
if service.Spec.ExternalTrafficPolicy != corev1.ServiceExternalTrafficPolicyCluster {
|
||||
t.Error("service externalTrafficPolicy was not set for clusterIP Service with externalIPs")
|
||||
}
|
||||
|
||||
service, err = client.CoreV1().Services(ns.Name).Get(context.TODO(), service.Name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
t.Fatalf("error getting service: %v", err)
|
||||
}
|
||||
|
||||
if service.Spec.ExternalTrafficPolicy != corev1.ServiceExternalTrafficPolicyCluster {
|
||||
t.Error("service externalTrafficPolicy was not set for clusterIP Service with externalIPs")
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user