@@ -26,13 +26,15 @@ import (
26
26
"math/rand"
27
27
"net"
28
28
"os/exec"
29
+ "sort"
29
30
"strings"
30
31
"time"
31
32
32
33
. "github.com/onsi/ginkgo/v2"
33
34
. "github.com/onsi/gomega"
34
35
"github.com/pkg/errors"
35
36
v1 "k8s.io/api/apps/v1"
37
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
36
38
"k8s.io/apimachinery/pkg/types"
37
39
"k8s.io/klog/v2"
38
40
@@ -140,7 +142,7 @@ func ApplyClusterTemplateAndWait(ctx context.Context, input ApplyClusterTemplate
140
142
input .WaitForControlPlaneInitialized (ctx , input , result )
141
143
142
144
Byf ("Waiting for the machine deployments to be provisioned" )
143
- result .MachineDeployments = framework . DiscoveryAndWaitForMachineDeployments (ctx , framework.DiscoveryAndWaitForMachineDeploymentsInput {
145
+ result .MachineDeployments = DiscoveryAndWaitForMachineDeployments (ctx , framework.DiscoveryAndWaitForMachineDeploymentsInput {
144
146
Lister : input .ClusterProxy .GetClient (),
145
147
Cluster : result .Cluster ,
146
148
}, input .WaitForMachineDeployments ... )
@@ -151,6 +153,71 @@ func ApplyClusterTemplateAndWait(ctx context.Context, input ApplyClusterTemplate
151
153
}
152
154
}
153
155
156
+ // DiscoveryAndWaitForMachineDeployments discovers the MachineDeployments existing in a cluster and waits for them to be ready (all the machine provisioned).
157
+ func DiscoveryAndWaitForMachineDeployments (ctx context.Context , input framework.DiscoveryAndWaitForMachineDeploymentsInput , intervals ... interface {}) []* clusterv1.MachineDeployment {
158
+ Expect (ctx ).NotTo (BeNil (), "ctx is required for DiscoveryAndWaitForMachineDeployments" )
159
+ Expect (input .Lister ).ToNot (BeNil (), "Invalid argument. input.Lister can't be nil when calling DiscoveryAndWaitForMachineDeployments" )
160
+ Expect (input .Cluster ).ToNot (BeNil (), "Invalid argument. input.Cluster can't be nil when calling DiscoveryAndWaitForMachineDeployments" )
161
+
162
+ machineDeployments := framework .GetMachineDeploymentsByCluster (ctx , framework.GetMachineDeploymentsByClusterInput {
163
+ Lister : input .Lister ,
164
+ ClusterName : input .Cluster .Name ,
165
+ Namespace : input .Cluster .Namespace ,
166
+ })
167
+ for _ , deployment := range machineDeployments {
168
+ WaitForMachineDeploymentNodesToExist (ctx , framework.WaitForMachineDeploymentNodesToExistInput {
169
+ Lister : input .Lister ,
170
+ Cluster : input .Cluster ,
171
+ MachineDeployment : deployment ,
172
+ }, intervals ... )
173
+
174
+ framework .AssertMachineDeploymentFailureDomains (ctx , framework.AssertMachineDeploymentFailureDomainsInput {
175
+ Lister : input .Lister ,
176
+ Cluster : input .Cluster ,
177
+ MachineDeployment : deployment ,
178
+ })
179
+ }
180
+ return machineDeployments
181
+ }
182
+
183
+ // WaitForMachineDeploymentNodesToExist waits until all nodes associated with a machine deployment exist.
184
+ func WaitForMachineDeploymentNodesToExist (ctx context.Context , input framework.WaitForMachineDeploymentNodesToExistInput , intervals ... interface {}) {
185
+ Expect (ctx ).NotTo (BeNil (), "ctx is required for WaitForMachineDeploymentNodesToExist" )
186
+ Expect (input .Lister ).ToNot (BeNil (), "Invalid argument. input.Lister can't be nil when calling WaitForMachineDeploymentNodesToExist" )
187
+ Expect (input .MachineDeployment ).ToNot (BeNil (), "Invalid argument. input.MachineDeployment can't be nil when calling WaitForMachineDeploymentNodesToExist" )
188
+
189
+ By ("Waiting for the workload nodes to exist" )
190
+ Eventually (func (g Gomega ) {
191
+ selectorMap , err := metav1 .LabelSelectorAsMap (& input .MachineDeployment .Spec .Selector )
192
+ g .Expect (err ).ToNot (HaveOccurred ())
193
+ ms := & clusterv1.MachineSetList {}
194
+ err = input .Lister .List (ctx , ms , client .InNamespace (input .Cluster .Namespace ), client .MatchingLabels (selectorMap ))
195
+ g .Expect (err ).ToNot (HaveOccurred ())
196
+ g .Expect (ms .Items ).NotTo (BeEmpty ())
197
+ machineSet := ms .Items [0 ]
198
+ sort .Slice (ms .Items , func (i , j int ) bool {
199
+ return ms .Items [j ].CreationTimestamp .After (ms .Items [i ].CreationTimestamp .Time )
200
+ })
201
+ for _ , ms := range ms .Items {
202
+ if * machineSet .Spec .Replicas == * input .MachineDeployment .Spec .Replicas {
203
+ machineSet = ms
204
+ }
205
+ }
206
+ selectorMap , err = metav1 .LabelSelectorAsMap (& machineSet .Spec .Selector )
207
+ g .Expect (err ).ToNot (HaveOccurred ())
208
+ machines := & clusterv1.MachineList {}
209
+ err = input .Lister .List (ctx , machines , client .InNamespace (machineSet .Namespace ), client .MatchingLabels (selectorMap ))
210
+ g .Expect (err ).ToNot (HaveOccurred ())
211
+ count := 0
212
+ for _ , machine := range machines .Items {
213
+ if machine .Status .NodeRef != nil {
214
+ count ++
215
+ }
216
+ }
217
+ g .Expect (count ).To (Equal (int (* input .MachineDeployment .Spec .Replicas )))
218
+ }, intervals ... ).Should (Succeed (), "Timed out waiting for %d nodes to be created for MachineDeployment %s" , int (* input .MachineDeployment .Spec .Replicas ), klog .KObj (input .MachineDeployment ))
219
+ }
220
+
154
221
func SetControllerVersionAndWait (ctx context.Context , proxy framework.ClusterProxy , version string ) {
155
222
cp := & v1.Deployment {}
156
223
Expect (proxy .GetClient ().Get (ctx , types.NamespacedName {
0 commit comments