99 "time"
1010
1111 "github.com/stretchr/testify/require"
12- v1 "k8s.io/api/core/v1"
12+ appsv1 "k8s.io/api/apps/v1"
13+ "k8s.io/apimachinery/pkg/types"
1314 "k8s.io/apimachinery/pkg/util/wait"
1415 "k8s.io/kubernetes/test/e2e/framework"
1516 e2ekubectl "k8s.io/kubernetes/test/e2e/framework/kubectl"
@@ -18,6 +19,10 @@ import (
1819 "sigs.k8s.io/network-policy-api/conformance/utils/config"
1920)
2021
22+ var (
23+ numStatufulSetReplicas int32 = 2
24+ )
25+
2126// PokeServer is a utility function that checks if the connection from the provided clientPod in clientNamespace towards the targetHost:targetPort
2227// using the provided protocol can be established or not and returns the result based on if the expectation is shouldConnect or !shouldConnect
2328func PokeServer (t * testing.T , clientNamespace , clientPod , protocol , targetHost string , targetPort int32 , timeout time.Duration , shouldConnect bool ) bool {
@@ -71,45 +76,28 @@ func PokeServer(t *testing.T, clientNamespace, clientPod, protocol, targetHost s
7176
7277// NamespacesMustBeReady waits until all Pods are marked Ready. This will
7378// cause the test to halt if the specified timeout is exceeded.
74- func NamespacesMustBeReady (t * testing.T , c client.Client , timeoutConfig config.TimeoutConfig , namespaces []string ) {
79+ func NamespacesMustBeReady (t * testing.T , c client.Client , timeoutConfig config.TimeoutConfig , namespaces []string , statefulSetNames [] string ) {
7580 t .Helper ()
81+ ctx , cancel := context .WithTimeout (context .Background (), timeoutConfig .NamespacesMustBeReady )
82+ defer cancel ()
7683
7784 waitErr := wait .PollImmediate (1 * time .Second , timeoutConfig .NamespacesMustBeReady , func () (bool , error ) {
78- ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
79- defer cancel ()
80-
81- for _ , ns := range namespaces {
82- podList := & v1.PodList {}
83- err := c .List (ctx , podList , client .InNamespace (ns ))
84- if err != nil {
85- t .Errorf ("Error listing Pods: %v" , err )
85+ for i , ns := range namespaces {
86+ statefulSet := & appsv1.StatefulSet {}
87+ statefulSetKey := types.NamespacedName {
88+ Namespace : ns ,
89+ Name : statefulSetNames [i ],
8690 }
87- for _ , pod := range podList . Items {
88- if ! findPodConditionInList ( t , pod . Status . Conditions , "Ready" , "True" ) &&
89- pod . Status . Phase != v1 . PodSucceeded {
90- t . Logf ( "%s/%s Pod not ready yet" , ns , pod . Name )
91- return false , nil
92- }
91+ if err := c . Get ( ctx , statefulSetKey , statefulSet ); err != nil {
92+ t . Errorf ( "Error retrieving StatefulSet %s from namespace %s: %v" , statefulSetNames [ i ], ns , err )
93+ }
94+ if statefulSet . Status . ReadyReplicas != numStatufulSetReplicas {
95+ t . Logf ( "StatefulSet replicas in namespace %s not rolled out yet. %d/%d replicas are available." , ns , statefulSet . Status . ReadyReplicas , numStatufulSetReplicas )
96+ return false , nil
9397 }
9498 }
95- t .Logf ("Namespaces and Pods in %s namespaces ready" , strings .Join (namespaces , ", " ))
99+ t .Logf ("Namespaces and Pods in %s namespaces are ready" , strings .Join (namespaces , ", " ))
96100 return true , nil
97101 })
98102 require .NoErrorf (t , waitErr , "error waiting for %s namespaces to be ready" , strings .Join (namespaces , ", " ))
99103}
100-
101- func findPodConditionInList (t * testing.T , conditions []v1.PodCondition , condName , condValue string ) bool {
102- t .Helper ()
103-
104- for _ , cond := range conditions {
105- if cond .Type == v1 .PodConditionType (condName ) {
106- if cond .Status == v1 .ConditionStatus (condValue ) {
107- return true
108- }
109- t .Logf ("%s condition set to %s, expected %s" , condName , cond .Status , condValue )
110- }
111- }
112-
113- t .Logf ("%s was not in conditions list" , condName )
114- return false
115- }
0 commit comments