github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/orbiter/kinds/clusters/kubernetes/machines.go (about)

     1  package kubernetes
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/caos/orbos/internal/helpers"
     7  	"github.com/caos/orbos/internal/operator/orbiter/kinds/clusters/core/infra"
     8  	"github.com/caos/orbos/mntr"
     9  )
    10  
    11  func alignMachines(
    12  	monitor mntr.Monitor,
    13  	controlplanePool *initializedPool,
    14  	workerPools []*initializedPool,
    15  	initializeMachine func(infra.Machine, *initializedPool) initializedMachine,
    16  ) (bool, []*initializedMachine, error) {
    17  	wCount := 0
    18  	for _, w := range workerPools {
    19  		wCount += w.desired.Nodes
    20  	}
    21  	monitor.WithFields(map[string]interface{}{
    22  		"control_plane_nodes": controlplanePool.desired.Nodes,
    23  		"worker_nodes":        wCount,
    24  	}).Debug("Ensuring scale")
    25  
    26  	var machines []*initializedMachine
    27  	upscalingDone := true
    28  	var (
    29  		wg  sync.WaitGroup
    30  		err error
    31  	)
    32  	alignPool := func(pool *initializedPool) {
    33  		defer wg.Done()
    34  
    35  		if pool.upscaling > 0 {
    36  			upscalingDone = false
    37  			machines, alignErr := newMachines(pool.infra, pool.upscaling, pool.desired.Nodes)
    38  			if alignErr != nil {
    39  				err = helpers.Concat(err, alignErr)
    40  				return
    41  			}
    42  			for _, machine := range machines {
    43  				initializeMachine(machine, pool)
    44  			}
    45  		}
    46  
    47  		if err != nil {
    48  			return
    49  		}
    50  		poolMachines, listErr := pool.machines()
    51  		if listErr != nil {
    52  			err = helpers.Concat(err, listErr)
    53  			return
    54  		}
    55  		machines = append(machines, poolMachines...)
    56  	}
    57  
    58  	wg.Add(1)
    59  	go alignPool(controlplanePool)
    60  
    61  	for _, workerPool := range workerPools {
    62  		wg.Add(1)
    63  		go alignPool(workerPool)
    64  	}
    65  	wg.Wait()
    66  	if err != nil {
    67  		return false, machines, err
    68  	}
    69  
    70  	if !upscalingDone {
    71  		monitor.Info("Upscaled machines are not ready yet")
    72  		return false, machines, nil
    73  	}
    74  	return true, machines, nil
    75  }