Skip to content

Commit 70221e0

Browse files
committed
concurrently wait for masters to become ready if --wait is set (or timeout)
1 parent 51ebcab commit 70221e0

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

pkg/cluster/cluster.go

+22-15
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ initNodeFinished:
185185

186186
// vars to support waiting for master nodes to be ready
187187
var waitForMasterWaitgroup sync.WaitGroup
188-
waitForMasterErrChan := make(chan error, 1)
188+
waitForMasterErrChan := make(chan error, 1) // FIXME: we should do something different here, e.g. concurrently read from the channel and append to a slice of errors
189189

190190
// create all other nodes, but skip the init node
191191
for _, node := range cluster.Nodes {
@@ -212,29 +212,36 @@ initNodeFinished:
212212
// asynchronously wait for this master node to be ready (by checking the logs for a specific log mesage)
213213
if node.Role == k3d.MasterRole && cluster.ClusterCreationOpts.WaitForMaster >= 0 {
214214
waitForMasterWaitgroup.Add(1)
215-
go func(masterNode *k3d.Node, waitgroup *sync.WaitGroup) {
216-
defer waitgroup.Done()
215+
go func(masterNode *k3d.Node) {
217216
log.Debugf("Starting to wait for master node '%s'", masterNode.Name)
218217
// TODO: it may be better to give endtime=starttime+timeout here so that there is no difference between the instances (go func may be called with a few (milli-)seconds difference)
219-
if err := WaitForNodeLogMessage(runtime, masterNode, "Wrote kubeconfig", (time.Duration(cluster.ClusterCreationOpts.WaitForMaster) * time.Second)); err != nil {
220-
waitForMasterErrChan <- err
218+
err := WaitForNodeLogMessage(runtime, masterNode, "Wrote kubeconfig", (time.Duration(cluster.ClusterCreationOpts.WaitForMaster) * time.Second))
219+
waitForMasterErrChan <- err
220+
if err == nil {
221+
log.Debugf("Master Node '%s' ready", masterNode.Name)
221222
}
222-
log.Debugf("Master Node '%s' ready", masterNode.Name)
223-
}(node, &waitForMasterWaitgroup)
223+
}(node)
224224
}
225225
}
226226

227-
// block until all masters are ready (if --wait was set)
227+
// block until all masters are ready (if --wait was set) and collect errors if not
228228
if cluster.ClusterCreationOpts.WaitForMaster >= 0 {
229-
waitForMasterWaitgroup.Wait()
230-
close(waitForMasterErrChan)
231-
if len(waitForMasterErrChan) != 0 {
232-
log.Errorln("Failed to bring up all master nodes in time")
233-
errs := []error{}
229+
errs := []error{}
230+
go func() {
234231
for elem := range waitForMasterErrChan {
235-
errs = append(errs, elem)
232+
if elem != nil {
233+
errs = append(errs, elem)
234+
}
235+
waitForMasterWaitgroup.Done()
236+
}
237+
}()
238+
waitForMasterWaitgroup.Wait()
239+
if len(errs) != 0 {
240+
log.Errorln("Failed to bring up all master nodes in time. Check the logs:")
241+
for _, e := range errs {
242+
log.Errorln(">>> ", e)
236243
}
237-
return fmt.Errorf("Combined error log:\n%+v", errs)
244+
return fmt.Errorf("Failed to bring up cluster") // TODO: in case of failure, we should rollback
238245
}
239246
}
240247

0 commit comments

Comments
 (0)