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

     1  package gce
     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(
    29  		monitor mntr.Monitor,
    30  		finishedChan chan struct{},
    31  		desiredTree *tree.Tree,
    32  		currentTree *tree.Tree,
    33  	) (
    34  		queryFunc orbiter.QueryFunc,
    35  		destroyFunc orbiter.DestroyFunc,
    36  		configureFunc orbiter.ConfigureFunc,
    37  		migrate bool,
    38  		secrets map[string]*secret.Secret,
    39  		err error,
    40  	) {
    41  		defer func() {
    42  			if err != nil {
    43  				err = fmt.Errorf("building %s failed: %w", desiredTree.Common.Kind, err)
    44  			}
    45  		}()
    46  		desiredKind, err := parseDesiredV0(desiredTree)
    47  		if err != nil {
    48  			return nil, nil, nil, migrate, nil, fmt.Errorf("parsing desired state failed: %w", err)
    49  		}
    50  		desiredTree.Parsed = desiredKind
    51  		secrets = make(map[string]*secret.Secret, 0)
    52  		secret.AppendSecrets("", secrets, getSecretsMap(desiredKind), nil, nil)
    53  
    54  		if desiredKind.Spec.RebootRequired == nil {
    55  			desiredKind.Spec.RebootRequired = make([]string, 0)
    56  			migrate = true
    57  		}
    58  
    59  		if desiredKind.Spec.Verbose && !monitor.IsVerbose() {
    60  			monitor = monitor.Verbose()
    61  		}
    62  
    63  		for _, pool := range desiredKind.Spec.Pools {
    64  			if pool.StorageDiskType == "" {
    65  				pool.StorageDiskType = "pd-standard"
    66  				migrate = true
    67  			}
    68  		}
    69  
    70  		if err := desiredKind.validateAdapt(); err != nil {
    71  			return nil, nil, nil, migrate, nil, err
    72  		}
    73  
    74  		lbCurrent := &tree.Tree{}
    75  		var lbQuery orbiter.QueryFunc
    76  
    77  		lbQuery, lbDestroy, lbConfigure, migrateLocal, lbSecrets, err := loadbalancers.GetQueryAndDestroyFunc(monitor, whitelist, desiredKind.Loadbalancing, lbCurrent, finishedChan)
    78  		if err != nil {
    79  			return nil, nil, nil, migrate, nil, err
    80  		}
    81  		if migrateLocal {
    82  			migrate = true
    83  		}
    84  		secret.AppendSecrets("", secrets, lbSecrets, nil, nil)
    85  
    86  		svcFunc := func() (*machinesService, error) {
    87  			return service(monitor, &desiredKind.Spec, orbID, providerID, oneoff)
    88  		}
    89  
    90  		current := &Current{
    91  			Common: tree.NewCommon("orbiter.caos.ch/GCEProvider", "v0", false),
    92  		}
    93  		currentTree.Parsed = current
    94  
    95  		return func(nodeAgentsCurrent *common.CurrentNodeAgents, nodeAgentsDesired *common.DesiredNodeAgents, _ map[string]interface{}) (ensureFunc orbiter.EnsureFunc, err error) {
    96  				defer func() {
    97  					if err != nil {
    98  						err = fmt.Errorf("querying %s failed: %w", desiredKind.Common.Kind, err)
    99  					}
   100  				}()
   101  
   102  				if err := desiredKind.validateQuery(); err != nil {
   103  					return nil, err
   104  				}
   105  
   106  				svc, err := svcFunc()
   107  				if err != nil {
   108  					return nil, err
   109  				}
   110  
   111  				if err := svc.use(desiredKind.Spec.SSHKey); err != nil {
   112  					return nil, err
   113  				}
   114  
   115  				if _, err := lbQuery(nodeAgentsCurrent, nodeAgentsDesired, nil); err != nil {
   116  					return nil, err
   117  				}
   118  
   119  				_, naFuncs := core.NodeAgentFuncs(monitor, repoURL, repoKey, pprof)
   120  
   121  				return query(&desiredKind.Spec, current, lbCurrent.Parsed, svc, nodeAgentsCurrent, nodeAgentsDesired, naFuncs, orbiterCommit)
   122  			}, func(delegates map[string]interface{}) error {
   123  				if err := lbDestroy(delegates); err != nil {
   124  					return err
   125  				}
   126  
   127  				ctx, err := svcFunc()
   128  				if err != nil {
   129  					return err
   130  				}
   131  				return destroy(ctx, delegates)
   132  			}, func(orb orbcfg.Orb) error {
   133  
   134  				if err := lbConfigure(orb); err != nil {
   135  					return err
   136  				}
   137  
   138  				if desiredKind.Spec.SSHKey == nil ||
   139  					desiredKind.Spec.SSHKey.Private == nil || desiredKind.Spec.SSHKey.Private.Value == "" ||
   140  					desiredKind.Spec.SSHKey.Public == nil || desiredKind.Spec.SSHKey.Public.Value == "" {
   141  					priv, pub := ssh.Generate()
   142  					desiredKind.Spec.SSHKey = &SSHKey{
   143  						Private: &secret.Secret{Value: priv},
   144  						Public:  &secret.Secret{Value: pub},
   145  					}
   146  				}
   147  
   148  				svc, err := svcFunc()
   149  				if err != nil {
   150  					return err
   151  				}
   152  
   153  				if err := desiredKind.validateJSONKey(); err != nil {
   154  					// TODO: Create service account and write its json key to desiredKind.Spec.JSONKey and push repo
   155  					// return err
   156  					return nil
   157  				}
   158  
   159  				if err := svc.use(desiredKind.Spec.SSHKey); err != nil {
   160  					panic(err)
   161  				}
   162  
   163  				return core.ConfigureNodeAgents(svc, svc.context.monitor, orb, pprof)
   164  			},
   165  			migrate,
   166  			secrets,
   167  			nil
   168  	}
   169  }