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

     1  package cs
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/caos/orbos/internal/operator/common"
     7  	"github.com/caos/orbos/internal/operator/orbiter"
     8  	"github.com/caos/orbos/internal/operator/orbiter/kinds/loadbalancers"
     9  	"github.com/caos/orbos/internal/operator/orbiter/kinds/loadbalancers/dynamic"
    10  	"github.com/caos/orbos/internal/operator/orbiter/kinds/providers/core"
    11  	"github.com/caos/orbos/internal/ssh"
    12  	"github.com/caos/orbos/mntr"
    13  	orbcfg "github.com/caos/orbos/pkg/orb"
    14  	"github.com/caos/orbos/pkg/secret"
    15  	"github.com/caos/orbos/pkg/tree"
    16  )
    17  
    18  func AdaptFunc(
    19  	providerID,
    20  	orbID string,
    21  	whitelist dynamic.WhiteListFunc,
    22  	orbiterCommit,
    23  	repoURL,
    24  	repoKey string,
    25  	oneoff bool,
    26  	pprof bool,
    27  ) orbiter.AdaptFunc {
    28  	return func(monitor mntr.Monitor, finishedChan chan struct{}, desiredTree *tree.Tree, currentTree *tree.Tree) (queryFunc orbiter.QueryFunc, destroyFunc orbiter.DestroyFunc, configureFunc orbiter.ConfigureFunc, migrate bool, secrets map[string]*secret.Secret, err error) {
    29  		defer func() {
    30  			if err != nil {
    31  				err = fmt.Errorf("building %s failed: %w", desiredTree.Common.Kind, err)
    32  			}
    33  		}()
    34  		desiredKind, err := parseDesired(desiredTree)
    35  		if err != nil {
    36  			return nil, nil, nil, migrate, nil, fmt.Errorf("parsing desired state failed: %w", err)
    37  		}
    38  		desiredTree.Parsed = desiredKind
    39  		secrets = make(map[string]*secret.Secret, 0)
    40  		secret.AppendSecrets("", secrets, getSecretsMap(desiredKind), nil, nil)
    41  
    42  		if desiredKind.Spec.RebootRequired == nil {
    43  			desiredKind.Spec.RebootRequired = make([]string, 0)
    44  			migrate = true
    45  		}
    46  
    47  		if desiredKind.Spec.Verbose && !monitor.IsVerbose() {
    48  			monitor = monitor.Verbose()
    49  		}
    50  
    51  		if err := desiredKind.validateAdapt(); err != nil {
    52  			return nil, nil, nil, migrate, nil, err
    53  		}
    54  
    55  		lbCurrent := &tree.Tree{}
    56  		var lbQuery orbiter.QueryFunc
    57  
    58  		lbQuery, lbDestroy, lbConfigure, migrateLocal, lbSecrets, err := loadbalancers.GetQueryAndDestroyFunc(monitor, whitelist, desiredKind.Loadbalancing, lbCurrent, finishedChan)
    59  		if err != nil {
    60  			return nil, nil, nil, migrate, nil, err
    61  		}
    62  		if migrateLocal {
    63  			migrate = true
    64  		}
    65  		secret.AppendSecrets("", secrets, lbSecrets, nil, nil)
    66  
    67  		ctx := buildContext(monitor, &desiredKind.Spec, orbID, providerID, oneoff)
    68  
    69  		current := &Current{
    70  			Common: tree.NewCommon("orbiter.caos.ch/CloudScaleProvider", "v0", false),
    71  		}
    72  		currentTree.Parsed = current
    73  
    74  		return func(nodeAgentsCurrent *common.CurrentNodeAgents, nodeAgentsDesired *common.DesiredNodeAgents, _ map[string]interface{}) (ensureFunc orbiter.EnsureFunc, err error) {
    75  				defer func() {
    76  					if err != nil {
    77  						err = fmt.Errorf("querying %s failed: %w", desiredKind.Common.Kind, err)
    78  					}
    79  				}()
    80  
    81  				if err := desiredKind.validateQuery(); err != nil {
    82  					return nil, err
    83  				}
    84  
    85  				if err := ctx.machinesService.use(desiredKind.Spec.SSHKey); err != nil {
    86  					return nil, err
    87  				}
    88  
    89  				if _, err := lbQuery(nodeAgentsCurrent, nodeAgentsDesired, nil); err != nil {
    90  					return nil, err
    91  				}
    92  
    93  				_, naFuncs := core.NodeAgentFuncs(monitor, repoURL, repoKey, pprof)
    94  
    95  				return query(&desiredKind.Spec, current, lbCurrent.Parsed, ctx, nodeAgentsCurrent, nodeAgentsDesired, naFuncs, orbiterCommit)
    96  			}, func(delegates map[string]interface{}) error {
    97  				if err := lbDestroy(delegates); err != nil {
    98  					return err
    99  				}
   100  
   101  				if err := ctx.machinesService.use(desiredKind.Spec.SSHKey); err != nil {
   102  					return err
   103  				}
   104  
   105  				return destroy(ctx, current)
   106  			}, func(orb orbcfg.Orb) error {
   107  
   108  				if err := lbConfigure(orb); err != nil {
   109  					return err
   110  				}
   111  
   112  				if desiredKind.Spec.SSHKey == nil ||
   113  					desiredKind.Spec.SSHKey.Private == nil || desiredKind.Spec.SSHKey.Private.Value == "" ||
   114  					desiredKind.Spec.SSHKey.Public == nil || desiredKind.Spec.SSHKey.Public.Value == "" {
   115  					priv, pub := ssh.Generate()
   116  					desiredKind.Spec.SSHKey = &SSHKey{
   117  						Private: &secret.Secret{Value: priv},
   118  						Public:  &secret.Secret{Value: pub},
   119  					}
   120  				}
   121  
   122  				if err := desiredKind.validateAPIToken(); err != nil {
   123  					return nil
   124  				}
   125  
   126  				if err := ctx.machinesService.use(desiredKind.Spec.SSHKey); err != nil {
   127  					panic(err)
   128  				}
   129  
   130  				return core.ConfigureNodeAgents(ctx.machinesService, ctx.monitor, orb, pprof)
   131  			}, migrate, secrets, nil
   132  	}
   133  }