github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/orbiter/kinds/providers/gce/pool.go (about)

     1  package gce
     2  
     3  import (
     4  	"github.com/caos/orbos/internal/operator/orbiter/kinds/clusters/core/infra"
     5  	"github.com/caos/orbos/internal/operator/orbiter/kinds/providers/core"
     6  	uuid "github.com/satori/go.uuid"
     7  	"google.golang.org/api/compute/v1"
     8  )
     9  
    10  var _ infra.Pool = (*infraPool)(nil)
    11  
    12  type infraPool struct {
    13  	pool       string
    14  	normalized []*normalizedLoadbalancer
    15  	svc        *machinesService
    16  	machines   core.MachinesService
    17  }
    18  
    19  func newInfraPool(pool string, svc *machinesService, normalized []*normalizedLoadbalancer, machines core.MachinesService) *infraPool {
    20  	return &infraPool{
    21  		pool:       pool,
    22  		normalized: normalized,
    23  		svc:        svc,
    24  		machines:   machines,
    25  	}
    26  }
    27  
    28  func (i *infraPool) EnsureMember(machine infra.Machine) error {
    29  	return i.ensureMembers(machine)
    30  }
    31  
    32  func (i *infraPool) EnsureMembers() error {
    33  	return i.ensureMembers(nil)
    34  }
    35  
    36  func (i *infraPool) ensureMembers(machine infra.Machine) error {
    37  
    38  	allInstances, err := getAllInstances(i.svc)
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	poolInstances := allInstances[i.pool]
    44  
    45  	for _, n := range i.normalized {
    46  	destpoolLoop:
    47  		for _, destPool := range n.targetPool.destPools {
    48  			if destPool == i.pool {
    49  
    50  				var addInstances []*instance
    51  			addInstanceLoop:
    52  				for _, instance := range poolInstances {
    53  					if machine != nil && machine.ID() != instance.ID() {
    54  						continue addInstanceLoop
    55  					}
    56  					for _, tpInstance := range n.targetPool.gce.Instances {
    57  						if instance.url == tpInstance {
    58  							continue addInstanceLoop
    59  						}
    60  					}
    61  					addInstances = append(addInstances, instance)
    62  				}
    63  
    64  				if len(addInstances) == 0 {
    65  					continue destpoolLoop
    66  				}
    67  
    68  				if err := operateFunc(
    69  					n.targetPool.log("Adding instances to target pool", true, addInstances),
    70  					computeOpCall(i.svc.context.client.TargetPools.
    71  						AddInstance(
    72  							i.svc.context.projectID,
    73  							i.svc.context.desired.Region,
    74  							n.targetPool.gce.Name,
    75  							&compute.TargetPoolsAddInstanceRequest{Instances: instances(addInstances).refs()},
    76  						).
    77  						RequestId(uuid.NewV1().String()).
    78  						Do),
    79  					toErrFunc(n.targetPool.log("Instances added to target pool", false, addInstances)),
    80  				)(); err != nil {
    81  					return err
    82  				}
    83  			}
    84  		}
    85  	}
    86  	return nil
    87  }
    88  
    89  func (i *infraPool) DesiredMembers(instances int) int {
    90  	return i.machines.DesiredMachines(i.pool, instances)
    91  }
    92  
    93  func (i *infraPool) GetMachines() (infra.Machines, error) {
    94  	return i.machines.List(i.pool)
    95  }
    96  
    97  func (i *infraPool) AddMachine(desiredInstances int) (infra.Machines, error) {
    98  	return i.machines.Create(i.pool, desiredInstances)
    99  }