@@ -24,6 +24,7 @@ import (
24
24
"testing"
25
25
"time"
26
26
27
+ "github.com/google/go-cmp/cmp"
27
28
corev1 "k8s.io/api/core/v1"
28
29
discoveryv1 "k8s.io/api/discovery/v1"
29
30
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -568,6 +569,88 @@ func Test_TransitionsForTrafficDistribution(t *testing.T) {
568
569
logsBuffer .Reset ()
569
570
}
570
571
572
+ func Test_TrafficDistribution_FeatureGateEnableDisable (t * testing.T ) {
573
+
574
+ ////////////////////////////////////////////////////////////////////////////
575
+ // Start kube-apiserver with ServiceTrafficDistribution feature-gate
576
+ // enabled.
577
+ ////////////////////////////////////////////////////////////////////////////
578
+
579
+ featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .ServiceTrafficDistribution , true )
580
+
581
+ sharedEtcd := framework .SharedEtcd ()
582
+ server1 := kubeapiservertesting .StartTestServerOrDie (t , nil , framework .DefaultTestServerFlags (), sharedEtcd )
583
+
584
+ client1 , err := clientset .NewForConfig (server1 .ClientConfig )
585
+ if err != nil {
586
+ t .Fatalf ("Error creating clientset: %v" , err )
587
+ }
588
+
589
+ ////////////////////////////////////////////////////////////////////////////
590
+ // Create a Service and set `trafficDistribution: PreferLocal` field.
591
+ //
592
+ // Assert that the field is present in the created Service.
593
+ ////////////////////////////////////////////////////////////////////////////
594
+
595
+ ctx := ktesting .Init (t )
596
+ defer ctx .Cancel ("test has completed" )
597
+
598
+ ns := framework .CreateNamespaceOrDie (client1 , "test-service-traffic-distribution" , t )
599
+
600
+ trafficDist := corev1 .ServiceTrafficDistributionPreferClose
601
+ svcToCreate := & corev1.Service {
602
+ ObjectMeta : metav1.ObjectMeta {
603
+ Name : "test-service" ,
604
+ Namespace : ns .GetName (),
605
+ },
606
+ Spec : corev1.ServiceSpec {
607
+ Selector : map [string ]string {
608
+ "app" : "v1" ,
609
+ },
610
+ Ports : []corev1.ServicePort {
611
+ {Name : "port-443" , Port : 443 , Protocol : "TCP" , TargetPort : intstr .FromInt32 (443 )},
612
+ },
613
+ TrafficDistribution : & trafficDist ,
614
+ },
615
+ }
616
+ svc , err := client1 .CoreV1 ().Services (ns .Name ).Create (ctx , svcToCreate , metav1.CreateOptions {})
617
+ if err != nil {
618
+ t .Fatalf ("Failed to create test service: %v" , err )
619
+ }
620
+
621
+ if diff := cmp .Diff (svcToCreate .Spec .TrafficDistribution , svc .Spec .TrafficDistribution ); diff != "" {
622
+ t .Fatalf ("Unexpected diff found in service .spec.trafficDistribution after creation: (-want, +got)\n :%v" , diff )
623
+ }
624
+
625
+ ////////////////////////////////////////////////////////////////////////////
626
+ // Restart the kube-apiserver with ServiceTrafficDistribution feature-gate
627
+ // disabled. Update the test service.
628
+ //
629
+ // Assert that updating the service does not drop the field since it was
630
+ // being used previously.
631
+ ////////////////////////////////////////////////////////////////////////////
632
+
633
+ server1 .TearDownFn ()
634
+ featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .ServiceTrafficDistribution , false )
635
+ server2 := kubeapiservertesting .StartTestServerOrDie (t , nil , framework .DefaultTestServerFlags (), sharedEtcd )
636
+ defer server2 .TearDownFn ()
637
+ client2 , err := clientset .NewForConfig (server2 .ClientConfig )
638
+ if err != nil {
639
+ t .Fatalf ("Error creating clientset: %v" , err )
640
+ }
641
+
642
+ svcToUpdate := svcToCreate .DeepCopy ()
643
+ svcToUpdate .Spec .Selector = map [string ]string {"app" : "v2" }
644
+ svc , err = client2 .CoreV1 ().Services (ns .Name ).Update (ctx , svcToUpdate , metav1.UpdateOptions {})
645
+ if err != nil {
646
+ t .Fatalf ("Failed to update test service: %v" , err )
647
+ }
648
+
649
+ if diff := cmp .Diff (svcToUpdate .Spec .TrafficDistribution , svc .Spec .TrafficDistribution ); diff != "" {
650
+ t .Fatalf ("Unexpected diff found in service .spec.trafficDistribution after update: (-want, +got)\n :%v" , diff )
651
+ }
652
+ }
653
+
571
654
func Test_ServiceClusterIPSelector (t * testing.T ) {
572
655
server := kubeapiservertesting .StartTestServerOrDie (t , nil , framework .DefaultTestServerFlags (), framework .SharedEtcd ())
573
656
defer server .TearDownFn ()
0 commit comments