9
9
"time"
10
10
11
11
"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"
13
14
"k8s.io/apimachinery/pkg/util/wait"
14
15
"k8s.io/kubernetes/test/e2e/framework"
15
16
e2ekubectl "k8s.io/kubernetes/test/e2e/framework/kubectl"
@@ -18,6 +19,10 @@ import (
18
19
"sigs.k8s.io/network-policy-api/conformance/utils/config"
19
20
)
20
21
22
+ var (
23
+ numStatufulSetReplicas int32 = 2
24
+ )
25
+
21
26
// PokeServer is a utility function that checks if the connection from the provided clientPod in clientNamespace towards the targetHost:targetPort
22
27
// using the provided protocol can be established or not and returns the result based on if the expectation is shouldConnect or !shouldConnect
23
28
func 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
71
76
72
77
// NamespacesMustBeReady waits until all Pods are marked Ready. This will
73
78
// 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 ) {
75
80
t .Helper ()
81
+ ctx , cancel := context .WithTimeout (context .Background (), timeoutConfig .NamespacesMustBeReady )
82
+ defer cancel ()
76
83
77
84
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 ],
86
90
}
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
93
97
}
94
98
}
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 , ", " ))
96
100
return true , nil
97
101
})
98
102
require .NoErrorf (t , waitErr , "error waiting for %s namespaces to be ready" , strings .Join (namespaces , ", " ))
99
103
}
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