github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/orbiter/kinds/loadbalancers/dynamic/wrap/machinesservicelb.go (about)

     1  package wrap
     2  
     3  import (
     4  	"github.com/caos/orbos/internal/operator/orbiter/kinds/clusters/core/infra"
     5  	"github.com/caos/orbos/internal/operator/orbiter/kinds/loadbalancers/dynamic"
     6  	"github.com/caos/orbos/internal/operator/orbiter/kinds/providers/core"
     7  )
     8  
     9  var _ core.MachinesService = (*CmpSvcLB)(nil)
    10  
    11  type CmpSvcLB struct {
    12  	core.MachinesService
    13  	dynamic dynamic.Current
    14  	vrrp    *dynamic.VRRP
    15  	vip     func(*dynamic.VIP) string
    16  }
    17  
    18  func MachinesService(svc core.MachinesService, curr dynamic.Current, vrrp *dynamic.VRRP, vip func(*dynamic.VIP) string) *CmpSvcLB {
    19  	return &CmpSvcLB{
    20  		MachinesService: svc,
    21  		dynamic:         curr,
    22  		vrrp:            vrrp,
    23  		vip:             vip,
    24  	}
    25  }
    26  
    27  func (i *CmpSvcLB) InitializeDesiredNodeAgents() (bool, error) {
    28  	pools, err := i.ListPools()
    29  	if err != nil {
    30  		return false, err
    31  	}
    32  
    33  	done := true
    34  	for _, pool := range pools {
    35  		poolDone, err := i.desire(pool)
    36  		if !poolDone {
    37  			done = false
    38  		}
    39  		if err != nil {
    40  			return done, err
    41  		}
    42  	}
    43  	return done, nil
    44  }
    45  
    46  func (i *CmpSvcLB) Create(poolName string, desiredInstances int) (infra.Machines, error) {
    47  	cmp, err := i.MachinesService.Create(poolName, desiredInstances)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	_, err = i.desire(poolName)
    53  	machines := make([]infra.Machine, 0)
    54  
    55  	for _, infraMachine := range cmp {
    56  		machines = append(machines, machine(infraMachine, func() error {
    57  			_, err := i.desire(poolName)
    58  			return err
    59  		}))
    60  	}
    61  	return machines, err
    62  }
    63  
    64  func (c *CmpSvcLB) desire(selfPool string) (bool, error) {
    65  	return c.dynamic.Current.Desire(selfPool, c, c.vrrp, c.vip)
    66  }