@@ -28,26 +28,27 @@ import (
28
28
"time"
29
29
30
30
"github.com/onsi/ginkgo/v2"
31
+ appsv1 "k8s.io/api/apps/v1"
31
32
v1 "k8s.io/api/core/v1"
32
33
discoveryv1 "k8s.io/api/discovery/v1"
33
34
policyv1 "k8s.io/api/policy/v1"
34
35
apierrors "k8s.io/apimachinery/pkg/api/errors"
35
36
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
36
- "k8s.io/apimachinery/pkg/labels"
37
37
"k8s.io/apimachinery/pkg/util/intstr"
38
38
utilnet "k8s.io/apimachinery/pkg/util/net"
39
39
"k8s.io/apimachinery/pkg/util/sets"
40
40
"k8s.io/apimachinery/pkg/util/uuid"
41
41
"k8s.io/apimachinery/pkg/util/wait"
42
42
clientset "k8s.io/client-go/kubernetes"
43
43
"k8s.io/kubernetes/test/e2e/framework"
44
+ e2edeployment "k8s.io/kubernetes/test/e2e/framework/deployment"
44
45
e2enode "k8s.io/kubernetes/test/e2e/framework/node"
45
- e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
46
46
e2epodoutput "k8s.io/kubernetes/test/e2e/framework/pod/output"
47
47
e2erc "k8s.io/kubernetes/test/e2e/framework/rc"
48
48
testutils "k8s.io/kubernetes/test/utils"
49
49
imageutils "k8s.io/kubernetes/test/utils/image"
50
50
netutils "k8s.io/utils/net"
51
+ "k8s.io/utils/ptr"
51
52
)
52
53
53
54
// NodePortRange should match whatever the default/configured range is
@@ -655,23 +656,25 @@ func (j *TestJig) waitForCondition(ctx context.Context, timeout time.Duration, m
655
656
return service , nil
656
657
}
657
658
658
- // newRCTemplate returns the default v1.ReplicationController object for
659
- // this j, but does not actually create the RC. The default RC has the same
659
+ // newDeploymentTemplate returns the default appsv1.Deployment object for
660
+ // this j, but does not actually create the Deployment. The default Deployment has the same
660
661
// name as the j and runs the "netexec" container.
661
- func (j * TestJig ) newRCTemplate () * v1. ReplicationController {
662
+ func (j * TestJig ) newDeploymentTemplate () * appsv1. Deployment {
662
663
var replicas int32 = 1
663
664
var grace int64 = 3 // so we don't race with kube-proxy when scaling up/down
664
665
665
- rc := & v1. ReplicationController {
666
+ deployment := & appsv1. Deployment {
666
667
ObjectMeta : metav1.ObjectMeta {
667
668
Namespace : j .Namespace ,
668
669
Name : j .Name ,
669
670
Labels : j .Labels ,
670
671
},
671
- Spec : v1. ReplicationControllerSpec {
672
+ Spec : appsv1. DeploymentSpec {
672
673
Replicas : & replicas ,
673
- Selector : j .Labels ,
674
- Template : & v1.PodTemplateSpec {
674
+ Selector : & metav1.LabelSelector {
675
+ MatchLabels : j .Labels ,
676
+ },
677
+ Template : v1.PodTemplateSpec {
675
678
ObjectMeta : metav1.ObjectMeta {
676
679
Labels : j .Labels ,
677
680
},
@@ -697,32 +700,32 @@ func (j *TestJig) newRCTemplate() *v1.ReplicationController {
697
700
},
698
701
},
699
702
}
700
- return rc
703
+ return deployment
701
704
}
702
705
703
- // AddRCAntiAffinity adds AntiAffinity to the given ReplicationController .
704
- func (j * TestJig ) AddRCAntiAffinity ( rc * v1. ReplicationController ) {
706
+ // AddDeploymentAntiAffinity adds AntiAffinity to the given Deployment .
707
+ func (j * TestJig ) AddDeploymentAntiAffinity ( deployment * appsv1. Deployment ) {
705
708
var replicas int32 = 2
706
709
707
- rc .Spec .Replicas = & replicas
708
- if rc .Spec .Template .Spec .Affinity == nil {
709
- rc .Spec .Template .Spec .Affinity = & v1.Affinity {}
710
+ deployment .Spec .Replicas = & replicas
711
+ if deployment .Spec .Template .Spec .Affinity == nil {
712
+ deployment .Spec .Template .Spec .Affinity = & v1.Affinity {}
710
713
}
711
- if rc .Spec .Template .Spec .Affinity .PodAntiAffinity == nil {
712
- rc .Spec .Template .Spec .Affinity .PodAntiAffinity = & v1.PodAntiAffinity {}
714
+ if deployment .Spec .Template .Spec .Affinity .PodAntiAffinity == nil {
715
+ deployment .Spec .Template .Spec .Affinity .PodAntiAffinity = & v1.PodAntiAffinity {}
713
716
}
714
- rc .Spec .Template .Spec .Affinity .PodAntiAffinity .RequiredDuringSchedulingIgnoredDuringExecution = append (
715
- rc .Spec .Template .Spec .Affinity .PodAntiAffinity .RequiredDuringSchedulingIgnoredDuringExecution ,
717
+ deployment .Spec .Template .Spec .Affinity .PodAntiAffinity .RequiredDuringSchedulingIgnoredDuringExecution = append (
718
+ deployment .Spec .Template .Spec .Affinity .PodAntiAffinity .RequiredDuringSchedulingIgnoredDuringExecution ,
716
719
v1.PodAffinityTerm {
717
720
LabelSelector : & metav1.LabelSelector {MatchLabels : j .Labels },
718
721
Namespaces : nil ,
719
722
TopologyKey : "kubernetes.io/hostname" ,
720
723
})
721
724
}
722
725
723
- // CreatePDB returns a PodDisruptionBudget for the given ReplicationController , or returns an error if a PodDisruptionBudget isn't ready
724
- func (j * TestJig ) CreatePDB (ctx context.Context , rc * v1. ReplicationController ) (* policyv1.PodDisruptionBudget , error ) {
725
- pdb := j .newPDBTemplate (rc )
726
+ // CreatePDB returns a PodDisruptionBudget for the given Deployment , or returns an error if a PodDisruptionBudget isn't ready
727
+ func (j * TestJig ) CreatePDB (ctx context.Context , deployment * appsv1. Deployment ) (* policyv1.PodDisruptionBudget , error ) {
728
+ pdb := j .newPDBTemplate (deployment )
726
729
newPdb , err := j .Client .PolicyV1 ().PodDisruptionBudgets (j .Namespace ).Create (ctx , pdb , metav1.CreateOptions {})
727
730
if err != nil {
728
731
return nil , fmt .Errorf ("failed to create PDB %q %v" , pdb .Name , err )
@@ -736,8 +739,8 @@ func (j *TestJig) CreatePDB(ctx context.Context, rc *v1.ReplicationController) (
736
739
737
740
// newPDBTemplate returns the default policyv1.PodDisruptionBudget object for
738
741
// this j, but does not actually create the PDB. The default PDB specifies a
739
- // MinAvailable of N-1 and matches the pods created by the RC .
740
- func (j * TestJig ) newPDBTemplate (rc * v1. ReplicationController ) * policyv1.PodDisruptionBudget {
742
+ // MinAvailable of N-1 and matches the pods created by the Deployment .
743
+ func (j * TestJig ) newPDBTemplate (rc * appsv1. Deployment ) * policyv1.PodDisruptionBudget {
741
744
minAvailable := intstr .FromInt32 (* rc .Spec .Replicas - 1 )
742
745
743
746
pdb := & policyv1.PodDisruptionBudget {
@@ -755,49 +758,43 @@ func (j *TestJig) newPDBTemplate(rc *v1.ReplicationController) *policyv1.PodDisr
755
758
return pdb
756
759
}
757
760
758
- // Run creates a ReplicationController and Pod(s) and waits for the
759
- // Pod(s) to be running. Callers can provide a function to tweak the RC object
761
+ // Run creates a Deployment and Pod(s) and waits for the
762
+ // Pod(s) to be running. Callers can provide a function to tweak the Deployment object
760
763
// before it is created.
761
- func (j * TestJig ) Run (ctx context.Context , tweak func (rc * v1. ReplicationController )) (* v1. ReplicationController , error ) {
762
- rc := j .newRCTemplate ()
764
+ func (j * TestJig ) Run (ctx context.Context , tweak func (rc * appsv1. Deployment )) (* appsv1. Deployment , error ) {
765
+ deployment := j .newDeploymentTemplate ()
763
766
if tweak != nil {
764
- tweak (rc )
767
+ tweak (deployment )
765
768
}
766
- result , err := j .Client .CoreV1 ().ReplicationControllers (j .Namespace ).Create (ctx , rc , metav1.CreateOptions {})
769
+
770
+ result , err := j .Client .AppsV1 ().Deployments (j .Namespace ).Create (ctx , deployment , metav1.CreateOptions {})
767
771
if err != nil {
768
- return nil , fmt .Errorf ("failed to create RC %q: %w" , rc .Name , err )
772
+ return nil , fmt .Errorf ("failed to create Deployment %q: %w" , deployment .Name , err )
769
773
}
770
- pods , err := j .waitForPodsCreated (ctx , int (* (rc .Spec .Replicas )))
774
+
775
+ err = e2edeployment .WaitForDeploymentComplete (j .Client , result )
771
776
if err != nil {
772
- return nil , fmt .Errorf ("failed to create pods: %w" , err )
773
- }
774
- if err := j .waitForPodsReady (ctx , pods ); err != nil {
775
- return nil , fmt .Errorf ("failed waiting for pods to be running: %w" , err )
777
+ return nil , fmt .Errorf ("failed waiting for Deployment %q: %w" , deployment .Name , err )
776
778
}
779
+
777
780
return result , nil
778
781
}
779
782
780
783
// Scale scales pods to the given replicas
781
- func (j * TestJig ) Scale (ctx context.Context , replicas int ) error {
782
- rc := j .Name
783
- scale , err := j .Client .CoreV1 ().ReplicationControllers (j .Namespace ).GetScale (ctx , rc , metav1.GetOptions {})
784
+ func (j * TestJig ) Scale (replicas int ) error {
785
+ deployment , err := e2edeployment .UpdateDeploymentWithRetries (j .Client , j .Namespace , j .Name , func (deployment * appsv1.Deployment ) {
786
+ deployment .Spec .Replicas = ptr .To (int32 (replicas ))
787
+ })
784
788
if err != nil {
785
- return fmt .Errorf ("failed to get scale for RC %q: %w" , rc , err )
789
+ return fmt .Errorf ("failed to scale Deployment %q: %w" , j . Name , err )
786
790
}
787
791
788
- scale .ResourceVersion = "" // indicate the scale update should be unconditional
789
- scale .Spec .Replicas = int32 (replicas )
790
- _ , err = j .Client .CoreV1 ().ReplicationControllers (j .Namespace ).UpdateScale (ctx , rc , scale , metav1.UpdateOptions {})
791
- if err != nil {
792
- return fmt .Errorf ("failed to scale RC %q: %w" , rc , err )
793
- }
794
- pods , err := j .waitForPodsCreated (ctx , replicas )
792
+ err = e2edeployment .WaitForDeploymentComplete (j .Client , deployment )
793
+
795
794
if err != nil {
796
- return fmt .Errorf ("failed waiting for pods: %w" , err )
797
- }
798
- if err := j .waitForPodsReady (ctx , pods ); err != nil {
799
- return fmt .Errorf ("failed waiting for pods to be running: %w" , err )
795
+ return fmt .Errorf ("failed waiting for Deployment %q: %w" , j .Name , err )
800
796
}
797
+
801
798
return nil
802
799
}
803
800
@@ -816,43 +813,6 @@ func (j *TestJig) waitForPdbReady(ctx context.Context) error {
816
813
return fmt .Errorf ("timeout waiting for PDB %q to be ready" , j .Name )
817
814
}
818
815
819
- func (j * TestJig ) waitForPodsCreated (ctx context.Context , replicas int ) ([]string , error ) {
820
- // TODO (pohly): replace with gomega.Eventually
821
- timeout := 2 * time .Minute
822
- // List the pods, making sure we observe all the replicas.
823
- label := labels .SelectorFromSet (labels .Set (j .Labels ))
824
- framework .Logf ("Waiting up to %v for %d pods to be created" , timeout , replicas )
825
- for start := time .Now (); time .Since (start ) < timeout && ctx .Err () == nil ; time .Sleep (2 * time .Second ) {
826
- options := metav1.ListOptions {LabelSelector : label .String ()}
827
- pods , err := j .Client .CoreV1 ().Pods (j .Namespace ).List (ctx , options )
828
- if err != nil {
829
- return nil , err
830
- }
831
-
832
- found := []string {}
833
- for _ , pod := range pods .Items {
834
- if pod .DeletionTimestamp != nil {
835
- continue
836
- }
837
- found = append (found , pod .Name )
838
- }
839
- if len (found ) == replicas {
840
- framework .Logf ("Found all %d pods" , replicas )
841
- return found , nil
842
- }
843
- framework .Logf ("Found %d/%d pods - will retry" , len (found ), replicas )
844
- }
845
- return nil , fmt .Errorf ("timeout waiting for %d pods to be created" , replicas )
846
- }
847
-
848
- func (j * TestJig ) waitForPodsReady (ctx context.Context , pods []string ) error {
849
- timeout := 2 * time .Minute
850
- if ! e2epod .CheckPodsRunningReady (ctx , j .Client , j .Namespace , pods , timeout ) {
851
- return fmt .Errorf ("timeout waiting for %d pods to be ready" , len (pods ))
852
- }
853
- return nil
854
- }
855
-
856
816
func testReachabilityOverServiceName (ctx context.Context , serviceName string , sp v1.ServicePort , execPod * v1.Pod ) error {
857
817
return testEndpointReachability (ctx , serviceName , sp .Port , sp .Protocol , execPod )
858
818
}
0 commit comments