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

     1  package orb
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/caos/orbos/internal/operator/orbiter/kinds/clusters"
     7  	"github.com/caos/orbos/internal/operator/orbiter/kinds/clusters/core/infra"
     8  	"github.com/caos/orbos/internal/operator/orbiter/kinds/providers"
     9  	"github.com/caos/orbos/mntr"
    10  	"github.com/caos/orbos/pkg/labels"
    11  	"github.com/caos/orbos/pkg/tree"
    12  )
    13  
    14  type MachinesFunc func(monitor mntr.Monitor, desiredTree *tree.Tree, orbID string) (machineIDs []string, machines map[string]infra.Machine, err error)
    15  
    16  func ListMachines(operarorLabels *labels.Operator) MachinesFunc {
    17  	return func(monitor mntr.Monitor, desiredTree *tree.Tree, orbID string) (machineIDs []string, machines map[string]infra.Machine, err error) {
    18  		defer func() {
    19  			if err != nil {
    20  				err = fmt.Errorf("building %s failed: %w", desiredTree.Common.Kind, err)
    21  			}
    22  		}()
    23  
    24  		desiredKind, err := ParseDesiredV0(desiredTree)
    25  		if err != nil {
    26  			return nil, nil, fmt.Errorf("parsing desired state failed: %w", err)
    27  		}
    28  		desiredTree.Parsed = desiredKind
    29  
    30  		machines = make(map[string]infra.Machine, 0)
    31  		machineIDs = make([]string, 0)
    32  
    33  		for clusterID, clusterTree := range desiredKind.Clusters {
    34  			clusterCurrent := &tree.Tree{}
    35  			_, _, _, _, _, err := clusters.GetQueryAndDestroyFuncs(
    36  				monitor,
    37  				operarorLabels,
    38  				clusterID,
    39  				clusterTree,
    40  				true,
    41  				false,
    42  				false,
    43  				clusterCurrent,
    44  				nil,
    45  				nil,
    46  				nil,
    47  				nil,
    48  			)
    49  			if err != nil {
    50  				return nil, nil, err
    51  			}
    52  		}
    53  
    54  		for provID, providerTree := range desiredKind.Providers {
    55  
    56  			providerMachines, err := providers.ListMachines(
    57  				monitor.WithFields(map[string]interface{}{"provider": provID}),
    58  				providerTree,
    59  				provID,
    60  				orbID,
    61  			)
    62  			if err != nil {
    63  				return nil, nil, err
    64  			}
    65  
    66  			for id, providerMachine := range providerMachines {
    67  				machineID := provID + "." + id
    68  				machineIDs = append(machineIDs, machineID)
    69  				machines[machineID] = providerMachine
    70  			}
    71  		}
    72  
    73  		return machineIDs, machines, nil
    74  	}
    75  }