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

     1  package kubernetes
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/caos/orbos/internal/operator/common"
     8  	"github.com/caos/orbos/internal/operator/orbiter"
     9  	"github.com/caos/orbos/internal/operator/orbiter/kinds/clusters/core/infra"
    10  	"github.com/caos/orbos/mntr"
    11  	"github.com/caos/orbos/pkg/git"
    12  	"github.com/caos/orbos/pkg/kubernetes"
    13  )
    14  
    15  func query(
    16  	monitor mntr.Monitor,
    17  	clusterID string,
    18  	desired *DesiredV0,
    19  	current *CurrentCluster,
    20  	providerCurrents map[string]interface{},
    21  	nodeAgentsCurrent *common.CurrentNodeAgents,
    22  	nodeAgentsDesired *common.DesiredNodeAgents,
    23  	k8sClient *kubernetes.Client,
    24  	oneoff bool,
    25  	gitClient *git.Client,
    26  ) (orbiter.EnsureFunc, error) {
    27  
    28  	cloudPools, kubeAPIAddress, providerK8sSpec, privateInterface, err := GetProviderInfos(desired, providerCurrents)
    29  	if err != nil {
    30  		panic(err)
    31  	}
    32  
    33  	if err := poolIsConfigured(&desired.Spec.ControlPlane, cloudPools); err != nil {
    34  		return nil, err
    35  	}
    36  
    37  	for _, w := range desired.Spec.Workers {
    38  		if err := poolIsConfigured(w, cloudPools); err != nil {
    39  			return nil, err
    40  		}
    41  	}
    42  
    43  	controlplane, controlplaneMachines, workers, workerMachines, initializeMachine, uninitializeMachine, err := initialize(
    44  		monitor,
    45  		current,
    46  		*desired,
    47  		nodeAgentsCurrent,
    48  		nodeAgentsDesired,
    49  		cloudPools,
    50  		k8sClient,
    51  		func(machine *initializedMachine) {
    52  			firewallFunc(monitor, *desired)(machine)
    53  		})
    54  
    55  	return func(psf func(mntr.Monitor) error) *orbiter.EnsureResult {
    56  		return orbiter.ToEnsureResult(ensure(
    57  			monitor,
    58  			clusterID,
    59  			desired,
    60  			kubeAPIAddress,
    61  			psf,
    62  			k8sClient,
    63  			oneoff,
    64  			controlplane,
    65  			controlplaneMachines,
    66  			workers,
    67  			workerMachines,
    68  			initializeMachine,
    69  			uninitializeMachine,
    70  			gitClient,
    71  			providerK8sSpec,
    72  			privateInterface,
    73  		))
    74  	}, err
    75  }
    76  
    77  func poolIsConfigured(poolSpec *Pool, infra map[string]map[string]infra.Pool) error {
    78  	prov, ok := infra[poolSpec.Provider]
    79  	if !ok {
    80  		return fmt.Errorf("provider %s not configured", poolSpec.Provider)
    81  	}
    82  	if _, ok := prov[poolSpec.Pool]; !ok {
    83  		return fmt.Errorf("pool %s not configured on provider %s", poolSpec.Pool, poolSpec.Provider)
    84  	}
    85  	return nil
    86  }
    87  
    88  func GetProviderInfos(desired *DesiredV0, providerCurrents map[string]interface{}) (map[string]map[string]infra.Pool, *infra.Address, infra.Kubernetes, string, error) {
    89  	cloudPools := make(map[string]map[string]infra.Pool)
    90  	var (
    91  		kubeAPIAddress   *infra.Address
    92  		providerK8sSpec  infra.Kubernetes
    93  		privateInterface string
    94  	)
    95  
    96  	for providerName, provider := range providerCurrents {
    97  		if cloudPools[providerName] == nil {
    98  			cloudPools[providerName] = make(map[string]infra.Pool)
    99  		}
   100  		prov := provider.(infra.ProviderCurrent)
   101  		providerPools := prov.Pools()
   102  		providerIngresses := prov.Ingresses()
   103  		for providerPoolName, providerPool := range providerPools {
   104  			cloudPools[providerName][providerPoolName] = providerPool
   105  			if desired.Spec.ControlPlane.Provider == providerName && desired.Spec.ControlPlane.Pool == providerPoolName {
   106  				var ok bool
   107  				kubeAPIAddress, ok = providerIngresses["kubeapi"]
   108  				if !ok {
   109  					return nil, nil, providerK8sSpec, "", errors.New("no externally reachable address named kubeapi found")
   110  				}
   111  				providerK8sSpec = prov.Kubernetes()
   112  				privateInterface = prov.PrivateInterface()
   113  			}
   114  		}
   115  	}
   116  	return cloudPools, kubeAPIAddress, providerK8sSpec, privateInterface, nil
   117  }